root / kamaki / cli / commands / cyclades.py @ 54b6be76
History | View | Annotate | Download (22.9 kB)
1 | a494a741 | Stavros Sachtouris | # Copyright 2011-2013 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | 7493ccb6 | Stavros Sachtouris | #
|
3 | 7493ccb6 | Stavros Sachtouris | # Redistribution and use in source and binary forms, with or
|
4 | 7493ccb6 | Stavros Sachtouris | # without modification, are permitted provided that the following
|
5 | 7493ccb6 | Stavros Sachtouris | # conditions are met:
|
6 | 7493ccb6 | Stavros Sachtouris | #
|
7 | 7493ccb6 | Stavros Sachtouris | # 1. Redistributions of source code must retain the above
|
8 | 7493ccb6 | Stavros Sachtouris | # copyright notice, this list of conditions and the following
|
9 | 7493ccb6 | Stavros Sachtouris | # disclaimer.
|
10 | 7493ccb6 | Stavros Sachtouris | #
|
11 | 7493ccb6 | Stavros Sachtouris | # 2. Redistributions in binary form must reproduce the above
|
12 | 7493ccb6 | Stavros Sachtouris | # copyright notice, this list of conditions and the following
|
13 | 7493ccb6 | Stavros Sachtouris | # disclaimer in the documentation and/or other materials
|
14 | 7493ccb6 | Stavros Sachtouris | # provided with the distribution.
|
15 | 7493ccb6 | Stavros Sachtouris | #
|
16 | 7493ccb6 | Stavros Sachtouris | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | 7493ccb6 | Stavros Sachtouris | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | 7493ccb6 | Stavros Sachtouris | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | 7493ccb6 | Stavros Sachtouris | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | 7493ccb6 | Stavros Sachtouris | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | 7493ccb6 | Stavros Sachtouris | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | 7493ccb6 | Stavros Sachtouris | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | 7493ccb6 | Stavros Sachtouris | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | 7493ccb6 | Stavros Sachtouris | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | 7493ccb6 | Stavros Sachtouris | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | 7493ccb6 | Stavros Sachtouris | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | 7493ccb6 | Stavros Sachtouris | # POSSIBILITY OF SUCH DAMAGE.
|
28 | 7493ccb6 | Stavros Sachtouris | #
|
29 | 7493ccb6 | Stavros Sachtouris | # The views and conclusions contained in the software and
|
30 | 7493ccb6 | Stavros Sachtouris | # documentation are those of the authors and should not be
|
31 | 7493ccb6 | Stavros Sachtouris | # interpreted as representing official policies, either expressed
|
32 | 7493ccb6 | Stavros Sachtouris | # or implied, of GRNET S.A.
|
33 | 7493ccb6 | Stavros Sachtouris | |
34 | f3e94e06 | Stavros Sachtouris | from kamaki.cli import command |
35 | d486baec | Stavros Sachtouris | from kamaki.cli.command_tree import CommandTree |
36 | 117ca598 | Stavros Sachtouris | from kamaki.cli.utils import print_dict |
37 | 54b6be76 | Stavros Sachtouris | from kamaki.cli.errors import raiseCLIError, CLISyntaxError, CLIBaseUrlError |
38 | 7493ccb6 | Stavros Sachtouris | from kamaki.clients.cyclades import CycladesClient, ClientError |
39 | 57d622b6 | Stavros Sachtouris | from kamaki.cli.argument import FlagArgument, ValueArgument, KeyValueArgument |
40 | bd40efdf | Stavros Sachtouris | from kamaki.cli.argument import ProgressBarArgument, DateArgument, IntArgument |
41 | b04288f7 | Stavros Sachtouris | from kamaki.cli.commands import _command_init, errors |
42 | 545c6c29 | Stavros Sachtouris | from kamaki.cli.commands import _optional_output_cmd, _optional_json |
43 | 5eae854d | Stavros Sachtouris | |
44 | f3e94e06 | Stavros Sachtouris | from base64 import b64encode |
45 | 234954d1 | Stavros Sachtouris | from os.path import exists |
46 | 234954d1 | Stavros Sachtouris | |
47 | 234954d1 | Stavros Sachtouris | |
48 | a29d2f88 | Stavros Sachtouris | server_cmds = CommandTree('server', 'Cyclades/Compute API server commands') |
49 | a29d2f88 | Stavros Sachtouris | flavor_cmds = CommandTree('flavor', 'Cyclades/Compute API flavor commands') |
50 | a29d2f88 | Stavros Sachtouris | network_cmds = CommandTree('network', 'Cyclades/Compute API network commands') |
51 | 8741c407 | Stavros Sachtouris | _commands = [server_cmds, flavor_cmds, network_cmds] |
52 | 234954d1 | Stavros Sachtouris | |
53 | 7493ccb6 | Stavros Sachtouris | |
54 | c314fc10 | Stavros Sachtouris | about_authentication = '\nUser Authentication:\
|
55 | 4018326d | Stavros Sachtouris | \n* to check authentication: /user authenticate\
|
56 | b04288f7 | Stavros Sachtouris | \n* to set authentication token: /config set token <token>'
|
57 | 18edacfe | Stavros Sachtouris | |
58 | d7259dd5 | Stavros Sachtouris | howto_personality = [ |
59 | d7259dd5 | Stavros Sachtouris | 'Defines a file to be injected to VMs personality.',
|
60 | 8194b51b | Stavros Sachtouris | 'Personality value syntax: PATH,[SERVER_PATH,[OWNER,[GROUP,[MODE]]]]',
|
61 | d7259dd5 | Stavros Sachtouris | ' PATH: of local file to be injected',
|
62 | d7259dd5 | Stavros Sachtouris | ' SERVER_PATH: destination location inside server Image',
|
63 | d7259dd5 | Stavros Sachtouris | ' OWNER: user id of destination file owner',
|
64 | d7259dd5 | Stavros Sachtouris | ' GROUP: group id or name to own destination file',
|
65 | d7259dd5 | Stavros Sachtouris | ' MODEL: permition in octal (e.g. 0777 or o+rwx)']
|
66 | d7259dd5 | Stavros Sachtouris | |
67 | 18edacfe | Stavros Sachtouris | |
68 | 5eae854d | Stavros Sachtouris | class _init_cyclades(_command_init): |
69 | b04288f7 | Stavros Sachtouris | @errors.generic.all
|
70 | 236e7d08 | Stavros Sachtouris | def _run(self, service='compute'): |
71 | 234954d1 | Stavros Sachtouris | token = self.config.get(service, 'token')\ |
72 | 234954d1 | Stavros Sachtouris | or self.config.get('global', 'token') |
73 | 54b6be76 | Stavros Sachtouris | |
74 | 54b6be76 | Stavros Sachtouris | if getattr(self, 'auth_base', False): |
75 | 54b6be76 | Stavros Sachtouris | cyclades_endpoints = self.auth_base.get_service_endpoints(
|
76 | 54b6be76 | Stavros Sachtouris | self.config.get('cyclades', 'type'), |
77 | 54b6be76 | Stavros Sachtouris | self.config.get('cyclades', 'version')) |
78 | 54b6be76 | Stavros Sachtouris | base_url = cyclades_endpoints['publicURL']
|
79 | 54b6be76 | Stavros Sachtouris | else:
|
80 | 54b6be76 | Stavros Sachtouris | base_url = self.config.get('cyclades', 'url') |
81 | 54b6be76 | Stavros Sachtouris | if not base_url: |
82 | 54b6be76 | Stavros Sachtouris | raise CLIBaseUrlError(service='cyclades') |
83 | 54b6be76 | Stavros Sachtouris | |
84 | 7493ccb6 | Stavros Sachtouris | self.client = CycladesClient(base_url=base_url, token=token)
|
85 | f47417e7 | Stavros Sachtouris | self._set_log_params()
|
86 | c5b9380c | Stavros Sachtouris | self._update_max_threads()
|
87 | 7493ccb6 | Stavros Sachtouris | |
88 | b04288f7 | Stavros Sachtouris | def main(self): |
89 | b04288f7 | Stavros Sachtouris | self._run()
|
90 | 236e7d08 | Stavros Sachtouris | |
91 | 234954d1 | Stavros Sachtouris | |
92 | d486baec | Stavros Sachtouris | @command(server_cmds)
|
93 | 545c6c29 | Stavros Sachtouris | class server_list(_init_cyclades, _optional_json): |
94 | c314fc10 | Stavros Sachtouris | """List Virtual Machines accessible by user"""
|
95 | 18edacfe | Stavros Sachtouris | |
96 | 18edacfe | Stavros Sachtouris | __doc__ += about_authentication |
97 | 7493ccb6 | Stavros Sachtouris | |
98 | e15d78e2 | Stavros Sachtouris | arguments = dict(
|
99 | f40f0cb7 | Stavros Sachtouris | detail=FlagArgument('show detailed output', ('-l', '--details')), |
100 | 67469d65 | Stavros Sachtouris | since=DateArgument( |
101 | a382ea56 | Stavros Sachtouris | 'show only items since date (\' d/m/Y H:M:S \')',
|
102 | a4d0d88a | Stavros Sachtouris | '--since'),
|
103 | f40f0cb7 | Stavros Sachtouris | limit=IntArgument('limit number of listed VMs', ('-n', '--number')), |
104 | a4d0d88a | Stavros Sachtouris | more=FlagArgument( |
105 | ddc0b290 | Stavros Sachtouris | 'output results in pages (-n to set items per page, default 10)',
|
106 | ed9af02c | Stavros Sachtouris | '--more'),
|
107 | ed9af02c | Stavros Sachtouris | enum=FlagArgument('Enumerate results', '--enumerate') |
108 | e15d78e2 | Stavros Sachtouris | ) |
109 | e15d78e2 | Stavros Sachtouris | |
110 | b04288f7 | Stavros Sachtouris | @errors.generic.all
|
111 | b04288f7 | Stavros Sachtouris | @errors.cyclades.connection
|
112 | b04288f7 | Stavros Sachtouris | @errors.cyclades.date
|
113 | b04288f7 | Stavros Sachtouris | def _run(self): |
114 | b04288f7 | Stavros Sachtouris | servers = self.client.list_servers(self['detail'], self['since']) |
115 | 545c6c29 | Stavros Sachtouris | |
116 | 545c6c29 | Stavros Sachtouris | kwargs = dict(with_enumeration=self['enum']) |
117 | a4d0d88a | Stavros Sachtouris | if self['more']: |
118 | 545c6c29 | Stavros Sachtouris | kwargs['page_size'] = self['limit'] if self['limit'] else 10 |
119 | 545c6c29 | Stavros Sachtouris | elif self['limit']: |
120 | 545c6c29 | Stavros Sachtouris | servers = servers[:self['limit']] |
121 | 545c6c29 | Stavros Sachtouris | self._print(servers, **kwargs)
|
122 | 7493ccb6 | Stavros Sachtouris | |
123 | b04288f7 | Stavros Sachtouris | def main(self): |
124 | b04288f7 | Stavros Sachtouris | super(self.__class__, self)._run() |
125 | b04288f7 | Stavros Sachtouris | self._run()
|
126 | b04288f7 | Stavros Sachtouris | |
127 | 234954d1 | Stavros Sachtouris | |
128 | d486baec | Stavros Sachtouris | @command(server_cmds)
|
129 | 545c6c29 | Stavros Sachtouris | class server_info(_init_cyclades, _optional_json): |
130 | 67469d65 | Stavros Sachtouris | """Detailed information on a Virtual Machine
|
131 | 67469d65 | Stavros Sachtouris | Contains:
|
132 | 67469d65 | Stavros Sachtouris | - name, id, status, create/update dates
|
133 | 67469d65 | Stavros Sachtouris | - network interfaces
|
134 | 67469d65 | Stavros Sachtouris | - metadata (e.g. os, superuser) and diagnostics
|
135 | 67469d65 | Stavros Sachtouris | - hardware flavor and os image ids
|
136 | 67469d65 | Stavros Sachtouris | """
|
137 | 7493ccb6 | Stavros Sachtouris | |
138 | b04288f7 | Stavros Sachtouris | @errors.generic.all
|
139 | b04288f7 | Stavros Sachtouris | @errors.cyclades.connection
|
140 | 5a673575 | Stavros Sachtouris | @errors.cyclades.server_id
|
141 | b04288f7 | Stavros Sachtouris | def _run(self, server_id): |
142 | bcef3ac9 | Stavros Sachtouris | self._print(self.client.get_server_details(server_id), print_dict) |
143 | 7493ccb6 | Stavros Sachtouris | |
144 | b04288f7 | Stavros Sachtouris | def main(self, server_id): |
145 | b04288f7 | Stavros Sachtouris | super(self.__class__, self)._run() |
146 | b04288f7 | Stavros Sachtouris | self._run(server_id=server_id)
|
147 | b04288f7 | Stavros Sachtouris | |
148 | 234954d1 | Stavros Sachtouris | |
149 | 57d622b6 | Stavros Sachtouris | class PersonalityArgument(KeyValueArgument): |
150 | 234954d1 | Stavros Sachtouris | @property
|
151 | f3e94e06 | Stavros Sachtouris | def value(self): |
152 | 57d622b6 | Stavros Sachtouris | return self._value if hasattr(self, '_value') else [] |
153 | 234954d1 | Stavros Sachtouris | |
154 | 234954d1 | Stavros Sachtouris | @value.setter
|
155 | f3e94e06 | Stavros Sachtouris | def value(self, newvalue): |
156 | f3e94e06 | Stavros Sachtouris | if newvalue == self.default: |
157 | f3e94e06 | Stavros Sachtouris | return self.value |
158 | 57d622b6 | Stavros Sachtouris | self._value = []
|
159 | 57d622b6 | Stavros Sachtouris | for i, terms in enumerate(newvalue): |
160 | 57d622b6 | Stavros Sachtouris | termlist = terms.split(',')
|
161 | 57d622b6 | Stavros Sachtouris | if len(termlist) > 5: |
162 | de73876b | Stavros Sachtouris | msg = 'Wrong number of terms (should be 1 to 5)'
|
163 | de73876b | Stavros Sachtouris | raiseCLIError(CLISyntaxError(msg), details=howto_personality) |
164 | 57d622b6 | Stavros Sachtouris | path = termlist[0]
|
165 | 57d622b6 | Stavros Sachtouris | if not exists(path): |
166 | de73876b | Stavros Sachtouris | raiseCLIError( |
167 | de73876b | Stavros Sachtouris | None,
|
168 | 8194b51b | Stavros Sachtouris | '--personality: File %s does not exist' % path,
|
169 | 8194b51b | Stavros Sachtouris | importance=1,
|
170 | 8194b51b | Stavros Sachtouris | details=howto_personality) |
171 | 57d622b6 | Stavros Sachtouris | self._value.append(dict(path=path)) |
172 | 57d622b6 | Stavros Sachtouris | with open(path) as f: |
173 | 57d622b6 | Stavros Sachtouris | self._value[i]['contents'] = b64encode(f.read()) |
174 | 57d622b6 | Stavros Sachtouris | try:
|
175 | 57d622b6 | Stavros Sachtouris | self._value[i]['path'] = termlist[1] |
176 | 57d622b6 | Stavros Sachtouris | self._value[i]['owner'] = termlist[2] |
177 | 57d622b6 | Stavros Sachtouris | self._value[i]['group'] = termlist[3] |
178 | 57d622b6 | Stavros Sachtouris | self._value[i]['mode'] = termlist[4] |
179 | 57d622b6 | Stavros Sachtouris | except IndexError: |
180 | 57d622b6 | Stavros Sachtouris | pass
|
181 | f3e94e06 | Stavros Sachtouris | |
182 | 234954d1 | Stavros Sachtouris | |
183 | d486baec | Stavros Sachtouris | @command(server_cmds)
|
184 | 545c6c29 | Stavros Sachtouris | class server_create(_init_cyclades, _optional_json): |
185 | 8194b51b | Stavros Sachtouris | """Create a server (aka Virtual Machine)
|
186 | 8194b51b | Stavros Sachtouris | Parameters:
|
187 | 439826ec | Stavros Sachtouris | - name: (single quoted text)
|
188 | 439826ec | Stavros Sachtouris | - flavor id: Hardware flavor. Pick one from: /flavor list
|
189 | 439826ec | Stavros Sachtouris | - image id: OS images. Pick one from: /image list
|
190 | 8194b51b | Stavros Sachtouris | """
|
191 | 7493ccb6 | Stavros Sachtouris | |
192 | 93914390 | Stavros Sachtouris | arguments = dict(
|
193 | 93914390 | Stavros Sachtouris | personality=PersonalityArgument( |
194 | 545c6c29 | Stavros Sachtouris | (80 * ' ').join(howto_personality), ('-p', '--personality')) |
195 | 93914390 | Stavros Sachtouris | ) |
196 | f3e94e06 | Stavros Sachtouris | |
197 | b04288f7 | Stavros Sachtouris | @errors.generic.all
|
198 | b04288f7 | Stavros Sachtouris | @errors.cyclades.connection
|
199 | b04288f7 | Stavros Sachtouris | @errors.plankton.id
|
200 | b04288f7 | Stavros Sachtouris | @errors.cyclades.flavor_id
|
201 | b04288f7 | Stavros Sachtouris | def _run(self, name, flavor_id, image_id): |
202 | bcef3ac9 | Stavros Sachtouris | self._print(
|
203 | bcef3ac9 | Stavros Sachtouris | self.client.create_server(
|
204 | bcef3ac9 | Stavros Sachtouris | name, int(flavor_id), image_id, self['personality']), |
205 | bcef3ac9 | Stavros Sachtouris | print_dict) |
206 | b04288f7 | Stavros Sachtouris | |
207 | 7493ccb6 | Stavros Sachtouris | def main(self, name, flavor_id, image_id): |
208 | b04288f7 | Stavros Sachtouris | super(self.__class__, self)._run() |
209 | b04288f7 | Stavros Sachtouris | self._run(name=name, flavor_id=flavor_id, image_id=image_id)
|
210 | 7493ccb6 | Stavros Sachtouris | |
211 | 234954d1 | Stavros Sachtouris | |
212 | d486baec | Stavros Sachtouris | @command(server_cmds)
|
213 | cd295a1d | Stavros Sachtouris | class server_rename(_init_cyclades, _optional_output_cmd): |
214 | 22b6f063 | Stavros Sachtouris | """Set/update a server (VM) name
|
215 | 5a673575 | Stavros Sachtouris | VM names are not unique, therefore multiple servers may share the same name
|
216 | 22b6f063 | Stavros Sachtouris | """
|
217 | 7493ccb6 | Stavros Sachtouris | |
218 | 5a673575 | Stavros Sachtouris | @errors.generic.all
|
219 | 5a673575 | Stavros Sachtouris | @errors.cyclades.connection
|
220 | 5a673575 | Stavros Sachtouris | @errors.cyclades.server_id
|
221 | 5a673575 | Stavros Sachtouris | def _run(self, server_id, new_name): |
222 | cd295a1d | Stavros Sachtouris | self._optional_output(
|
223 | cd295a1d | Stavros Sachtouris | self.client.update_server_name(int(server_id), new_name)) |
224 | 5a673575 | Stavros Sachtouris | |
225 | 7493ccb6 | Stavros Sachtouris | def main(self, server_id, new_name): |
226 | 5a673575 | Stavros Sachtouris | super(self.__class__, self)._run() |
227 | 5a673575 | Stavros Sachtouris | self._run(server_id=server_id, new_name=new_name)
|
228 | 234954d1 | Stavros Sachtouris | |
229 | 7493ccb6 | Stavros Sachtouris | |
230 | d486baec | Stavros Sachtouris | @command(server_cmds)
|
231 | cd295a1d | Stavros Sachtouris | class server_delete(_init_cyclades, _optional_output_cmd): |
232 | 22b6f063 | Stavros Sachtouris | """Delete a server (VM)"""
|
233 | 7493ccb6 | Stavros Sachtouris | |
234 | 5a673575 | Stavros Sachtouris | @errors.generic.all
|
235 | 5a673575 | Stavros Sachtouris | @errors.cyclades.connection
|
236 | 5a673575 | Stavros Sachtouris | @errors.cyclades.server_id
|
237 | 5a673575 | Stavros Sachtouris | def _run(self, server_id): |
238 | cd295a1d | Stavros Sachtouris | self._optional_output(self.client.delete_server(int(server_id))) |
239 | 5a673575 | Stavros Sachtouris | |
240 | 5a673575 | Stavros Sachtouris | def main(self, server_id): |
241 | 5a673575 | Stavros Sachtouris | super(self.__class__, self)._run() |
242 | 5a673575 | Stavros Sachtouris | self._run(server_id=server_id)
|
243 | 234954d1 | Stavros Sachtouris | |
244 | 7493ccb6 | Stavros Sachtouris | |
245 | d486baec | Stavros Sachtouris | @command(server_cmds)
|
246 | cd295a1d | Stavros Sachtouris | class server_reboot(_init_cyclades, _optional_output_cmd): |
247 | 22b6f063 | Stavros Sachtouris | """Reboot a server (VM)"""
|
248 | 18edacfe | Stavros Sachtouris | |
249 | 93914390 | Stavros Sachtouris | arguments = dict(
|
250 | f40f0cb7 | Stavros Sachtouris | hard=FlagArgument('perform a hard reboot', ('-f', '--force')) |
251 | 93914390 | Stavros Sachtouris | ) |
252 | 7493ccb6 | Stavros Sachtouris | |
253 | 5a673575 | Stavros Sachtouris | @errors.generic.all
|
254 | 5a673575 | Stavros Sachtouris | @errors.cyclades.connection
|
255 | 5a673575 | Stavros Sachtouris | @errors.cyclades.server_id
|
256 | 5a673575 | Stavros Sachtouris | def _run(self, server_id): |
257 | cd295a1d | Stavros Sachtouris | self._optional_output(
|
258 | cd295a1d | Stavros Sachtouris | self.client.reboot_server(int(server_id), self['hard'])) |
259 | 5a673575 | Stavros Sachtouris | |
260 | 7493ccb6 | Stavros Sachtouris | def main(self, server_id): |
261 | 5a673575 | Stavros Sachtouris | super(self.__class__, self)._run() |
262 | 5a673575 | Stavros Sachtouris | self._run(server_id=server_id)
|
263 | 234954d1 | Stavros Sachtouris | |
264 | 7493ccb6 | Stavros Sachtouris | |
265 | d486baec | Stavros Sachtouris | @command(server_cmds)
|
266 | cd295a1d | Stavros Sachtouris | class server_start(_init_cyclades, _optional_output_cmd): |
267 | 22b6f063 | Stavros Sachtouris | """Start an existing server (VM)"""
|
268 | 7493ccb6 | Stavros Sachtouris | |
269 | 5a673575 | Stavros Sachtouris | @errors.generic.all
|
270 | 5a673575 | Stavros Sachtouris | @errors.cyclades.connection
|
271 | 5a673575 | Stavros Sachtouris | @errors.cyclades.server_id
|
272 | 5a673575 | Stavros Sachtouris | def _run(self, server_id): |
273 | cd295a1d | Stavros Sachtouris | self._optional_output(self.client.start_server(int(server_id))) |
274 | 5a673575 | Stavros Sachtouris | |
275 | 7493ccb6 | Stavros Sachtouris | def main(self, server_id): |
276 | 5a673575 | Stavros Sachtouris | super(self.__class__, self)._run() |
277 | 5a673575 | Stavros Sachtouris | self._run(server_id=server_id)
|
278 | 234954d1 | Stavros Sachtouris | |
279 | 7493ccb6 | Stavros Sachtouris | |
280 | d486baec | Stavros Sachtouris | @command(server_cmds)
|
281 | cd295a1d | Stavros Sachtouris | class server_shutdown(_init_cyclades, _optional_output_cmd): |
282 | 22b6f063 | Stavros Sachtouris | """Shutdown an active server (VM)"""
|
283 | 7493ccb6 | Stavros Sachtouris | |
284 | 5a673575 | Stavros Sachtouris | @errors.generic.all
|
285 | 5a673575 | Stavros Sachtouris | @errors.cyclades.connection
|
286 | 5a673575 | Stavros Sachtouris | @errors.cyclades.server_id
|
287 | 5a673575 | Stavros Sachtouris | def _run(self, server_id): |
288 | cd295a1d | Stavros Sachtouris | self._optional_output(self.client.shutdown_server(int(server_id))) |
289 | 5a673575 | Stavros Sachtouris | |
290 | 7493ccb6 | Stavros Sachtouris | def main(self, server_id): |
291 | 5a673575 | Stavros Sachtouris | super(self.__class__, self)._run() |
292 | 5a673575 | Stavros Sachtouris | self._run(server_id=server_id)
|
293 | 234954d1 | Stavros Sachtouris | |
294 | 7493ccb6 | Stavros Sachtouris | |
295 | d486baec | Stavros Sachtouris | @command(server_cmds)
|
296 | 545c6c29 | Stavros Sachtouris | class server_console(_init_cyclades, _optional_json): |
297 | 22b6f063 | Stavros Sachtouris | """Get a VNC console to access an existing server (VM)
|
298 | 22b6f063 | Stavros Sachtouris | Console connection information provided (at least):
|
299 | 439826ec | Stavros Sachtouris | - host: (url or address) a VNC host
|
300 | 439826ec | Stavros Sachtouris | - port: (int) the gateway to enter VM on host
|
301 | 439826ec | Stavros Sachtouris | - password: for VNC authorization
|
302 | 22b6f063 | Stavros Sachtouris | """
|
303 | 7493ccb6 | Stavros Sachtouris | |
304 | 5a673575 | Stavros Sachtouris | @errors.generic.all
|
305 | 5a673575 | Stavros Sachtouris | @errors.cyclades.connection
|
306 | 5a673575 | Stavros Sachtouris | @errors.cyclades.server_id
|
307 | 5a673575 | Stavros Sachtouris | def _run(self, server_id): |
308 | bcef3ac9 | Stavros Sachtouris | self._print(
|
309 | bcef3ac9 | Stavros Sachtouris | self.client.get_server_console(int(server_id)), print_dict) |
310 | 5a673575 | Stavros Sachtouris | |
311 | 7493ccb6 | Stavros Sachtouris | def main(self, server_id): |
312 | 5a673575 | Stavros Sachtouris | super(self.__class__, self)._run() |
313 | 5a673575 | Stavros Sachtouris | self._run(server_id=server_id)
|
314 | 7493ccb6 | Stavros Sachtouris | |
315 | 234954d1 | Stavros Sachtouris | |
316 | d486baec | Stavros Sachtouris | @command(server_cmds)
|
317 | 7493ccb6 | Stavros Sachtouris | class server_firewall(_init_cyclades): |
318 | cd295a1d | Stavros Sachtouris | """Manage server (VM) firewall profiles for public networks"""
|
319 | cd295a1d | Stavros Sachtouris | |
320 | cd295a1d | Stavros Sachtouris | |
321 | cd295a1d | Stavros Sachtouris | @command(server_cmds)
|
322 | cd295a1d | Stavros Sachtouris | class server_firewall_set(_init_cyclades, _optional_output_cmd): |
323 | 22b6f063 | Stavros Sachtouris | """Set the server (VM) firewall profile on VMs public network
|
324 | 22b6f063 | Stavros Sachtouris | Values for profile:
|
325 | 439826ec | Stavros Sachtouris | - DISABLED: Shutdown firewall
|
326 | 439826ec | Stavros Sachtouris | - ENABLED: Firewall in normal mode
|
327 | 439826ec | Stavros Sachtouris | - PROTECTED: Firewall in secure mode
|
328 | 22b6f063 | Stavros Sachtouris | """
|
329 | 7493ccb6 | Stavros Sachtouris | |
330 | 5a673575 | Stavros Sachtouris | @errors.generic.all
|
331 | 5a673575 | Stavros Sachtouris | @errors.cyclades.connection
|
332 | 5a673575 | Stavros Sachtouris | @errors.cyclades.server_id
|
333 | 5a673575 | Stavros Sachtouris | @errors.cyclades.firewall
|
334 | 5a673575 | Stavros Sachtouris | def _run(self, server_id, profile): |
335 | cd295a1d | Stavros Sachtouris | self._optional_output(self.client.set_firewall_profile( |
336 | cd295a1d | Stavros Sachtouris | server_id=int(server_id), profile=('%s' % profile).upper())) |
337 | 5a673575 | Stavros Sachtouris | |
338 | 7493ccb6 | Stavros Sachtouris | def main(self, server_id, profile): |
339 | 5a673575 | Stavros Sachtouris | super(self.__class__, self)._run() |
340 | 5a673575 | Stavros Sachtouris | self._run(server_id=server_id, profile=profile)
|
341 | 234954d1 | Stavros Sachtouris | |
342 | f3e94e06 | Stavros Sachtouris | |
343 | d486baec | Stavros Sachtouris | @command(server_cmds)
|
344 | cd295a1d | Stavros Sachtouris | class server_firewall_get(_init_cyclades): |
345 | cd295a1d | Stavros Sachtouris | """Get the server (VM) firewall profile for its public network"""
|
346 | cd295a1d | Stavros Sachtouris | |
347 | cd295a1d | Stavros Sachtouris | @errors.generic.all
|
348 | cd295a1d | Stavros Sachtouris | @errors.cyclades.connection
|
349 | cd295a1d | Stavros Sachtouris | @errors.cyclades.server_id
|
350 | cd295a1d | Stavros Sachtouris | def _run(self, server_id): |
351 | cd295a1d | Stavros Sachtouris | print(self.client.get_firewall_profile(server_id))
|
352 | cd295a1d | Stavros Sachtouris | |
353 | cd295a1d | Stavros Sachtouris | def main(self, server_id): |
354 | cd295a1d | Stavros Sachtouris | super(self.__class__, self)._run() |
355 | cd295a1d | Stavros Sachtouris | self._run(server_id=server_id)
|
356 | cd295a1d | Stavros Sachtouris | |
357 | cd295a1d | Stavros Sachtouris | |
358 | cd295a1d | Stavros Sachtouris | @command(server_cmds)
|
359 | 545c6c29 | Stavros Sachtouris | class server_addr(_init_cyclades, _optional_json): |
360 | 22b6f063 | Stavros Sachtouris | """List the addresses of all network interfaces on a server (VM)"""
|
361 | 7493ccb6 | Stavros Sachtouris | |
362 | cd295a1d | Stavros Sachtouris | arguments = dict(
|
363 | 545c6c29 | Stavros Sachtouris | enum=FlagArgument('Enumerate results', '--enumerate') |
364 | cd295a1d | Stavros Sachtouris | ) |
365 | cd295a1d | Stavros Sachtouris | |
366 | 5a673575 | Stavros Sachtouris | @errors.generic.all
|
367 | 5a673575 | Stavros Sachtouris | @errors.cyclades.connection
|
368 | 5a673575 | Stavros Sachtouris | @errors.cyclades.server_id
|
369 | 5a673575 | Stavros Sachtouris | def _run(self, server_id): |
370 | 5a673575 | Stavros Sachtouris | reply = self.client.list_server_nics(int(server_id)) |
371 | 545c6c29 | Stavros Sachtouris | self._print(
|
372 | 545c6c29 | Stavros Sachtouris | reply, with_enumeration=self['enum'] and len(reply) > 1) |
373 | 7493ccb6 | Stavros Sachtouris | |
374 | 5a673575 | Stavros Sachtouris | def main(self, server_id): |
375 | 5a673575 | Stavros Sachtouris | super(self.__class__, self)._run() |
376 | 5a673575 | Stavros Sachtouris | self._run(server_id=server_id)
|
377 | 5a673575 | Stavros Sachtouris | |
378 | 234954d1 | Stavros Sachtouris | |
379 | d486baec | Stavros Sachtouris | @command(server_cmds)
|
380 | cd295a1d | Stavros Sachtouris | class server_metadata(_init_cyclades): |
381 | cd295a1d | Stavros Sachtouris | """Manage Server metadata (key:value pairs of server attributes)"""
|
382 | cd295a1d | Stavros Sachtouris | |
383 | cd295a1d | Stavros Sachtouris | |
384 | cd295a1d | Stavros Sachtouris | @command(server_cmds)
|
385 | 545c6c29 | Stavros Sachtouris | class server_metadata_list(_init_cyclades, _optional_json): |
386 | cd295a1d | Stavros Sachtouris | """Get server metadata"""
|
387 | 7493ccb6 | Stavros Sachtouris | |
388 | 5a673575 | Stavros Sachtouris | @errors.generic.all
|
389 | 5a673575 | Stavros Sachtouris | @errors.cyclades.connection
|
390 | 5a673575 | Stavros Sachtouris | @errors.cyclades.server_id
|
391 | 5a673575 | Stavros Sachtouris | @errors.cyclades.metadata
|
392 | 5a673575 | Stavros Sachtouris | def _run(self, server_id, key=''): |
393 | 545c6c29 | Stavros Sachtouris | self._print(
|
394 | bcef3ac9 | Stavros Sachtouris | self.client.get_server_metadata(int(server_id), key), print_dict) |
395 | 5a673575 | Stavros Sachtouris | |
396 | a34888b4 | Stavros Sachtouris | def main(self, server_id, key=''): |
397 | 5a673575 | Stavros Sachtouris | super(self.__class__, self)._run() |
398 | 5a673575 | Stavros Sachtouris | self._run(server_id=server_id, key=key)
|
399 | 7493ccb6 | Stavros Sachtouris | |
400 | 234954d1 | Stavros Sachtouris | |
401 | d486baec | Stavros Sachtouris | @command(server_cmds)
|
402 | 545c6c29 | Stavros Sachtouris | class server_metadata_set(_init_cyclades, _optional_json): |
403 | cd295a1d | Stavros Sachtouris | """Set / update server(VM) metadata
|
404 | cd295a1d | Stavros Sachtouris | Metadata should be given in key/value pairs in key=value format
|
405 | cd295a1d | Stavros Sachtouris | For example:
|
406 | cd295a1d | Stavros Sachtouris | /server metadata set <server id> key1=value1 key2=value2
|
407 | cd295a1d | Stavros Sachtouris | Old, unreferenced metadata will remain intact
|
408 | 22b6f063 | Stavros Sachtouris | """
|
409 | 7493ccb6 | Stavros Sachtouris | |
410 | 5a673575 | Stavros Sachtouris | @errors.generic.all
|
411 | 5a673575 | Stavros Sachtouris | @errors.cyclades.connection
|
412 | 5a673575 | Stavros Sachtouris | @errors.cyclades.server_id
|
413 | cd295a1d | Stavros Sachtouris | def _run(self, server_id, keyvals): |
414 | 545c6c29 | Stavros Sachtouris | assert keyvals, 'Please, add some metadata ( key=value)' |
415 | cd295a1d | Stavros Sachtouris | metadata = dict()
|
416 | cd295a1d | Stavros Sachtouris | for keyval in keyvals: |
417 | cd295a1d | Stavros Sachtouris | k, sep, v = keyval.partition('=')
|
418 | cd295a1d | Stavros Sachtouris | if sep and k: |
419 | cd295a1d | Stavros Sachtouris | metadata[k] = v |
420 | cd295a1d | Stavros Sachtouris | else:
|
421 | cd295a1d | Stavros Sachtouris | raiseCLIError( |
422 | cd295a1d | Stavros Sachtouris | 'Invalid piece of metadata %s' % keyval,
|
423 | cd295a1d | Stavros Sachtouris | importance=2, details=[
|
424 | cd295a1d | Stavros Sachtouris | 'Correct metadata format: key=val',
|
425 | cd295a1d | Stavros Sachtouris | 'For example:',
|
426 | cd295a1d | Stavros Sachtouris | '/server metadata set <server id>'
|
427 | cd295a1d | Stavros Sachtouris | 'key1=value1 key2=value2'])
|
428 | 545c6c29 | Stavros Sachtouris | self._print(
|
429 | bcef3ac9 | Stavros Sachtouris | self.client.update_server_metadata(int(server_id), **metadata), |
430 | bcef3ac9 | Stavros Sachtouris | print_dict) |
431 | cd295a1d | Stavros Sachtouris | |
432 | cd295a1d | Stavros Sachtouris | def main(self, server_id, *key_equals_val): |
433 | 5a673575 | Stavros Sachtouris | super(self.__class__, self)._run() |
434 | cd295a1d | Stavros Sachtouris | self._run(server_id=server_id, keyvals=key_equals_val)
|
435 | 7493ccb6 | Stavros Sachtouris | |
436 | 234954d1 | Stavros Sachtouris | |
437 | d486baec | Stavros Sachtouris | @command(server_cmds)
|
438 | cd295a1d | Stavros Sachtouris | class server_metadata_delete(_init_cyclades, _optional_output_cmd): |
439 | 22b6f063 | Stavros Sachtouris | """Delete server (VM) metadata"""
|
440 | 7493ccb6 | Stavros Sachtouris | |
441 | 5a673575 | Stavros Sachtouris | @errors.generic.all
|
442 | 5a673575 | Stavros Sachtouris | @errors.cyclades.connection
|
443 | 5a673575 | Stavros Sachtouris | @errors.cyclades.server_id
|
444 | 5a673575 | Stavros Sachtouris | @errors.cyclades.metadata
|
445 | 5a673575 | Stavros Sachtouris | def _run(self, server_id, key): |
446 | cd295a1d | Stavros Sachtouris | self._optional_output(
|
447 | cd295a1d | Stavros Sachtouris | self.client.delete_server_metadata(int(server_id), key)) |
448 | 5a673575 | Stavros Sachtouris | |
449 | 7493ccb6 | Stavros Sachtouris | def main(self, server_id, key): |
450 | 5a673575 | Stavros Sachtouris | super(self.__class__, self)._run() |
451 | 5a673575 | Stavros Sachtouris | self._run(server_id=server_id, key=key)
|
452 | 234954d1 | Stavros Sachtouris | |
453 | 7493ccb6 | Stavros Sachtouris | |
454 | d486baec | Stavros Sachtouris | @command(server_cmds)
|
455 | 545c6c29 | Stavros Sachtouris | class server_stats(_init_cyclades, _optional_json): |
456 | 22b6f063 | Stavros Sachtouris | """Get server (VM) statistics"""
|
457 | 7493ccb6 | Stavros Sachtouris | |
458 | 5a673575 | Stavros Sachtouris | @errors.generic.all
|
459 | 5a673575 | Stavros Sachtouris | @errors.cyclades.connection
|
460 | 5a673575 | Stavros Sachtouris | @errors.cyclades.server_id
|
461 | 5a673575 | Stavros Sachtouris | def _run(self, server_id): |
462 | bcef3ac9 | Stavros Sachtouris | self._print(self.client.get_server_stats(int(server_id)), print_dict) |
463 | 5a673575 | Stavros Sachtouris | |
464 | 7493ccb6 | Stavros Sachtouris | def main(self, server_id): |
465 | 5a673575 | Stavros Sachtouris | super(self.__class__, self)._run() |
466 | 5a673575 | Stavros Sachtouris | self._run(server_id=server_id)
|
467 | 7493ccb6 | Stavros Sachtouris | |
468 | 234954d1 | Stavros Sachtouris | |
469 | d486baec | Stavros Sachtouris | @command(server_cmds)
|
470 | fd1f1d96 | Stavros Sachtouris | class server_wait(_init_cyclades): |
471 | fd1f1d96 | Stavros Sachtouris | """Wait for server to finish [BUILD, STOPPED, REBOOT, ACTIVE]"""
|
472 | fd1f1d96 | Stavros Sachtouris | |
473 | 93914390 | Stavros Sachtouris | arguments = dict(
|
474 | 93914390 | Stavros Sachtouris | progress_bar=ProgressBarArgument( |
475 | 93914390 | Stavros Sachtouris | 'do not show progress bar',
|
476 | f40f0cb7 | Stavros Sachtouris | ('-N', '--no-progress-bar'), |
477 | 93914390 | Stavros Sachtouris | False
|
478 | 93914390 | Stavros Sachtouris | ) |
479 | 93914390 | Stavros Sachtouris | ) |
480 | fd1f1d96 | Stavros Sachtouris | |
481 | 5a673575 | Stavros Sachtouris | @errors.generic.all
|
482 | 5a673575 | Stavros Sachtouris | @errors.cyclades.connection
|
483 | 5a673575 | Stavros Sachtouris | @errors.cyclades.server_id
|
484 | 5a673575 | Stavros Sachtouris | def _run(self, server_id, currect_status): |
485 | 5a673575 | Stavros Sachtouris | (progress_bar, wait_cb) = self._safe_progress_bar(
|
486 | 5a673575 | Stavros Sachtouris | 'Server %s still in %s mode' % (server_id, currect_status))
|
487 | 5a673575 | Stavros Sachtouris | |
488 | 6cdfdcf3 | Stavros Sachtouris | try:
|
489 | 5a673575 | Stavros Sachtouris | new_mode = self.client.wait_server(
|
490 | 5a673575 | Stavros Sachtouris | server_id, |
491 | 6cdfdcf3 | Stavros Sachtouris | currect_status, |
492 | 6cdfdcf3 | Stavros Sachtouris | wait_cb=wait_cb) |
493 | 5a673575 | Stavros Sachtouris | except Exception: |
494 | 5a673575 | Stavros Sachtouris | self._safe_progress_bar_finish(progress_bar)
|
495 | 5a673575 | Stavros Sachtouris | raise
|
496 | 5a673575 | Stavros Sachtouris | finally:
|
497 | 5a673575 | Stavros Sachtouris | self._safe_progress_bar_finish(progress_bar)
|
498 | fd1f1d96 | Stavros Sachtouris | if new_mode:
|
499 | a10f5561 | Stavros Sachtouris | print('Server %s is now in %s mode' % (server_id, new_mode))
|
500 | fd1f1d96 | Stavros Sachtouris | else:
|
501 | 43ee6ae1 | Stavros Sachtouris | raiseCLIError(None, 'Time out') |
502 | fd1f1d96 | Stavros Sachtouris | |
503 | 5a673575 | Stavros Sachtouris | def main(self, server_id, currect_status='BUILD'): |
504 | 5a673575 | Stavros Sachtouris | super(self.__class__, self)._run() |
505 | 5a673575 | Stavros Sachtouris | self._run(server_id=server_id, currect_status=currect_status)
|
506 | 5a673575 | Stavros Sachtouris | |
507 | fd1f1d96 | Stavros Sachtouris | |
508 | d486baec | Stavros Sachtouris | @command(flavor_cmds)
|
509 | 545c6c29 | Stavros Sachtouris | class flavor_list(_init_cyclades, _optional_json): |
510 | bd40efdf | Stavros Sachtouris | """List available hardware flavors"""
|
511 | 7493ccb6 | Stavros Sachtouris | |
512 | 93914390 | Stavros Sachtouris | arguments = dict(
|
513 | f40f0cb7 | Stavros Sachtouris | detail=FlagArgument('show detailed output', ('-l', '--details')), |
514 | f40f0cb7 | Stavros Sachtouris | limit=IntArgument('limit # of listed flavors', ('-n', '--number')), |
515 | bd40efdf | Stavros Sachtouris | more=FlagArgument( |
516 | de73876b | Stavros Sachtouris | 'output results in pages (-n to set items per page, default 10)',
|
517 | ed9af02c | Stavros Sachtouris | '--more'),
|
518 | ed9af02c | Stavros Sachtouris | enum=FlagArgument('Enumerate results', '--enumerate') |
519 | 93914390 | Stavros Sachtouris | ) |
520 | 7493ccb6 | Stavros Sachtouris | |
521 | 5a673575 | Stavros Sachtouris | @errors.generic.all
|
522 | 5a673575 | Stavros Sachtouris | @errors.cyclades.connection
|
523 | 5a673575 | Stavros Sachtouris | def _run(self): |
524 | 5a673575 | Stavros Sachtouris | flavors = self.client.list_flavors(self['detail']) |
525 | 5a673575 | Stavros Sachtouris | pg_size = 10 if self['more'] and not self['limit'] else self['limit'] |
526 | 545c6c29 | Stavros Sachtouris | self._print(
|
527 | ed9af02c | Stavros Sachtouris | flavors, |
528 | ed9af02c | Stavros Sachtouris | with_redundancy=self['detail'], |
529 | ed9af02c | Stavros Sachtouris | page_size=pg_size, |
530 | ed9af02c | Stavros Sachtouris | with_enumeration=self['enum']) |
531 | 5a673575 | Stavros Sachtouris | |
532 | 7493ccb6 | Stavros Sachtouris | def main(self): |
533 | 5a673575 | Stavros Sachtouris | super(self.__class__, self)._run() |
534 | 5a673575 | Stavros Sachtouris | self._run()
|
535 | 7493ccb6 | Stavros Sachtouris | |
536 | 234954d1 | Stavros Sachtouris | |
537 | d486baec | Stavros Sachtouris | @command(flavor_cmds)
|
538 | 545c6c29 | Stavros Sachtouris | class flavor_info(_init_cyclades, _optional_json): |
539 | 769dbf53 | Stavros Sachtouris | """Detailed information on a hardware flavor
|
540 | ddc0b290 | Stavros Sachtouris | To get a list of available flavors and flavor ids, try /flavor list
|
541 | ddc0b290 | Stavros Sachtouris | """
|
542 | 7493ccb6 | Stavros Sachtouris | |
543 | 5a673575 | Stavros Sachtouris | @errors.generic.all
|
544 | 5a673575 | Stavros Sachtouris | @errors.cyclades.connection
|
545 | 5a673575 | Stavros Sachtouris | @errors.cyclades.flavor_id
|
546 | 5a673575 | Stavros Sachtouris | def _run(self, flavor_id): |
547 | bcef3ac9 | Stavros Sachtouris | self._print(
|
548 | bcef3ac9 | Stavros Sachtouris | self.client.get_flavor_details(int(flavor_id)), print_dict) |
549 | 7493ccb6 | Stavros Sachtouris | |
550 | 5a673575 | Stavros Sachtouris | def main(self, flavor_id): |
551 | 5a673575 | Stavros Sachtouris | super(self.__class__, self)._run() |
552 | 5a673575 | Stavros Sachtouris | self._run(flavor_id=flavor_id)
|
553 | 5a673575 | Stavros Sachtouris | |
554 | 234954d1 | Stavros Sachtouris | |
555 | d486baec | Stavros Sachtouris | @command(network_cmds)
|
556 | 545c6c29 | Stavros Sachtouris | class network_info(_init_cyclades, _optional_json): |
557 | ddc0b290 | Stavros Sachtouris | """Detailed information on a network
|
558 | ddc0b290 | Stavros Sachtouris | To get a list of available networks and network ids, try /network list
|
559 | ddc0b290 | Stavros Sachtouris | """
|
560 | c17b8bc0 | Stavros Sachtouris | |
561 | 5a673575 | Stavros Sachtouris | @errors.generic.all
|
562 | 5a673575 | Stavros Sachtouris | @errors.cyclades.connection
|
563 | 5a673575 | Stavros Sachtouris | @errors.cyclades.network_id
|
564 | 5a673575 | Stavros Sachtouris | def _run(self, network_id): |
565 | 5a673575 | Stavros Sachtouris | network = self.client.get_network_details(int(network_id)) |
566 | 545c6c29 | Stavros Sachtouris | self._print(network, print_dict, exclude=('id')) |
567 | 5a673575 | Stavros Sachtouris | |
568 | c17b8bc0 | Stavros Sachtouris | def main(self, network_id): |
569 | 5a673575 | Stavros Sachtouris | super(self.__class__, self)._run() |
570 | 5a673575 | Stavros Sachtouris | self._run(network_id=network_id)
|
571 | c17b8bc0 | Stavros Sachtouris | |
572 | c17b8bc0 | Stavros Sachtouris | |
573 | c17b8bc0 | Stavros Sachtouris | @command(network_cmds)
|
574 | 545c6c29 | Stavros Sachtouris | class network_list(_init_cyclades, _optional_json): |
575 | 7493ccb6 | Stavros Sachtouris | """List networks"""
|
576 | 7493ccb6 | Stavros Sachtouris | |
577 | 93914390 | Stavros Sachtouris | arguments = dict(
|
578 | f40f0cb7 | Stavros Sachtouris | detail=FlagArgument('show detailed output', ('-l', '--details')), |
579 | f40f0cb7 | Stavros Sachtouris | limit=IntArgument('limit # of listed networks', ('-n', '--number')), |
580 | ddc0b290 | Stavros Sachtouris | more=FlagArgument( |
581 | ddc0b290 | Stavros Sachtouris | 'output results in pages (-n to set items per page, default 10)',
|
582 | ed9af02c | Stavros Sachtouris | '--more'),
|
583 | ed9af02c | Stavros Sachtouris | enum=FlagArgument('Enumerate results', '--enumerate') |
584 | 93914390 | Stavros Sachtouris | ) |
585 | 7493ccb6 | Stavros Sachtouris | |
586 | 5a673575 | Stavros Sachtouris | @errors.generic.all
|
587 | 5a673575 | Stavros Sachtouris | @errors.cyclades.connection
|
588 | 5a673575 | Stavros Sachtouris | def _run(self): |
589 | 5a673575 | Stavros Sachtouris | networks = self.client.list_networks(self['detail']) |
590 | 545c6c29 | Stavros Sachtouris | kwargs = dict(with_enumeration=self['enum']) |
591 | ddc0b290 | Stavros Sachtouris | if self['more']: |
592 | 545c6c29 | Stavros Sachtouris | kwargs['page_size'] = self['limit'] or 10 |
593 | 0e806947 | Stavros Sachtouris | elif self['limit']: |
594 | 545c6c29 | Stavros Sachtouris | networks = networks[:self['limit']] |
595 | 545c6c29 | Stavros Sachtouris | self._print(networks, **kwargs)
|
596 | 7493ccb6 | Stavros Sachtouris | |
597 | 5a673575 | Stavros Sachtouris | def main(self): |
598 | 5a673575 | Stavros Sachtouris | super(self.__class__, self)._run() |
599 | 5a673575 | Stavros Sachtouris | self._run()
|
600 | 5a673575 | Stavros Sachtouris | |
601 | 234954d1 | Stavros Sachtouris | |
602 | d486baec | Stavros Sachtouris | @command(network_cmds)
|
603 | 545c6c29 | Stavros Sachtouris | class network_create(_init_cyclades, _optional_json): |
604 | 0e806947 | Stavros Sachtouris | """Create an (unconnected) network"""
|
605 | 7493ccb6 | Stavros Sachtouris | |
606 | 93914390 | Stavros Sachtouris | arguments = dict(
|
607 | 0e806947 | Stavros Sachtouris | cidr=ValueArgument('explicitly set cidr', '--with-cidr'), |
608 | 0e806947 | Stavros Sachtouris | gateway=ValueArgument('explicitly set gateway', '--with-gateway'), |
609 | 9d1db11e | Stavros Sachtouris | dhcp=FlagArgument('Use dhcp (default: off)', '--with-dhcp'), |
610 | f6f81cd3 | Stavros Sachtouris | type=ValueArgument( |
611 | f6f81cd3 | Stavros Sachtouris | 'Valid network types are '
|
612 | f6f81cd3 | Stavros Sachtouris | 'CUSTOM, IP_LESS_ROUTED, MAC_FILTERED (default), PHYSICAL_VLAN',
|
613 | f6f81cd3 | Stavros Sachtouris | '--with-type',
|
614 | f6f81cd3 | Stavros Sachtouris | default='MAC_FILTERED')
|
615 | 93914390 | Stavros Sachtouris | ) |
616 | 7493ccb6 | Stavros Sachtouris | |
617 | 5a673575 | Stavros Sachtouris | @errors.generic.all
|
618 | 5a673575 | Stavros Sachtouris | @errors.cyclades.connection
|
619 | 5a673575 | Stavros Sachtouris | @errors.cyclades.network_max
|
620 | 5a673575 | Stavros Sachtouris | def _run(self, name): |
621 | bcef3ac9 | Stavros Sachtouris | self._print(self.client.create_network( |
622 | de73876b | Stavros Sachtouris | name, |
623 | 5a673575 | Stavros Sachtouris | cidr=self['cidr'], |
624 | 5a673575 | Stavros Sachtouris | gateway=self['gateway'], |
625 | 5a673575 | Stavros Sachtouris | dhcp=self['dhcp'], |
626 | bcef3ac9 | Stavros Sachtouris | type=self['type']), print_dict) |
627 | 5a673575 | Stavros Sachtouris | |
628 | 7493ccb6 | Stavros Sachtouris | def main(self, name): |
629 | 5a673575 | Stavros Sachtouris | super(self.__class__, self)._run() |
630 | 5a673575 | Stavros Sachtouris | self._run(name)
|
631 | 7493ccb6 | Stavros Sachtouris | |
632 | 234954d1 | Stavros Sachtouris | |
633 | d486baec | Stavros Sachtouris | @command(network_cmds)
|
634 | 1b73b4c1 | Stavros Sachtouris | class network_rename(_init_cyclades, _optional_output_cmd): |
635 | 0e806947 | Stavros Sachtouris | """Set the name of a network"""
|
636 | 7493ccb6 | Stavros Sachtouris | |
637 | 5a673575 | Stavros Sachtouris | @errors.generic.all
|
638 | 5a673575 | Stavros Sachtouris | @errors.cyclades.connection
|
639 | 5a673575 | Stavros Sachtouris | @errors.cyclades.network_id
|
640 | 5a673575 | Stavros Sachtouris | def _run(self, network_id, new_name): |
641 | 1b73b4c1 | Stavros Sachtouris | self._optional_output(
|
642 | 1b73b4c1 | Stavros Sachtouris | self.client.update_network_name(int(network_id), new_name)) |
643 | 5a673575 | Stavros Sachtouris | |
644 | 7493ccb6 | Stavros Sachtouris | def main(self, network_id, new_name): |
645 | 5a673575 | Stavros Sachtouris | super(self.__class__, self)._run() |
646 | 5a673575 | Stavros Sachtouris | self._run(network_id=network_id, new_name=new_name)
|
647 | 7493ccb6 | Stavros Sachtouris | |
648 | 234954d1 | Stavros Sachtouris | |
649 | d486baec | Stavros Sachtouris | @command(network_cmds)
|
650 | 1b73b4c1 | Stavros Sachtouris | class network_delete(_init_cyclades, _optional_output_cmd): |
651 | 7493ccb6 | Stavros Sachtouris | """Delete a network"""
|
652 | 7493ccb6 | Stavros Sachtouris | |
653 | 5a673575 | Stavros Sachtouris | @errors.generic.all
|
654 | 5a673575 | Stavros Sachtouris | @errors.cyclades.connection
|
655 | 5a673575 | Stavros Sachtouris | @errors.cyclades.network_id
|
656 | 5a673575 | Stavros Sachtouris | @errors.cyclades.network_in_use
|
657 | 5a673575 | Stavros Sachtouris | def _run(self, network_id): |
658 | 1b73b4c1 | Stavros Sachtouris | self._optional_output(self.client.delete_network(int(network_id))) |
659 | 5a673575 | Stavros Sachtouris | |
660 | 7493ccb6 | Stavros Sachtouris | def main(self, network_id): |
661 | 5a673575 | Stavros Sachtouris | super(self.__class__, self)._run() |
662 | 5a673575 | Stavros Sachtouris | self._run(network_id=network_id)
|
663 | 7493ccb6 | Stavros Sachtouris | |
664 | 234954d1 | Stavros Sachtouris | |
665 | d486baec | Stavros Sachtouris | @command(network_cmds)
|
666 | 1b73b4c1 | Stavros Sachtouris | class network_connect(_init_cyclades, _optional_output_cmd): |
667 | 7493ccb6 | Stavros Sachtouris | """Connect a server to a network"""
|
668 | 7493ccb6 | Stavros Sachtouris | |
669 | 5a673575 | Stavros Sachtouris | @errors.generic.all
|
670 | 5a673575 | Stavros Sachtouris | @errors.cyclades.connection
|
671 | 5a673575 | Stavros Sachtouris | @errors.cyclades.server_id
|
672 | 5a673575 | Stavros Sachtouris | @errors.cyclades.network_id
|
673 | 5a673575 | Stavros Sachtouris | def _run(self, server_id, network_id): |
674 | 1b73b4c1 | Stavros Sachtouris | self._optional_output(
|
675 | 1b73b4c1 | Stavros Sachtouris | self.client.connect_server(int(server_id), int(network_id))) |
676 | 5a673575 | Stavros Sachtouris | |
677 | 7493ccb6 | Stavros Sachtouris | def main(self, server_id, network_id): |
678 | 5a673575 | Stavros Sachtouris | super(self.__class__, self)._run() |
679 | 5a673575 | Stavros Sachtouris | self._run(server_id=server_id, network_id=network_id)
|
680 | 7493ccb6 | Stavros Sachtouris | |
681 | 234954d1 | Stavros Sachtouris | |
682 | d486baec | Stavros Sachtouris | @command(network_cmds)
|
683 | 7493ccb6 | Stavros Sachtouris | class network_disconnect(_init_cyclades): |
684 | 0e806947 | Stavros Sachtouris | """Disconnect a nic that connects a server to a network
|
685 | 0e806947 | Stavros Sachtouris | Nic ids are listed as "attachments" in detailed network information
|
686 | 439826ec | Stavros Sachtouris | To get detailed network information: /network info <network id>
|
687 | 0e806947 | Stavros Sachtouris | """
|
688 | 7493ccb6 | Stavros Sachtouris | |
689 | 5a673575 | Stavros Sachtouris | @errors.cyclades.nic_format
|
690 | 5a673575 | Stavros Sachtouris | def _server_id_from_nic(self, nic_id): |
691 | 5a673575 | Stavros Sachtouris | return nic_id.split('-')[1] |
692 | 5a673575 | Stavros Sachtouris | |
693 | 5a673575 | Stavros Sachtouris | @errors.generic.all
|
694 | 5a673575 | Stavros Sachtouris | @errors.cyclades.connection
|
695 | 5a673575 | Stavros Sachtouris | @errors.cyclades.server_id
|
696 | 5a673575 | Stavros Sachtouris | @errors.cyclades.nic_id
|
697 | 5a673575 | Stavros Sachtouris | def _run(self, nic_id, server_id): |
698 | 1b73b4c1 | Stavros Sachtouris | num_of_disconnected = self.client.disconnect_server(server_id, nic_id)
|
699 | 1b73b4c1 | Stavros Sachtouris | if not num_of_disconnected: |
700 | 5a673575 | Stavros Sachtouris | raise ClientError(
|
701 | 5a673575 | Stavros Sachtouris | 'Network Interface %s not found on server %s' % (
|
702 | 5a673575 | Stavros Sachtouris | nic_id, |
703 | 5a673575 | Stavros Sachtouris | server_id), |
704 | 5a673575 | Stavros Sachtouris | status=404)
|
705 | 1b73b4c1 | Stavros Sachtouris | print('Disconnected %s connections' % num_of_disconnected)
|
706 | 5a673575 | Stavros Sachtouris | |
707 | 7493ccb6 | Stavros Sachtouris | def main(self, nic_id): |
708 | 5a673575 | Stavros Sachtouris | super(self.__class__, self)._run() |
709 | 5a673575 | Stavros Sachtouris | server_id = self._server_id_from_nic(nic_id=nic_id)
|
710 | 5a673575 | Stavros Sachtouris | self._run(nic_id=nic_id, server_id=server_id) |