Spotter-VM/basic/srv/vm/cli.py

133 lines
6.6 KiB
Python
Raw Normal View History

2018-08-02 10:41:40 +02:00
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import argparse
import getpass
2018-08-02 10:41:40 +02:00
import sys
2018-09-03 17:24:48 +02:00
sys.path.append('/srv/vm')
2018-08-02 10:41:40 +02:00
2018-09-04 21:42:26 +02:00
from mgr import VMMgr
2018-08-02 10:41:40 +02:00
2018-09-03 17:24:48 +02:00
parser = argparse.ArgumentParser(description='VM application manager')
2018-08-02 10:41:40 +02:00
subparsers = parser.add_subparsers()
parser_update_login = subparsers.add_parser('update-login', help='Updates application login')
parser_update_login.set_defaults(action='update-login')
parser_update_login.add_argument('app', help='Application name')
parser_update_login.add_argument('login', help='Administrative login')
parser_update_login.add_argument('password', help='Administrative password')
parser_show_tiles = subparsers.add_parser('show-tiles', help='Shows application tiles in Portal')
parser_show_tiles.set_defaults(action='show-tiles')
parser_show_tiles.add_argument('app', help='Application name')
parser_hide_tiles = subparsers.add_parser('hide-tiles', help='Hides application tiles in Portal')
parser_hide_tiles.set_defaults(action='hide-tiles')
parser_hide_tiles.add_argument('app', help='Application name')
parser_start_app = subparsers.add_parser('start-app', help='Start application including it\'s dependencies')
parser_start_app.set_defaults(action='start-app')
parser_start_app.add_argument('app', help='Application name')
parser_stop_app = subparsers.add_parser('stop-app', help='Stops application including it\'s dependencies if they are not used by another running application')
parser_stop_app.set_defaults(action='stop-app')
parser_stop_app.add_argument('app', help='Application name')
parser_enable_autostart = subparsers.add_parser('enable-autostart', help='Enables application autostart')
parser_enable_autostart.set_defaults(action='enable-autostart')
parser_enable_autostart.add_argument('app', help='Application name')
parser_disable_autostart = subparsers.add_parser('disable-autostart', help='Disables application autostart')
parser_disable_autostart.set_defaults(action='disable-autostart')
parser_disable_autostart.add_argument('app', help='Application name')
2018-08-31 09:13:18 +02:00
parser_rebuild_issue = subparsers.add_parser('rebuild-issue', help='Rebuilds /etc/issue using current settings - used on VM startup')
parser_rebuild_issue.set_defaults(action='rebuild-issue')
parser_clean_ephemeral = subparsers.add_parser('clean-ephemeral', help='Cleans container ephemeral layer. Intended to be used with LXC hooks')
parser_clean_ephemeral.add_argument('lxc', nargs=argparse.REMAINDER)
parser_clean_ephemeral.set_defaults(action='clean-ephemeral')
2018-09-12 10:36:48 +02:00
parser_register_container = subparsers.add_parser('register-container', help='Register and assigns IP to an application container. Intended to be used with LXC hooks')
parser_register_container.add_argument('lxc', nargs=argparse.REMAINDER)
parser_register_container.set_defaults(action='register-container')
parser_unregister_container = subparsers.add_parser('unregister-container', help='Removes IP assignment for an application container. Intended to be used with LXC hooks')
parser_unregister_container.add_argument('lxc', nargs=argparse.REMAINDER)
parser_unregister_container.set_defaults(action='unregister-container')
2018-08-02 10:41:40 +02:00
parser_register_proxy = subparsers.add_parser('register-proxy', help='Rebuilds nginx proxy target for an application container')
parser_register_proxy.set_defaults(action='register-proxy')
parser_register_proxy.add_argument('app', help='Application name')
parser_unregister_proxy = subparsers.add_parser('unregister-proxy', help='Removes nginx proxy target for an application container')
parser_unregister_proxy.set_defaults(action='unregister-proxy')
parser_unregister_proxy.add_argument('app', help='Application name')
parser_update_host = subparsers.add_parser('update-host', help='Rebuilds domain structure of VM with new host name and new HTTPS port')
parser_update_host.set_defaults(action='update-host')
parser_update_host.add_argument('domain', help='Domain name')
parser_update_host.add_argument('port', help='HTTPS port')
parser_update_common = subparsers.add_parser('update-common', help='Updates common configuration properties used by multiple applications')
parser_update_common.set_defaults(action='update-common')
parser_update_common.add_argument('--email', help='Administrative e-mail address')
parser_update_common.add_argument('--gmaps-api-key', help='Google Maps API key')
parser_update_password = subparsers.add_parser('update-password', help='Updates password for HDD encryption and WSGI administration interface')
parser_update_password.set_defaults(action='update-password')
parser_create_selfsigned = subparsers.add_parser('create-selfsigned', help='Creates and installs selfsigned certificate for currently set domain')
parser_create_selfsigned.set_defaults(action='create-selfsigned')
2018-08-02 10:41:40 +02:00
parser_request_cert = subparsers.add_parser('request-cert', help='Requests and installs Let\'s Encrypt certificate for currently set domain')
parser_request_cert.set_defaults(action='request-cert')
parser_install_cert = subparsers.add_parser('install-cert', help='Installs user supplied certificate')
parser_install_cert.set_defaults(action='install-cert')
parser_install_cert.add_argument('certificate', help='Certificate file')
parser_install_cert.add_argument('key', help='Key file')
args = parser.parse_args()
2018-09-04 21:42:26 +02:00
mgr = VMMgr()
2018-08-02 10:41:40 +02:00
if args.action == 'update-login':
mgr.update_login(args.app, args.login, args.password)
elif args.action == 'show-tiles':
mgr.show_tiles(args.app)
elif args.action == 'hide-tiles':
mgr.hide_tiles(args.app)
elif args.action == 'start-app':
mgr.start_app(args.app)
elif args.action == 'stop-app':
mgr.stop_app(args.app)
elif args.action == 'enable-autostart':
mgr.enable_autostart(args.app)
elif args.action == 'disable-autostart':
mgr.disable_autostart(args.app)
2018-08-31 09:13:18 +02:00
elif args.action == 'rebuild-issue':
mgr.rebuild_issue()
elif args.action == 'clean-ephemeral':
mgr.clean_ephemeral()
2018-09-12 10:36:48 +02:00
elif args.action == 'register-container':
mgr.register_container()
elif args.action == 'unregister-container':
mgr.unregister_container()
2018-08-02 10:41:40 +02:00
elif args.action == 'register-proxy':
mgr.register_proxy(args.app)
elif args.action == 'unregister-proxy':
mgr.unregister_proxy(args.app)
elif args.action == 'update-host':
mgr.update_host(args.domain, args.port)
elif args.action == 'update-common':
mgr.update_common(args.email, args.gmaps_api_key)
elif args.action == 'update-password':
oldpassword = getpass.getpass('Old password: ')
newpassword = getpass.getpass('New password: ')
mgr.update_password(oldpassword, newpassword)
elif args.action == 'create-selfsigned':
mgr.create_selfsigned()
2018-08-02 10:41:40 +02:00
elif args.action == 'request-cert':
mgr.request_cert()
elif args.action == 'install-cert':
mgr.install_cert(args.certificate, args.key)