This repository has been archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvbox_cli.py
executable file
·214 lines (184 loc) · 6.4 KB
/
vbox_cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python3
"""
Python CLI tool to perform basic VirtualBox processes tasks via `vboxmanage`
Authored by Justin W. Flory.
Wrapper program for the VirtualBox `vboxmanage` tool provided by Oracle. This
wrapper reduces complexity for common, frequent operations. As written, it
could be used for automation for basic VirtualBox operations and expanded if
needed.
This wrapper script was originally written for NSSA-244 Virtualization course
assignment at the Rochester Institute of Technology in Spring 2019. Prof.
Garret Arcoraci taught the course.
LICENSE: Mozilla Public License 2.0
"""
import argparse
import subprocess
import sys
def opt_create(args):
"""Create a new VirtualBox virtual machine with vboxmanage."""
vbox_cmd_str = 'vboxmanage createvm --register --name ' \
+ args.vbox_vm_name
if args.vbox_vm_groups is not None:
vbox_cmd_str += ' --groups ' + args.vbox_vm_groups
elif args.vbox_vm_os is not None:
vbox_cmd_str += ' --ostype ' + args.vbox_vm_os
subprocess.run(vbox_cmd_str.split(), check=True)
def opt_delete(args):
"""Delete an existing VirtualBox virtual machine with vboxmanage."""
subprocess.run([
'vboxmanage',
'unregistervm',
'--delete',
args.vbox_vm_identifier],
check=True)
def opt_info(args):
"""List all VirtualBox virtual machines or details from a specific one."""
# Look up details for a specific VM
if args.vbox_vm_identifier is not None:
subprocess.run([
'vboxmanage',
'showvminfo',
'--details',
args.vbox_vm_identifier],
check=True)
# List all registered VMs
else:
subprocess.run([
'vboxmanage',
'list',
'vms'],
check=True)
def opt_status(args):
"""Start or stop an existing VirtualBox virtual machine."""
# Start a headless VM
if args.vbox_vm_action == 'start':
subprocess.run([
'vboxmanage',
'startvm',
args.vbox_vm_identifier,
'--type',
'headless'],
check=True)
# Stop a running VM
elif args.vbox_vm_action == 'stop':
subprocess.run([
'vboxmanage',
'controlvm',
args.vbox_vm_identifier,
'poweroff'],
check=True)
else:
print('//TODO: proper error handling')
def main():
"""Main method to set up argument parsing."""
# Set up argument parser and subparsers
parser = argparse.ArgumentParser(
description='Wrapper script to simplify basic VirtualBox tasks',
epilog='Note: "VMs" refers to virtual machines')
subparsers = parser.add_subparsers(
help='sub-command help')
# Parser for "create" sub-command
parser_create = subparsers.add_parser(
'create',
help='create a new VirtualBox VM')
parser_create.add_argument(
'--name',
dest='vbox_vm_name',
help='(required) name of new VM',
metavar='<vm_name>',
required=True)
parser_create.add_argument(
'-g', '--groups',
dest='vbox_vm_groups',
help='optionally specify groups to assign to VM',
metavar='<group>, ...')
parser_create.add_argument(
'-o', '--operating-system',
dest='vbox_vm_os',
help='optionally specify an operating system to use',
metavar='<os_name>')
# Parser for "delete" sub-command
parser_delete = subparsers.add_parser(
'delete',
help='delete an existing VirtualBox VM')
vm_delete_exclusive_args = \
parser_delete.add_mutually_exclusive_group(required=True)
vm_delete_exclusive_args.add_argument(
'--name',
dest='vbox_vm_identifier',
help='name of VM to permanently delete',
metavar='<vm_name>')
vm_delete_exclusive_args.add_argument(
'--uuid',
dest='vbox_vm_identifier',
help='UUID of VM to permanently delete',
metavar='<vm_uuid>')
# Parser for "info" sub-command
parser_info = subparsers.add_parser(
'info',
help='list all VirtualBox VMs or get info about a specific VM')
vm_info_exclusive_args = \
parser_info.add_mutually_exclusive_group()
vm_info_exclusive_args.add_argument(
'--name',
dest='vbox_vm_identifier',
help='name of VM',
metavar='<vm_name>')
vm_info_exclusive_args.add_argument(
'--uuid',
dest='vbox_vm_identifier',
help='UUID of VM',
metavar='<vm_uuid>')
# Parser for "status" sub-command
parser_status = subparsers.add_parser(
'status',
help='start or stop a VM')
vm_status_exclusive_args_ops = \
parser_status.add_mutually_exclusive_group(required=True)
vm_status_exclusive_args_ops.add_argument(
'--start',
action='store_const',
const='start',
dest='vbox_vm_action',
help='start a specified VM')
vm_status_exclusive_args_ops.add_argument(
'--stop',
action='store_const',
const='stop',
dest='vbox_vm_action',
help='stop a specified VM')
vm_status_exclusive_args_label = \
parser_status.add_mutually_exclusive_group(required=True)
vm_status_exclusive_args_label.add_argument(
'--name',
dest='vbox_vm_identifier',
help='name of VM',
metavar='<vm_name>')
vm_status_exclusive_args_label.add_argument(
'--uuid',
dest='vbox_vm_identifier',
help='UUID of VM',
metavar='<vm_uuid>')
# Actual processing of arguments and send to function
args = parser.parse_args()
if sys.argv[1] == 'create':
opt_create(args)
elif sys.argv[1] == 'delete':
opt_delete(args)
elif sys.argv[1] == 'info':
opt_info(args)
elif sys.argv[1] == 'status':
opt_status(args)
else:
print('//TODO: proper error handling')
main()
# Resources used:
# - https://docs.python.org/3.7/library/argparse.html
# - https://docs.python.org/3.7/howto/argparse.html
# - https://stackoverflow.com/a/8810797
# - https://blog.scottlowe.org/2016/11/10/intro-to-vbox-cli/
# - https://www.cyberciti.biz/faq/python-execute-unix-linux-command-examples/
# - https://docs.python.org/3/library/subprocess.html
# - https://docs.python.org/3.7/tutorial/datastructures.html
# - https://stackoverflow.com/a/8113787
# - https://www.python.org/dev/peps/pep-0257/