root / kamaki / cli / commands / cyclades.py @ 6d190dd1
History | View | Annotate | Download (33.4 kB)
1 |
# Copyright 2011-2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from kamaki.cli import command |
35 |
from kamaki.cli.command_tree import CommandTree |
36 |
from kamaki.cli.utils import ( |
37 |
print_dict, remove_from_items, filter_dicts_by_dict) |
38 |
from kamaki.cli.errors import raiseCLIError, CLISyntaxError, CLIBaseUrlError |
39 |
from kamaki.clients.cyclades import CycladesClient, ClientError |
40 |
from kamaki.cli.argument import FlagArgument, ValueArgument, KeyValueArgument |
41 |
from kamaki.cli.argument import ProgressBarArgument, DateArgument, IntArgument |
42 |
from kamaki.cli.commands import _command_init, errors, addLogSettings |
43 |
from kamaki.cli.commands import ( |
44 |
_optional_output_cmd, _optional_json, _name_filter, _id_filter) |
45 |
|
46 |
from base64 import b64encode |
47 |
from os.path import exists |
48 |
|
49 |
|
50 |
server_cmds = CommandTree('server', 'Cyclades/Compute API server commands') |
51 |
flavor_cmds = CommandTree('flavor', 'Cyclades/Compute API flavor commands') |
52 |
network_cmds = CommandTree('network', 'Cyclades/Compute API network commands') |
53 |
_commands = [server_cmds, flavor_cmds, network_cmds] |
54 |
|
55 |
|
56 |
about_authentication = '\nUser Authentication:\
|
57 |
\n* to check authentication: /user authenticate\
|
58 |
\n* to set authentication token: /config set cloud.<cloud>.token <token>'
|
59 |
|
60 |
howto_personality = [ |
61 |
'Defines a file to be injected to VMs personality.',
|
62 |
'Personality value syntax: PATH,[SERVER_PATH,[OWNER,[GROUP,[MODE]]]]',
|
63 |
' PATH: of local file to be injected',
|
64 |
' SERVER_PATH: destination location inside server Image',
|
65 |
' OWNER: user id of destination file owner',
|
66 |
' GROUP: group id or name to own destination file',
|
67 |
' MODEL: permition in octal (e.g. 0777 or o+rwx)']
|
68 |
|
69 |
|
70 |
class _server_wait(object): |
71 |
|
72 |
wait_arguments = dict(
|
73 |
progress_bar=ProgressBarArgument( |
74 |
'do not show progress bar',
|
75 |
('-N', '--no-progress-bar'), |
76 |
False
|
77 |
) |
78 |
) |
79 |
|
80 |
def _wait(self, server_id, currect_status): |
81 |
(progress_bar, wait_cb) = self._safe_progress_bar(
|
82 |
'Server %s still in %s mode' % (server_id, currect_status))
|
83 |
|
84 |
try:
|
85 |
new_mode = self.client.wait_server(
|
86 |
server_id, |
87 |
currect_status, |
88 |
wait_cb=wait_cb) |
89 |
except Exception: |
90 |
raise
|
91 |
finally:
|
92 |
self._safe_progress_bar_finish(progress_bar)
|
93 |
if new_mode:
|
94 |
print('Server %s is now in %s mode' % (server_id, new_mode))
|
95 |
else:
|
96 |
raiseCLIError(None, 'Time out') |
97 |
|
98 |
|
99 |
class _network_wait(object): |
100 |
|
101 |
wait_arguments = dict(
|
102 |
progress_bar=ProgressBarArgument( |
103 |
'do not show progress bar',
|
104 |
('-N', '--no-progress-bar'), |
105 |
False
|
106 |
) |
107 |
) |
108 |
|
109 |
def _wait(self, net_id, currect_status): |
110 |
(progress_bar, wait_cb) = self._safe_progress_bar(
|
111 |
'Network %s still in %s mode' % (net_id, currect_status))
|
112 |
|
113 |
try:
|
114 |
new_mode = self.client.wait_network(
|
115 |
net_id, |
116 |
currect_status, |
117 |
wait_cb=wait_cb) |
118 |
except Exception: |
119 |
raise
|
120 |
finally:
|
121 |
self._safe_progress_bar_finish(progress_bar)
|
122 |
if new_mode:
|
123 |
print('Network %s is now in %s mode' % (net_id, new_mode))
|
124 |
else:
|
125 |
raiseCLIError(None, 'Time out') |
126 |
|
127 |
|
128 |
class _init_cyclades(_command_init): |
129 |
@errors.generic.all
|
130 |
@addLogSettings
|
131 |
def _run(self, service='compute'): |
132 |
if getattr(self, 'cloud', None): |
133 |
base_url = self._custom_url(service)\
|
134 |
or self._custom_url('cyclades') |
135 |
if base_url:
|
136 |
token = self._custom_token(service)\
|
137 |
or self._custom_token('cyclades')\ |
138 |
or self.config.get_cloud('token') |
139 |
self.client = CycladesClient(
|
140 |
base_url=base_url, token=token) |
141 |
return
|
142 |
else:
|
143 |
self.cloud = 'default' |
144 |
if getattr(self, 'auth_base', False): |
145 |
cyclades_endpoints = self.auth_base.get_service_endpoints(
|
146 |
self._custom_type('cyclades') or 'compute', |
147 |
self._custom_version('cyclades') or '') |
148 |
base_url = cyclades_endpoints['publicURL']
|
149 |
token = self.auth_base.token
|
150 |
self.client = CycladesClient(base_url=base_url, token=token)
|
151 |
else:
|
152 |
raise CLIBaseUrlError(service='cyclades') |
153 |
|
154 |
def main(self): |
155 |
self._run()
|
156 |
|
157 |
|
158 |
@command(server_cmds)
|
159 |
class server_list(_init_cyclades, _optional_json, _name_filter, _id_filter): |
160 |
"""List Virtual Machines accessible by user"""
|
161 |
|
162 |
PERMANENTS = ('id', 'name') |
163 |
|
164 |
__doc__ += about_authentication |
165 |
|
166 |
arguments = dict(
|
167 |
detail=FlagArgument('show detailed output', ('-l', '--details')), |
168 |
since=DateArgument( |
169 |
'show only items since date (\' d/m/Y H:M:S \')',
|
170 |
'--since'),
|
171 |
limit=IntArgument('limit number of listed VMs', ('-n', '--number')), |
172 |
more=FlagArgument( |
173 |
'output results in pages (-n to set items per page, default 10)',
|
174 |
'--more'),
|
175 |
enum=FlagArgument('Enumerate results', '--enumerate'), |
176 |
flavor_id=ValueArgument('filter by flavor id', ('--flavor-id')), |
177 |
image_id=ValueArgument('filter by image id', ('--image-id')), |
178 |
meta=KeyValueArgument('filter by metadata key=values', ('--metadata')), |
179 |
meta_like=KeyValueArgument( |
180 |
'print only if in key=value, the value is part of actual value',
|
181 |
('--metadata-like')),
|
182 |
) |
183 |
|
184 |
def _add_user_name(self, servers): |
185 |
uuids = self._uuids2usernames(list(set( |
186 |
[srv['user_id'] for srv in servers] + |
187 |
[srv['tenant_id'] for srv in servers]))) |
188 |
for srv in servers: |
189 |
srv['user_id'] += ' (%s)' % uuids[srv['user_id']] |
190 |
srv['tenant_id'] += ' (%s)' % uuids[srv['tenant_id']] |
191 |
return servers
|
192 |
|
193 |
def _filter_by_image(self, servers): |
194 |
iid = self['image_id'] |
195 |
new_servers = [] |
196 |
for srv in servers: |
197 |
if srv['image']['id'] == iid: |
198 |
new_servers.append(srv) |
199 |
return new_servers
|
200 |
|
201 |
def _filter_by_flavor(self, servers): |
202 |
fid = self['flavor_id'] |
203 |
new_servers = [] |
204 |
for srv in servers: |
205 |
if '%s' % srv['flavor']['id'] == '%s' % fid: |
206 |
new_servers.append(srv) |
207 |
return new_servers
|
208 |
|
209 |
def _filter_by_metadata(self, servers): |
210 |
new_servers = [] |
211 |
for srv in servers: |
212 |
if not 'metadata' in srv: |
213 |
continue
|
214 |
meta = [dict(srv['metadata'])] |
215 |
if self['meta']: |
216 |
meta = filter_dicts_by_dict(meta, self['meta']) |
217 |
if meta and self['meta_like']: |
218 |
meta = filter_dicts_by_dict( |
219 |
meta, self['meta_like'], exact_match=False) |
220 |
if meta:
|
221 |
new_servers.append(srv) |
222 |
return new_servers
|
223 |
|
224 |
@errors.generic.all
|
225 |
@errors.cyclades.connection
|
226 |
@errors.cyclades.date
|
227 |
def _run(self): |
228 |
withimage = bool(self['image_id']) |
229 |
withflavor = bool(self['flavor_id']) |
230 |
withmeta = bool(self['meta'] or self['meta_like']) |
231 |
detail = self['detail'] or withimage or withflavor or withmeta |
232 |
servers = self.client.list_servers(detail, self['since']) |
233 |
|
234 |
servers = self._filter_by_name(servers)
|
235 |
servers = self._filter_by_id(servers)
|
236 |
if withimage:
|
237 |
servers = self._filter_by_image(servers)
|
238 |
if withflavor:
|
239 |
servers = self._filter_by_flavor(servers)
|
240 |
if withmeta:
|
241 |
servers = self._filter_by_metadata(servers)
|
242 |
|
243 |
if self['detail'] and not self['json_output']: |
244 |
servers = self._add_user_name(servers)
|
245 |
elif not (self['detail'] or self['json_output']): |
246 |
remove_from_items(servers, 'links')
|
247 |
if detail and not self['detail']: |
248 |
for srv in servers: |
249 |
for key in set(srv).difference(self.PERMANENTS): |
250 |
srv.pop(key) |
251 |
kwargs = dict(with_enumeration=self['enum']) |
252 |
if self['more']: |
253 |
kwargs['page_size'] = self['limit'] if self['limit'] else 10 |
254 |
elif self['limit']: |
255 |
servers = servers[:self['limit']] |
256 |
self._print(servers, **kwargs)
|
257 |
|
258 |
def main(self): |
259 |
super(self.__class__, self)._run() |
260 |
self._run()
|
261 |
|
262 |
|
263 |
@command(server_cmds)
|
264 |
class server_info(_init_cyclades, _optional_json): |
265 |
"""Detailed information on a Virtual Machine
|
266 |
Contains:
|
267 |
- name, id, status, create/update dates
|
268 |
- network interfaces
|
269 |
- metadata (e.g. os, superuser) and diagnostics
|
270 |
- hardware flavor and os image ids
|
271 |
"""
|
272 |
|
273 |
@errors.generic.all
|
274 |
@errors.cyclades.connection
|
275 |
@errors.cyclades.server_id
|
276 |
def _run(self, server_id): |
277 |
self._print(self.client.get_server_details(server_id), print_dict) |
278 |
|
279 |
def main(self, server_id): |
280 |
super(self.__class__, self)._run() |
281 |
self._run(server_id=server_id)
|
282 |
|
283 |
|
284 |
class PersonalityArgument(KeyValueArgument): |
285 |
@property
|
286 |
def value(self): |
287 |
return self._value if hasattr(self, '_value') else [] |
288 |
|
289 |
@value.setter
|
290 |
def value(self, newvalue): |
291 |
if newvalue == self.default: |
292 |
return self.value |
293 |
self._value = []
|
294 |
for i, terms in enumerate(newvalue): |
295 |
termlist = terms.split(',')
|
296 |
if len(termlist) > 5: |
297 |
msg = 'Wrong number of terms (should be 1 to 5)'
|
298 |
raiseCLIError(CLISyntaxError(msg), details=howto_personality) |
299 |
path = termlist[0]
|
300 |
if not exists(path): |
301 |
raiseCLIError( |
302 |
None,
|
303 |
'--personality: File %s does not exist' % path,
|
304 |
importance=1,
|
305 |
details=howto_personality) |
306 |
self._value.append(dict(path=path)) |
307 |
with open(path) as f: |
308 |
self._value[i]['contents'] = b64encode(f.read()) |
309 |
try:
|
310 |
self._value[i]['path'] = termlist[1] |
311 |
self._value[i]['owner'] = termlist[2] |
312 |
self._value[i]['group'] = termlist[3] |
313 |
self._value[i]['mode'] = termlist[4] |
314 |
except IndexError: |
315 |
pass
|
316 |
|
317 |
|
318 |
@command(server_cmds)
|
319 |
class server_create(_init_cyclades, _optional_json, _server_wait): |
320 |
"""Create a server (aka Virtual Machine)
|
321 |
Parameters:
|
322 |
- name: (single quoted text)
|
323 |
- flavor id: Hardware flavor. Pick one from: /flavor list
|
324 |
- image id: OS images. Pick one from: /image list
|
325 |
"""
|
326 |
|
327 |
arguments = dict(
|
328 |
personality=PersonalityArgument( |
329 |
(80 * ' ').join(howto_personality), ('-p', '--personality')), |
330 |
wait=FlagArgument('Wait server to build', ('-w', '--wait')) |
331 |
) |
332 |
|
333 |
@errors.generic.all
|
334 |
@errors.cyclades.connection
|
335 |
@errors.plankton.id
|
336 |
@errors.cyclades.flavor_id
|
337 |
def _run(self, name, flavor_id, image_id): |
338 |
r = self.client.create_server(
|
339 |
name, int(flavor_id), image_id, self['personality']) |
340 |
self._print(r, print_dict)
|
341 |
if self['wait']: |
342 |
self._wait(r['id'], r['status']) |
343 |
|
344 |
def main(self, name, flavor_id, image_id): |
345 |
super(self.__class__, self)._run() |
346 |
self._run(name=name, flavor_id=flavor_id, image_id=image_id)
|
347 |
|
348 |
|
349 |
@command(server_cmds)
|
350 |
class server_rename(_init_cyclades, _optional_output_cmd): |
351 |
"""Set/update a server (VM) name
|
352 |
VM names are not unique, therefore multiple servers may share the same name
|
353 |
"""
|
354 |
|
355 |
@errors.generic.all
|
356 |
@errors.cyclades.connection
|
357 |
@errors.cyclades.server_id
|
358 |
def _run(self, server_id, new_name): |
359 |
self._optional_output(
|
360 |
self.client.update_server_name(int(server_id), new_name)) |
361 |
|
362 |
def main(self, server_id, new_name): |
363 |
super(self.__class__, self)._run() |
364 |
self._run(server_id=server_id, new_name=new_name)
|
365 |
|
366 |
|
367 |
@command(server_cmds)
|
368 |
class server_delete(_init_cyclades, _optional_output_cmd, _server_wait): |
369 |
"""Delete a server (VM)"""
|
370 |
|
371 |
arguments = dict(
|
372 |
wait=FlagArgument('Wait server to be destroyed', ('-w', '--wait')) |
373 |
) |
374 |
|
375 |
@errors.generic.all
|
376 |
@errors.cyclades.connection
|
377 |
@errors.cyclades.server_id
|
378 |
def _run(self, server_id): |
379 |
status = 'DELETED'
|
380 |
if self['wait']: |
381 |
details = self.client.get_server_details(server_id)
|
382 |
status = details['status']
|
383 |
|
384 |
r = self.client.delete_server(int(server_id)) |
385 |
self._optional_output(r)
|
386 |
|
387 |
if self['wait']: |
388 |
self._wait(server_id, status)
|
389 |
|
390 |
def main(self, server_id): |
391 |
super(self.__class__, self)._run() |
392 |
self._run(server_id=server_id)
|
393 |
|
394 |
|
395 |
@command(server_cmds)
|
396 |
class server_reboot(_init_cyclades, _optional_output_cmd, _server_wait): |
397 |
"""Reboot a server (VM)"""
|
398 |
|
399 |
arguments = dict(
|
400 |
hard=FlagArgument('perform a hard reboot', ('-f', '--force')), |
401 |
wait=FlagArgument('Wait server to be destroyed', ('-w', '--wait')) |
402 |
) |
403 |
|
404 |
@errors.generic.all
|
405 |
@errors.cyclades.connection
|
406 |
@errors.cyclades.server_id
|
407 |
def _run(self, server_id): |
408 |
r = self.client.reboot_server(int(server_id), self['hard']) |
409 |
self._optional_output(r)
|
410 |
|
411 |
if self['wait']: |
412 |
self._wait(server_id, 'REBOOT') |
413 |
|
414 |
def main(self, server_id): |
415 |
super(self.__class__, self)._run() |
416 |
self._run(server_id=server_id)
|
417 |
|
418 |
|
419 |
@command(server_cmds)
|
420 |
class server_start(_init_cyclades, _optional_output_cmd, _server_wait): |
421 |
"""Start an existing server (VM)"""
|
422 |
|
423 |
arguments = dict(
|
424 |
wait=FlagArgument('Wait server to be destroyed', ('-w', '--wait')) |
425 |
) |
426 |
|
427 |
@errors.generic.all
|
428 |
@errors.cyclades.connection
|
429 |
@errors.cyclades.server_id
|
430 |
def _run(self, server_id): |
431 |
status = 'ACTIVE'
|
432 |
if self['wait']: |
433 |
details = self.client.get_server_details(server_id)
|
434 |
status = details['status']
|
435 |
if status in ('ACTIVE', ): |
436 |
return
|
437 |
|
438 |
r = self.client.start_server(int(server_id)) |
439 |
self._optional_output(r)
|
440 |
|
441 |
if self['wait']: |
442 |
self._wait(server_id, status)
|
443 |
|
444 |
def main(self, server_id): |
445 |
super(self.__class__, self)._run() |
446 |
self._run(server_id=server_id)
|
447 |
|
448 |
|
449 |
@command(server_cmds)
|
450 |
class server_shutdown(_init_cyclades, _optional_output_cmd, _server_wait): |
451 |
"""Shutdown an active server (VM)"""
|
452 |
|
453 |
arguments = dict(
|
454 |
wait=FlagArgument('Wait server to be destroyed', ('-w', '--wait')) |
455 |
) |
456 |
|
457 |
@errors.generic.all
|
458 |
@errors.cyclades.connection
|
459 |
@errors.cyclades.server_id
|
460 |
def _run(self, server_id): |
461 |
status = 'STOPPED'
|
462 |
if self['wait']: |
463 |
details = self.client.get_server_details(server_id)
|
464 |
status = details['status']
|
465 |
if status in ('STOPPED', ): |
466 |
return
|
467 |
|
468 |
r = self.client.shutdown_server(int(server_id)) |
469 |
self._optional_output(r)
|
470 |
|
471 |
if self['wait']: |
472 |
self._wait(server_id, status)
|
473 |
|
474 |
def main(self, server_id): |
475 |
super(self.__class__, self)._run() |
476 |
self._run(server_id=server_id)
|
477 |
|
478 |
|
479 |
@command(server_cmds)
|
480 |
class server_console(_init_cyclades, _optional_json): |
481 |
"""Get a VNC console to access an existing server (VM)
|
482 |
Console connection information provided (at least):
|
483 |
- host: (url or address) a VNC host
|
484 |
- port: (int) the gateway to enter VM on host
|
485 |
- password: for VNC authorization
|
486 |
"""
|
487 |
|
488 |
@errors.generic.all
|
489 |
@errors.cyclades.connection
|
490 |
@errors.cyclades.server_id
|
491 |
def _run(self, server_id): |
492 |
self._print(
|
493 |
self.client.get_server_console(int(server_id)), print_dict) |
494 |
|
495 |
def main(self, server_id): |
496 |
super(self.__class__, self)._run() |
497 |
self._run(server_id=server_id)
|
498 |
|
499 |
|
500 |
@command(server_cmds)
|
501 |
class server_resize(_init_cyclades, _optional_output_cmd): |
502 |
"""Set a different flavor for an existing server
|
503 |
To get server ids and flavor ids:
|
504 |
/server list
|
505 |
/flavor list
|
506 |
"""
|
507 |
|
508 |
@errors.generic.all
|
509 |
@errors.cyclades.connection
|
510 |
@errors.cyclades.server_id
|
511 |
@errors.cyclades.flavor_id
|
512 |
def _run(self, server_id, flavor_id): |
513 |
self._optional_output(self.client.resize_server(server_id, flavor_id)) |
514 |
|
515 |
def main(self, server_id, flavor_id): |
516 |
super(self.__class__, self)._run() |
517 |
self._run(server_id=server_id, flavor_id=flavor_id)
|
518 |
|
519 |
|
520 |
@command(server_cmds)
|
521 |
class server_firewall(_init_cyclades): |
522 |
"""Manage server (VM) firewall profiles for public networks"""
|
523 |
|
524 |
|
525 |
@command(server_cmds)
|
526 |
class server_firewall_set(_init_cyclades, _optional_output_cmd): |
527 |
"""Set the server (VM) firewall profile on VMs public network
|
528 |
Values for profile:
|
529 |
- DISABLED: Shutdown firewall
|
530 |
- ENABLED: Firewall in normal mode
|
531 |
- PROTECTED: Firewall in secure mode
|
532 |
"""
|
533 |
|
534 |
@errors.generic.all
|
535 |
@errors.cyclades.connection
|
536 |
@errors.cyclades.server_id
|
537 |
@errors.cyclades.firewall
|
538 |
def _run(self, server_id, profile): |
539 |
self._optional_output(self.client.set_firewall_profile( |
540 |
server_id=int(server_id), profile=('%s' % profile).upper())) |
541 |
|
542 |
def main(self, server_id, profile): |
543 |
super(self.__class__, self)._run() |
544 |
self._run(server_id=server_id, profile=profile)
|
545 |
|
546 |
|
547 |
@command(server_cmds)
|
548 |
class server_firewall_get(_init_cyclades): |
549 |
"""Get the server (VM) firewall profile for its public network"""
|
550 |
|
551 |
@errors.generic.all
|
552 |
@errors.cyclades.connection
|
553 |
@errors.cyclades.server_id
|
554 |
def _run(self, server_id): |
555 |
print(self.client.get_firewall_profile(server_id))
|
556 |
|
557 |
def main(self, server_id): |
558 |
super(self.__class__, self)._run() |
559 |
self._run(server_id=server_id)
|
560 |
|
561 |
|
562 |
@command(server_cmds)
|
563 |
class server_addr(_init_cyclades, _optional_json): |
564 |
"""List the addresses of all network interfaces on a server (VM)"""
|
565 |
|
566 |
arguments = dict(
|
567 |
enum=FlagArgument('Enumerate results', '--enumerate') |
568 |
) |
569 |
|
570 |
@errors.generic.all
|
571 |
@errors.cyclades.connection
|
572 |
@errors.cyclades.server_id
|
573 |
def _run(self, server_id): |
574 |
reply = self.client.list_server_nics(int(server_id)) |
575 |
self._print(
|
576 |
reply, with_enumeration=self['enum'] and len(reply) > 1) |
577 |
|
578 |
def main(self, server_id): |
579 |
super(self.__class__, self)._run() |
580 |
self._run(server_id=server_id)
|
581 |
|
582 |
|
583 |
@command(server_cmds)
|
584 |
class server_metadata(_init_cyclades): |
585 |
"""Manage Server metadata (key:value pairs of server attributes)"""
|
586 |
|
587 |
|
588 |
@command(server_cmds)
|
589 |
class server_metadata_list(_init_cyclades, _optional_json): |
590 |
"""Get server metadata"""
|
591 |
|
592 |
@errors.generic.all
|
593 |
@errors.cyclades.connection
|
594 |
@errors.cyclades.server_id
|
595 |
@errors.cyclades.metadata
|
596 |
def _run(self, server_id, key=''): |
597 |
self._print(
|
598 |
self.client.get_server_metadata(int(server_id), key), print_dict) |
599 |
|
600 |
def main(self, server_id, key=''): |
601 |
super(self.__class__, self)._run() |
602 |
self._run(server_id=server_id, key=key)
|
603 |
|
604 |
|
605 |
@command(server_cmds)
|
606 |
class server_metadata_set(_init_cyclades, _optional_json): |
607 |
"""Set / update server(VM) metadata
|
608 |
Metadata should be given in key/value pairs in key=value format
|
609 |
For example: /server metadata set <server id> key1=value1 key2=value2
|
610 |
Old, unreferenced metadata will remain intact
|
611 |
"""
|
612 |
|
613 |
@errors.generic.all
|
614 |
@errors.cyclades.connection
|
615 |
@errors.cyclades.server_id
|
616 |
def _run(self, server_id, keyvals): |
617 |
assert keyvals, 'Please, add some metadata ( key=value)' |
618 |
metadata = dict()
|
619 |
for keyval in keyvals: |
620 |
k, sep, v = keyval.partition('=')
|
621 |
if sep and k: |
622 |
metadata[k] = v |
623 |
else:
|
624 |
raiseCLIError( |
625 |
'Invalid piece of metadata %s' % keyval,
|
626 |
importance=2, details=[
|
627 |
'Correct metadata format: key=val',
|
628 |
'For example:',
|
629 |
'/server metadata set <server id>'
|
630 |
'key1=value1 key2=value2'])
|
631 |
self._print(
|
632 |
self.client.update_server_metadata(int(server_id), **metadata), |
633 |
print_dict) |
634 |
|
635 |
def main(self, server_id, *key_equals_val): |
636 |
super(self.__class__, self)._run() |
637 |
self._run(server_id=server_id, keyvals=key_equals_val)
|
638 |
|
639 |
|
640 |
@command(server_cmds)
|
641 |
class server_metadata_delete(_init_cyclades, _optional_output_cmd): |
642 |
"""Delete server (VM) metadata"""
|
643 |
|
644 |
@errors.generic.all
|
645 |
@errors.cyclades.connection
|
646 |
@errors.cyclades.server_id
|
647 |
@errors.cyclades.metadata
|
648 |
def _run(self, server_id, key): |
649 |
self._optional_output(
|
650 |
self.client.delete_server_metadata(int(server_id), key)) |
651 |
|
652 |
def main(self, server_id, key): |
653 |
super(self.__class__, self)._run() |
654 |
self._run(server_id=server_id, key=key)
|
655 |
|
656 |
|
657 |
@command(server_cmds)
|
658 |
class server_stats(_init_cyclades, _optional_json): |
659 |
"""Get server (VM) statistics"""
|
660 |
|
661 |
@errors.generic.all
|
662 |
@errors.cyclades.connection
|
663 |
@errors.cyclades.server_id
|
664 |
def _run(self, server_id): |
665 |
self._print(self.client.get_server_stats(int(server_id)), print_dict) |
666 |
|
667 |
def main(self, server_id): |
668 |
super(self.__class__, self)._run() |
669 |
self._run(server_id=server_id)
|
670 |
|
671 |
|
672 |
@command(server_cmds)
|
673 |
class server_wait(_init_cyclades, _server_wait): |
674 |
"""Wait for server to finish [BUILD, STOPPED, REBOOT, ACTIVE]"""
|
675 |
|
676 |
@errors.generic.all
|
677 |
@errors.cyclades.connection
|
678 |
@errors.cyclades.server_id
|
679 |
def _run(self, server_id, currect_status): |
680 |
self._wait(server_id, currect_status)
|
681 |
|
682 |
def main(self, server_id, currect_status='BUILD'): |
683 |
super(self.__class__, self)._run() |
684 |
self._run(server_id=server_id, currect_status=currect_status)
|
685 |
|
686 |
|
687 |
@command(flavor_cmds)
|
688 |
class flavor_list(_init_cyclades, _optional_json, _name_filter, _id_filter): |
689 |
"""List available hardware flavors"""
|
690 |
|
691 |
arguments = dict(
|
692 |
detail=FlagArgument('show detailed output', ('-l', '--details')), |
693 |
limit=IntArgument('limit # of listed flavors', ('-n', '--number')), |
694 |
more=FlagArgument( |
695 |
'output results in pages (-n to set items per page, default 10)',
|
696 |
'--more'),
|
697 |
enum=FlagArgument('Enumerate results', '--enumerate') |
698 |
) |
699 |
|
700 |
@errors.generic.all
|
701 |
@errors.cyclades.connection
|
702 |
def _run(self): |
703 |
flavors = self.client.list_flavors(self['detail']) |
704 |
flavors = self._filter_by_name(flavors)
|
705 |
flavors = self._filter_by_id(flavors)
|
706 |
if not (self['detail'] or self['json_output']): |
707 |
remove_from_items(flavors, 'links')
|
708 |
pg_size = 10 if self['more'] and not self['limit'] else self['limit'] |
709 |
self._print(
|
710 |
flavors, |
711 |
with_redundancy=self['detail'], |
712 |
page_size=pg_size, |
713 |
with_enumeration=self['enum']) |
714 |
|
715 |
def main(self): |
716 |
super(self.__class__, self)._run() |
717 |
self._run()
|
718 |
|
719 |
|
720 |
@command(flavor_cmds)
|
721 |
class flavor_info(_init_cyclades, _optional_json): |
722 |
"""Detailed information on a hardware flavor
|
723 |
To get a list of available flavors and flavor ids, try /flavor list
|
724 |
"""
|
725 |
|
726 |
@errors.generic.all
|
727 |
@errors.cyclades.connection
|
728 |
@errors.cyclades.flavor_id
|
729 |
def _run(self, flavor_id): |
730 |
self._print(
|
731 |
self.client.get_flavor_details(int(flavor_id)), print_dict) |
732 |
|
733 |
def main(self, flavor_id): |
734 |
super(self.__class__, self)._run() |
735 |
self._run(flavor_id=flavor_id)
|
736 |
|
737 |
|
738 |
@command(network_cmds)
|
739 |
class network_info(_init_cyclades, _optional_json): |
740 |
"""Detailed information on a network
|
741 |
To get a list of available networks and network ids, try /network list
|
742 |
"""
|
743 |
|
744 |
@errors.generic.all
|
745 |
@errors.cyclades.connection
|
746 |
@errors.cyclades.network_id
|
747 |
def _run(self, network_id): |
748 |
network = self.client.get_network_details(int(network_id)) |
749 |
self._print(network, print_dict, exclude=('id')) |
750 |
|
751 |
def main(self, network_id): |
752 |
super(self.__class__, self)._run() |
753 |
self._run(network_id=network_id)
|
754 |
|
755 |
|
756 |
@command(network_cmds)
|
757 |
class network_list(_init_cyclades, _optional_json, _name_filter, _id_filter): |
758 |
"""List networks"""
|
759 |
|
760 |
arguments = dict(
|
761 |
detail=FlagArgument('show detailed output', ('-l', '--details')), |
762 |
limit=IntArgument('limit # of listed networks', ('-n', '--number')), |
763 |
more=FlagArgument( |
764 |
'output results in pages (-n to set items per page, default 10)',
|
765 |
'--more'),
|
766 |
enum=FlagArgument('Enumerate results', '--enumerate') |
767 |
) |
768 |
|
769 |
@errors.generic.all
|
770 |
@errors.cyclades.connection
|
771 |
def _run(self): |
772 |
networks = self.client.list_networks(self['detail']) |
773 |
networks = self._filter_by_name(networks)
|
774 |
networks = self._filter_by_id(networks)
|
775 |
if not (self['detail'] or self['json_output']): |
776 |
remove_from_items(networks, 'links')
|
777 |
kwargs = dict(with_enumeration=self['enum']) |
778 |
if self['more']: |
779 |
kwargs['page_size'] = self['limit'] or 10 |
780 |
elif self['limit']: |
781 |
networks = networks[:self['limit']] |
782 |
self._print(networks, **kwargs)
|
783 |
|
784 |
def main(self): |
785 |
super(self.__class__, self)._run() |
786 |
self._run()
|
787 |
|
788 |
|
789 |
@command(network_cmds)
|
790 |
class network_create(_init_cyclades, _optional_json, _network_wait): |
791 |
"""Create an (unconnected) network"""
|
792 |
|
793 |
arguments = dict(
|
794 |
cidr=ValueArgument('explicitly set cidr', '--with-cidr'), |
795 |
gateway=ValueArgument('explicitly set gateway', '--with-gateway'), |
796 |
dhcp=FlagArgument('Use dhcp (default: off)', '--with-dhcp'), |
797 |
type=ValueArgument( |
798 |
'Valid network types are '
|
799 |
'CUSTOM, IP_LESS_ROUTED, MAC_FILTERED (default), PHYSICAL_VLAN',
|
800 |
'--with-type',
|
801 |
default='MAC_FILTERED'),
|
802 |
wait=FlagArgument('Wait network to build', ('-w', '--wait')) |
803 |
) |
804 |
|
805 |
@errors.generic.all
|
806 |
@errors.cyclades.connection
|
807 |
@errors.cyclades.network_max
|
808 |
def _run(self, name): |
809 |
r = self.client.create_network(
|
810 |
name, |
811 |
cidr=self['cidr'], |
812 |
gateway=self['gateway'], |
813 |
dhcp=self['dhcp'], |
814 |
type=self['type']) |
815 |
self._print(r, print_dict)
|
816 |
|
817 |
if self['wait']: |
818 |
self._wait(r['id'], 'PENDING') |
819 |
|
820 |
def main(self, name): |
821 |
super(self.__class__, self)._run() |
822 |
self._run(name)
|
823 |
|
824 |
|
825 |
@command(network_cmds)
|
826 |
class network_rename(_init_cyclades, _optional_output_cmd): |
827 |
"""Set the name of a network"""
|
828 |
|
829 |
@errors.generic.all
|
830 |
@errors.cyclades.connection
|
831 |
@errors.cyclades.network_id
|
832 |
def _run(self, network_id, new_name): |
833 |
self._optional_output(
|
834 |
self.client.update_network_name(int(network_id), new_name)) |
835 |
|
836 |
def main(self, network_id, new_name): |
837 |
super(self.__class__, self)._run() |
838 |
self._run(network_id=network_id, new_name=new_name)
|
839 |
|
840 |
|
841 |
@command(network_cmds)
|
842 |
class network_delete(_init_cyclades, _optional_output_cmd, _network_wait): |
843 |
"""Delete a network"""
|
844 |
|
845 |
arguments = dict(
|
846 |
wait=FlagArgument('Wait network to build', ('-w', '--wait')) |
847 |
) |
848 |
|
849 |
@errors.generic.all
|
850 |
@errors.cyclades.connection
|
851 |
@errors.cyclades.network_id
|
852 |
@errors.cyclades.network_in_use
|
853 |
def _run(self, network_id): |
854 |
status = 'DELETED'
|
855 |
if self['wait']: |
856 |
r = self.client.get_network_details(network_id)
|
857 |
status = r['status']
|
858 |
if status in ('DELETED', ): |
859 |
return
|
860 |
|
861 |
r = self.client.delete_network(int(network_id)) |
862 |
self._optional_output(r)
|
863 |
|
864 |
if self['wait']: |
865 |
self._wait(network_id, status)
|
866 |
|
867 |
def main(self, network_id): |
868 |
super(self.__class__, self)._run() |
869 |
self._run(network_id=network_id)
|
870 |
|
871 |
|
872 |
@command(network_cmds)
|
873 |
class network_connect(_init_cyclades, _optional_output_cmd): |
874 |
"""Connect a server to a network"""
|
875 |
|
876 |
@errors.generic.all
|
877 |
@errors.cyclades.connection
|
878 |
@errors.cyclades.server_id
|
879 |
@errors.cyclades.network_id
|
880 |
def _run(self, server_id, network_id): |
881 |
self._optional_output(
|
882 |
self.client.connect_server(int(server_id), int(network_id))) |
883 |
|
884 |
def main(self, server_id, network_id): |
885 |
super(self.__class__, self)._run() |
886 |
self._run(server_id=server_id, network_id=network_id)
|
887 |
|
888 |
|
889 |
@command(network_cmds)
|
890 |
class network_disconnect(_init_cyclades): |
891 |
"""Disconnect a nic that connects a server to a network
|
892 |
Nic ids are listed as "attachments" in detailed network information
|
893 |
To get detailed network information: /network info <network id>
|
894 |
"""
|
895 |
|
896 |
@errors.cyclades.nic_format
|
897 |
def _server_id_from_nic(self, nic_id): |
898 |
return nic_id.split('-')[1] |
899 |
|
900 |
@errors.generic.all
|
901 |
@errors.cyclades.connection
|
902 |
@errors.cyclades.server_id
|
903 |
@errors.cyclades.nic_id
|
904 |
def _run(self, nic_id, server_id): |
905 |
num_of_disconnected = self.client.disconnect_server(server_id, nic_id)
|
906 |
if not num_of_disconnected: |
907 |
raise ClientError(
|
908 |
'Network Interface %s not found on server %s' % (
|
909 |
nic_id, |
910 |
server_id), |
911 |
status=404)
|
912 |
print('Disconnected %s connections' % num_of_disconnected)
|
913 |
|
914 |
def main(self, nic_id): |
915 |
super(self.__class__, self)._run() |
916 |
server_id = self._server_id_from_nic(nic_id=nic_id)
|
917 |
self._run(nic_id=nic_id, server_id=server_id)
|
918 |
|
919 |
|
920 |
@command(network_cmds)
|
921 |
class network_wait(_init_cyclades, _network_wait): |
922 |
"""Wait for server to finish [PENDING, ACTIVE, DELETED]"""
|
923 |
|
924 |
@errors.generic.all
|
925 |
@errors.cyclades.connection
|
926 |
@errors.cyclades.network_id
|
927 |
def _run(self, network_id, currect_status): |
928 |
self._wait(network_id, currect_status)
|
929 |
|
930 |
def main(self, network_id, currect_status='PENDING'): |
931 |
super(self.__class__, self)._run() |
932 |
self._run(network_id=network_id, currect_status=currect_status)
|
933 |
|
934 |
|
935 |
@command(server_cmds)
|
936 |
class server_ip(_init_cyclades): |
937 |
"""Manage floating IPs for the servers"""
|
938 |
|
939 |
|
940 |
@command(server_cmds)
|
941 |
class server_ip_pools(_init_cyclades, _optional_json): |
942 |
"""List all floating pools of floating ips"""
|
943 |
|
944 |
@errors.generic.all
|
945 |
@errors.cyclades.connection
|
946 |
def _run(self): |
947 |
r = self.client.get_floating_ip_pools()
|
948 |
self._print(r if self['json_output'] else r['floating_ip_pools']) |
949 |
|
950 |
def main(self): |
951 |
super(self.__class__, self)._run() |
952 |
self._run()
|
953 |
|
954 |
|
955 |
@command(server_cmds)
|
956 |
class server_ip_list(_init_cyclades, _optional_json): |
957 |
"""List all floating ips"""
|
958 |
|
959 |
@errors.generic.all
|
960 |
@errors.cyclades.connection
|
961 |
def _run(self): |
962 |
r = self.client.get_floating_ips()
|
963 |
self._print(r if self['json_output'] else r['floating_ips']) |
964 |
|
965 |
def main(self): |
966 |
super(self.__class__, self)._run() |
967 |
self._run()
|
968 |
|
969 |
|
970 |
@command(server_cmds)
|
971 |
class server_ip_info(_init_cyclades, _optional_json): |
972 |
"""A floating IPs' details"""
|
973 |
|
974 |
@errors.generic.all
|
975 |
@errors.cyclades.connection
|
976 |
def _run(self, ip): |
977 |
self._print(self.client.get_floating_ip(ip), print_dict) |
978 |
|
979 |
def main(self, ip): |
980 |
super(self.__class__, self)._run() |
981 |
self._run(ip=ip)
|
982 |
|
983 |
|
984 |
@command(server_cmds)
|
985 |
class server_ip_create(_init_cyclades, _optional_json): |
986 |
"""Create a new floating IP"""
|
987 |
|
988 |
arguments = dict(
|
989 |
pool=ValueArgument('Source IP pool', ('--pool'), None) |
990 |
) |
991 |
|
992 |
@errors.generic.all
|
993 |
@errors.cyclades.connection
|
994 |
def _run(self, ip=None): |
995 |
self._print([self.client.alloc_floating_ip(self['pool'], ip)]) |
996 |
|
997 |
def main(self, requested_address=None): |
998 |
super(self.__class__, self)._run() |
999 |
self._run(ip=requested_address)
|
1000 |
|
1001 |
|
1002 |
@command(server_cmds)
|
1003 |
class server_ip_delete(_init_cyclades, _optional_output_cmd): |
1004 |
"""Delete a floating ip"""
|
1005 |
|
1006 |
@errors.generic.all
|
1007 |
@errors.cyclades.connection
|
1008 |
def _run(self, ip): |
1009 |
self._optional_output(self.client.delete_floating_ip(ip)) |
1010 |
|
1011 |
def main(self, ip): |
1012 |
super(self.__class__, self)._run() |
1013 |
self._run(ip=ip)
|
1014 |
|
1015 |
|
1016 |
@command(server_cmds)
|
1017 |
class server_ip_attach(_init_cyclades, _optional_output_cmd): |
1018 |
"""Attach a floating ip to a server with server_id
|
1019 |
"""
|
1020 |
|
1021 |
@errors.generic.all
|
1022 |
@errors.cyclades.connection
|
1023 |
@errors.cyclades.server_id
|
1024 |
def _run(self, server_id, ip): |
1025 |
self._optional_output(self.client.attach_floating_ip(server_id, ip)) |
1026 |
|
1027 |
def main(self, server_id, ip): |
1028 |
super(self.__class__, self)._run() |
1029 |
self._run(server_id=server_id, ip=ip)
|
1030 |
|
1031 |
|
1032 |
@command(server_cmds)
|
1033 |
class server_ip_detach(_init_cyclades, _optional_output_cmd): |
1034 |
"""Detach floating IP from server
|
1035 |
"""
|
1036 |
|
1037 |
@errors.generic.all
|
1038 |
@errors.cyclades.connection
|
1039 |
@errors.cyclades.server_id
|
1040 |
def _run(self, server_id, ip): |
1041 |
self._optional_output(self.client.detach_floating_ip(server_id, ip)) |
1042 |
|
1043 |
def main(self, server_id, ip): |
1044 |
super(self.__class__, self)._run() |
1045 |
self._run(server_id=server_id, ip=ip)
|