forked from puppetlabs-toy-chest/vmpooler-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmpooler_client_app.py
executable file
·336 lines (257 loc) · 10.5 KB
/
vmpooler_client_app.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/usr/bin/env python
"""
.. module:: vmpooler_client
:synopsis: Manage resources in the vmpooler from the command-line.
:platform: Unix, Linux, Windows
:license: BSD
.. moduleauthor:: Ryan Gard <ryan.gard@puppetlabs.com>
.. moduleauthor:: Joe Pinsonault <joe.pinsonault@puppetlabs.com>
"""
#===================================================================================================
# Imports
#===================================================================================================
from __future__ import print_function
import sys
from vmpooler_client.conf_file import load_config
from vmpooler_client.command_parser import CommandParser, valid_lifetime
from vmpooler_client.commands import config, lifetime, token, vm
from vmpooler_client.version import version
#===================================================================================================
# Functions: Private (Subcommands)
#===================================================================================================
def _configure_config_subcommands(cmd_parser):
"""Configure the subcommands for the "config" top-level command.
Args:
cmd_parser |vmpooler_client.command_parser.CommandParser| = The command parser.
Returns:
|None|
Raises:
|None|
"""
parent = 'config'
# Set Subcommand
sub_cmd = 'set'
cmd_parser.add_sub_command(parent,
sub_cmd,
desc='Set a config value',
func=config.set)
cmd_parser.add_sub_command_arg(parent,
sub_cmd,
name='key',
help='The config option to set')
cmd_parser.add_sub_command_arg(parent,
sub_cmd,
name='value',
help='The value to set for the config option')
# List Subcommand
sub_cmd = 'list'
cmd_parser.add_sub_command(parent,
sub_cmd,
desc='List all the config settings',
func=config.list)
# Get Subcommand
sub_cmd = 'get'
cmd_parser.add_sub_command(parent,
sub_cmd,
desc='Read a config value',
func=config.get)
cmd_parser.add_sub_command_arg(parent,
sub_cmd,
name='key',
help='The config option to read')
# Unset Subcommand
sub_cmd = 'unset'
cmd_parser.add_sub_command(parent,
sub_cmd,
desc='Remove a config option from the config',
func=config.unset)
cmd_parser.add_sub_command_arg(parent,
sub_cmd,
name='key',
help='The config option to unset')
def _configure_lifetime_subcommands(cmd_parser):
"""Configure the subcommands for the "lifetime" top-level command.
Args:
cmd_parser |vmpooler_client.command_parser.CommandParser| = The command parser.
Returns:
|None|
Raises:
|None|
"""
parent = 'lifetime'
# Set Subcommand
sub_cmd = 'set'
cmd_parser.add_sub_command(parent,
sub_cmd,
desc='Set the total lifetime (in hours) for a VM instance',
func=lifetime.set)
cmd_parser.add_sub_command_arg(parent,
sub_cmd,
name='hostname',
help='The hostname of the VM to set the lifetime expiry')
cmd_parser.add_sub_command_arg(parent,
sub_cmd,
name='hours',
help='The number of hours to set for the lifetime expiry',
type=valid_lifetime)
# Get Subcommand
sub_cmd = 'get'
cmd_parser.add_sub_command(parent,
sub_cmd,
desc='Get the lifetime (in hours) for a VM instance',
func=lifetime.get)
cmd_parser.add_sub_command_arg(parent,
sub_cmd,
name='hostname',
help='Retrieve the lifetime expiry for VM hostname')
# Extend Subcommand
sub_cmd = 'extend'
cmd_parser.add_sub_command(parent,
sub_cmd,
desc='Extend the lifetime (in hours) for a VM',
func=lifetime.extend)
cmd_parser.add_sub_command_arg(parent,
sub_cmd,
name='hostname',
help='Extend the lifetime expiry for VM hostname')
cmd_parser.add_sub_command_arg(parent,
sub_cmd,
name='hours',
help='The number of hours to extend the lifetime expiry',
type=valid_lifetime)
def _configure_token_subcommands(cmd_parser):
"""Configure the subcommands for the "token" top-level command.
Args:
cmd_parser |vmpooler_client.command_parser.CommandParser| = The command parser.
Returns:
|None|
Raises:
|None|
"""
parent = 'token'
# Create Subcommand
cmd_parser.add_sub_command(parent,
'create',
desc='Generate an authorization token',
func=token.create)
# Validate Subcommand
sub_cmd = 'validate'
cmd_parser.add_sub_command(parent,
sub_cmd,
desc='Validate an authorization token',
func=token.validate)
cmd_parser.add_sub_command_arg(parent,
sub_cmd,
name='token',
help='The token to validate')
# Revoke Subcommand
sub_cmd = 'revoke'
cmd_parser.add_sub_command(parent,
sub_cmd,
desc='Revoke an authorization token',
func=token.revoke)
cmd_parser.add_sub_command_arg(parent,
sub_cmd,
name='token',
help='The token to revoke')
def _configure_vm_subcommands(cmd_parser):
"""Configure the subcommands for the "vm" top-level command.
Args:
cmd_parser |vmpooler_client.command_parser.CommandParser| = The command parser.
Returns:
|None|
Raises:
|None|
"""
parent = 'vm'
# The string can't have tabs in it because they mess up the formatting
list_help = """list available templates on VM pooler.
Optionally provide a search string to filter the platforms by.
The search string is matched fuzzily. For example:
"centos-6-x86" will be matched by any of the following:
"cos", "cent86", "tox", "centos86", but not:
"centosbob", "centos-6-x86bob", 'bobcos'.
"""
# List Subcommand
sub_cmd = 'list'
cmd_parser.add_sub_command(parent, sub_cmd, desc=list_help, func=vm.list)
cmd_parser.add_sub_command_arg(parent, sub_cmd, name='platform', nargs='?', default='')
# Get Subcommand
sub_cmd = 'get'
cmd_parser.add_sub_command(parent, sub_cmd, desc='Get a vm from the pool', func=vm.get)
cmd_parser.add_sub_command_arg(parent, sub_cmd, name='platform', help='The type of vm to aquire')
# Info Subcommand
sub_cmd = 'info'
cmd_parser.add_sub_command(parent, sub_cmd, desc='Display VM information', func=vm.info)
cmd_parser.add_sub_command_arg(parent, sub_cmd, name='hostname', help='The hostname of the VM')
# Destroy Subcommand
sub_cmd = 'destroy'
cmd_parser.add_sub_command(parent, sub_cmd, desc='Destroy vm', func=vm.destroy)
cmd_parser.add_sub_command_arg(parent, sub_cmd, name='hostname', help='VM hostname to destory')
# Running Subcommand
sub_cmd = 'running'
cmd_parser.add_sub_command(parent, sub_cmd, desc='List running VMs', func=vm.running)
# Destory All Subcommand
sub_cmd = 'destroy_all'
cmd_parser.add_sub_command(parent, sub_cmd, desc='Destroy all running VMs', func=vm.destroy_all)
#===================================================================================================
# Functions: Public
#===================================================================================================
def configure_command_parser(argv):
"""Configure the custom command parser.
Args:
argv |list| = List of CLI commands and arguments.
Returns:
|vmpooler_client.command_parser.CommandParser| = A custom command parser.
Raises:
|None|
"""
# Custom parser for CLI commands and sub-commands
cmd_parser = CommandParser(argv)
# Top-level commands WITHOUT sub-commands
cmd_parser.add_command('version',
desc='Print the vmpooler_client_app version',
func=lambda *args, **kwargs: print(version))
# Top-level commands with sub-commands
cmd_parser.add_command('config', desc='Read and modify the vmpooler configuration file')
cmd_parser.add_command('lifetime', desc='Manage the lifetime of VM instances')
cmd_parser.add_command('token', desc='Manage auth tokens')
cmd_parser.add_command('vm', desc='Discover and reserve VM instances')
# Configure subcommands
_configure_config_subcommands(cmd_parser)
_configure_lifetime_subcommands(cmd_parser)
_configure_token_subcommands(cmd_parser)
_configure_vm_subcommands(cmd_parser)
return cmd_parser
#===================================================================================================
# Main
#===================================================================================================
def main(argv):
"""Main program routine.
Args:
argv |sys.argv| = The raw command-line input from the user.
Returns:
|int| = The exit code to return to the shell.
Raises:
|None|
"""
exit_code = 0
try:
config = load_config()
except RuntimeError as e:
print(e)
# Should succeed the second time
config = load_config()
try:
# Parse the command-line and validate user input
cmd_parser = configure_command_parser(argv)
# Execute the associated behavior with given sub-command and arguments
cmd_parser.parse_execute(config=config)
print('\nSuccess!')
except RuntimeError as e:
exit_code = 1
print(e)
print('\nFailed!')
return exit_code
if __name__ == '__main__':
sys.exit(main(sys.argv))