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

81 lines
3.0 KiB
Python
Executable File

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import argparse
import getpass
import sys
sys.path.append('/srv/vm')
from mgr import VMMgr
parser = argparse.ArgumentParser(description='VM application manager')
subparsers = parser.add_subparsers()
parser_install = subparsers.add_parser('install')
parser_install.set_defaults(action='install')
parser_change_password = subparsers.add_parser('change-password')
parser_change_password.set_defaults(action='change-password')
parser_update_login = subparsers.add_parser('update-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_rebuild_issue = subparsers.add_parser('rebuild-issue')
parser_rebuild_issue.set_defaults(action='rebuild-issue')
parser_prepare_container = subparsers.add_parser('prepare-container')
parser_prepare_container.add_argument('lxc', nargs=argparse.REMAINDER)
parser_prepare_container.set_defaults(action='prepare-container')
parser_register_container = subparsers.add_parser('register-container')
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')
parser_unregister_container.add_argument('lxc', nargs=argparse.REMAINDER)
parser_unregister_container.set_defaults(action='unregister-container')
parser_register_proxy = subparsers.add_parser('register-proxy')
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')
parser_unregister_proxy.set_defaults(action='unregister-proxy')
parser_unregister_proxy.add_argument('app', help='Application name')
args = parser.parse_args()
mgr = VMMgr()
if args.action == 'install':
# Used during VM installation
mgr.rebuild_nginx()
mgr.create_selfsigned_cert()
elif args.action == 'change-password':
# Used during VM packaging
oldpassword = getpass.getpass('Old password: ')
newpassword = getpass.getpass('New password: ')
mgr.update_password(oldpassword, newpassword)
elif args.action == 'update-login':
# Used by app install scripts
mgr.update_login(args.app, args.login, args.password)
elif args.action == 'rebuild-issue':
# Used on VM startup
mgr.rebuild_issue()
elif args.action == 'prepare-container':
# Used with LXC hooks
mgr.prepare_container()
elif args.action == 'register-container':
# Used with LXC hooks
mgr.register_container()
elif args.action == 'unregister-container':
# Used with LXC hooks
mgr.unregister_container()
elif args.action == 'register-proxy':
# Used in init scripts
mgr.register_proxy(args.app)
elif args.action == 'unregister-proxy':
# Used in init scripts
mgr.unregister_proxy(args.app)