root / kamaki / cli.py @ 8614814c
History | View | Annotate | Download (30.5 kB)
1 |
#!/usr/bin/env python
|
---|---|
2 |
|
3 |
# Copyright 2011-2012 GRNET S.A. All rights reserved.
|
4 |
#
|
5 |
# Redistribution and use in source and binary forms, with or
|
6 |
# without modification, are permitted provided that the following
|
7 |
# conditions are met:
|
8 |
#
|
9 |
# 1. Redistributions of source code must retain the above
|
10 |
# copyright notice, this list of conditions and the following
|
11 |
# disclaimer.
|
12 |
#
|
13 |
# 2. Redistributions in binary form must reproduce the above
|
14 |
# copyright notice, this list of conditions and the following
|
15 |
# disclaimer in the documentation and/or other materials
|
16 |
# provided with the distribution.
|
17 |
#
|
18 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
19 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
20 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
21 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
22 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
23 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
24 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
25 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
26 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
27 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
28 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
29 |
# POSSIBILITY OF SUCH DAMAGE.
|
30 |
#
|
31 |
# The views and conclusions contained in the software and
|
32 |
# documentation are those of the authors and should not be
|
33 |
# interpreted as representing official policies, either expressed
|
34 |
# or implied, of GRNET S.A.
|
35 |
|
36 |
"""
|
37 |
To add a command create a new class and add a 'command' decorator. The class
|
38 |
must have a 'main' method which will contain the code to be executed.
|
39 |
Optionally a command can implement an 'update_parser' class method in order
|
40 |
to add command line arguments, or modify the OptionParser in any way.
|
41 |
|
42 |
The name of the class is important and it will determine the name and grouping
|
43 |
of the command. This behavior can be overriden with the 'group' and 'name'
|
44 |
decorator arguments:
|
45 |
|
46 |
@command(api='compute')
|
47 |
class server_list(object):
|
48 |
# This command will be named 'list' under group 'server'
|
49 |
...
|
50 |
|
51 |
@command(api='compute', name='ls')
|
52 |
class server_list(object):
|
53 |
# This command will be named 'ls' under group 'server'
|
54 |
...
|
55 |
|
56 |
The docstring of a command class will be used as the command description in
|
57 |
help messages, unless overriden with the 'description' decorator argument.
|
58 |
|
59 |
The syntax of a command will be generated dynamically based on the signature
|
60 |
of the 'main' method, unless overriden with the 'syntax' decorator argument:
|
61 |
|
62 |
def main(self, server_id, network=None):
|
63 |
# This syntax of this command will be: '<server id> [network]'
|
64 |
...
|
65 |
|
66 |
The order of commands is important, it will be preserved in the help output.
|
67 |
"""
|
68 |
|
69 |
from __future__ import print_function |
70 |
|
71 |
import inspect |
72 |
import logging |
73 |
import sys |
74 |
|
75 |
from argparse import ArgumentParser |
76 |
from base64 import b64encode |
77 |
from os.path import abspath, basename, exists |
78 |
from sys import exit, stdout, stderr |
79 |
|
80 |
try:
|
81 |
from collections import OrderedDict |
82 |
except ImportError: |
83 |
from ordereddict import OrderedDict |
84 |
|
85 |
from colors import magenta, red, yellow |
86 |
from progress.bar import IncrementalBar |
87 |
from requests.exceptions import ConnectionError |
88 |
|
89 |
from . import clients
|
90 |
from .config import Config |
91 |
from .utils import print_addresses, print_dict, print_items |
92 |
|
93 |
|
94 |
_commands = OrderedDict() |
95 |
|
96 |
|
97 |
GROUPS = { |
98 |
'config': "Configuration commands", |
99 |
'server': "Compute API server commands", |
100 |
'flavor': "Compute API flavor commands", |
101 |
'image': "Compute or Glance API image commands", |
102 |
'network': "Compute API network commands (Cyclades extension)", |
103 |
'store': "Storage API commands", |
104 |
'astakos': "Astakos API commands"} |
105 |
|
106 |
|
107 |
class ProgressBar(IncrementalBar): |
108 |
suffix = '%(percent)d%% - %(eta)ds'
|
109 |
|
110 |
|
111 |
def command(api=None, group=None, name=None, syntax=None): |
112 |
"""Class decorator that registers a class as a CLI command."""
|
113 |
|
114 |
def decorator(cls): |
115 |
grp, sep, cmd = cls.__name__.partition('_')
|
116 |
if not sep: |
117 |
grp, cmd = None, cls.__name__
|
118 |
|
119 |
cls.api = api |
120 |
cls.group = group or grp
|
121 |
cls.name = name or cmd
|
122 |
|
123 |
short_description, sep, long_description = cls.__doc__.partition('\n')
|
124 |
cls.description = short_description |
125 |
cls.long_description = long_description or short_description
|
126 |
|
127 |
cls.syntax = syntax |
128 |
if cls.syntax is None: |
129 |
# Generate a syntax string based on main's arguments
|
130 |
spec = inspect.getargspec(cls.main.im_func) |
131 |
args = spec.args[1:]
|
132 |
n = len(args) - len(spec.defaults or ()) |
133 |
required = ' '.join('<%s>' % x.replace('_', ' ') for x in args[:n]) |
134 |
optional = ' '.join('[%s]' % x.replace('_', ' ') for x in args[n:]) |
135 |
cls.syntax = ' '.join(x for x in [required, optional] if x) |
136 |
if spec.varargs:
|
137 |
cls.syntax += ' <%s ...>' % spec.varargs
|
138 |
|
139 |
if cls.group not in _commands: |
140 |
_commands[cls.group] = OrderedDict() |
141 |
_commands[cls.group][cls.name] = cls |
142 |
return cls
|
143 |
return decorator
|
144 |
|
145 |
|
146 |
@command(api='config') |
147 |
class config_list(object): |
148 |
"""List configuration options"""
|
149 |
|
150 |
def update_parser(self, parser): |
151 |
parser.add_argument('-a', dest='all', action='store_true', |
152 |
default=False, help='include default values') |
153 |
|
154 |
def main(self): |
155 |
include_defaults = self.args.all
|
156 |
for section in sorted(self.config.sections()): |
157 |
items = self.config.items(section, include_defaults)
|
158 |
for key, val in sorted(items): |
159 |
print('%s.%s = %s' % (section, key, val))
|
160 |
|
161 |
|
162 |
@command(api='config') |
163 |
class config_get(object): |
164 |
"""Show a configuration option"""
|
165 |
|
166 |
def main(self, option): |
167 |
section, sep, key = option.rpartition('.')
|
168 |
section = section or 'global' |
169 |
value = self.config.get(section, key)
|
170 |
if value is not None: |
171 |
print(value) |
172 |
|
173 |
|
174 |
@command(api='config') |
175 |
class config_set(object): |
176 |
"""Set a configuration option"""
|
177 |
|
178 |
def main(self, option, value): |
179 |
section, sep, key = option.rpartition('.')
|
180 |
section = section or 'global' |
181 |
self.config.set(section, key, value)
|
182 |
self.config.write()
|
183 |
|
184 |
|
185 |
@command(api='config') |
186 |
class config_delete(object): |
187 |
"""Delete a configuration option (and use the default value)"""
|
188 |
|
189 |
def main(self, option): |
190 |
section, sep, key = option.rpartition('.')
|
191 |
section = section or 'global' |
192 |
self.config.remove_option(section, key)
|
193 |
self.config.write()
|
194 |
|
195 |
|
196 |
@command(api='compute') |
197 |
class server_list(object): |
198 |
"""List servers"""
|
199 |
|
200 |
def update_parser(self, parser): |
201 |
parser.add_argument('-l', dest='detail', action='store_true', |
202 |
default=False, help='show detailed output') |
203 |
|
204 |
def main(self): |
205 |
servers = self.client.list_servers(self.args.detail) |
206 |
print_items(servers) |
207 |
|
208 |
|
209 |
@command(api='compute') |
210 |
class server_info(object): |
211 |
"""Get server details"""
|
212 |
|
213 |
def main(self, server_id): |
214 |
server = self.client.get_server_details(int(server_id)) |
215 |
print_dict(server) |
216 |
|
217 |
|
218 |
@command(api='compute') |
219 |
class server_create(object): |
220 |
"""Create a server"""
|
221 |
|
222 |
def update_parser(self, parser): |
223 |
parser.add_argument('--personality', dest='personalities', |
224 |
action='append', default=[],
|
225 |
metavar='PATH[,SERVER PATH[,OWNER[,GROUP,[MODE]]]]',
|
226 |
help='add a personality file')
|
227 |
|
228 |
def main(self, name, flavor_id, image_id): |
229 |
personalities = [] |
230 |
for personality in self.args.personalities: |
231 |
p = personality.split(',')
|
232 |
p.extend([None] * (5 - len(p))) # Fill missing fields with None |
233 |
|
234 |
path = p[0]
|
235 |
|
236 |
if not path: |
237 |
print("Invalid personality argument '%s'" % p)
|
238 |
return 1 |
239 |
if not exists(path): |
240 |
print("File %s does not exist" % path)
|
241 |
return 1 |
242 |
|
243 |
with open(path) as f: |
244 |
contents = b64encode(f.read()) |
245 |
|
246 |
d = {'path': p[1] or abspath(path), 'contents': contents} |
247 |
if p[2]: |
248 |
d['owner'] = p[2] |
249 |
if p[3]: |
250 |
d['group'] = p[3] |
251 |
if p[4]: |
252 |
d['mode'] = int(p[4]) |
253 |
personalities.append(d) |
254 |
|
255 |
reply = self.client.create_server(name, int(flavor_id), image_id, |
256 |
personalities) |
257 |
print_dict(reply) |
258 |
|
259 |
|
260 |
@command(api='compute') |
261 |
class server_rename(object): |
262 |
"""Update a server's name"""
|
263 |
|
264 |
def main(self, server_id, new_name): |
265 |
self.client.update_server_name(int(server_id), new_name) |
266 |
|
267 |
|
268 |
@command(api='compute') |
269 |
class server_delete(object): |
270 |
"""Delete a server"""
|
271 |
|
272 |
def main(self, server_id): |
273 |
self.client.delete_server(int(server_id)) |
274 |
|
275 |
|
276 |
@command(api='compute') |
277 |
class server_reboot(object): |
278 |
"""Reboot a server"""
|
279 |
|
280 |
def update_parser(self, parser): |
281 |
parser.add_argument('-f', dest='hard', action='store_true', |
282 |
default=False, help='perform a hard reboot') |
283 |
|
284 |
def main(self, server_id): |
285 |
self.client.reboot_server(int(server_id), self.args.hard) |
286 |
|
287 |
|
288 |
@command(api='cyclades') |
289 |
class server_start(object): |
290 |
"""Start a server"""
|
291 |
|
292 |
def main(self, server_id): |
293 |
self.client.start_server(int(server_id)) |
294 |
|
295 |
|
296 |
@command(api='cyclades') |
297 |
class server_shutdown(object): |
298 |
"""Shutdown a server"""
|
299 |
|
300 |
def main(self, server_id): |
301 |
self.client.shutdown_server(int(server_id)) |
302 |
|
303 |
|
304 |
@command(api='cyclades') |
305 |
class server_console(object): |
306 |
"""Get a VNC console"""
|
307 |
|
308 |
def main(self, server_id): |
309 |
reply = self.client.get_server_console(int(server_id)) |
310 |
print_dict(reply) |
311 |
|
312 |
|
313 |
@command(api='cyclades') |
314 |
class server_firewall(object): |
315 |
"""Set the server's firewall profile"""
|
316 |
|
317 |
def main(self, server_id, profile): |
318 |
self.client.set_firewall_profile(int(server_id), profile) |
319 |
|
320 |
|
321 |
@command(api='cyclades') |
322 |
class server_addr(object): |
323 |
"""List a server's addresses"""
|
324 |
|
325 |
def main(self, server_id, network=None): |
326 |
reply = self.client.list_server_addresses(int(server_id), network) |
327 |
margin = max(len(x['name']) for x in reply) |
328 |
print_addresses(reply, margin) |
329 |
|
330 |
|
331 |
@command(api='compute') |
332 |
class server_meta(object): |
333 |
"""Get a server's metadata"""
|
334 |
|
335 |
def main(self, server_id, key=None): |
336 |
reply = self.client.get_server_metadata(int(server_id), key) |
337 |
print_dict(reply) |
338 |
|
339 |
|
340 |
@command(api='compute') |
341 |
class server_addmeta(object): |
342 |
"""Add server metadata"""
|
343 |
|
344 |
def main(self, server_id, key, val): |
345 |
reply = self.client.create_server_metadata(int(server_id), key, val) |
346 |
print_dict(reply) |
347 |
|
348 |
|
349 |
@command(api='compute') |
350 |
class server_setmeta(object): |
351 |
"""Update server's metadata"""
|
352 |
|
353 |
def main(self, server_id, key, val): |
354 |
metadata = {key: val} |
355 |
reply = self.client.update_server_metadata(int(server_id), **metadata) |
356 |
print_dict(reply) |
357 |
|
358 |
|
359 |
@command(api='compute') |
360 |
class server_delmeta(object): |
361 |
"""Delete server metadata"""
|
362 |
|
363 |
def main(self, server_id, key): |
364 |
self.client.delete_server_metadata(int(server_id), key) |
365 |
|
366 |
|
367 |
@command(api='cyclades') |
368 |
class server_stats(object): |
369 |
"""Get server statistics"""
|
370 |
|
371 |
def main(self, server_id): |
372 |
reply = self.client.get_server_stats(int(server_id)) |
373 |
print_dict(reply, exclude=('serverRef',))
|
374 |
|
375 |
|
376 |
@command(api='compute') |
377 |
class flavor_list(object): |
378 |
"""List flavors"""
|
379 |
|
380 |
def update_parser(self, parser): |
381 |
parser.add_argument('-l', dest='detail', action='store_true', |
382 |
default=False, help='show detailed output') |
383 |
|
384 |
def main(self): |
385 |
flavors = self.client.list_flavors(self.args.detail) |
386 |
print_items(flavors) |
387 |
|
388 |
|
389 |
@command(api='compute') |
390 |
class flavor_info(object): |
391 |
"""Get flavor details"""
|
392 |
|
393 |
def main(self, flavor_id): |
394 |
flavor = self.client.get_flavor_details(int(flavor_id)) |
395 |
print_dict(flavor) |
396 |
|
397 |
|
398 |
@command(api='compute') |
399 |
class image_list(object): |
400 |
"""List images"""
|
401 |
|
402 |
def update_parser(self, parser): |
403 |
parser.add_argument('-l', dest='detail', action='store_true', |
404 |
default=False, help='show detailed output') |
405 |
|
406 |
def main(self): |
407 |
images = self.client.list_images(self.args.detail) |
408 |
print_items(images) |
409 |
|
410 |
|
411 |
@command(api='compute') |
412 |
class image_info(object): |
413 |
"""Get image details"""
|
414 |
|
415 |
def main(self, image_id): |
416 |
image = self.client.get_image_details(image_id)
|
417 |
print_dict(image) |
418 |
|
419 |
|
420 |
@command(api='compute') |
421 |
class image_delete(object): |
422 |
"""Delete image"""
|
423 |
|
424 |
def main(self, image_id): |
425 |
self.client.delete_image(image_id)
|
426 |
|
427 |
|
428 |
@command(api='compute') |
429 |
class image_properties(object): |
430 |
"""Get image properties"""
|
431 |
|
432 |
def main(self, image_id, key=None): |
433 |
reply = self.client.get_image_metadata(image_id, key)
|
434 |
print_dict(reply) |
435 |
|
436 |
|
437 |
@command(api='compute') |
438 |
class image_addproperty(object): |
439 |
"""Add an image property"""
|
440 |
|
441 |
def main(self, image_id, key, val): |
442 |
reply = self.client.create_image_metadata(image_id, key, val)
|
443 |
print_dict(reply) |
444 |
|
445 |
|
446 |
@command(api='compute') |
447 |
class image_setproperty(object): |
448 |
"""Update an image property"""
|
449 |
|
450 |
def main(self, image_id, key, val): |
451 |
metadata = {key: val} |
452 |
reply = self.client.update_image_metadata(image_id, **metadata)
|
453 |
print_dict(reply) |
454 |
|
455 |
|
456 |
@command(api='compute') |
457 |
class image_delproperty(object): |
458 |
"""Delete an image property"""
|
459 |
|
460 |
def main(self, image_id, key): |
461 |
self.client.delete_image_metadata(image_id, key)
|
462 |
|
463 |
|
464 |
@command(api='cyclades') |
465 |
class network_list(object): |
466 |
"""List networks"""
|
467 |
|
468 |
def update_parser(self, parser): |
469 |
parser.add_argument('-l', dest='detail', action='store_true', |
470 |
default=False, help='show detailed output') |
471 |
|
472 |
def main(self): |
473 |
networks = self.client.list_networks(self.args.detail) |
474 |
print_items(networks) |
475 |
|
476 |
|
477 |
@command(api='cyclades') |
478 |
class network_create(object): |
479 |
"""Create a network"""
|
480 |
|
481 |
def main(self, name): |
482 |
reply = self.client.create_network(name)
|
483 |
print_dict(reply) |
484 |
|
485 |
|
486 |
@command(api='cyclades') |
487 |
class network_info(object): |
488 |
"""Get network details"""
|
489 |
|
490 |
def main(self, network_id): |
491 |
network = self.client.get_network_details(network_id)
|
492 |
print_dict(network) |
493 |
|
494 |
|
495 |
@command(api='cyclades') |
496 |
class network_rename(object): |
497 |
"""Update network name"""
|
498 |
|
499 |
def main(self, network_id, new_name): |
500 |
self.client.update_network_name(network_id, new_name)
|
501 |
|
502 |
|
503 |
@command(api='cyclades') |
504 |
class network_delete(object): |
505 |
"""Delete a network"""
|
506 |
|
507 |
def main(self, network_id): |
508 |
self.client.delete_network(network_id)
|
509 |
|
510 |
|
511 |
@command(api='cyclades') |
512 |
class network_connect(object): |
513 |
"""Connect a server to a network"""
|
514 |
|
515 |
def main(self, server_id, network_id): |
516 |
self.client.connect_server(server_id, network_id)
|
517 |
|
518 |
|
519 |
@command(api='cyclades') |
520 |
class network_disconnect(object): |
521 |
"""Disconnect a server from a network"""
|
522 |
|
523 |
def main(self, server_id, network_id): |
524 |
self.client.disconnect_server(server_id, network_id)
|
525 |
|
526 |
|
527 |
@command(api='image') |
528 |
class image_public(object): |
529 |
"""List public images"""
|
530 |
|
531 |
def update_parser(self, parser): |
532 |
parser.add_argument('-l', dest='detail', action='store_true', |
533 |
default=False, help='show detailed output') |
534 |
parser.add_argument('--container-format', dest='container_format', |
535 |
metavar='FORMAT', help='filter by container format') |
536 |
parser.add_argument('--disk-format', dest='disk_format', |
537 |
metavar='FORMAT', help='filter by disk format') |
538 |
parser.add_argument('--name', dest='name', metavar='NAME', |
539 |
help='filter by name')
|
540 |
parser.add_argument('--size-min', dest='size_min', metavar='BYTES', |
541 |
help='filter by minimum size')
|
542 |
parser.add_argument('--size-max', dest='size_max', metavar='BYTES', |
543 |
help='filter by maximum size')
|
544 |
parser.add_argument('--status', dest='status', metavar='STATUS', |
545 |
help='filter by status')
|
546 |
parser.add_argument('--order', dest='order', metavar='FIELD', |
547 |
help='order by FIELD (use a - prefix to reverse order)')
|
548 |
|
549 |
def main(self): |
550 |
filters = {} |
551 |
for filter in ('container_format', 'disk_format', 'name', 'size_min', |
552 |
'size_max', 'status'): |
553 |
val = getattr(self.args, filter, None) |
554 |
if val is not None: |
555 |
filters[filter] = val
|
556 |
|
557 |
order = self.args.order or '' |
558 |
images = self.client.list_public(self.args.detail, filters=filters, |
559 |
order=order) |
560 |
print_items(images, title=('name',))
|
561 |
|
562 |
|
563 |
@command(api='image') |
564 |
class image_meta(object): |
565 |
"""Get image metadata"""
|
566 |
|
567 |
def main(self, image_id): |
568 |
image = self.client.get_meta(image_id)
|
569 |
print_dict(image) |
570 |
|
571 |
|
572 |
@command(api='image') |
573 |
class image_register(object): |
574 |
"""Register an image"""
|
575 |
|
576 |
def update_parser(self, parser): |
577 |
parser.add_argument('--checksum', dest='checksum', metavar='CHECKSUM', |
578 |
help='set image checksum')
|
579 |
parser.add_argument('--container-format', dest='container_format', |
580 |
metavar='FORMAT', help='set container format') |
581 |
parser.add_argument('--disk-format', dest='disk_format', |
582 |
metavar='FORMAT', help='set disk format') |
583 |
parser.add_argument('--id', dest='id', |
584 |
metavar='ID', help='set image ID') |
585 |
parser.add_argument('--owner', dest='owner', |
586 |
metavar='USER', help='set image owner (admin only)') |
587 |
parser.add_argument('--property', dest='properties', action='append', |
588 |
metavar='KEY=VAL',
|
589 |
help='add a property (can be used multiple times)')
|
590 |
parser.add_argument('--public', dest='is_public', action='store_true', |
591 |
help='mark image as public')
|
592 |
parser.add_argument('--size', dest='size', metavar='SIZE', |
593 |
help='set image size')
|
594 |
|
595 |
def main(self, name, location): |
596 |
if not location.startswith('pithos://'): |
597 |
account = self.config.get('storage', 'account') |
598 |
container = self.config.get('storage', 'container') |
599 |
location = 'pithos://%s/%s/%s' % (account, container, location)
|
600 |
|
601 |
params = {} |
602 |
for key in ('checksum', 'container_format', 'disk_format', 'id', |
603 |
'owner', 'size'): |
604 |
val = getattr(self.args, key) |
605 |
if val is not None: |
606 |
params[key] = val |
607 |
|
608 |
if self.args.is_public: |
609 |
params['is_public'] = 'true' |
610 |
|
611 |
properties = {} |
612 |
for property in self.args.properties or []: |
613 |
key, sep, val = property.partition('=') |
614 |
if not sep: |
615 |
print("Invalid property '%s'" % property) |
616 |
return 1 |
617 |
properties[key.strip()] = val.strip() |
618 |
|
619 |
self.client.register(name, location, params, properties)
|
620 |
|
621 |
|
622 |
@command(api='image') |
623 |
class image_members(object): |
624 |
"""Get image members"""
|
625 |
|
626 |
def main(self, image_id): |
627 |
members = self.client.list_members(image_id)
|
628 |
for member in members: |
629 |
print(member['member_id'])
|
630 |
|
631 |
|
632 |
@command(api='image') |
633 |
class image_shared(object): |
634 |
"""List shared images"""
|
635 |
|
636 |
def main(self, member): |
637 |
images = self.client.list_shared(member)
|
638 |
for image in images: |
639 |
print(image['image_id'])
|
640 |
|
641 |
|
642 |
@command(api='image') |
643 |
class image_addmember(object): |
644 |
"""Add a member to an image"""
|
645 |
|
646 |
def main(self, image_id, member): |
647 |
self.client.add_member(image_id, member)
|
648 |
|
649 |
|
650 |
@command(api='image') |
651 |
class image_delmember(object): |
652 |
"""Remove a member from an image"""
|
653 |
|
654 |
def main(self, image_id, member): |
655 |
self.client.remove_member(image_id, member)
|
656 |
|
657 |
|
658 |
@command(api='image') |
659 |
class image_setmembers(object): |
660 |
"""Set the members of an image"""
|
661 |
|
662 |
def main(self, image_id, *member): |
663 |
self.client.set_members(image_id, member)
|
664 |
|
665 |
|
666 |
class _store_account_command(object): |
667 |
"""Base class for account level storage commands"""
|
668 |
|
669 |
def update_parser(self, parser): |
670 |
parser.add_argument('--account', dest='account', metavar='NAME', |
671 |
help="Specify an account to use")
|
672 |
|
673 |
def progress(self, message): |
674 |
"""Return a generator function to be used for progress tracking"""
|
675 |
|
676 |
MESSAGE_LENGTH = 25
|
677 |
|
678 |
def progress_gen(n): |
679 |
msg = message.ljust(MESSAGE_LENGTH) |
680 |
for i in ProgressBar(msg).iter(range(n)): |
681 |
yield
|
682 |
yield
|
683 |
|
684 |
return progress_gen
|
685 |
|
686 |
def main(self): |
687 |
if self.args.account is not None: |
688 |
self.client.account = self.args.account |
689 |
|
690 |
|
691 |
class _store_container_command(_store_account_command): |
692 |
"""Base class for container level storage commands"""
|
693 |
|
694 |
def update_parser(self, parser): |
695 |
super(_store_container_command, self).update_parser(parser) |
696 |
parser.add_argument('--container', dest='container', metavar='NAME', |
697 |
help="Specify a container to use")
|
698 |
|
699 |
def main(self): |
700 |
super(_store_container_command, self).main() |
701 |
if self.args.container is not None: |
702 |
self.client.container = self.args.container |
703 |
|
704 |
|
705 |
@command(api='storage') |
706 |
class store_create(_store_account_command): |
707 |
"""Create a container"""
|
708 |
|
709 |
def main(self, container): |
710 |
super(store_create, self).main() |
711 |
self.client.create_container(container)
|
712 |
|
713 |
|
714 |
@command(api='storage') |
715 |
class store_container(_store_account_command): |
716 |
"""Get container info"""
|
717 |
|
718 |
def main(self, container): |
719 |
super(store_container, self).main() |
720 |
reply = self.client.get_container_meta(container)
|
721 |
print_dict(reply) |
722 |
|
723 |
|
724 |
@command(api='storage') |
725 |
class store_list(_store_container_command): |
726 |
"""List objects"""
|
727 |
|
728 |
def format_size(self, size): |
729 |
units = ('B', 'K', 'M', 'G', 'T') |
730 |
size = float(size)
|
731 |
for unit in units: |
732 |
if size <= 1024: |
733 |
break
|
734 |
size /= 1024
|
735 |
s = ('%.1f' % size).rstrip('.0') |
736 |
return s + unit
|
737 |
|
738 |
def main(self, path=''): |
739 |
super(store_list, self).main() |
740 |
for object in self.client.list_objects(): |
741 |
size = self.format_size(object['bytes']) |
742 |
print('%6s %s' % (size, object['name'])) |
743 |
|
744 |
|
745 |
@command(api='storage') |
746 |
class store_upload(_store_container_command): |
747 |
"""Upload a file"""
|
748 |
|
749 |
def main(self, path, remote_path=None): |
750 |
super(store_upload, self).main() |
751 |
|
752 |
if remote_path is None: |
753 |
remote_path = basename(path) |
754 |
with open(path) as f: |
755 |
hash_cb = self.progress('Calculating block hashes') |
756 |
upload_cb = self.progress('Uploading blocks') |
757 |
self.client.create_object(remote_path, f, hash_cb=hash_cb,
|
758 |
upload_cb=upload_cb) |
759 |
|
760 |
|
761 |
@command(api='storage') |
762 |
class store_download(_store_container_command): |
763 |
"""Download a file"""
|
764 |
|
765 |
def main(self, remote_path, local_path='-'): |
766 |
super(store_download, self).main() |
767 |
|
768 |
f, size = self.client.get_object(remote_path)
|
769 |
out = open(local_path, 'w') if local_path != '-' else stdout |
770 |
|
771 |
blocksize = 4 * 1024 ** 2 |
772 |
nblocks = 1 + (size - 1) // blocksize |
773 |
|
774 |
cb = self.progress('Downloading blocks') if local_path != '-' else None |
775 |
if cb:
|
776 |
gen = cb(nblocks) |
777 |
gen.next() |
778 |
|
779 |
data = f.read(blocksize) |
780 |
while data:
|
781 |
out.write(data) |
782 |
data = f.read(blocksize) |
783 |
if cb:
|
784 |
gen.next() |
785 |
|
786 |
|
787 |
@command(api='storage') |
788 |
class store_delete(_store_container_command): |
789 |
"""Delete a file"""
|
790 |
|
791 |
def main(self, path): |
792 |
super(store_delete, self).main() |
793 |
self.client.delete_object(path)
|
794 |
|
795 |
|
796 |
@command(api='storage') |
797 |
class store_purge(_store_account_command): |
798 |
"""Purge a container"""
|
799 |
|
800 |
def main(self, container): |
801 |
super(store_purge, self).main() |
802 |
self.client.purge_container(container)
|
803 |
|
804 |
|
805 |
@command(api='astakos') |
806 |
class astakos_authenticate(object): |
807 |
"""Authenticate a user"""
|
808 |
|
809 |
def main(self): |
810 |
reply = self.client.authenticate()
|
811 |
print_dict(reply) |
812 |
|
813 |
|
814 |
def print_groups(): |
815 |
print('\nGroups:')
|
816 |
for group in _commands: |
817 |
description = GROUPS.get(group, '')
|
818 |
print(' ', group.ljust(12), description) |
819 |
|
820 |
|
821 |
def print_commands(group): |
822 |
description = GROUPS.get(group, '')
|
823 |
if description:
|
824 |
print('\n' + description)
|
825 |
|
826 |
print('\nCommands:')
|
827 |
for name, cls in _commands[group].items(): |
828 |
print(' ', name.ljust(14), cls.description) |
829 |
|
830 |
|
831 |
def add_handler(name, level, prefix=''): |
832 |
h = logging.StreamHandler() |
833 |
fmt = logging.Formatter(prefix + '%(message)s')
|
834 |
h.setFormatter(fmt) |
835 |
logger = logging.getLogger(name) |
836 |
logger.addHandler(h) |
837 |
logger.setLevel(level) |
838 |
|
839 |
|
840 |
def main(): |
841 |
exe = basename(sys.argv[0])
|
842 |
parser = ArgumentParser(add_help=False)
|
843 |
parser.prog = '%s <group> <command>' % exe
|
844 |
parser.add_argument('-h', '--help', dest='help', action='store_true', |
845 |
default=False,
|
846 |
help="Show this help message and exit")
|
847 |
parser.add_argument('--config', dest='config', metavar='PATH', |
848 |
help="Specify the path to the configuration file")
|
849 |
parser.add_argument('-d', '--debug', dest='debug', action='store_true', |
850 |
default=False,
|
851 |
help="Include debug output")
|
852 |
parser.add_argument('-i', '--include', dest='include', action='store_true', |
853 |
default=False,
|
854 |
help="Include protocol headers in the output")
|
855 |
parser.add_argument('-s', '--silent', dest='silent', action='store_true', |
856 |
default=False,
|
857 |
help="Silent mode, don't output anything")
|
858 |
parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', |
859 |
default=False,
|
860 |
help="Make the operation more talkative")
|
861 |
parser.add_argument('-V', '--version', dest='version', action='store_true', |
862 |
default=False,
|
863 |
help="Show version number and quit")
|
864 |
parser.add_argument('-o', dest='options', action='append', |
865 |
default=[], metavar="KEY=VAL",
|
866 |
help="Override a config values")
|
867 |
|
868 |
args, argv = parser.parse_known_args() |
869 |
|
870 |
if args.version:
|
871 |
import kamaki |
872 |
print("kamaki %s" % kamaki.__version__)
|
873 |
exit(0) |
874 |
|
875 |
config = Config(args.config) if args.config else Config() |
876 |
|
877 |
for option in args.options: |
878 |
keypath, sep, val = option.partition('=')
|
879 |
if not sep: |
880 |
print("Invalid option '%s'" % option)
|
881 |
exit(1) |
882 |
section, sep, key = keypath.partition('.')
|
883 |
if not sep: |
884 |
print("Invalid option '%s'" % option)
|
885 |
exit(1) |
886 |
config.override(section.strip(), key.strip(), val.strip()) |
887 |
|
888 |
apis = set(['config']) |
889 |
for api in ('compute', 'image', 'storage', 'astakos'): |
890 |
if config.getboolean(api, 'enable'): |
891 |
apis.add(api) |
892 |
if config.getboolean('compute', 'cyclades_extensions'): |
893 |
apis.add('cyclades')
|
894 |
if config.getboolean('storage', 'pithos_extensions'): |
895 |
apis.add('pithos')
|
896 |
|
897 |
# Remove commands that belong to APIs that are not included
|
898 |
for group, group_commands in _commands.items(): |
899 |
for name, cls in group_commands.items(): |
900 |
if cls.api not in apis: |
901 |
del group_commands[name]
|
902 |
if not group_commands: |
903 |
del _commands[group]
|
904 |
|
905 |
group = argv.pop(0) if argv else None |
906 |
|
907 |
if not group: |
908 |
parser.print_help() |
909 |
print_groups() |
910 |
exit(0) |
911 |
|
912 |
if group not in _commands: |
913 |
parser.print_help() |
914 |
print_groups() |
915 |
exit(1) |
916 |
|
917 |
parser.prog = '%s %s <command>' % (exe, group)
|
918 |
command = argv.pop(0) if argv else None |
919 |
|
920 |
if not command: |
921 |
parser.print_help() |
922 |
print_commands(group) |
923 |
exit(0) |
924 |
|
925 |
if command not in _commands[group]: |
926 |
parser.print_help() |
927 |
print_commands(group) |
928 |
exit(1) |
929 |
|
930 |
cmd = _commands[group][command]() |
931 |
|
932 |
parser.prog = '%s %s %s' % (exe, group, command)
|
933 |
if cmd.syntax:
|
934 |
parser.prog += ' %s' % cmd.syntax
|
935 |
parser.description = cmd.description |
936 |
parser.epilog = ''
|
937 |
if hasattr(cmd, 'update_parser'): |
938 |
cmd.update_parser(parser) |
939 |
|
940 |
args, argv = parser.parse_known_args() |
941 |
|
942 |
if args.help:
|
943 |
parser.print_help() |
944 |
exit(0) |
945 |
|
946 |
if args.silent:
|
947 |
add_handler('', logging.CRITICAL)
|
948 |
elif args.debug:
|
949 |
add_handler('requests', logging.INFO, prefix='* ') |
950 |
add_handler('clients.send', logging.DEBUG, prefix='> ') |
951 |
add_handler('clients.recv', logging.DEBUG, prefix='< ') |
952 |
elif args.verbose:
|
953 |
add_handler('requests', logging.INFO, prefix='* ') |
954 |
add_handler('clients.send', logging.INFO, prefix='> ') |
955 |
add_handler('clients.recv', logging.INFO, prefix='< ') |
956 |
elif args.include:
|
957 |
add_handler('clients.recv', logging.INFO)
|
958 |
else:
|
959 |
add_handler('', logging.WARNING)
|
960 |
|
961 |
api = cmd.api |
962 |
if api in ('compute', 'cyclades'): |
963 |
url = config.get('compute', 'url') |
964 |
token = config.get('compute', 'token') or config.get('global', 'token') |
965 |
if config.getboolean('compute', 'cyclades_extensions'): |
966 |
cmd.client = clients.cyclades(url, token) |
967 |
else:
|
968 |
cmd.client = clients.compute(url, token) |
969 |
elif api in ('storage', 'pithos'): |
970 |
url = config.get('storage', 'url') |
971 |
token = config.get('storage', 'token') or config.get('global', 'token') |
972 |
account = config.get('storage', 'account') |
973 |
container = config.get('storage', 'container') |
974 |
if config.getboolean('storage', 'pithos_extensions'): |
975 |
cmd.client = clients.pithos(url, token, account, container) |
976 |
else:
|
977 |
cmd.client = clients.storage(url, token, account, container) |
978 |
elif api == 'image': |
979 |
url = config.get('image', 'url') |
980 |
token = config.get('image', 'token') or config.get('global', 'token') |
981 |
cmd.client = clients.image(url, token) |
982 |
elif api == 'astakos': |
983 |
url = config.get('astakos', 'url') |
984 |
token = config.get('astakos', 'token') or config.get('global', 'token') |
985 |
cmd.client = clients.astakos(url, token) |
986 |
|
987 |
cmd.args = args |
988 |
cmd.config = config |
989 |
|
990 |
try:
|
991 |
ret = cmd.main(*argv[2:])
|
992 |
exit(ret)
|
993 |
except TypeError as e: |
994 |
if e.args and e.args[0].startswith('main()'): |
995 |
parser.print_help() |
996 |
exit(1) |
997 |
else:
|
998 |
raise
|
999 |
except clients.ClientError as err: |
1000 |
if err.status == 404: |
1001 |
message = yellow(err.message) |
1002 |
elif 500 <= err.status < 600: |
1003 |
message = magenta(err.message) |
1004 |
else:
|
1005 |
message = red(err.message) |
1006 |
|
1007 |
print(message, file=stderr) |
1008 |
if err.details and (args.verbose or args.debug): |
1009 |
print(err.details, file=stderr) |
1010 |
exit(2) |
1011 |
except ConnectionError as err: |
1012 |
print(red("Connection error"), file=stderr)
|
1013 |
exit(1) |
1014 |
|
1015 |
|
1016 |
if __name__ == '__main__': |
1017 |
main() |