root / kamaki / cli / commands / cyclades.py @ ec5d658f
History | View | Annotate | Download (46.1 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 base64 import b64encode |
35 |
from os.path import exists, expanduser |
36 |
from io import StringIO |
37 |
from pydoc import pager |
38 |
|
39 |
from kamaki.cli import command |
40 |
from kamaki.cli.command_tree import CommandTree |
41 |
from kamaki.cli.utils import remove_from_items, filter_dicts_by_dict |
42 |
from kamaki.cli.errors import ( |
43 |
raiseCLIError, CLISyntaxError, CLIBaseUrlError, CLIInvalidArgument) |
44 |
from kamaki.clients.cyclades import CycladesClient, ClientError |
45 |
from kamaki.cli.argument import FlagArgument, ValueArgument, KeyValueArgument |
46 |
from kamaki.cli.argument import ProgressBarArgument, DateArgument, IntArgument |
47 |
from kamaki.cli.commands import _command_init, errors, addLogSettings |
48 |
from kamaki.cli.commands import ( |
49 |
_optional_output_cmd, _optional_json, _name_filter, _id_filter) |
50 |
|
51 |
|
52 |
server_cmds = CommandTree('server', 'Cyclades/Compute API server commands') |
53 |
flavor_cmds = CommandTree('flavor', 'Cyclades/Compute API flavor commands') |
54 |
network_cmds = CommandTree('network', 'Cyclades/Compute API network commands') |
55 |
ip_cmds = CommandTree('ip', 'Cyclades/Compute API floating ip commands') |
56 |
_commands = [server_cmds, flavor_cmds, network_cmds, ip_cmds] |
57 |
|
58 |
|
59 |
about_authentication = '\nUser Authentication:\
|
60 |
\n* to check authentication: /user authenticate\
|
61 |
\n* to set authentication token: /config set cloud.<cloud>.token <token>'
|
62 |
|
63 |
howto_personality = [ |
64 |
'Defines a file to be injected to virtual servers file system.',
|
65 |
'syntax: PATH,[SERVER_PATH,[OWNER,[GROUP,[MODE]]]]',
|
66 |
' [local-path=]PATH: local file to be injected (relative or absolute)',
|
67 |
' [server-path=]SERVER_PATH: destination location inside server Image',
|
68 |
' [owner=]OWNER: virtual servers user id for the remote file',
|
69 |
' [group=]GROUP: virtual servers group id or name for the remote file',
|
70 |
' [mode=]MODE: permission in octal (e.g., 0777)',
|
71 |
'e.g., -p /tmp/my.file,owner=root,mode=0777']
|
72 |
|
73 |
|
74 |
class _service_wait(object): |
75 |
|
76 |
wait_arguments = dict(
|
77 |
progress_bar=ProgressBarArgument( |
78 |
'do not show progress bar', ('-N', '--no-progress-bar'), False) |
79 |
) |
80 |
|
81 |
def _wait( |
82 |
self, service, service_id, status_method, current_status,
|
83 |
countdown=True, timeout=60): |
84 |
(progress_bar, wait_cb) = self._safe_progress_bar(
|
85 |
'%s %s: status is still %s' % (
|
86 |
service, service_id, current_status), |
87 |
countdown=countdown, timeout=timeout) |
88 |
|
89 |
try:
|
90 |
new_mode = status_method( |
91 |
service_id, current_status, max_wait=timeout, wait_cb=wait_cb) |
92 |
if new_mode:
|
93 |
self.error('%s %s: status is now %s' % ( |
94 |
service, service_id, new_mode)) |
95 |
else:
|
96 |
self.error('%s %s: status is still %s' % ( |
97 |
service, service_id, current_status)) |
98 |
except KeyboardInterrupt: |
99 |
self.error('\n- canceled') |
100 |
finally:
|
101 |
self._safe_progress_bar_finish(progress_bar)
|
102 |
|
103 |
|
104 |
class _server_wait(_service_wait): |
105 |
|
106 |
def _wait(self, server_id, current_status, timeout=60): |
107 |
super(_server_wait, self)._wait( |
108 |
'Server', server_id, self.client.wait_server, current_status, |
109 |
countdown=(current_status not in ('BUILD', )), |
110 |
timeout=timeout if current_status not in ('BUILD', ) else 100) |
111 |
|
112 |
|
113 |
class _network_wait(_service_wait): |
114 |
|
115 |
def _wait(self, net_id, current_status, timeout=60): |
116 |
super(_network_wait, self)._wait( |
117 |
'Network', net_id, self.client.wait_network, current_status, |
118 |
timeout=timeout) |
119 |
|
120 |
|
121 |
class _firewall_wait(_service_wait): |
122 |
|
123 |
def _wait(self, server_id, current_status, timeout=60): |
124 |
super(_firewall_wait, self)._wait( |
125 |
'Firewall of server',
|
126 |
server_id, self.client.wait_firewall, current_status,
|
127 |
timeout=timeout) |
128 |
|
129 |
|
130 |
class _init_cyclades(_command_init): |
131 |
@errors.generic.all
|
132 |
@addLogSettings
|
133 |
def _run(self, service='compute'): |
134 |
if getattr(self, 'cloud', None): |
135 |
base_url = self._custom_url(service) or self._custom_url( |
136 |
'cyclades')
|
137 |
if base_url:
|
138 |
token = self._custom_token(service) or self._custom_token( |
139 |
'cyclades') or self.config.get_cloud('token') |
140 |
self.client = CycladesClient(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 servers accessible by user
|
161 |
Use filtering arguments (e.g., --name-like) to manage long server lists
|
162 |
"""
|
163 |
|
164 |
PERMANENTS = ('id', 'name') |
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( |
172 |
'limit number of listed virtual servers', ('-n', '--number')), |
173 |
more=FlagArgument( |
174 |
'output results in pages (-n to set items per page, default 10)',
|
175 |
'--more'),
|
176 |
enum=FlagArgument('Enumerate results', '--enumerate'), |
177 |
flavor_id=ValueArgument('filter by flavor id', ('--flavor-id')), |
178 |
image_id=ValueArgument('filter by image id', ('--image-id')), |
179 |
user_id=ValueArgument('filter by user id', ('--user-id')), |
180 |
user_name=ValueArgument('filter by user name', ('--user-name')), |
181 |
status=ValueArgument( |
182 |
'filter by status (ACTIVE, STOPPED, REBOOT, ERROR, etc.)',
|
183 |
('--status')),
|
184 |
meta=KeyValueArgument('filter by metadata key=values', ('--metadata')), |
185 |
meta_like=KeyValueArgument( |
186 |
'print only if in key=value, the value is part of actual value',
|
187 |
('--metadata-like')),
|
188 |
) |
189 |
|
190 |
def _add_user_name(self, servers): |
191 |
uuids = self._uuids2usernames(list(set( |
192 |
[srv['user_id'] for srv in servers] + |
193 |
[srv['tenant_id'] for srv in servers]))) |
194 |
for srv in servers: |
195 |
srv['user_id'] += ' (%s)' % uuids[srv['user_id']] |
196 |
srv['tenant_id'] += ' (%s)' % uuids[srv['tenant_id']] |
197 |
return servers
|
198 |
|
199 |
def _apply_common_filters(self, servers): |
200 |
common_filters = dict()
|
201 |
if self['status']: |
202 |
common_filters['status'] = self['status'] |
203 |
if self['user_id'] or self['user_name']: |
204 |
uuid = self['user_id'] or self._username2uuid(self['user_name']) |
205 |
common_filters['user_id'] = uuid
|
206 |
return filter_dicts_by_dict(servers, common_filters)
|
207 |
|
208 |
def _filter_by_image(self, servers): |
209 |
iid = self['image_id'] |
210 |
return [srv for srv in servers if srv['image']['id'] == iid] |
211 |
|
212 |
def _filter_by_flavor(self, servers): |
213 |
fid = self['flavor_id'] |
214 |
return [srv for srv in servers if ( |
215 |
'%s' % srv['image']['id'] == '%s' % fid)] |
216 |
|
217 |
def _filter_by_metadata(self, servers): |
218 |
new_servers = [] |
219 |
for srv in servers: |
220 |
if not 'metadata' in srv: |
221 |
continue
|
222 |
meta = [dict(srv['metadata'])] |
223 |
if self['meta']: |
224 |
meta = filter_dicts_by_dict(meta, self['meta']) |
225 |
if meta and self['meta_like']: |
226 |
meta = filter_dicts_by_dict( |
227 |
meta, self['meta_like'], exact_match=False) |
228 |
if meta:
|
229 |
new_servers.append(srv) |
230 |
return new_servers
|
231 |
|
232 |
@errors.generic.all
|
233 |
@errors.cyclades.connection
|
234 |
@errors.cyclades.date
|
235 |
def _run(self): |
236 |
withimage = bool(self['image_id']) |
237 |
withflavor = bool(self['flavor_id']) |
238 |
withmeta = bool(self['meta'] or self['meta_like']) |
239 |
withcommons = bool(
|
240 |
self['status'] or self['user_id'] or self['user_name']) |
241 |
detail = self['detail'] or ( |
242 |
withimage or withflavor or withmeta or withcommons) |
243 |
servers = self.client.list_servers(detail, self['since']) |
244 |
|
245 |
servers = self._filter_by_name(servers)
|
246 |
servers = self._filter_by_id(servers)
|
247 |
servers = self._apply_common_filters(servers)
|
248 |
if withimage:
|
249 |
servers = self._filter_by_image(servers)
|
250 |
if withflavor:
|
251 |
servers = self._filter_by_flavor(servers)
|
252 |
if withmeta:
|
253 |
servers = self._filter_by_metadata(servers)
|
254 |
|
255 |
if self['detail'] and not ( |
256 |
self['json_output'] or self['output_format']): |
257 |
servers = self._add_user_name(servers)
|
258 |
elif not (self['detail'] or ( |
259 |
self['json_output'] or self['output_format'])): |
260 |
remove_from_items(servers, 'links')
|
261 |
if detail and not self['detail']: |
262 |
for srv in servers: |
263 |
for key in set(srv).difference(self.PERMANENTS): |
264 |
srv.pop(key) |
265 |
kwargs = dict(with_enumeration=self['enum']) |
266 |
if self['more']: |
267 |
kwargs['out'] = StringIO()
|
268 |
kwargs['title'] = ()
|
269 |
if self['limit']: |
270 |
servers = servers[:self['limit']] |
271 |
self._print(servers, **kwargs)
|
272 |
if self['more']: |
273 |
pager(kwargs['out'].getvalue())
|
274 |
|
275 |
def main(self): |
276 |
super(self.__class__, self)._run() |
277 |
self._run()
|
278 |
|
279 |
|
280 |
@command(server_cmds)
|
281 |
class server_info(_init_cyclades, _optional_json): |
282 |
"""Detailed information on a Virtual Machine
|
283 |
Contains:
|
284 |
- name, id, status, create/update dates
|
285 |
- network interfaces
|
286 |
- metadata (e.g., os, superuser) and diagnostics
|
287 |
- hardware flavor and os image ids
|
288 |
"""
|
289 |
|
290 |
@errors.generic.all
|
291 |
@errors.cyclades.connection
|
292 |
@errors.cyclades.server_id
|
293 |
def _run(self, server_id): |
294 |
vm = self.client.get_server_details(server_id)
|
295 |
uuids = self._uuids2usernames([vm['user_id'], vm['tenant_id']]) |
296 |
vm['user_id'] += ' (%s)' % uuids[vm['user_id']] |
297 |
vm['tenant_id'] += ' (%s)' % uuids[vm['tenant_id']] |
298 |
self._print(vm, self.print_dict) |
299 |
|
300 |
def main(self, server_id): |
301 |
super(self.__class__, self)._run() |
302 |
self._run(server_id=server_id)
|
303 |
|
304 |
|
305 |
class PersonalityArgument(KeyValueArgument): |
306 |
|
307 |
terms = ( |
308 |
('local-path', 'contents'), |
309 |
('server-path', 'path'), |
310 |
('owner', 'owner'), |
311 |
('group', 'group'), |
312 |
('mode', 'mode')) |
313 |
|
314 |
@property
|
315 |
def value(self): |
316 |
return getattr(self, '_value', []) |
317 |
|
318 |
@value.setter
|
319 |
def value(self, newvalue): |
320 |
if newvalue == self.default: |
321 |
return self.value |
322 |
self._value, input_dict = [], {}
|
323 |
for i, terms in enumerate(newvalue): |
324 |
termlist = terms.split(',')
|
325 |
if len(termlist) > len(self.terms): |
326 |
msg = 'Wrong number of terms (1<=terms<=%s)' % len(self.terms) |
327 |
raiseCLIError(CLISyntaxError(msg), details=howto_personality) |
328 |
|
329 |
for k, v in self.terms: |
330 |
prefix = '%s=' % k
|
331 |
for item in termlist: |
332 |
if item.lower().startswith(prefix):
|
333 |
input_dict[k] = item[len(k) + 1:] |
334 |
break
|
335 |
item = None
|
336 |
if item:
|
337 |
termlist.remove(item) |
338 |
|
339 |
try:
|
340 |
path = input_dict['local-path']
|
341 |
except KeyError: |
342 |
path = termlist.pop(0)
|
343 |
if not path: |
344 |
raise CLIInvalidArgument(
|
345 |
'--personality: No local path specified',
|
346 |
details=howto_personality) |
347 |
|
348 |
if not exists(path): |
349 |
raise CLIInvalidArgument(
|
350 |
'--personality: File %s does not exist' % path,
|
351 |
details=howto_personality) |
352 |
|
353 |
self._value.append(dict(path=path)) |
354 |
with open(expanduser(path)) as f: |
355 |
self._value[i]['contents'] = b64encode(f.read()) |
356 |
for k, v in self.terms[1:]: |
357 |
try:
|
358 |
self._value[i][v] = input_dict[k]
|
359 |
except KeyError: |
360 |
try:
|
361 |
self._value[i][v] = termlist.pop(0) |
362 |
except IndexError: |
363 |
continue
|
364 |
if k in ('mode', ) and self._value[i][v]: |
365 |
try:
|
366 |
self._value[i][v] = int(self._value[i][v], 8) |
367 |
except ValueError as ve: |
368 |
raise CLIInvalidArgument(
|
369 |
'Personality mode must be in octal', details=[
|
370 |
'%s' % ve])
|
371 |
|
372 |
|
373 |
@command(server_cmds)
|
374 |
class server_create(_init_cyclades, _optional_json, _server_wait): |
375 |
"""Create a server (aka Virtual Machine)
|
376 |
Parameters:
|
377 |
- name: (single quoted text)
|
378 |
- flavor id: Hardware flavor. Pick one from: /flavor list
|
379 |
- image id: OS images. Pick one from: /image list
|
380 |
"""
|
381 |
|
382 |
arguments = dict(
|
383 |
personality=PersonalityArgument( |
384 |
(80 * ' ').join(howto_personality), ('-p', '--personality')), |
385 |
wait=FlagArgument('Wait server to build', ('-w', '--wait')), |
386 |
cluster_size=IntArgument( |
387 |
'Create a cluster of servers of this size. In this case, the name'
|
388 |
'parameter is the prefix of each server in the cluster (e.g.,'
|
389 |
'srv1, srv2, etc.',
|
390 |
'--cluster-size'),
|
391 |
max_threads=IntArgument( |
392 |
'Max threads in cluster mode (default 1)', '--threads') |
393 |
) |
394 |
|
395 |
@errors.cyclades.cluster_size
|
396 |
def _create_cluster(self, prefix, flavor_id, image_id, size): |
397 |
servers = [dict(
|
398 |
name='%s%s' % (prefix, i if size > 1 else ''), |
399 |
flavor_id=flavor_id, |
400 |
image_id=image_id, |
401 |
personality=self['personality']) for i in range(1, 1 + size)] |
402 |
if size == 1: |
403 |
return [self.client.create_server(**servers[0])] |
404 |
self.client.MAX_THREADS = int(self['max_threads'] or 1) |
405 |
try:
|
406 |
r = self.client.async_run(self.client.create_server, servers) |
407 |
return r
|
408 |
except Exception as e: |
409 |
if size == 1: |
410 |
raise e
|
411 |
try:
|
412 |
requested_names = [s['name'] for s in servers] |
413 |
spawned_servers = [dict(
|
414 |
name=s['name'],
|
415 |
id=s['id']) for s in self.client.list_servers() if ( |
416 |
s['name'] in requested_names)] |
417 |
self.error('Failed to build %s servers' % size) |
418 |
self.error('Found %s matching servers:' % len(spawned_servers)) |
419 |
self._print(spawned_servers, out=self._err) |
420 |
self.error('Check if any of these servers should be removed\n') |
421 |
except Exception as ne: |
422 |
self.error('Error (%s) while notifying about errors' % ne) |
423 |
finally:
|
424 |
raise e
|
425 |
|
426 |
@errors.generic.all
|
427 |
@errors.cyclades.connection
|
428 |
@errors.plankton.id
|
429 |
@errors.cyclades.flavor_id
|
430 |
def _run(self, name, flavor_id, image_id): |
431 |
for r in self._create_cluster( |
432 |
name, flavor_id, image_id, size=self['cluster_size'] or 1): |
433 |
if not r: |
434 |
self.error('Create %s: server response was %s' % (name, r)) |
435 |
continue
|
436 |
usernames = self._uuids2usernames(
|
437 |
[r['user_id'], r['tenant_id']]) |
438 |
r['user_id'] += ' (%s)' % usernames[r['user_id']] |
439 |
r['tenant_id'] += ' (%s)' % usernames[r['tenant_id']] |
440 |
self._print(r, self.print_dict) |
441 |
if self['wait']: |
442 |
self._wait(r['id'], r['status']) |
443 |
self.writeln(' ') |
444 |
|
445 |
def main(self, name, flavor_id, image_id): |
446 |
super(self.__class__, self)._run() |
447 |
self._run(name=name, flavor_id=flavor_id, image_id=image_id)
|
448 |
|
449 |
|
450 |
@command(server_cmds)
|
451 |
class server_rename(_init_cyclades, _optional_output_cmd): |
452 |
"""Set/update a virtual server name
|
453 |
virtual server names are not unique, therefore multiple servers may share
|
454 |
the same name
|
455 |
"""
|
456 |
|
457 |
@errors.generic.all
|
458 |
@errors.cyclades.connection
|
459 |
@errors.cyclades.server_id
|
460 |
def _run(self, server_id, new_name): |
461 |
self._optional_output(
|
462 |
self.client.update_server_name(int(server_id), new_name)) |
463 |
|
464 |
def main(self, server_id, new_name): |
465 |
super(self.__class__, self)._run() |
466 |
self._run(server_id=server_id, new_name=new_name)
|
467 |
|
468 |
|
469 |
@command(server_cmds)
|
470 |
class server_delete(_init_cyclades, _optional_output_cmd, _server_wait): |
471 |
"""Delete a virtual server"""
|
472 |
|
473 |
arguments = dict(
|
474 |
wait=FlagArgument('Wait server to be destroyed', ('-w', '--wait')), |
475 |
cluster=FlagArgument( |
476 |
'(DANGEROUS) Delete all virtual servers prefixed with the cluster '
|
477 |
'prefix. In that case, the prefix replaces the server id',
|
478 |
'--cluster')
|
479 |
) |
480 |
|
481 |
def _server_ids(self, server_var): |
482 |
if self['cluster']: |
483 |
return [s['id'] for s in self.client.list_servers() if ( |
484 |
s['name'].startswith(server_var))]
|
485 |
|
486 |
@errors.cyclades.server_id
|
487 |
def _check_server_id(self, server_id): |
488 |
return server_id
|
489 |
|
490 |
return [_check_server_id(self, server_id=server_var), ] |
491 |
|
492 |
@errors.generic.all
|
493 |
@errors.cyclades.connection
|
494 |
def _run(self, server_var): |
495 |
for server_id in self._server_ids(server_var): |
496 |
if self['wait']: |
497 |
details = self.client.get_server_details(server_id)
|
498 |
status = details['status']
|
499 |
|
500 |
r = self.client.delete_server(server_id)
|
501 |
self._optional_output(r)
|
502 |
|
503 |
if self['wait']: |
504 |
self._wait(server_id, status)
|
505 |
|
506 |
def main(self, server_id_or_cluster_prefix): |
507 |
super(self.__class__, self)._run() |
508 |
self._run(server_id_or_cluster_prefix)
|
509 |
|
510 |
|
511 |
@command(server_cmds)
|
512 |
class server_reboot(_init_cyclades, _optional_output_cmd, _server_wait): |
513 |
"""Reboot a virtual server"""
|
514 |
|
515 |
arguments = dict(
|
516 |
hard=FlagArgument( |
517 |
'perform a hard reboot (deprecated)', ('-f', '--force')), |
518 |
type=ValueArgument('SOFT or HARD - default: SOFT', ('--type')), |
519 |
wait=FlagArgument('Wait server to be destroyed', ('-w', '--wait')) |
520 |
) |
521 |
|
522 |
@errors.generic.all
|
523 |
@errors.cyclades.connection
|
524 |
@errors.cyclades.server_id
|
525 |
def _run(self, server_id): |
526 |
hard_reboot = self['hard'] |
527 |
if hard_reboot:
|
528 |
self.error(
|
529 |
'WARNING: -f/--force will be deprecated in version 0.12\n'
|
530 |
'\tIn the future, please use --type=hard instead')
|
531 |
if self['type']: |
532 |
if self['type'].lower() in ('soft', ): |
533 |
hard_reboot = False
|
534 |
elif self['type'].lower() in ('hard', ): |
535 |
hard_reboot = True
|
536 |
else:
|
537 |
raise CLISyntaxError(
|
538 |
'Invalid reboot type %s' % self['type'], |
539 |
importance=2, details=[
|
540 |
'--type values are either SOFT (default) or HARD'])
|
541 |
|
542 |
r = self.client.reboot_server(int(server_id), hard_reboot) |
543 |
self._optional_output(r)
|
544 |
|
545 |
if self['wait']: |
546 |
self._wait(server_id, 'REBOOT') |
547 |
|
548 |
def main(self, server_id): |
549 |
super(self.__class__, self)._run() |
550 |
self._run(server_id=server_id)
|
551 |
|
552 |
|
553 |
@command(server_cmds)
|
554 |
class server_start(_init_cyclades, _optional_output_cmd, _server_wait): |
555 |
"""Start an existing virtual server"""
|
556 |
|
557 |
arguments = dict(
|
558 |
wait=FlagArgument('Wait server to be destroyed', ('-w', '--wait')) |
559 |
) |
560 |
|
561 |
@errors.generic.all
|
562 |
@errors.cyclades.connection
|
563 |
@errors.cyclades.server_id
|
564 |
def _run(self, server_id): |
565 |
status = 'ACTIVE'
|
566 |
if self['wait']: |
567 |
details = self.client.get_server_details(server_id)
|
568 |
status = details['status']
|
569 |
if status in ('ACTIVE', ): |
570 |
return
|
571 |
|
572 |
r = self.client.start_server(int(server_id)) |
573 |
self._optional_output(r)
|
574 |
|
575 |
if self['wait']: |
576 |
self._wait(server_id, status)
|
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_shutdown(_init_cyclades, _optional_output_cmd, _server_wait): |
585 |
"""Shutdown an active virtual server"""
|
586 |
|
587 |
arguments = dict(
|
588 |
wait=FlagArgument('Wait server to be destroyed', ('-w', '--wait')) |
589 |
) |
590 |
|
591 |
@errors.generic.all
|
592 |
@errors.cyclades.connection
|
593 |
@errors.cyclades.server_id
|
594 |
def _run(self, server_id): |
595 |
status = 'STOPPED'
|
596 |
if self['wait']: |
597 |
details = self.client.get_server_details(server_id)
|
598 |
status = details['status']
|
599 |
if status in ('STOPPED', ): |
600 |
return
|
601 |
|
602 |
r = self.client.shutdown_server(int(server_id)) |
603 |
self._optional_output(r)
|
604 |
|
605 |
if self['wait']: |
606 |
self._wait(server_id, status)
|
607 |
|
608 |
def main(self, server_id): |
609 |
super(self.__class__, self)._run() |
610 |
self._run(server_id=server_id)
|
611 |
|
612 |
|
613 |
@command(server_cmds)
|
614 |
class server_console(_init_cyclades, _optional_json): |
615 |
"""Get a VNC console to access an existing virtual server
|
616 |
Console connection information provided (at least):
|
617 |
- host: (url or address) a VNC host
|
618 |
- port: (int) the gateway to enter virtual server on host
|
619 |
- password: for VNC authorization
|
620 |
"""
|
621 |
|
622 |
@errors.generic.all
|
623 |
@errors.cyclades.connection
|
624 |
@errors.cyclades.server_id
|
625 |
def _run(self, server_id): |
626 |
self._print(
|
627 |
self.client.get_server_console(int(server_id)), self.print_dict) |
628 |
|
629 |
def main(self, server_id): |
630 |
super(self.__class__, self)._run() |
631 |
self._run(server_id=server_id)
|
632 |
|
633 |
|
634 |
@command(server_cmds)
|
635 |
class server_resize(_init_cyclades, _optional_output_cmd): |
636 |
"""Set a different flavor for an existing server
|
637 |
To get server ids and flavor ids:
|
638 |
/server list
|
639 |
/flavor list
|
640 |
"""
|
641 |
|
642 |
@errors.generic.all
|
643 |
@errors.cyclades.connection
|
644 |
@errors.cyclades.server_id
|
645 |
@errors.cyclades.flavor_id
|
646 |
def _run(self, server_id, flavor_id): |
647 |
self._optional_output(self.client.resize_server(server_id, flavor_id)) |
648 |
|
649 |
def main(self, server_id, flavor_id): |
650 |
super(self.__class__, self)._run() |
651 |
self._run(server_id=server_id, flavor_id=flavor_id)
|
652 |
|
653 |
|
654 |
@command(server_cmds)
|
655 |
class server_firewall(_init_cyclades): |
656 |
"""Manage virtual server firewall profiles for public networks"""
|
657 |
|
658 |
|
659 |
@command(server_cmds)
|
660 |
class server_firewall_set( |
661 |
_init_cyclades, _optional_output_cmd, _firewall_wait): |
662 |
"""Set the firewall profile on virtual server public network
|
663 |
Values for profile:
|
664 |
- DISABLED: Shutdown firewall
|
665 |
- ENABLED: Firewall in normal mode
|
666 |
- PROTECTED: Firewall in secure mode
|
667 |
"""
|
668 |
|
669 |
arguments = dict(
|
670 |
wait=FlagArgument('Wait server firewall to build', ('-w', '--wait')), |
671 |
timeout=IntArgument( |
672 |
'Set wait timeout in seconds (default: 60)', '--timeout', |
673 |
default=60)
|
674 |
) |
675 |
|
676 |
@errors.generic.all
|
677 |
@errors.cyclades.connection
|
678 |
@errors.cyclades.server_id
|
679 |
@errors.cyclades.firewall
|
680 |
def _run(self, server_id, profile): |
681 |
if self['timeout'] and not self['wait']: |
682 |
raise CLIInvalidArgument('Invalid use of --timeout', details=[ |
683 |
'Timeout is used only along with -w/--wait'])
|
684 |
old_profile = self.client.get_firewall_profile(server_id)
|
685 |
if old_profile.lower() == profile.lower():
|
686 |
self.error('Firewall of server %s: allready in status %s' % ( |
687 |
server_id, old_profile)) |
688 |
else:
|
689 |
self._optional_output(self.client.set_firewall_profile( |
690 |
server_id=int(server_id), profile=('%s' % profile).upper())) |
691 |
if self['wait']: |
692 |
self._wait(server_id, old_profile, timeout=self['timeout']) |
693 |
|
694 |
def main(self, server_id, profile): |
695 |
super(self.__class__, self)._run() |
696 |
self._run(server_id=server_id, profile=profile)
|
697 |
|
698 |
|
699 |
@command(server_cmds)
|
700 |
class server_firewall_get(_init_cyclades): |
701 |
"""Get the firewall profile for a virtual servers' public network"""
|
702 |
|
703 |
@errors.generic.all
|
704 |
@errors.cyclades.connection
|
705 |
@errors.cyclades.server_id
|
706 |
def _run(self, server_id): |
707 |
self.writeln(self.client.get_firewall_profile(server_id)) |
708 |
|
709 |
def main(self, server_id): |
710 |
super(self.__class__, self)._run() |
711 |
self._run(server_id=server_id)
|
712 |
|
713 |
|
714 |
@command(server_cmds)
|
715 |
class server_addr(_init_cyclades, _optional_json): |
716 |
"""List the addresses of all network interfaces on a virtual server"""
|
717 |
|
718 |
arguments = dict(
|
719 |
enum=FlagArgument('Enumerate results', '--enumerate') |
720 |
) |
721 |
|
722 |
@errors.generic.all
|
723 |
@errors.cyclades.connection
|
724 |
@errors.cyclades.server_id
|
725 |
def _run(self, server_id): |
726 |
reply = self.client.list_server_nics(int(server_id)) |
727 |
self._print(reply, with_enumeration=self['enum'] and (reply) > 1) |
728 |
|
729 |
def main(self, server_id): |
730 |
super(self.__class__, self)._run() |
731 |
self._run(server_id=server_id)
|
732 |
|
733 |
|
734 |
@command(server_cmds)
|
735 |
class server_metadata(_init_cyclades): |
736 |
"""Manage Server metadata (key:value pairs of server attributes)"""
|
737 |
|
738 |
|
739 |
@command(server_cmds)
|
740 |
class server_metadata_list(_init_cyclades, _optional_json): |
741 |
"""Get server metadata"""
|
742 |
|
743 |
@errors.generic.all
|
744 |
@errors.cyclades.connection
|
745 |
@errors.cyclades.server_id
|
746 |
@errors.cyclades.metadata
|
747 |
def _run(self, server_id, key=''): |
748 |
self._print(
|
749 |
self.client.get_server_metadata(int(server_id), key), |
750 |
self.print_dict)
|
751 |
|
752 |
def main(self, server_id, key=''): |
753 |
super(self.__class__, self)._run() |
754 |
self._run(server_id=server_id, key=key)
|
755 |
|
756 |
|
757 |
@command(server_cmds)
|
758 |
class server_metadata_set(_init_cyclades, _optional_json): |
759 |
"""Set / update virtual server metadata
|
760 |
Metadata should be given in key/value pairs in key=value format
|
761 |
For example: /server metadata set <server id> key1=value1 key2=value2
|
762 |
Old, unreferenced metadata will remain intact
|
763 |
"""
|
764 |
|
765 |
@errors.generic.all
|
766 |
@errors.cyclades.connection
|
767 |
@errors.cyclades.server_id
|
768 |
def _run(self, server_id, keyvals): |
769 |
assert keyvals, 'Please, add some metadata ( key=value)' |
770 |
metadata = dict()
|
771 |
for keyval in keyvals: |
772 |
k, sep, v = keyval.partition('=')
|
773 |
if sep and k: |
774 |
metadata[k] = v |
775 |
else:
|
776 |
raiseCLIError( |
777 |
'Invalid piece of metadata %s' % keyval,
|
778 |
importance=2, details=[
|
779 |
'Correct metadata format: key=val',
|
780 |
'For example:',
|
781 |
'/server metadata set <server id>'
|
782 |
'key1=value1 key2=value2'])
|
783 |
self._print(
|
784 |
self.client.update_server_metadata(int(server_id), **metadata), |
785 |
self.print_dict)
|
786 |
|
787 |
def main(self, server_id, *key_equals_val): |
788 |
super(self.__class__, self)._run() |
789 |
self._run(server_id=server_id, keyvals=key_equals_val)
|
790 |
|
791 |
|
792 |
@command(server_cmds)
|
793 |
class server_metadata_delete(_init_cyclades, _optional_output_cmd): |
794 |
"""Delete virtual server metadata"""
|
795 |
|
796 |
@errors.generic.all
|
797 |
@errors.cyclades.connection
|
798 |
@errors.cyclades.server_id
|
799 |
@errors.cyclades.metadata
|
800 |
def _run(self, server_id, key): |
801 |
self._optional_output(
|
802 |
self.client.delete_server_metadata(int(server_id), key)) |
803 |
|
804 |
def main(self, server_id, key): |
805 |
super(self.__class__, self)._run() |
806 |
self._run(server_id=server_id, key=key)
|
807 |
|
808 |
|
809 |
@command(server_cmds)
|
810 |
class server_stats(_init_cyclades, _optional_json): |
811 |
"""Get virtual server statistics"""
|
812 |
|
813 |
@errors.generic.all
|
814 |
@errors.cyclades.connection
|
815 |
@errors.cyclades.server_id
|
816 |
def _run(self, server_id): |
817 |
self._print(
|
818 |
self.client.get_server_stats(int(server_id)), self.print_dict) |
819 |
|
820 |
def main(self, server_id): |
821 |
super(self.__class__, self)._run() |
822 |
self._run(server_id=server_id)
|
823 |
|
824 |
|
825 |
@command(server_cmds)
|
826 |
class server_wait(_init_cyclades, _server_wait): |
827 |
"""Wait for server to finish [BUILD, STOPPED, REBOOT, ACTIVE]"""
|
828 |
|
829 |
arguments = dict(
|
830 |
timeout=IntArgument( |
831 |
'Wait limit in seconds (default: 60)', '--timeout', default=60) |
832 |
) |
833 |
|
834 |
@errors.generic.all
|
835 |
@errors.cyclades.connection
|
836 |
@errors.cyclades.server_id
|
837 |
def _run(self, server_id, current_status): |
838 |
r = self.client.get_server_details(server_id)
|
839 |
if r['status'].lower() == current_status.lower(): |
840 |
self._wait(server_id, current_status, timeout=self['timeout']) |
841 |
else:
|
842 |
self.error(
|
843 |
'Server %s: Cannot wait for status %s, '
|
844 |
'status is already %s' % (
|
845 |
server_id, current_status, r['status']))
|
846 |
|
847 |
def main(self, server_id, current_status='BUILD'): |
848 |
super(self.__class__, self)._run() |
849 |
self._run(server_id=server_id, current_status=current_status)
|
850 |
|
851 |
|
852 |
@command(flavor_cmds)
|
853 |
class flavor_list(_init_cyclades, _optional_json, _name_filter, _id_filter): |
854 |
"""List available hardware flavors"""
|
855 |
|
856 |
PERMANENTS = ('id', 'name') |
857 |
|
858 |
arguments = dict(
|
859 |
detail=FlagArgument('show detailed output', ('-l', '--details')), |
860 |
limit=IntArgument('limit # of listed flavors', ('-n', '--number')), |
861 |
more=FlagArgument( |
862 |
'output results in pages (-n to set items per page, default 10)',
|
863 |
'--more'),
|
864 |
enum=FlagArgument('Enumerate results', '--enumerate'), |
865 |
ram=ValueArgument('filter by ram', ('--ram')), |
866 |
vcpus=ValueArgument('filter by number of VCPUs', ('--vcpus')), |
867 |
disk=ValueArgument('filter by disk size in GB', ('--disk')), |
868 |
disk_template=ValueArgument( |
869 |
'filter by disk_templace', ('--disk-template')) |
870 |
) |
871 |
|
872 |
def _apply_common_filters(self, flavors): |
873 |
common_filters = dict()
|
874 |
if self['ram']: |
875 |
common_filters['ram'] = self['ram'] |
876 |
if self['vcpus']: |
877 |
common_filters['vcpus'] = self['vcpus'] |
878 |
if self['disk']: |
879 |
common_filters['disk'] = self['disk'] |
880 |
if self['disk_template']: |
881 |
common_filters['SNF:disk_template'] = self['disk_template'] |
882 |
return filter_dicts_by_dict(flavors, common_filters)
|
883 |
|
884 |
@errors.generic.all
|
885 |
@errors.cyclades.connection
|
886 |
def _run(self): |
887 |
withcommons = self['ram'] or self['vcpus'] or ( |
888 |
self['disk'] or self['disk_template']) |
889 |
detail = self['detail'] or withcommons |
890 |
flavors = self.client.list_flavors(detail)
|
891 |
flavors = self._filter_by_name(flavors)
|
892 |
flavors = self._filter_by_id(flavors)
|
893 |
if withcommons:
|
894 |
flavors = self._apply_common_filters(flavors)
|
895 |
if not (self['detail'] or ( |
896 |
self['json_output'] or self['output_format'])): |
897 |
remove_from_items(flavors, 'links')
|
898 |
if detail and not self['detail']: |
899 |
for flv in flavors: |
900 |
for key in set(flv).difference(self.PERMANENTS): |
901 |
flv.pop(key) |
902 |
kwargs = dict(out=StringIO(), title=()) if self['more'] else {} |
903 |
self._print(
|
904 |
flavors, |
905 |
with_redundancy=self['detail'], with_enumeration=self['enum'], |
906 |
**kwargs) |
907 |
if self['more']: |
908 |
pager(kwargs['out'].getvalue())
|
909 |
|
910 |
def main(self): |
911 |
super(self.__class__, self)._run() |
912 |
self._run()
|
913 |
|
914 |
|
915 |
@command(flavor_cmds)
|
916 |
class flavor_info(_init_cyclades, _optional_json): |
917 |
"""Detailed information on a hardware flavor
|
918 |
To get a list of available flavors and flavor ids, try /flavor list
|
919 |
"""
|
920 |
|
921 |
@errors.generic.all
|
922 |
@errors.cyclades.connection
|
923 |
@errors.cyclades.flavor_id
|
924 |
def _run(self, flavor_id): |
925 |
self._print(
|
926 |
self.client.get_flavor_details(int(flavor_id)), self.print_dict) |
927 |
|
928 |
def main(self, flavor_id): |
929 |
super(self.__class__, self)._run() |
930 |
self._run(flavor_id=flavor_id)
|
931 |
|
932 |
|
933 |
def _add_name(self, net): |
934 |
user_id, tenant_id, uuids = net['user_id'], net['tenant_id'], [] |
935 |
if user_id:
|
936 |
uuids.append(user_id) |
937 |
if tenant_id:
|
938 |
uuids.append(tenant_id) |
939 |
if uuids:
|
940 |
usernames = self._uuids2usernames(uuids)
|
941 |
if user_id:
|
942 |
net['user_id'] += ' (%s)' % usernames[user_id] |
943 |
if tenant_id:
|
944 |
net['tenant_id'] += ' (%s)' % usernames[tenant_id] |
945 |
|
946 |
|
947 |
@command(network_cmds)
|
948 |
class network_info(_init_cyclades, _optional_json): |
949 |
"""Detailed information on a network
|
950 |
To get a list of available networks and network ids, try /network list
|
951 |
"""
|
952 |
|
953 |
@errors.generic.all
|
954 |
@errors.cyclades.connection
|
955 |
@errors.cyclades.network_id
|
956 |
def _run(self, network_id): |
957 |
network = self.client.get_network_details(int(network_id)) |
958 |
_add_name(self, network)
|
959 |
self._print(network, self.print_dict, exclude=('id')) |
960 |
|
961 |
def main(self, network_id): |
962 |
super(self.__class__, self)._run() |
963 |
self._run(network_id=network_id)
|
964 |
|
965 |
|
966 |
@command(network_cmds)
|
967 |
class network_list(_init_cyclades, _optional_json, _name_filter, _id_filter): |
968 |
"""List networks"""
|
969 |
|
970 |
PERMANENTS = ('id', 'name') |
971 |
|
972 |
arguments = dict(
|
973 |
detail=FlagArgument('show detailed output', ('-l', '--details')), |
974 |
limit=IntArgument('limit # of listed networks', ('-n', '--number')), |
975 |
more=FlagArgument( |
976 |
'output results in pages (-n to set items per page, default 10)',
|
977 |
'--more'),
|
978 |
enum=FlagArgument('Enumerate results', '--enumerate'), |
979 |
status=ValueArgument('filter by status', ('--status')), |
980 |
public=FlagArgument('only public networks', ('--public')), |
981 |
private=FlagArgument('only private networks', ('--private')), |
982 |
dhcp=FlagArgument('show networks with dhcp', ('--with-dhcp')), |
983 |
no_dhcp=FlagArgument('show networks without dhcp', ('--without-dhcp')), |
984 |
user_id=ValueArgument('filter by user id', ('--user-id')), |
985 |
user_name=ValueArgument('filter by user name', ('--user-name')), |
986 |
gateway=ValueArgument('filter by gateway (IPv4)', ('--gateway')), |
987 |
gateway6=ValueArgument('filter by gateway (IPv6)', ('--gateway6')), |
988 |
cidr=ValueArgument('filter by cidr (IPv4)', ('--cidr')), |
989 |
cidr6=ValueArgument('filter by cidr (IPv6)', ('--cidr6')), |
990 |
type=ValueArgument('filter by type', ('--type')), |
991 |
) |
992 |
|
993 |
def _apply_common_filters(self, networks): |
994 |
common_filter = dict()
|
995 |
if self['public']: |
996 |
if self['private']: |
997 |
return []
|
998 |
common_filter['public'] = self['public'] |
999 |
elif self['private']: |
1000 |
common_filter['public'] = False |
1001 |
if self['dhcp']: |
1002 |
if self['no_dhcp']: |
1003 |
return []
|
1004 |
common_filter['dhcp'] = True |
1005 |
elif self['no_dhcp']: |
1006 |
common_filter['dhcp'] = False |
1007 |
if self['user_id'] or self['user_name']: |
1008 |
uuid = self['user_id'] or self._username2uuid(self['user_name']) |
1009 |
common_filter['user_id'] = uuid
|
1010 |
for term in ('status', 'gateway', 'gateway6', 'cidr', 'cidr6', 'type'): |
1011 |
if self[term]: |
1012 |
common_filter[term] = self[term]
|
1013 |
return filter_dicts_by_dict(networks, common_filter)
|
1014 |
|
1015 |
def _add_name(self, networks, key='user_id'): |
1016 |
uuids = self._uuids2usernames(
|
1017 |
list(set([net[key] for net in networks]))) |
1018 |
for net in networks: |
1019 |
v = net.get(key, None)
|
1020 |
if v:
|
1021 |
net[key] += ' (%s)' % uuids[v]
|
1022 |
return networks
|
1023 |
|
1024 |
@errors.generic.all
|
1025 |
@errors.cyclades.connection
|
1026 |
def _run(self): |
1027 |
withcommons = False
|
1028 |
for term in ( |
1029 |
'status', 'public', 'private', 'user_id', 'user_name', 'type', |
1030 |
'gateway', 'gateway6', 'cidr', 'cidr6', 'dhcp', 'no_dhcp'): |
1031 |
if self[term]: |
1032 |
withcommons = True
|
1033 |
break
|
1034 |
detail = self['detail'] or withcommons |
1035 |
networks = self.client.list_networks(detail)
|
1036 |
networks = self._filter_by_name(networks)
|
1037 |
networks = self._filter_by_id(networks)
|
1038 |
if withcommons:
|
1039 |
networks = self._apply_common_filters(networks)
|
1040 |
if not (self['detail'] or ( |
1041 |
self['json_output'] or self['output_format'])): |
1042 |
remove_from_items(networks, 'links')
|
1043 |
if detail and not self['detail']: |
1044 |
for net in networks: |
1045 |
for key in set(net).difference(self.PERMANENTS): |
1046 |
net.pop(key) |
1047 |
if self['detail'] and not ( |
1048 |
self['json_output'] or self['output_format']): |
1049 |
self._add_name(networks)
|
1050 |
self._add_name(networks, 'tenant_id') |
1051 |
kwargs = dict(with_enumeration=self['enum']) |
1052 |
if self['more']: |
1053 |
kwargs['out'] = StringIO()
|
1054 |
kwargs['title'] = ()
|
1055 |
if self['limit']: |
1056 |
networks = networks[:self['limit']] |
1057 |
self._print(networks, **kwargs)
|
1058 |
if self['more']: |
1059 |
pager(kwargs['out'].getvalue())
|
1060 |
|
1061 |
def main(self): |
1062 |
super(self.__class__, self)._run() |
1063 |
self._run()
|
1064 |
|
1065 |
|
1066 |
@command(network_cmds)
|
1067 |
class network_create(_init_cyclades, _optional_json, _network_wait): |
1068 |
"""Create an (unconnected) network"""
|
1069 |
|
1070 |
arguments = dict(
|
1071 |
cidr=ValueArgument('explicitly set cidr', '--with-cidr'), |
1072 |
gateway=ValueArgument('explicitly set gateway', '--with-gateway'), |
1073 |
dhcp=FlagArgument('Use dhcp (default: off)', '--with-dhcp'), |
1074 |
type=ValueArgument( |
1075 |
'Valid network types are '
|
1076 |
'CUSTOM, IP_LESS_ROUTED, MAC_FILTERED (default), PHYSICAL_VLAN',
|
1077 |
'--with-type',
|
1078 |
default='MAC_FILTERED'),
|
1079 |
wait=FlagArgument('Wait network to build', ('-w', '--wait')) |
1080 |
) |
1081 |
|
1082 |
@errors.generic.all
|
1083 |
@errors.cyclades.connection
|
1084 |
@errors.cyclades.network_max
|
1085 |
def _run(self, name): |
1086 |
r = self.client.create_network(
|
1087 |
name, |
1088 |
cidr=self['cidr'], |
1089 |
gateway=self['gateway'], |
1090 |
dhcp=self['dhcp'], |
1091 |
type=self['type']) |
1092 |
_add_name(self, r)
|
1093 |
self._print(r, self.print_dict) |
1094 |
if self['wait'] and r['status'] in ('PENDING', ): |
1095 |
self._wait(r['id'], 'PENDING') |
1096 |
|
1097 |
def main(self, name): |
1098 |
super(self.__class__, self)._run() |
1099 |
self._run(name)
|
1100 |
|
1101 |
|
1102 |
@command(network_cmds)
|
1103 |
class network_rename(_init_cyclades, _optional_output_cmd): |
1104 |
"""Set the name of a network"""
|
1105 |
|
1106 |
@errors.generic.all
|
1107 |
@errors.cyclades.connection
|
1108 |
@errors.cyclades.network_id
|
1109 |
def _run(self, network_id, new_name): |
1110 |
self._optional_output(
|
1111 |
self.client.update_network_name(int(network_id), new_name)) |
1112 |
|
1113 |
def main(self, network_id, new_name): |
1114 |
super(self.__class__, self)._run() |
1115 |
self._run(network_id=network_id, new_name=new_name)
|
1116 |
|
1117 |
|
1118 |
@command(network_cmds)
|
1119 |
class network_delete(_init_cyclades, _optional_output_cmd, _network_wait): |
1120 |
"""Delete a network"""
|
1121 |
|
1122 |
arguments = dict(
|
1123 |
wait=FlagArgument('Wait network to build', ('-w', '--wait')) |
1124 |
) |
1125 |
|
1126 |
@errors.generic.all
|
1127 |
@errors.cyclades.connection
|
1128 |
@errors.cyclades.network_in_use
|
1129 |
@errors.cyclades.network_id
|
1130 |
def _run(self, network_id): |
1131 |
status = 'DELETED'
|
1132 |
if self['wait']: |
1133 |
r = self.client.get_network_details(network_id)
|
1134 |
status = r['status']
|
1135 |
if status in ('DELETED', ): |
1136 |
return
|
1137 |
|
1138 |
r = self.client.delete_network(int(network_id)) |
1139 |
self._optional_output(r)
|
1140 |
|
1141 |
if self['wait']: |
1142 |
self._wait(network_id, status)
|
1143 |
|
1144 |
def main(self, network_id): |
1145 |
super(self.__class__, self)._run() |
1146 |
self._run(network_id=network_id)
|
1147 |
|
1148 |
|
1149 |
@command(network_cmds)
|
1150 |
class network_connect(_init_cyclades, _optional_output_cmd): |
1151 |
"""Connect a server to a network"""
|
1152 |
|
1153 |
@errors.generic.all
|
1154 |
@errors.cyclades.connection
|
1155 |
@errors.cyclades.server_id
|
1156 |
@errors.cyclades.network_id
|
1157 |
def _run(self, server_id, network_id): |
1158 |
self._optional_output(
|
1159 |
self.client.connect_server(int(server_id), int(network_id))) |
1160 |
|
1161 |
def main(self, server_id, network_id): |
1162 |
super(self.__class__, self)._run() |
1163 |
self._run(server_id=server_id, network_id=network_id)
|
1164 |
|
1165 |
|
1166 |
@command(network_cmds)
|
1167 |
class network_disconnect(_init_cyclades): |
1168 |
"""Disconnect a nic that connects a server to a network
|
1169 |
Nic ids are listed as "attachments" in detailed network information
|
1170 |
To get detailed network information: /network info <network id>
|
1171 |
"""
|
1172 |
|
1173 |
@errors.cyclades.nic_format
|
1174 |
def _server_id_from_nic(self, nic_id): |
1175 |
return nic_id.split('-')[1] |
1176 |
|
1177 |
@errors.generic.all
|
1178 |
@errors.cyclades.connection
|
1179 |
@errors.cyclades.server_id
|
1180 |
@errors.cyclades.nic_id
|
1181 |
def _run(self, nic_id, server_id): |
1182 |
num_of_disconnected = self.client.disconnect_server(server_id, nic_id)
|
1183 |
if not num_of_disconnected: |
1184 |
raise ClientError(
|
1185 |
'Network Interface %s not found on server %s' % (
|
1186 |
nic_id, server_id), |
1187 |
status=404)
|
1188 |
print('Disconnected %s connections' % num_of_disconnected)
|
1189 |
|
1190 |
def main(self, nic_id): |
1191 |
super(self.__class__, self)._run() |
1192 |
server_id = self._server_id_from_nic(nic_id=nic_id)
|
1193 |
self._run(nic_id=nic_id, server_id=server_id)
|
1194 |
|
1195 |
|
1196 |
@command(network_cmds)
|
1197 |
class network_wait(_init_cyclades, _network_wait): |
1198 |
"""Wait for server to finish [PENDING, ACTIVE, DELETED]"""
|
1199 |
|
1200 |
arguments = dict(
|
1201 |
timeout=IntArgument( |
1202 |
'Wait limit in seconds (default: 60)', '--timeout', default=60) |
1203 |
) |
1204 |
|
1205 |
@errors.generic.all
|
1206 |
@errors.cyclades.connection
|
1207 |
@errors.cyclades.network_id
|
1208 |
def _run(self, network_id, current_status): |
1209 |
net = self.client.get_network_details(network_id)
|
1210 |
if net['status'].lower() == current_status.lower(): |
1211 |
self._wait(network_id, current_status, timeout=self['timeout']) |
1212 |
else:
|
1213 |
self.error(
|
1214 |
'Network %s: Cannot wait for status %s, '
|
1215 |
'status is already %s' % (
|
1216 |
network_id, current_status, net['status']))
|
1217 |
|
1218 |
def main(self, network_id, current_status='PENDING'): |
1219 |
super(self.__class__, self)._run() |
1220 |
self._run(network_id=network_id, current_status=current_status)
|
1221 |
|
1222 |
|
1223 |
@command(ip_cmds)
|
1224 |
class ip_pools(_init_cyclades, _optional_json): |
1225 |
"""List pools of floating IPs"""
|
1226 |
|
1227 |
@errors.generic.all
|
1228 |
@errors.cyclades.connection
|
1229 |
def _run(self): |
1230 |
r = self.client.get_floating_ip_pools()
|
1231 |
self._print(r if self['json_output'] or self['output_format'] else r[ |
1232 |
'floating_ip_pools'])
|
1233 |
|
1234 |
def main(self): |
1235 |
super(self.__class__, self)._run() |
1236 |
self._run()
|
1237 |
|
1238 |
|
1239 |
@command(ip_cmds)
|
1240 |
class ip_list(_init_cyclades, _optional_json): |
1241 |
"""List reserved floating IPs"""
|
1242 |
|
1243 |
@errors.generic.all
|
1244 |
@errors.cyclades.connection
|
1245 |
def _run(self): |
1246 |
r = self.client.get_floating_ips()
|
1247 |
self._print(r if self['json_output'] or self['output_format'] else r[ |
1248 |
'floating_ips'])
|
1249 |
|
1250 |
def main(self): |
1251 |
super(self.__class__, self)._run() |
1252 |
self._run()
|
1253 |
|
1254 |
|
1255 |
@command(ip_cmds)
|
1256 |
class ip_info(_init_cyclades, _optional_json): |
1257 |
"""Details for an IP"""
|
1258 |
|
1259 |
@errors.generic.all
|
1260 |
@errors.cyclades.connection
|
1261 |
def _run(self, ip): |
1262 |
self._print(self.client.get_floating_ip(ip), self.print_dict) |
1263 |
|
1264 |
def main(self, IP): |
1265 |
super(self.__class__, self)._run() |
1266 |
self._run(ip=IP)
|
1267 |
|
1268 |
|
1269 |
@command(ip_cmds)
|
1270 |
class ip_reserve(_init_cyclades, _optional_json): |
1271 |
"""Reserve a floating IP
|
1272 |
An IP is reserved from an IP pool. The default IP pool is chosen
|
1273 |
automatically, but there is the option if specifying an explicit IP pool.
|
1274 |
"""
|
1275 |
|
1276 |
arguments = dict(pool=ValueArgument('Source IP pool', ('--pool'), None)) |
1277 |
|
1278 |
@errors.generic.all
|
1279 |
@errors.cyclades.connection
|
1280 |
def _run(self, ip=None): |
1281 |
self._print([self.client.alloc_floating_ip(self['pool'], ip)]) |
1282 |
|
1283 |
def main(self, requested_IP=None): |
1284 |
super(self.__class__, self)._run() |
1285 |
self._run(ip=requested_IP)
|
1286 |
|
1287 |
|
1288 |
@command(ip_cmds)
|
1289 |
class ip_release(_init_cyclades, _optional_output_cmd): |
1290 |
"""Release a floating IP
|
1291 |
The release IP is "returned" to the IP pool it came from.
|
1292 |
"""
|
1293 |
|
1294 |
@errors.generic.all
|
1295 |
@errors.cyclades.connection
|
1296 |
def _run(self, ip): |
1297 |
self._optional_output(self.client.delete_floating_ip(ip)) |
1298 |
|
1299 |
def main(self, IP): |
1300 |
super(self.__class__, self)._run() |
1301 |
self._run(ip=IP)
|
1302 |
|
1303 |
|
1304 |
@command(ip_cmds)
|
1305 |
class ip_attach(_init_cyclades, _optional_output_cmd): |
1306 |
"""Attach a floating IP to a server
|
1307 |
"""
|
1308 |
|
1309 |
@errors.generic.all
|
1310 |
@errors.cyclades.connection
|
1311 |
@errors.cyclades.server_id
|
1312 |
def _run(self, server_id, ip): |
1313 |
self._optional_output(self.client.attach_floating_ip(server_id, ip)) |
1314 |
|
1315 |
def main(self, server_id, IP): |
1316 |
super(self.__class__, self)._run() |
1317 |
self._run(server_id=server_id, ip=IP)
|
1318 |
|
1319 |
|
1320 |
@command(ip_cmds)
|
1321 |
class ip_detach(_init_cyclades, _optional_output_cmd): |
1322 |
"""Detach a floating IP from a server
|
1323 |
"""
|
1324 |
|
1325 |
@errors.generic.all
|
1326 |
@errors.cyclades.connection
|
1327 |
@errors.cyclades.server_id
|
1328 |
def _run(self, server_id, ip): |
1329 |
self._optional_output(self.client.detach_floating_ip(server_id, ip)) |
1330 |
|
1331 |
def main(self, server_id, IP): |
1332 |
super(self.__class__, self)._run() |
1333 |
self._run(server_id=server_id, ip=IP)
|