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