mirror of
https://github.com/gryf/boxpy.git
synced 2025-12-19 05:30:18 +01:00
Added two new commands for starting and stopping VMs.
This commit is contained in:
37
box.py
37
box.py
@@ -1427,6 +1427,35 @@ def connect(args):
|
|||||||
f'@localhost:{conf.port}'], False).returncode
|
f'@localhost:{conf.port}'], False).returncode
|
||||||
|
|
||||||
|
|
||||||
|
def _set_vmstate(name, state):
|
||||||
|
|
||||||
|
vbox = VBoxManage(name)
|
||||||
|
if not vbox.get_vm_info():
|
||||||
|
LOG.fatal(f'No machine has been found with a name `{name}`.')
|
||||||
|
return 20
|
||||||
|
|
||||||
|
if vbox.running and state == "start":
|
||||||
|
LOG.info(f'VM "{name}" is already running.')
|
||||||
|
return
|
||||||
|
|
||||||
|
if not vbox.running and state == "stop":
|
||||||
|
LOG.info(f'VM "{name}" is already stopped.')
|
||||||
|
return
|
||||||
|
|
||||||
|
if state == "start":
|
||||||
|
vbox.poweron()
|
||||||
|
else:
|
||||||
|
vbox.acpipowerbutton()
|
||||||
|
|
||||||
|
|
||||||
|
def vmstart(args):
|
||||||
|
_set_vmstate(args.name, 'start')
|
||||||
|
|
||||||
|
|
||||||
|
def vmstop(args):
|
||||||
|
_set_vmstate(args.name, 'stop')
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(description="Automate deployment and "
|
parser = argparse.ArgumentParser(description="Automate deployment and "
|
||||||
"maintenance of VMs using cloud config,"
|
"maintenance of VMs using cloud config,"
|
||||||
@@ -1521,6 +1550,14 @@ def main():
|
|||||||
rebuild.add_argument('-v', '--version', help='distribution version')
|
rebuild.add_argument('-v', '--version', help='distribution version')
|
||||||
rebuild.set_defaults(func=vmrebuild)
|
rebuild.set_defaults(func=vmrebuild)
|
||||||
|
|
||||||
|
start = subparsers.add_parser('start', help='start VM')
|
||||||
|
start.add_argument('name', help='name or UUID of the VM')
|
||||||
|
start.set_defaults(func=vmstart)
|
||||||
|
|
||||||
|
stop = subparsers.add_parser('stop', help='stop VM')
|
||||||
|
stop.add_argument('name', help='name or UUID of the VM')
|
||||||
|
stop.set_defaults(func=vmstop)
|
||||||
|
|
||||||
completion = subparsers.add_parser('completion', help='generate shell '
|
completion = subparsers.add_parser('completion', help='generate shell '
|
||||||
'completion')
|
'completion')
|
||||||
completion.add_argument('shell', choices=['bash'],
|
completion.add_argument('shell', choices=['bash'],
|
||||||
|
|||||||
Reference in New Issue
Block a user