-
Notifications
You must be signed in to change notification settings - Fork 4
/
dewpoint
executable file
·489 lines (389 loc) · 16.7 KB
/
dewpoint
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#!/usr/bin/python
# Dewpoint is a command line tool for interacting with cloud servers.
#
# Copyright 2010 Second Story
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os, re, sys, time, libcloud, copy, ConfigParser, json
from optparse import OptionParser, HelpFormatter
from libcloud.types import Provider
from libcloud.providers import get_driver
def main(argv=None):
dp = Dewpoint()
return dp.dispatch()
########################
class Dewpoint(object):
"""Dewpoint is a command line tool for interacting with cloud server instances.
Usage: %prog [options] <command> ...
Commands:
create-node <nodename> Create a new node
destroy-node <nodename> Destroy an existing node
find-node <nodename> Find an existing node by name
list-nodes List all existing nodes
list-sizes List all valid server sizes
list-images List all available server images
help <command> Return more detailed help on command
"""
state_name_for = {
0: 'running',
1: 'rebooting',
2: 'terminated',
3: 'pending',
4: 'unknown',
}
def cmd_create_node(self, options, arguments):
"""Create a new node and print its details. If a node with the same name already existed, print its details instead.
Usage: %prog [options] create-node <name>
"""
if len(arguments) < 1:
self.fail("create-node requires the name of the node to create as an argument.")
[name] = arguments
node = self.find_node(name)
if (node):
self.succeed(message="Node \"%s\" already exists!" % name, data=node)
image = self.find_image(options.image)
if (not image):
print options.image
self.fail("Missing or invalid image type provided.")
size = self.find_size(options.size)
if (not size):
print options.size
self.fail("Missing or invalid node size provided.")
node = self.connection.create_node(name=name, image=image, size=size)
if self.options.wait:
running_node = self.wait_for_running_node(name, timeout=self.options.wait)
else:
running_node = None
if (node):
if (running_node): node.state = running_node.state
self.succeed(message="Node \"%s\" created!" % name, data=node)
def cmd_destroy_node(self, options, arguments):
"""Destroy a single node by name.
Usage: %prog [options] destroy-node <name>
"""
if len(arguments) < 1:
self.fail("destroy-node requires the name of the node to destroy as an argument")
[name] = arguments
node = self.wait_for_running_node(name)
if not node:
self.fail("No running node found with name \"%s\"" % name)
elif node.destroy():
self.succeed(message="Node \"%s\" destroyed!" % name)
else:
self.fail("Could not destroy node \"%s\"" % name)
def cmd_find_node(self, options, arguments):
"""Find a single node by name and print its details.
Usage: %prog [options] find-node <name>
"""
if len(arguments) < 1:
self.fail("find-node requires the name of the node as an argument")
[name] = arguments
if self.options.wait:
node = self.wait_for_running_node(name, timeout=self.options.wait)
else:
node = self.find_node(name)
if node:
self.succeed(data=node)
else:
self.fail("No node found with name \"%s\"" % name)
def cmd_list_nodes(self, options, arguments):
"""Fetch a list of all registered nodes.
Usage: %prog [options] list-nodes
"""
nodes = self.connection.list_nodes()
self.succeed(data=nodes)
def cmd_list_sizes(self, options, arguments):
"""Fetch a list of all available server sizes.
Usage: %prog [options] list-sizes
"""
sizes = self.connection.list_sizes()
self.succeed(data=sizes, data_type='size')
def cmd_list_images(self, options, arguments):
"""Fetch a list of all available server images.
Usage: %prog [options] list-images
"""
images = self.connection.list_images()
self.succeed(data=images, data_type='image')
def cmd_help(self, options, arguments):
"""Return more detailed help on command
Usage: %prog [options] help <command>
"""
help_command = arguments.pop(0) if arguments else None
method = self.get_command_method(help_command)
if method:
self.parser.usage = method.__doc__
else:
self.parser.usage = self.__doc__
self.parser.print_help()
#####
def __init__(self):
self.options, self.arguments = self.parse_args()
self.process_ini_files()
self.set_defaults_from_ini()
def process_ini_files(self):
"""Load any config files into our settings property."""
try:
config_path = self.get_config_path()
config = ConfigParser.RawConfigParser()
config.read(config_path)
except Exception as e:
self.fail("Failed to parse configuration file: %s" % e)
self.settings = {}
for section in config.sections():
self.settings[section] = {}
for item in config.items(section):
self.settings[section][item[0]] = item[1]
def set_defaults_from_ini(self):
"""Apply defaults from our config files over any missing options."""
if not self.options.provider:
if 'default' in self.settings and 'provider' in self.settings['default']:
self.options.provider = self.settings['default']['provider']
else:
self.fail("No cloud provider setting was defined.")
provider_lc = self.options.provider.lower()
if not self.options.user:
if provider_lc in self.settings and 'user' in self.settings[provider_lc]:
self.options.user = self.settings[provider_lc]['user']
else:
self.fail("No user was defined for the cloud provider.")
if not self.options.key:
if provider_lc in self.settings and 'key' in self.settings[provider_lc]:
self.options.key = self.settings[provider_lc]['key']
else:
self.fail("No key was defined for the cloud provider.")
if not self.options.size:
if provider_lc in self.settings and 'default_size' in self.settings[provider_lc]:
self.options.size = self.settings[provider_lc]['default_size']
if not self.options.image:
if provider_lc in self.settings and 'default_image' in self.settings[provider_lc]:
self.options.image = self.settings[provider_lc]['default_image']
def get_config_path(self):
"""Retrieve the path to the config file."""
return os.path.expanduser(self.options.config_file)
def get_driver(self):
"""Get the libcloud driver based on the provider option."""
if hasattr(Provider, self.options.provider.upper()):
return get_driver(getattr(Provider, self.options.provider.upper()))
else:
self.fail("Could not find cloud provider with name \"%s\"." % self.options.provider)
def init_connection(self):
"""Initialize connection to cloud provider."""
try:
self.driver = self.get_driver()
self.connection = self.driver(self.options.user, self.options.key)
except libcloud.types.InvalidCredsException:
self.fail("Invalid or missing credentials for cloud provider.")
def dispatch(self):
"""Find and execute the command method."""
arguments = copy.copy(self.arguments)
options = copy.copy(self.options)
command = arguments.pop(0) if arguments else 'help'
method = self.get_command_method(command)
if not method: method = self.cmd_help
if (method != self.cmd_help):
self.init_connection()
return method(options, arguments)
def get_command_method(self, command):
"""Given a command name, return the command method."""
if not command: return None
cmd_formatted = 'cmd_' + command.replace('-', '_')
return getattr(self, cmd_formatted, None)
def parse_args(self):
"""Set up the command-line options for dewpoint."""
formatter = DocstringHelpFormatter()
self.parser = OptionParser(usage='', version="%prog 0.1.0", formatter=formatter)
self.parser.add_option("--size", "-s",
type="int",
help="size of image to create, as MB of RAM."
)
self.parser.add_option("--image", "-i",
help="name of image to use."
)
self.parser.add_option("--wait", "-w",
type="int",
help="when creating or finding nodes, wait up to WAIT seconds until the node is running before returning"
)
self.parser.add_option("--json", "-j",
action="store_true", dest="json",
help="return information about results in json format instead of human-readable"
)
self.parser.add_option("--provider",
help="Cloud provider to use."
)
self.parser.add_option("--user",
help="API username or id"
)
self.parser.add_option("--key",
help="API key"
)
self.parser.add_option("--config-file",
help="Path to a custom configuration file in ini format. [default: %default]"
)
self.parser.set_defaults(
config_file='~/.dewpoint/dewpoint.ini'
)
return self.parser.parse_args()
def fail(self, message):
"""Print an error message and stop execution with an error code."""
output = self.get_output(message=message)
self.print_formatted_output(output)
sys.exit(1)
def succeed(self, message=None, data=None, data_type="node"):
"""Print a success message and any provided data, then stop execution."""
output = self.get_output(message=message, data=data, data_type=data_type)
self.print_formatted_output(output)
sys.exit(0)
def get_output(self, message=None, data=None, data_type="node"):
"""Return a dictionary to be used as a content source for output."""
output = {}
if message: output['message'] = message
if data: output['data'] = []
if is_dict_like(data): data = [data]
if is_list_like(data):
for item in data:
output['data'].append(self.prepare_output_item(item, data_type))
return output
def print_formatted_output(self, output):
"""Format and print our output dictionary."""
if self.options.json:
self.print_json_output(output)
else:
self.print_text_output(output)
def print_json_output(self, output):
"""Print an output dictionary as json."""
print json.dumps(output, indent=2, sort_keys=True)
def print_text_output(self, output):
"""Print an output dictionary as text."""
if 'message' in output:
print output['message']
if 'data' in output and output['data']:
print "-----"
for item in output['data']:
self.print_item(item)
def print_item(self, item):
"""Print a single data item from an output dictionary as text."""
for key in item.keys():
print "%s: %s" % (key, item[key])
print "-----"
def prepare_output_item(self, item, data_type):
"""Convert a single data item into a dictionary for our output dictionary."""
format_method_name = 'get_output_for_' + data_type
method = getattr(self, format_method_name)
if (method): return method(item)
else: return None
def get_output_for_node(self, node):
"""Convert a single 'node' data item into a dictionary for our output dictionary."""
result = {}
result['uuid'] = node.uuid
result['name'] = node.name
result['state'] = node.state
result['state_name'] = self.state_name_for[node.state]
result['public_ip'] = node.public_ip
result['private_ip'] = node.private_ip
result['flavor_id'] = node.extra['flavorId']
result['image_id'] = node.extra['imageId']
result['password'] = node.extra['password']
return result
def get_output_for_image(self, image):
"""Convert a single 'image' data item into a dictionary for our output dictionary."""
result = {}
result["id"] = image.id
result["name"] = image.name
return result
def get_output_for_size(self, size):
"""Convert a single 'size' data item into a dictionary for our output dictionary."""
result = {}
result["id"] = size.id
result["name"] = size.name
result["ram"] = size.ram
result["disk"] = size.disk
result["bandwidth"] = size.bandwidth
result["price"] = size.price
return result
#####
def find_size(self, ram):
"""Find a node size object based on its ram."""
sizes = self.connection.list_sizes()
ram = int(ram)
return next((size for size in sizes if size.ram == ram), None)
def find_image(self, name):
"""Find an image type object based on its name."""
images = self.connection.list_images()
return next((image for image in images if name == image.name), None)
def find_node(self, name):
"""Find a node object based on its name."""
nodes = self.connection.list_nodes()
return next((node for node in nodes if name == node.name), None)
def wait_for_running_node(self, name, poll_period = 2, timeout=30):
"""Find a node object based on its name, waiting for it to be running.
This method blocks up to timeout seconds until the node object is
confirmed to be running. After the timeout, it may return a non-running
node, so you still need to check the state before performing
further operations on the node.
"""
elapsed_time = 0
while elapsed_time < timeout:
node = self.find_node(name)
if not node: return None
if node.state == 0: return node
time.sleep(poll_period)
elapsed_time = elapsed_time + poll_period
return node
#####
class DocstringHelpFormatter (HelpFormatter):
"""Format help based on docstrings."""
def __init__(self,
indent_increment=2,
max_help_position=24,
width=None,
short_first=1):
HelpFormatter.__init__(
self, indent_increment, max_help_position, width, short_first)
def format_usage(self, usage):
return ("%s\n") % self.trim(usage)
def format_heading(self, heading):
return "%*s%s:\n" % (self.current_indent, "", heading)
def trim(self, docstring):
"""Trim a doctring to remove indendation, as per PEP 257"""
if not docstring:
return ''
lines = docstring.expandtabs().splitlines()
indent = sys.maxint
for line in lines[1:]:
stripped = line.lstrip()
if stripped:
indent = min(indent, len(line) - len(stripped))
trimmed = [lines[0].strip()]
if indent < sys.maxint:
for line in lines[1:]:
trimmed.append(line[indent:].rstrip())
while trimmed and not trimmed[-1]:
trimmed.pop()
while trimmed and not trimmed[0]:
trimmed.pop(0)
return '\n'.join(trimmed)
#####
def is_dict_like(obj):
"""Check if the object appears to be dictionary-like."""
if obj and (hasattr(obj, '__dict__') or (hasattr(obj, 'keys') and hasattr(obj, '__getitem__'))):
return True
else:
return False
def is_list_like(obj):
"""Check if the object appears to be list-like."""
if obj and (not hasattr(obj, 'keys')) and hasattr(obj, '__getitem__'):
return True
else:
return False
if __name__ == "__main__":
sys.exit(main())