root / kamaki / cli / commands / image.py @ 95641ecc
History | View | Annotate | Download (27.2 kB)
1 | e3f01d64 | Stavros Sachtouris | # Copyright 2012-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.command
|
33 | 7493ccb6 | Stavros Sachtouris | |
34 | 623a4ceb | Stavros Sachtouris | from json import load, dumps |
35 | d77e33d4 | Stavros Sachtouris | from os import path |
36 | 623a4ceb | Stavros Sachtouris | from logging import getLogger |
37 | 623a4ceb | Stavros Sachtouris | |
38 | f3e94e06 | Stavros Sachtouris | from kamaki.cli import command |
39 | d486baec | Stavros Sachtouris | from kamaki.cli.command_tree import CommandTree |
40 | 117ca598 | Stavros Sachtouris | from kamaki.cli.utils import print_dict, print_json |
41 | 1395c40e | Stavros Sachtouris | from kamaki.clients.image import ImageClient |
42 | 00336c85 | Stavros Sachtouris | from kamaki.clients.pithos import PithosClient |
43 | 00336c85 | Stavros Sachtouris | from kamaki.clients.astakos import AstakosClient |
44 | 00336c85 | Stavros Sachtouris | from kamaki.clients import ClientError |
45 | 83c3ba87 | Stavros Sachtouris | from kamaki.cli.argument import FlagArgument, ValueArgument, KeyValueArgument |
46 | d77e33d4 | Stavros Sachtouris | from kamaki.cli.argument import IntArgument, ProgressBarArgument |
47 | bfb54881 | Stavros Sachtouris | from kamaki.cli.commands.cyclades import _init_cyclades |
48 | 8cec3671 | Stavros Sachtouris | from kamaki.cli.errors import raiseCLIError, CLIBaseUrlError |
49 | b4f69041 | Stavros Sachtouris | from kamaki.cli.commands import _command_init, errors, addLogSettings |
50 | 545c6c29 | Stavros Sachtouris | from kamaki.cli.commands import _optional_output_cmd, _optional_json |
51 | 0b368c8c | Stavros Sachtouris | |
52 | 234954d1 | Stavros Sachtouris | |
53 | a29d2f88 | Stavros Sachtouris | image_cmds = CommandTree( |
54 | a29d2f88 | Stavros Sachtouris | 'image',
|
55 | a29d2f88 | Stavros Sachtouris | 'Cyclades/Plankton API image commands\n'
|
56 | a29d2f88 | Stavros Sachtouris | 'image compute:\tCyclades/Compute API image commands')
|
57 | d486baec | Stavros Sachtouris | _commands = [image_cmds] |
58 | 234954d1 | Stavros Sachtouris | |
59 | 234954d1 | Stavros Sachtouris | |
60 | aa82dd5a | Stavros Sachtouris | howto_image_file = [ |
61 | aa82dd5a | Stavros Sachtouris | 'Kamaki commands to:',
|
62 | c626151a | Stavros Sachtouris | ' get current user id: /user authenticate',
|
63 | aa82dd5a | Stavros Sachtouris | ' check available containers: /file list',
|
64 | aa82dd5a | Stavros Sachtouris | ' create a new container: /file create <container>',
|
65 | aa82dd5a | Stavros Sachtouris | ' check container contents: /file list <container>',
|
66 | aa82dd5a | Stavros Sachtouris | ' upload files: /file upload <image file> <container>']
|
67 | aa82dd5a | Stavros Sachtouris | |
68 | aa82dd5a | Stavros Sachtouris | about_image_id = ['To see a list of available image ids: /image list']
|
69 | 15142309 | Stavros Sachtouris | |
70 | 15142309 | Stavros Sachtouris | |
71 | 623a4ceb | Stavros Sachtouris | log = getLogger(__name__) |
72 | 623a4ceb | Stavros Sachtouris | |
73 | 623a4ceb | Stavros Sachtouris | |
74 | 5eae854d | Stavros Sachtouris | class _init_image(_command_init): |
75 | a03ade9e | Stavros Sachtouris | @errors.generic.all
|
76 | b4f69041 | Stavros Sachtouris | @addLogSettings
|
77 | a03ade9e | Stavros Sachtouris | def _run(self): |
78 | b4f69041 | Stavros Sachtouris | if getattr(self, 'cloud', None): |
79 | b4f69041 | Stavros Sachtouris | img_url = self._custom_url('image') or self._custom_url('plankton') |
80 | b4f69041 | Stavros Sachtouris | if img_url:
|
81 | b4f69041 | Stavros Sachtouris | token = self._custom_token('image')\ |
82 | b4f69041 | Stavros Sachtouris | or self._custom_token('plankton')\ |
83 | 144b3551 | Stavros Sachtouris | or self.config.get_cloud(self.cloud, 'token') |
84 | b4f69041 | Stavros Sachtouris | self.client = ImageClient(base_url=img_url, token=token)
|
85 | b4f69041 | Stavros Sachtouris | return
|
86 | 8cec3671 | Stavros Sachtouris | if getattr(self, 'auth_base', False): |
87 | 8cec3671 | Stavros Sachtouris | plankton_endpoints = self.auth_base.get_service_endpoints(
|
88 | 72fa0010 | Stavros Sachtouris | self._custom_type('image') or self._custom_type( |
89 | 72fa0010 | Stavros Sachtouris | 'plankton') or 'image', |
90 | 72fa0010 | Stavros Sachtouris | self._custom_version('image') or self._custom_version( |
91 | 72fa0010 | Stavros Sachtouris | 'plankton') or '') |
92 | 8cec3671 | Stavros Sachtouris | base_url = plankton_endpoints['publicURL']
|
93 | b4f69041 | Stavros Sachtouris | token = self.auth_base.token
|
94 | 8cec3671 | Stavros Sachtouris | else:
|
95 | 8cec3671 | Stavros Sachtouris | raise CLIBaseUrlError(service='plankton') |
96 | a03ade9e | Stavros Sachtouris | self.client = ImageClient(base_url=base_url, token=token)
|
97 | a03ade9e | Stavros Sachtouris | |
98 | 7493ccb6 | Stavros Sachtouris | def main(self): |
99 | a03ade9e | Stavros Sachtouris | self._run()
|
100 | 7493ccb6 | Stavros Sachtouris | |
101 | 234954d1 | Stavros Sachtouris | |
102 | 573be34f | Stavros Sachtouris | # Plankton Image Commands
|
103 | 573be34f | Stavros Sachtouris | |
104 | 573be34f | Stavros Sachtouris | |
105 | aa82dd5a | Stavros Sachtouris | def _validate_image_meta(json_dict, return_str=False): |
106 | 623a4ceb | Stavros Sachtouris | """
|
107 | 623a4ceb | Stavros Sachtouris | :param json_dict" (dict) json-formated, of the form
|
108 | 623a4ceb | Stavros Sachtouris | {"key1": "val1", "key2": "val2", ...}
|
109 | 623a4ceb | Stavros Sachtouris |
|
110 | 623a4ceb | Stavros Sachtouris | :param return_str: (boolean) if true, return a json dump
|
111 | 623a4ceb | Stavros Sachtouris |
|
112 | aa82dd5a | Stavros Sachtouris | :returns: (dict) if return_str is not True, else return str
|
113 | 623a4ceb | Stavros Sachtouris |
|
114 | 623a4ceb | Stavros Sachtouris | :raises TypeError, AttributeError: Invalid json format
|
115 | 623a4ceb | Stavros Sachtouris |
|
116 | 623a4ceb | Stavros Sachtouris | :raises AssertionError: Valid json but invalid image properties dict
|
117 | 623a4ceb | Stavros Sachtouris | """
|
118 | 9553da85 | Stavros Sachtouris | json_str = dumps(json_dict, indent=2)
|
119 | 623a4ceb | Stavros Sachtouris | for k, v in json_dict.items(): |
120 | aa82dd5a | Stavros Sachtouris | if k.lower() == 'properties': |
121 | aa82dd5a | Stavros Sachtouris | for pk, pv in v.items(): |
122 | aa82dd5a | Stavros Sachtouris | prop_ok = not (isinstance(pv, dict) or isinstance(pv, list)) |
123 | aa82dd5a | Stavros Sachtouris | assert prop_ok, 'Invalid property value for key %s' % pk |
124 | aa82dd5a | Stavros Sachtouris | key_ok = not (' ' in k or '-' in k) |
125 | aa82dd5a | Stavros Sachtouris | assert key_ok, 'Invalid property key %s' % k |
126 | aa82dd5a | Stavros Sachtouris | continue
|
127 | aa82dd5a | Stavros Sachtouris | meta_ok = not (isinstance(v, dict) or isinstance(v, list)) |
128 | aa82dd5a | Stavros Sachtouris | assert meta_ok, 'Invalid value for meta key %s' % k |
129 | aa82dd5a | Stavros Sachtouris | meta_ok = ' ' not in k |
130 | aa82dd5a | Stavros Sachtouris | assert meta_ok, 'Invalid meta key [%s]' % k |
131 | 623a4ceb | Stavros Sachtouris | json_dict[k] = '%s' % v
|
132 | 623a4ceb | Stavros Sachtouris | return json_str if return_str else json_dict |
133 | 623a4ceb | Stavros Sachtouris | |
134 | 623a4ceb | Stavros Sachtouris | |
135 | aa82dd5a | Stavros Sachtouris | def _load_image_meta(filepath): |
136 | 623a4ceb | Stavros Sachtouris | """
|
137 | 623a4ceb | Stavros Sachtouris | :param filepath: (str) the (relative) path of the metafile
|
138 | 623a4ceb | Stavros Sachtouris |
|
139 | 623a4ceb | Stavros Sachtouris | :returns: (dict) json_formated
|
140 | 623a4ceb | Stavros Sachtouris |
|
141 | 623a4ceb | Stavros Sachtouris | :raises TypeError, AttributeError: Invalid json format
|
142 | 623a4ceb | Stavros Sachtouris |
|
143 | 623a4ceb | Stavros Sachtouris | :raises AssertionError: Valid json but invalid image properties dict
|
144 | 623a4ceb | Stavros Sachtouris | """
|
145 | d77e33d4 | Stavros Sachtouris | with open(path.abspath(filepath)) as f: |
146 | 623a4ceb | Stavros Sachtouris | meta_dict = load(f) |
147 | 623a4ceb | Stavros Sachtouris | try:
|
148 | aa82dd5a | Stavros Sachtouris | return _validate_image_meta(meta_dict)
|
149 | 623a4ceb | Stavros Sachtouris | except AssertionError: |
150 | 623a4ceb | Stavros Sachtouris | log.debug('Failed to load properties from file %s' % filepath)
|
151 | 623a4ceb | Stavros Sachtouris | raise
|
152 | 623a4ceb | Stavros Sachtouris | |
153 | 623a4ceb | Stavros Sachtouris | |
154 | aa82dd5a | Stavros Sachtouris | def _validate_image_location(location): |
155 | aa82dd5a | Stavros Sachtouris | """
|
156 | f2ea1314 | Stavros Sachtouris | :param location: (str) pithos://<user-id>/<container>/<image-path>
|
157 | aa82dd5a | Stavros Sachtouris |
|
158 | f2ea1314 | Stavros Sachtouris | :returns: (<user-id>, <container>, <image-path>)
|
159 | aa82dd5a | Stavros Sachtouris |
|
160 | aa82dd5a | Stavros Sachtouris | :raises AssertionError: if location is invalid
|
161 | aa82dd5a | Stavros Sachtouris | """
|
162 | aa82dd5a | Stavros Sachtouris | prefix = 'pithos://'
|
163 | aa82dd5a | Stavros Sachtouris | msg = 'Invalid prefix for location %s , try: %s' % (location, prefix)
|
164 | aa82dd5a | Stavros Sachtouris | assert location.startswith(prefix), msg
|
165 | aa82dd5a | Stavros Sachtouris | service, sep, rest = location.partition('://')
|
166 | c626151a | Stavros Sachtouris | assert sep and rest, 'Location %s is missing user-id' % location |
167 | aa82dd5a | Stavros Sachtouris | uuid, sep, rest = rest.partition('/')
|
168 | aa82dd5a | Stavros Sachtouris | assert sep and rest, 'Location %s is missing container' % location |
169 | aa82dd5a | Stavros Sachtouris | container, sep, img_path = rest.partition('/')
|
170 | aa82dd5a | Stavros Sachtouris | assert sep and img_path, 'Location %s is missing image path' % location |
171 | aa82dd5a | Stavros Sachtouris | return uuid, container, img_path
|
172 | aa82dd5a | Stavros Sachtouris | |
173 | aa82dd5a | Stavros Sachtouris | |
174 | d486baec | Stavros Sachtouris | @command(image_cmds)
|
175 | 545c6c29 | Stavros Sachtouris | class image_list(_init_image, _optional_json): |
176 | 573be34f | Stavros Sachtouris | """List images accessible by user"""
|
177 | 7493ccb6 | Stavros Sachtouris | |
178 | 2d1202ee | Stavros Sachtouris | PERMANENTS = ( |
179 | 2d1202ee | Stavros Sachtouris | 'id', 'name', |
180 | 2d1202ee | Stavros Sachtouris | 'status', 'container_format', 'disk_format', 'size') |
181 | 2d1202ee | Stavros Sachtouris | |
182 | 1ae79e60 | Stavros Sachtouris | arguments = dict(
|
183 | f40f0cb7 | Stavros Sachtouris | detail=FlagArgument('show detailed output', ('-l', '--details')), |
184 | 1ae79e60 | Stavros Sachtouris | container_format=ValueArgument( |
185 | 1ae79e60 | Stavros Sachtouris | 'filter by container format',
|
186 | 1ae79e60 | Stavros Sachtouris | '--container-format'),
|
187 | 1ae79e60 | Stavros Sachtouris | disk_format=ValueArgument('filter by disk format', '--disk-format'), |
188 | 1ae79e60 | Stavros Sachtouris | name=ValueArgument('filter by name', '--name'), |
189 | faeccbf9 | Stavros Sachtouris | name_pref=ValueArgument( |
190 | faeccbf9 | Stavros Sachtouris | 'filter by name prefix (case insensitive)',
|
191 | faeccbf9 | Stavros Sachtouris | '--name-prefix'),
|
192 | faeccbf9 | Stavros Sachtouris | name_suff=ValueArgument( |
193 | faeccbf9 | Stavros Sachtouris | 'filter by name suffix (case insensitive)',
|
194 | faeccbf9 | Stavros Sachtouris | '--name-suffix'),
|
195 | faeccbf9 | Stavros Sachtouris | name_like=ValueArgument( |
196 | faeccbf9 | Stavros Sachtouris | 'print only if name contains this (case insensitive)',
|
197 | faeccbf9 | Stavros Sachtouris | '--name-like'),
|
198 | 1ae79e60 | Stavros Sachtouris | size_min=IntArgument('filter by minimum size', '--size-min'), |
199 | 1ae79e60 | Stavros Sachtouris | size_max=IntArgument('filter by maximum size', '--size-max'), |
200 | 1ae79e60 | Stavros Sachtouris | status=ValueArgument('filter by status', '--status'), |
201 | f9457c89 | Stavros Sachtouris | owner=ValueArgument('filter by owner', '--owner'), |
202 | 95641ecc | Stavros Sachtouris | owner_name=ValueArgument('filter by owners username', '--owner-name'), |
203 | 1ae79e60 | Stavros Sachtouris | order=ValueArgument( |
204 | 1ae79e60 | Stavros Sachtouris | 'order by FIELD ( - to reverse order)',
|
205 | 1ae79e60 | Stavros Sachtouris | '--order',
|
206 | 83c3ba87 | Stavros Sachtouris | default=''),
|
207 | f40f0cb7 | Stavros Sachtouris | limit=IntArgument('limit number of listed images', ('-n', '--number')), |
208 | 83c3ba87 | Stavros Sachtouris | more=FlagArgument( |
209 | 83c3ba87 | Stavros Sachtouris | 'output results in pages (-n to set items per page, default 10)',
|
210 | ed9af02c | Stavros Sachtouris | '--more'),
|
211 | 2d1202ee | Stavros Sachtouris | enum=FlagArgument('Enumerate results', '--enumerate'), |
212 | 2d1202ee | Stavros Sachtouris | prop=KeyValueArgument('filter by property key=value', ('--property')) |
213 | 1ae79e60 | Stavros Sachtouris | ) |
214 | 7493ccb6 | Stavros Sachtouris | |
215 | f9457c89 | Stavros Sachtouris | def _filtered_by_owner(self, detail, *list_params): |
216 | f9457c89 | Stavros Sachtouris | images = [] |
217 | 95641ecc | Stavros Sachtouris | ouuid = self['owner'] or self._username2uuid(self['owner_name']) |
218 | 95641ecc | Stavros Sachtouris | if not ouuid: |
219 | 95641ecc | Stavros Sachtouris | return images
|
220 | f9457c89 | Stavros Sachtouris | for img in self.client.list_public(True, *list_params): |
221 | 95641ecc | Stavros Sachtouris | if img['owner'] == ouuid: |
222 | f9457c89 | Stavros Sachtouris | if not detail: |
223 | 95641ecc | Stavros Sachtouris | for key in set(img.keys()).difference(self.PERMANENTS): |
224 | f9457c89 | Stavros Sachtouris | img.pop(key) |
225 | f9457c89 | Stavros Sachtouris | images.append(img) |
226 | f9457c89 | Stavros Sachtouris | return images
|
227 | f9457c89 | Stavros Sachtouris | |
228 | faeccbf9 | Stavros Sachtouris | def _filtered_by_name(self, images): |
229 | faeccbf9 | Stavros Sachtouris | np, ns, nl = self['name_pref'], self['name_suff'], self['name_like'] |
230 | 95641ecc | Stavros Sachtouris | |
231 | 95641ecc | Stavros Sachtouris | def augment_owner(img): |
232 | 95641ecc | Stavros Sachtouris | uuid = img.get('owner', None) |
233 | 95641ecc | Stavros Sachtouris | if uuid and not self['json_output']: |
234 | 95641ecc | Stavros Sachtouris | img['owner'] = '%s (%s)' % (uuid, self._uuid2username(uuid)) |
235 | 95641ecc | Stavros Sachtouris | return img
|
236 | 95641ecc | Stavros Sachtouris | |
237 | 95641ecc | Stavros Sachtouris | return [augment_owner(img) for img in images if ( |
238 | faeccbf9 | Stavros Sachtouris | (not np) or img['name'].lower().startswith(np.lower())) and ( |
239 | faeccbf9 | Stavros Sachtouris | (not ns) or img['name'].lower().endswith(ns.lower())) and ( |
240 | faeccbf9 | Stavros Sachtouris | (not nl) or nl.lower() in img['name'].lower())] |
241 | faeccbf9 | Stavros Sachtouris | |
242 | 2d1202ee | Stavros Sachtouris | def _filtered_by_properties(self, images): |
243 | 2d1202ee | Stavros Sachtouris | new_images = [] |
244 | 2d1202ee | Stavros Sachtouris | for img in images: |
245 | 2d1202ee | Stavros Sachtouris | if set(self['prop'].items()).difference(img['properties'].items()): |
246 | 2d1202ee | Stavros Sachtouris | continue
|
247 | 2d1202ee | Stavros Sachtouris | if self['detail']: |
248 | 2d1202ee | Stavros Sachtouris | new_images.append(dict(img))
|
249 | 2d1202ee | Stavros Sachtouris | else:
|
250 | 2d1202ee | Stavros Sachtouris | new_images.append(dict())
|
251 | 2d1202ee | Stavros Sachtouris | for k in set(img).intersection(self.PERMANENTS): |
252 | 2d1202ee | Stavros Sachtouris | new_images[-1][k] = img[k]
|
253 | 2d1202ee | Stavros Sachtouris | return new_images
|
254 | 2d1202ee | Stavros Sachtouris | |
255 | a03ade9e | Stavros Sachtouris | @errors.generic.all
|
256 | a03ade9e | Stavros Sachtouris | @errors.cyclades.connection
|
257 | a03ade9e | Stavros Sachtouris | def _run(self): |
258 | a03ade9e | Stavros Sachtouris | super(self.__class__, self)._run() |
259 | 7493ccb6 | Stavros Sachtouris | filters = {} |
260 | fa984c2c | Stavros Sachtouris | for arg in set([ |
261 | 1ae79e60 | Stavros Sachtouris | 'container_format',
|
262 | 1ae79e60 | Stavros Sachtouris | 'disk_format',
|
263 | 1ae79e60 | Stavros Sachtouris | 'name',
|
264 | 1ae79e60 | Stavros Sachtouris | 'size_min',
|
265 | 1ae79e60 | Stavros Sachtouris | 'size_max',
|
266 | de73876b | Stavros Sachtouris | 'status']).intersection(self.arguments): |
267 | 1ae79e60 | Stavros Sachtouris | filters[arg] = self[arg]
|
268 | 1ae79e60 | Stavros Sachtouris | |
269 | 1ae79e60 | Stavros Sachtouris | order = self['order'] |
270 | 2d1202ee | Stavros Sachtouris | detail = self['detail'] or self['prop'] |
271 | 95641ecc | Stavros Sachtouris | if self['owner'] or self['owner_name']: |
272 | f9457c89 | Stavros Sachtouris | images = self._filtered_by_owner(detail, filters, order)
|
273 | f9457c89 | Stavros Sachtouris | else:
|
274 | f9457c89 | Stavros Sachtouris | images = self.client.list_public(detail, filters, order)
|
275 | f9457c89 | Stavros Sachtouris | |
276 | f5f35422 | Stavros Sachtouris | images = self._filtered_by_name(images)
|
277 | 2d1202ee | Stavros Sachtouris | if self['prop']: |
278 | 2d1202ee | Stavros Sachtouris | images = self._filtered_by_properties(images)
|
279 | 545c6c29 | Stavros Sachtouris | kwargs = dict(with_enumeration=self['enum']) |
280 | 83c3ba87 | Stavros Sachtouris | if self['more']: |
281 | 545c6c29 | Stavros Sachtouris | kwargs['page_size'] = self['limit'] or 10 |
282 | 83c3ba87 | Stavros Sachtouris | elif self['limit']: |
283 | 545c6c29 | Stavros Sachtouris | images = images[:self['limit']] |
284 | 545c6c29 | Stavros Sachtouris | self._print(images, **kwargs)
|
285 | 7493ccb6 | Stavros Sachtouris | |
286 | a03ade9e | Stavros Sachtouris | def main(self): |
287 | a03ade9e | Stavros Sachtouris | super(self.__class__, self)._run() |
288 | a03ade9e | Stavros Sachtouris | self._run()
|
289 | a03ade9e | Stavros Sachtouris | |
290 | 234954d1 | Stavros Sachtouris | |
291 | d486baec | Stavros Sachtouris | @command(image_cmds)
|
292 | 545c6c29 | Stavros Sachtouris | class image_meta(_init_image, _optional_json): |
293 | 15142309 | Stavros Sachtouris | """Get image metadata
|
294 | 15142309 | Stavros Sachtouris | Image metadata include:
|
295 | 439826ec | Stavros Sachtouris | - image file information (location, size, etc.)
|
296 | 439826ec | Stavros Sachtouris | - image information (id, name, etc.)
|
297 | 439826ec | Stavros Sachtouris | - image os properties (os, fs, etc.)
|
298 | 15142309 | Stavros Sachtouris | """
|
299 | 7493ccb6 | Stavros Sachtouris | |
300 | a03ade9e | Stavros Sachtouris | @errors.generic.all
|
301 | a03ade9e | Stavros Sachtouris | @errors.plankton.connection
|
302 | a03ade9e | Stavros Sachtouris | @errors.plankton.id
|
303 | a03ade9e | Stavros Sachtouris | def _run(self, image_id): |
304 | 545c6c29 | Stavros Sachtouris | self._print([self.client.get_meta(image_id)]) |
305 | 7493ccb6 | Stavros Sachtouris | |
306 | a03ade9e | Stavros Sachtouris | def main(self, image_id): |
307 | a03ade9e | Stavros Sachtouris | super(self.__class__, self)._run() |
308 | b04288f7 | Stavros Sachtouris | self._run(image_id=image_id)
|
309 | a03ade9e | Stavros Sachtouris | |
310 | 234954d1 | Stavros Sachtouris | |
311 | d486baec | Stavros Sachtouris | @command(image_cmds)
|
312 | 545c6c29 | Stavros Sachtouris | class image_register(_init_image, _optional_json): |
313 | fc903073 | Stavros Sachtouris | """(Re)Register an image"""
|
314 | 7493ccb6 | Stavros Sachtouris | |
315 | d77e33d4 | Stavros Sachtouris | container_info_cache = {} |
316 | d77e33d4 | Stavros Sachtouris | |
317 | 1ae79e60 | Stavros Sachtouris | arguments = dict(
|
318 | 1ae79e60 | Stavros Sachtouris | checksum=ValueArgument('set image checksum', '--checksum'), |
319 | 1ae79e60 | Stavros Sachtouris | container_format=ValueArgument( |
320 | 1ae79e60 | Stavros Sachtouris | 'set container format',
|
321 | 1ae79e60 | Stavros Sachtouris | '--container-format'),
|
322 | 1ae79e60 | Stavros Sachtouris | disk_format=ValueArgument('set disk format', '--disk-format'), |
323 | 1ae79e60 | Stavros Sachtouris | owner=ValueArgument('set image owner (admin only)', '--owner'), |
324 | 1ae79e60 | Stavros Sachtouris | properties=KeyValueArgument( |
325 | 1ae79e60 | Stavros Sachtouris | 'add property in key=value form (can be repeated)',
|
326 | 1736e06d | Stavros Sachtouris | ('-p', '--property')), |
327 | 1ae79e60 | Stavros Sachtouris | is_public=FlagArgument('mark image as public', '--public'), |
328 | 1ae79e60 | Stavros Sachtouris | size=IntArgument('set image size', '--size'), |
329 | aa82dd5a | Stavros Sachtouris | metafile=ValueArgument( |
330 | aa82dd5a | Stavros Sachtouris | 'Load metadata from a json-formated file <img-file>.meta :'
|
331 | aa82dd5a | Stavros Sachtouris | '{"key1": "val1", "key2": "val2", ..., "properties: {...}"}',
|
332 | aa82dd5a | Stavros Sachtouris | ('--metafile')),
|
333 | aa82dd5a | Stavros Sachtouris | metafile_force=FlagArgument( |
334 | aa82dd5a | Stavros Sachtouris | 'Store remote metadata object, even if it already exists',
|
335 | aa82dd5a | Stavros Sachtouris | ('-f', '--force')), |
336 | aa82dd5a | Stavros Sachtouris | no_metafile_upload=FlagArgument( |
337 | aa82dd5a | Stavros Sachtouris | 'Do not store metadata in remote meta file',
|
338 | aa82dd5a | Stavros Sachtouris | ('--no-metafile-upload')),
|
339 | f2ea1314 | Stavros Sachtouris | container=ValueArgument( |
340 | f2ea1314 | Stavros Sachtouris | 'Pithos+ container containing the image file',
|
341 | f2ea1314 | Stavros Sachtouris | ('-C', '--container')), |
342 | d77e33d4 | Stavros Sachtouris | uuid=ValueArgument('Custom user uuid', '--uuid'), |
343 | d77e33d4 | Stavros Sachtouris | local_image_path=ValueArgument( |
344 | d77e33d4 | Stavros Sachtouris | 'Local image file path to upload and register '
|
345 | d77e33d4 | Stavros Sachtouris | '(still need target file in the form container:remote-path )',
|
346 | d77e33d4 | Stavros Sachtouris | '--upload-image-file'),
|
347 | d77e33d4 | Stavros Sachtouris | progress_bar=ProgressBarArgument( |
348 | d77e33d4 | Stavros Sachtouris | 'Do not use progress bar', '--no-progress-bar', default=False) |
349 | 1ae79e60 | Stavros Sachtouris | ) |
350 | 7493ccb6 | Stavros Sachtouris | |
351 | f5c28bfa | Stavros Sachtouris | def _get_user_id(self): |
352 | 00336c85 | Stavros Sachtouris | atoken = self.client.token
|
353 | ef00bc31 | Stavros Sachtouris | if getattr(self, 'auth_base', False): |
354 | 9d8737a2 | Stavros Sachtouris | return self.auth_base.term('id', atoken) |
355 | ef00bc31 | Stavros Sachtouris | else:
|
356 | f5c28bfa | Stavros Sachtouris | astakos_url = self.config.get('user', 'url')\ |
357 | f5c28bfa | Stavros Sachtouris | or self.config.get('astakos', 'url') |
358 | ef00bc31 | Stavros Sachtouris | if not astakos_url: |
359 | ef00bc31 | Stavros Sachtouris | raise CLIBaseUrlError(service='astakos') |
360 | ef00bc31 | Stavros Sachtouris | user = AstakosClient(astakos_url, atoken) |
361 | 819311d3 | Stavros Sachtouris | return user.term('id') |
362 | 00336c85 | Stavros Sachtouris | |
363 | aa82dd5a | Stavros Sachtouris | def _get_pithos_client(self, container): |
364 | aa82dd5a | Stavros Sachtouris | if self['no_metafile_upload']: |
365 | aa82dd5a | Stavros Sachtouris | return None |
366 | 00336c85 | Stavros Sachtouris | ptoken = self.client.token
|
367 | ef00bc31 | Stavros Sachtouris | if getattr(self, 'auth_base', False): |
368 | ef00bc31 | Stavros Sachtouris | pithos_endpoints = self.auth_base.get_service_endpoints(
|
369 | df0045d8 | Stavros Sachtouris | 'object-store')
|
370 | ef00bc31 | Stavros Sachtouris | purl = pithos_endpoints['publicURL']
|
371 | ef00bc31 | Stavros Sachtouris | else:
|
372 | df0045d8 | Stavros Sachtouris | purl = self.config.get_cloud('pithos', 'url') |
373 | df0045d8 | Stavros Sachtouris | if not purl: |
374 | df0045d8 | Stavros Sachtouris | raise CLIBaseUrlError(service='pithos') |
375 | f5c28bfa | Stavros Sachtouris | return PithosClient(purl, ptoken, self._get_user_id(), container) |
376 | 00336c85 | Stavros Sachtouris | |
377 | aa82dd5a | Stavros Sachtouris | def _store_remote_metafile(self, pclient, remote_path, metadata): |
378 | 00336c85 | Stavros Sachtouris | return pclient.upload_from_string(
|
379 | d77e33d4 | Stavros Sachtouris | remote_path, _validate_image_meta(metadata, return_str=True),
|
380 | d77e33d4 | Stavros Sachtouris | container_info_cache=self.container_info_cache)
|
381 | 00336c85 | Stavros Sachtouris | |
382 | aa82dd5a | Stavros Sachtouris | def _load_params_from_file(self, location): |
383 | aa82dd5a | Stavros Sachtouris | params, properties = dict(), dict() |
384 | aa82dd5a | Stavros Sachtouris | pfile = self['metafile'] |
385 | aa82dd5a | Stavros Sachtouris | if pfile:
|
386 | 00336c85 | Stavros Sachtouris | try:
|
387 | aa82dd5a | Stavros Sachtouris | for k, v in _load_image_meta(pfile).items(): |
388 | aa82dd5a | Stavros Sachtouris | key = k.lower().replace('-', '_') |
389 | aa82dd5a | Stavros Sachtouris | if k == 'properties': |
390 | aa82dd5a | Stavros Sachtouris | for pk, pv in v.items(): |
391 | aa82dd5a | Stavros Sachtouris | properties[pk.upper().replace('-', '_')] = pv |
392 | aa82dd5a | Stavros Sachtouris | elif key == 'name': |
393 | aa82dd5a | Stavros Sachtouris | continue
|
394 | aa82dd5a | Stavros Sachtouris | elif key == 'location': |
395 | aa82dd5a | Stavros Sachtouris | if location:
|
396 | aa82dd5a | Stavros Sachtouris | continue
|
397 | aa82dd5a | Stavros Sachtouris | location = v |
398 | aa82dd5a | Stavros Sachtouris | else:
|
399 | aa82dd5a | Stavros Sachtouris | params[key] = v |
400 | aa82dd5a | Stavros Sachtouris | except Exception as e: |
401 | aa82dd5a | Stavros Sachtouris | raiseCLIError(e, 'Invalid json metadata config file')
|
402 | aa82dd5a | Stavros Sachtouris | return params, properties, location
|
403 | 7493ccb6 | Stavros Sachtouris | |
404 | aa82dd5a | Stavros Sachtouris | def _load_params_from_args(self, params, properties): |
405 | f769a16a | Stavros Sachtouris | for key in set([ |
406 | 1ae79e60 | Stavros Sachtouris | 'checksum',
|
407 | 1ae79e60 | Stavros Sachtouris | 'container_format',
|
408 | 1ae79e60 | Stavros Sachtouris | 'disk_format',
|
409 | 1ae79e60 | Stavros Sachtouris | 'owner',
|
410 | 1ae79e60 | Stavros Sachtouris | 'size',
|
411 | de73876b | Stavros Sachtouris | 'is_public']).intersection(self.arguments): |
412 | 1ae79e60 | Stavros Sachtouris | params[key] = self[key]
|
413 | aa82dd5a | Stavros Sachtouris | for k, v in self['properties'].items(): |
414 | aa82dd5a | Stavros Sachtouris | properties[k.upper().replace('-', '_')] = v |
415 | 1ae79e60 | Stavros Sachtouris | |
416 | aa82dd5a | Stavros Sachtouris | def _validate_location(self, location): |
417 | aa82dd5a | Stavros Sachtouris | if not location: |
418 | aa82dd5a | Stavros Sachtouris | raiseCLIError( |
419 | aa82dd5a | Stavros Sachtouris | 'No image file location provided',
|
420 | aa82dd5a | Stavros Sachtouris | importance=2, details=[
|
421 | aa82dd5a | Stavros Sachtouris | 'An image location is needed. Image location format:',
|
422 | c626151a | Stavros Sachtouris | ' pithos://<user-id>/<container>/<path>',
|
423 | f2ea1314 | Stavros Sachtouris | ' where an image file at the above location must exist.'
|
424 | aa82dd5a | Stavros Sachtouris | ] + howto_image_file) |
425 | aa82dd5a | Stavros Sachtouris | try:
|
426 | aa82dd5a | Stavros Sachtouris | return _validate_image_location(location)
|
427 | aa82dd5a | Stavros Sachtouris | except AssertionError as ae: |
428 | aa82dd5a | Stavros Sachtouris | raiseCLIError( |
429 | aa82dd5a | Stavros Sachtouris | ae, 'Invalid image location format',
|
430 | aa82dd5a | Stavros Sachtouris | importance=1, details=[
|
431 | aa82dd5a | Stavros Sachtouris | 'Valid image location format:',
|
432 | c626151a | Stavros Sachtouris | ' pithos://<user-id>/<container>/<img-file-path>'
|
433 | aa82dd5a | Stavros Sachtouris | ] + howto_image_file) |
434 | aa82dd5a | Stavros Sachtouris | |
435 | f2ea1314 | Stavros Sachtouris | def _mine_location(self, container_path): |
436 | f2ea1314 | Stavros Sachtouris | uuid = self['uuid'] or self._get_user_id() |
437 | f2ea1314 | Stavros Sachtouris | if self['container']: |
438 | f2ea1314 | Stavros Sachtouris | return uuid, self['container'], container_path |
439 | f2ea1314 | Stavros Sachtouris | container, sep, path = container_path.partition(':')
|
440 | f2ea1314 | Stavros Sachtouris | if not (bool(container) and bool(path)): |
441 | f2ea1314 | Stavros Sachtouris | raiseCLIError( |
442 | f2ea1314 | Stavros Sachtouris | 'Incorrect container-path format', importance=1, details=[ |
443 | f2ea1314 | Stavros Sachtouris | 'Use : to seperate container form path',
|
444 | f2ea1314 | Stavros Sachtouris | ' <container>:<image-path>',
|
445 | f2ea1314 | Stavros Sachtouris | 'OR',
|
446 | f2ea1314 | Stavros Sachtouris | 'Use -C to specifiy a container',
|
447 | f2ea1314 | Stavros Sachtouris | ' -C <container> <image-path>'] + howto_image_file)
|
448 | f2ea1314 | Stavros Sachtouris | |
449 | f2ea1314 | Stavros Sachtouris | return uuid, container, path
|
450 | f2ea1314 | Stavros Sachtouris | |
451 | aa82dd5a | Stavros Sachtouris | @errors.generic.all
|
452 | aa82dd5a | Stavros Sachtouris | @errors.plankton.connection
|
453 | f2ea1314 | Stavros Sachtouris | def _run(self, name, uuid, container, img_path): |
454 | d77e33d4 | Stavros Sachtouris | if self['local_image_path']: |
455 | d77e33d4 | Stavros Sachtouris | with open(self['local_image_path']) as f: |
456 | d77e33d4 | Stavros Sachtouris | pithos = self._get_pithos_client(container)
|
457 | d77e33d4 | Stavros Sachtouris | (pbar, upload_cb) = self._safe_progress_bar('Uploading') |
458 | d77e33d4 | Stavros Sachtouris | if pbar:
|
459 | d77e33d4 | Stavros Sachtouris | hash_bar = pbar.clone() |
460 | d77e33d4 | Stavros Sachtouris | hash_cb = hash_bar.get_generator('Calculating hashes')
|
461 | d77e33d4 | Stavros Sachtouris | pithos.upload_object( |
462 | d77e33d4 | Stavros Sachtouris | img_path, f, |
463 | d77e33d4 | Stavros Sachtouris | hash_cb=hash_cb, upload_cb=upload_cb, |
464 | d77e33d4 | Stavros Sachtouris | container_info_cache=self.container_info_cache)
|
465 | d77e33d4 | Stavros Sachtouris | pbar.finish() |
466 | d77e33d4 | Stavros Sachtouris | |
467 | f2ea1314 | Stavros Sachtouris | location = 'pithos://%s/%s/%s' % (uuid, container, img_path)
|
468 | f2ea1314 | Stavros Sachtouris | (params, properties, new_loc) = self._load_params_from_file(location)
|
469 | f2ea1314 | Stavros Sachtouris | if location != new_loc:
|
470 | f2ea1314 | Stavros Sachtouris | uuid, container, img_path = self._validate_location(new_loc)
|
471 | aa82dd5a | Stavros Sachtouris | self._load_params_from_args(params, properties)
|
472 | aa82dd5a | Stavros Sachtouris | pclient = self._get_pithos_client(container)
|
473 | aa82dd5a | Stavros Sachtouris | |
474 | aa82dd5a | Stavros Sachtouris | #check if metafile exists
|
475 | aa82dd5a | Stavros Sachtouris | meta_path = '%s.meta' % img_path
|
476 | aa82dd5a | Stavros Sachtouris | if pclient and not self['metafile_force']: |
477 | c4aefeaf | Stavros Sachtouris | try:
|
478 | aa82dd5a | Stavros Sachtouris | pclient.get_object_info(meta_path) |
479 | 81b0838d | Stavros Sachtouris | raiseCLIError( |
480 | 81b0838d | Stavros Sachtouris | 'Metadata file %s:%s already exists, abort' % (
|
481 | 81b0838d | Stavros Sachtouris | container, meta_path), |
482 | 81b0838d | Stavros Sachtouris | details=['Registration ABORTED', 'Try -f to overwrite']) |
483 | aa82dd5a | Stavros Sachtouris | except ClientError as ce: |
484 | aa82dd5a | Stavros Sachtouris | if ce.status != 404: |
485 | aa82dd5a | Stavros Sachtouris | raise
|
486 | aa82dd5a | Stavros Sachtouris | |
487 | aa82dd5a | Stavros Sachtouris | #register the image
|
488 | aa82dd5a | Stavros Sachtouris | try:
|
489 | aa82dd5a | Stavros Sachtouris | r = self.client.register(name, location, params, properties)
|
490 | aa82dd5a | Stavros Sachtouris | except ClientError as ce: |
491 | aa82dd5a | Stavros Sachtouris | if ce.status in (400, ): |
492 | c4aefeaf | Stavros Sachtouris | raiseCLIError( |
493 | aa82dd5a | Stavros Sachtouris | ce, 'Nonexistent image file location %s' % location,
|
494 | c4aefeaf | Stavros Sachtouris | details=[ |
495 | aa82dd5a | Stavros Sachtouris | 'Make sure the image file exists'] + howto_image_file)
|
496 | aa82dd5a | Stavros Sachtouris | raise
|
497 | aa82dd5a | Stavros Sachtouris | self._print(r, print_dict)
|
498 | a03ade9e | Stavros Sachtouris | |
499 | aa82dd5a | Stavros Sachtouris | #upload the metadata file
|
500 | 00336c85 | Stavros Sachtouris | if pclient:
|
501 | aa82dd5a | Stavros Sachtouris | try:
|
502 | aa82dd5a | Stavros Sachtouris | meta_headers = pclient.upload_from_string( |
503 | d77e33d4 | Stavros Sachtouris | meta_path, dumps(r, indent=2),
|
504 | d77e33d4 | Stavros Sachtouris | container_info_cache=self.container_info_cache)
|
505 | aa82dd5a | Stavros Sachtouris | except TypeError: |
506 | aa82dd5a | Stavros Sachtouris | print('Failed to dump metafile %s:%s' % (container, meta_path))
|
507 | aa82dd5a | Stavros Sachtouris | return
|
508 | 9553da85 | Stavros Sachtouris | if self['json_output']: |
509 | 9553da85 | Stavros Sachtouris | print_json(dict(
|
510 | aa82dd5a | Stavros Sachtouris | metafile_location='%s:%s' % (container, meta_path),
|
511 | aa82dd5a | Stavros Sachtouris | headers=meta_headers)) |
512 | 9553da85 | Stavros Sachtouris | else:
|
513 | aa82dd5a | Stavros Sachtouris | print('Metadata file uploaded as %s:%s (version %s)' % (
|
514 | aa82dd5a | Stavros Sachtouris | container, meta_path, meta_headers['x-object-version']))
|
515 | 00336c85 | Stavros Sachtouris | |
516 | f2ea1314 | Stavros Sachtouris | def main(self, name, container___image_path): |
517 | a03ade9e | Stavros Sachtouris | super(self.__class__, self)._run() |
518 | f2ea1314 | Stavros Sachtouris | self._run(name, *self._mine_location(container___image_path)) |
519 | 7493ccb6 | Stavros Sachtouris | |
520 | 234954d1 | Stavros Sachtouris | |
521 | d486baec | Stavros Sachtouris | @command(image_cmds)
|
522 | f5f35422 | Stavros Sachtouris | class image_unregister(_init_image, _optional_output_cmd): |
523 | 4a17d307 | Stavros Sachtouris | """Unregister an image (does not delete the image file)"""
|
524 | 4a17d307 | Stavros Sachtouris | |
525 | 4a17d307 | Stavros Sachtouris | @errors.generic.all
|
526 | 4a17d307 | Stavros Sachtouris | @errors.plankton.connection
|
527 | 4a17d307 | Stavros Sachtouris | @errors.plankton.id
|
528 | 4a17d307 | Stavros Sachtouris | def _run(self, image_id): |
529 | f5f35422 | Stavros Sachtouris | self._optional_output(self.client.unregister(image_id)) |
530 | 4a17d307 | Stavros Sachtouris | |
531 | 4a17d307 | Stavros Sachtouris | def main(self, image_id): |
532 | 4a17d307 | Stavros Sachtouris | super(self.__class__, self)._run() |
533 | 4a17d307 | Stavros Sachtouris | self._run(image_id=image_id)
|
534 | 4a17d307 | Stavros Sachtouris | |
535 | 4a17d307 | Stavros Sachtouris | |
536 | 4a17d307 | Stavros Sachtouris | @command(image_cmds)
|
537 | 545c6c29 | Stavros Sachtouris | class image_shared(_init_image, _optional_json): |
538 | f5f35422 | Stavros Sachtouris | """List images shared by a member"""
|
539 | f5f35422 | Stavros Sachtouris | |
540 | 236e7d08 | Stavros Sachtouris | @errors.generic.all
|
541 | 236e7d08 | Stavros Sachtouris | @errors.plankton.connection
|
542 | f5f35422 | Stavros Sachtouris | def _run(self, member): |
543 | 545c6c29 | Stavros Sachtouris | self._print(self.client.list_shared(member), title=('image_id',)) |
544 | 7493ccb6 | Stavros Sachtouris | |
545 | f5f35422 | Stavros Sachtouris | def main(self, member): |
546 | 236e7d08 | Stavros Sachtouris | super(self.__class__, self)._run() |
547 | f5f35422 | Stavros Sachtouris | self._run(member)
|
548 | 236e7d08 | Stavros Sachtouris | |
549 | 234954d1 | Stavros Sachtouris | |
550 | d486baec | Stavros Sachtouris | @command(image_cmds)
|
551 | f5f35422 | Stavros Sachtouris | class image_members(_init_image): |
552 | f5f35422 | Stavros Sachtouris | """Manage members. Members of an image are users who can modify it"""
|
553 | f5f35422 | Stavros Sachtouris | |
554 | f5f35422 | Stavros Sachtouris | |
555 | f5f35422 | Stavros Sachtouris | @command(image_cmds)
|
556 | 545c6c29 | Stavros Sachtouris | class image_members_list(_init_image, _optional_json): |
557 | f5f35422 | Stavros Sachtouris | """List members of an image"""
|
558 | f5f35422 | Stavros Sachtouris | |
559 | 236e7d08 | Stavros Sachtouris | @errors.generic.all
|
560 | 236e7d08 | Stavros Sachtouris | @errors.plankton.connection
|
561 | f5f35422 | Stavros Sachtouris | @errors.plankton.id
|
562 | f5f35422 | Stavros Sachtouris | def _run(self, image_id): |
563 | 545c6c29 | Stavros Sachtouris | self._print(self.client.list_members(image_id), title=('member_id',)) |
564 | 7493ccb6 | Stavros Sachtouris | |
565 | f5f35422 | Stavros Sachtouris | def main(self, image_id): |
566 | 236e7d08 | Stavros Sachtouris | super(self.__class__, self)._run() |
567 | f5f35422 | Stavros Sachtouris | self._run(image_id=image_id)
|
568 | 236e7d08 | Stavros Sachtouris | |
569 | 234954d1 | Stavros Sachtouris | |
570 | d486baec | Stavros Sachtouris | @command(image_cmds)
|
571 | f5f35422 | Stavros Sachtouris | class image_members_add(_init_image, _optional_output_cmd): |
572 | 7493ccb6 | Stavros Sachtouris | """Add a member to an image"""
|
573 | 7493ccb6 | Stavros Sachtouris | |
574 | 236e7d08 | Stavros Sachtouris | @errors.generic.all
|
575 | 236e7d08 | Stavros Sachtouris | @errors.plankton.connection
|
576 | 236e7d08 | Stavros Sachtouris | @errors.plankton.id
|
577 | b04288f7 | Stavros Sachtouris | def _run(self, image_id=None, member=None): |
578 | f5f35422 | Stavros Sachtouris | self._optional_output(self.client.add_member(image_id, member)) |
579 | 236e7d08 | Stavros Sachtouris | |
580 | 236e7d08 | Stavros Sachtouris | def main(self, image_id, member): |
581 | 236e7d08 | Stavros Sachtouris | super(self.__class__, self)._run() |
582 | b04288f7 | Stavros Sachtouris | self._run(image_id=image_id, member=member)
|
583 | 7493ccb6 | Stavros Sachtouris | |
584 | 234954d1 | Stavros Sachtouris | |
585 | d486baec | Stavros Sachtouris | @command(image_cmds)
|
586 | f5f35422 | Stavros Sachtouris | class image_members_delete(_init_image, _optional_output_cmd): |
587 | 7493ccb6 | Stavros Sachtouris | """Remove a member from an image"""
|
588 | 7493ccb6 | Stavros Sachtouris | |
589 | 236e7d08 | Stavros Sachtouris | @errors.generic.all
|
590 | 236e7d08 | Stavros Sachtouris | @errors.plankton.connection
|
591 | 236e7d08 | Stavros Sachtouris | @errors.plankton.id
|
592 | b04288f7 | Stavros Sachtouris | def _run(self, image_id=None, member=None): |
593 | f5f35422 | Stavros Sachtouris | self._optional_output(self.client.remove_member(image_id, member)) |
594 | 236e7d08 | Stavros Sachtouris | |
595 | 236e7d08 | Stavros Sachtouris | def main(self, image_id, member): |
596 | 236e7d08 | Stavros Sachtouris | super(self.__class__, self)._run() |
597 | b04288f7 | Stavros Sachtouris | self._run(image_id=image_id, member=member)
|
598 | 7493ccb6 | Stavros Sachtouris | |
599 | 234954d1 | Stavros Sachtouris | |
600 | d486baec | Stavros Sachtouris | @command(image_cmds)
|
601 | f5f35422 | Stavros Sachtouris | class image_members_set(_init_image, _optional_output_cmd): |
602 | 7493ccb6 | Stavros Sachtouris | """Set the members of an image"""
|
603 | 7493ccb6 | Stavros Sachtouris | |
604 | 236e7d08 | Stavros Sachtouris | @errors.generic.all
|
605 | 236e7d08 | Stavros Sachtouris | @errors.plankton.connection
|
606 | 236e7d08 | Stavros Sachtouris | @errors.plankton.id
|
607 | b04288f7 | Stavros Sachtouris | def _run(self, image_id, members): |
608 | f5f35422 | Stavros Sachtouris | self._optional_output(self.client.set_members(image_id, members)) |
609 | 236e7d08 | Stavros Sachtouris | |
610 | 236e7d08 | Stavros Sachtouris | def main(self, image_id, *members): |
611 | 236e7d08 | Stavros Sachtouris | super(self.__class__, self)._run() |
612 | b04288f7 | Stavros Sachtouris | self._run(image_id=image_id, members=members)
|
613 | f3e94e06 | Stavros Sachtouris | |
614 | f3e94e06 | Stavros Sachtouris | |
615 | 573be34f | Stavros Sachtouris | # Compute Image Commands
|
616 | 573be34f | Stavros Sachtouris | |
617 | 573be34f | Stavros Sachtouris | |
618 | d486baec | Stavros Sachtouris | @command(image_cmds)
|
619 | 8741c407 | Stavros Sachtouris | class image_compute(_init_cyclades): |
620 | a29d2f88 | Stavros Sachtouris | """Cyclades/Compute API image commands"""
|
621 | 8741c407 | Stavros Sachtouris | |
622 | 8741c407 | Stavros Sachtouris | |
623 | 8741c407 | Stavros Sachtouris | @command(image_cmds)
|
624 | 545c6c29 | Stavros Sachtouris | class image_compute_list(_init_cyclades, _optional_json): |
625 | f3e94e06 | Stavros Sachtouris | """List images"""
|
626 | f3e94e06 | Stavros Sachtouris | |
627 | 1ae79e60 | Stavros Sachtouris | arguments = dict(
|
628 | f40f0cb7 | Stavros Sachtouris | detail=FlagArgument('show detailed output', ('-l', '--details')), |
629 | f40f0cb7 | Stavros Sachtouris | limit=IntArgument('limit number listed images', ('-n', '--number')), |
630 | 15142309 | Stavros Sachtouris | more=FlagArgument( |
631 | 15142309 | Stavros Sachtouris | 'output results in pages (-n to set items per page, default 10)',
|
632 | ed9af02c | Stavros Sachtouris | '--more'),
|
633 | 545c6c29 | Stavros Sachtouris | enum=FlagArgument('Enumerate results', '--enumerate') |
634 | 1ae79e60 | Stavros Sachtouris | ) |
635 | f3e94e06 | Stavros Sachtouris | |
636 | 236e7d08 | Stavros Sachtouris | @errors.generic.all
|
637 | 236e7d08 | Stavros Sachtouris | @errors.cyclades.connection
|
638 | 236e7d08 | Stavros Sachtouris | def _run(self): |
639 | 236e7d08 | Stavros Sachtouris | images = self.client.list_images(self['detail']) |
640 | 545c6c29 | Stavros Sachtouris | kwargs = dict(with_enumeration=self['enum']) |
641 | 236e7d08 | Stavros Sachtouris | if self['more']: |
642 | 545c6c29 | Stavros Sachtouris | kwargs['page_size'] = self['limit'] or 10 |
643 | bcef3ac9 | Stavros Sachtouris | elif self['limit']: |
644 | 545c6c29 | Stavros Sachtouris | images = images[:self['limit']] |
645 | 545c6c29 | Stavros Sachtouris | self._print(images, **kwargs)
|
646 | 236e7d08 | Stavros Sachtouris | |
647 | f3e94e06 | Stavros Sachtouris | def main(self): |
648 | 236e7d08 | Stavros Sachtouris | super(self.__class__, self)._run() |
649 | 236e7d08 | Stavros Sachtouris | self._run()
|
650 | f3e94e06 | Stavros Sachtouris | |
651 | 234954d1 | Stavros Sachtouris | |
652 | d486baec | Stavros Sachtouris | @command(image_cmds)
|
653 | 545c6c29 | Stavros Sachtouris | class image_compute_info(_init_cyclades, _optional_json): |
654 | 15142309 | Stavros Sachtouris | """Get detailed information on an image"""
|
655 | f3e94e06 | Stavros Sachtouris | |
656 | 236e7d08 | Stavros Sachtouris | @errors.generic.all
|
657 | 236e7d08 | Stavros Sachtouris | @errors.cyclades.connection
|
658 | 236e7d08 | Stavros Sachtouris | @errors.plankton.id
|
659 | 236e7d08 | Stavros Sachtouris | def _run(self, image_id): |
660 | 236e7d08 | Stavros Sachtouris | image = self.client.get_image_details(image_id)
|
661 | bcef3ac9 | Stavros Sachtouris | self._print(image, print_dict)
|
662 | f3e94e06 | Stavros Sachtouris | |
663 | f3e94e06 | Stavros Sachtouris | def main(self, image_id): |
664 | 236e7d08 | Stavros Sachtouris | super(self.__class__, self)._run() |
665 | b04288f7 | Stavros Sachtouris | self._run(image_id=image_id)
|
666 | f3e94e06 | Stavros Sachtouris | |
667 | 234954d1 | Stavros Sachtouris | |
668 | d486baec | Stavros Sachtouris | @command(image_cmds)
|
669 | f5f35422 | Stavros Sachtouris | class image_compute_delete(_init_cyclades, _optional_output_cmd): |
670 | 24ff0a35 | Stavros Sachtouris | """Delete an image (WARNING: image file is also removed)"""
|
671 | f3e94e06 | Stavros Sachtouris | |
672 | 236e7d08 | Stavros Sachtouris | @errors.generic.all
|
673 | 236e7d08 | Stavros Sachtouris | @errors.cyclades.connection
|
674 | 236e7d08 | Stavros Sachtouris | @errors.plankton.id
|
675 | 236e7d08 | Stavros Sachtouris | def _run(self, image_id): |
676 | f5f35422 | Stavros Sachtouris | self._optional_output(self.client.delete_image(image_id)) |
677 | 236e7d08 | Stavros Sachtouris | |
678 | f3e94e06 | Stavros Sachtouris | def main(self, image_id): |
679 | 236e7d08 | Stavros Sachtouris | super(self.__class__, self)._run() |
680 | b04288f7 | Stavros Sachtouris | self._run(image_id=image_id)
|
681 | f3e94e06 | Stavros Sachtouris | |
682 | 234954d1 | Stavros Sachtouris | |
683 | d486baec | Stavros Sachtouris | @command(image_cmds)
|
684 | 8741c407 | Stavros Sachtouris | class image_compute_properties(_init_cyclades): |
685 | 395fbf9e | Stavros Sachtouris | """Manage properties related to OS installation in an image"""
|
686 | f5f35422 | Stavros Sachtouris | |
687 | f5f35422 | Stavros Sachtouris | |
688 | f5f35422 | Stavros Sachtouris | @command(image_cmds)
|
689 | 545c6c29 | Stavros Sachtouris | class image_compute_properties_list(_init_cyclades, _optional_json): |
690 | f5f35422 | Stavros Sachtouris | """List all image properties"""
|
691 | f5f35422 | Stavros Sachtouris | |
692 | f5f35422 | Stavros Sachtouris | @errors.generic.all
|
693 | f5f35422 | Stavros Sachtouris | @errors.cyclades.connection
|
694 | f5f35422 | Stavros Sachtouris | @errors.plankton.id
|
695 | f5f35422 | Stavros Sachtouris | def _run(self, image_id): |
696 | 545c6c29 | Stavros Sachtouris | self._print(self.client.get_image_metadata(image_id), print_dict) |
697 | f5f35422 | Stavros Sachtouris | |
698 | f5f35422 | Stavros Sachtouris | def main(self, image_id): |
699 | f5f35422 | Stavros Sachtouris | super(self.__class__, self)._run() |
700 | f5f35422 | Stavros Sachtouris | self._run(image_id=image_id)
|
701 | f5f35422 | Stavros Sachtouris | |
702 | f5f35422 | Stavros Sachtouris | |
703 | f5f35422 | Stavros Sachtouris | @command(image_cmds)
|
704 | 545c6c29 | Stavros Sachtouris | class image_compute_properties_get(_init_cyclades, _optional_json): |
705 | f5f35422 | Stavros Sachtouris | """Get an image property"""
|
706 | f5f35422 | Stavros Sachtouris | |
707 | 236e7d08 | Stavros Sachtouris | @errors.generic.all
|
708 | 236e7d08 | Stavros Sachtouris | @errors.cyclades.connection
|
709 | 236e7d08 | Stavros Sachtouris | @errors.plankton.id
|
710 | 236e7d08 | Stavros Sachtouris | @errors.plankton.metadata
|
711 | 236e7d08 | Stavros Sachtouris | def _run(self, image_id, key): |
712 | 545c6c29 | Stavros Sachtouris | self._print(self.client.get_image_metadata(image_id, key), print_dict) |
713 | 236e7d08 | Stavros Sachtouris | |
714 | f5f35422 | Stavros Sachtouris | def main(self, image_id, key): |
715 | 236e7d08 | Stavros Sachtouris | super(self.__class__, self)._run() |
716 | b04288f7 | Stavros Sachtouris | self._run(image_id=image_id, key=key)
|
717 | f3e94e06 | Stavros Sachtouris | |
718 | 234954d1 | Stavros Sachtouris | |
719 | d486baec | Stavros Sachtouris | @command(image_cmds)
|
720 | 545c6c29 | Stavros Sachtouris | class image_compute_properties_add(_init_cyclades, _optional_json): |
721 | f5f35422 | Stavros Sachtouris | """Add a property to an image"""
|
722 | f5f35422 | Stavros Sachtouris | |
723 | 236e7d08 | Stavros Sachtouris | @errors.generic.all
|
724 | 236e7d08 | Stavros Sachtouris | @errors.cyclades.connection
|
725 | 236e7d08 | Stavros Sachtouris | @errors.plankton.id
|
726 | b04288f7 | Stavros Sachtouris | @errors.plankton.metadata
|
727 | 236e7d08 | Stavros Sachtouris | def _run(self, image_id, key, val): |
728 | 545c6c29 | Stavros Sachtouris | self._print(
|
729 | 545c6c29 | Stavros Sachtouris | self.client.create_image_metadata(image_id, key, val), print_dict)
|
730 | 236e7d08 | Stavros Sachtouris | |
731 | f3e94e06 | Stavros Sachtouris | def main(self, image_id, key, val): |
732 | 236e7d08 | Stavros Sachtouris | super(self.__class__, self)._run() |
733 | b04288f7 | Stavros Sachtouris | self._run(image_id=image_id, key=key, val=val)
|
734 | f3e94e06 | Stavros Sachtouris | |
735 | 234954d1 | Stavros Sachtouris | |
736 | d486baec | Stavros Sachtouris | @command(image_cmds)
|
737 | 545c6c29 | Stavros Sachtouris | class image_compute_properties_set(_init_cyclades, _optional_json): |
738 | f5f35422 | Stavros Sachtouris | """Add / update a set of properties for an image
|
739 | f5f35422 | Stavros Sachtouris | proeprties must be given in the form key=value, e.v.
|
740 | f5f35422 | Stavros Sachtouris | /image compute properties set <image-id> key1=val1 key2=val2
|
741 | f5f35422 | Stavros Sachtouris | """
|
742 | f3e94e06 | Stavros Sachtouris | |
743 | 236e7d08 | Stavros Sachtouris | @errors.generic.all
|
744 | 236e7d08 | Stavros Sachtouris | @errors.cyclades.connection
|
745 | 236e7d08 | Stavros Sachtouris | @errors.plankton.id
|
746 | f5f35422 | Stavros Sachtouris | def _run(self, image_id, keyvals): |
747 | 545c6c29 | Stavros Sachtouris | meta = dict()
|
748 | f5f35422 | Stavros Sachtouris | for keyval in keyvals: |
749 | f5f35422 | Stavros Sachtouris | key, val = keyval.split('=')
|
750 | 545c6c29 | Stavros Sachtouris | meta[key] = val |
751 | 545c6c29 | Stavros Sachtouris | self._print(
|
752 | 545c6c29 | Stavros Sachtouris | self.client.update_image_metadata(image_id, **meta), print_dict)
|
753 | f5f35422 | Stavros Sachtouris | |
754 | f5f35422 | Stavros Sachtouris | def main(self, image_id, *key_equals_value): |
755 | 236e7d08 | Stavros Sachtouris | super(self.__class__, self)._run() |
756 | f5f35422 | Stavros Sachtouris | self._run(image_id=image_id, keyvals=key_equals_value)
|
757 | f3e94e06 | Stavros Sachtouris | |
758 | 234954d1 | Stavros Sachtouris | |
759 | d486baec | Stavros Sachtouris | @command(image_cmds)
|
760 | f5f35422 | Stavros Sachtouris | class image_compute_properties_delete(_init_cyclades, _optional_output_cmd): |
761 | f5f35422 | Stavros Sachtouris | """Delete a property from an image"""
|
762 | f3e94e06 | Stavros Sachtouris | |
763 | 236e7d08 | Stavros Sachtouris | @errors.generic.all
|
764 | 236e7d08 | Stavros Sachtouris | @errors.cyclades.connection
|
765 | 236e7d08 | Stavros Sachtouris | @errors.plankton.id
|
766 | 236e7d08 | Stavros Sachtouris | @errors.plankton.metadata
|
767 | 236e7d08 | Stavros Sachtouris | def _run(self, image_id, key): |
768 | f5f35422 | Stavros Sachtouris | self._optional_output(self.client.delete_image_metadata(image_id, key)) |
769 | 236e7d08 | Stavros Sachtouris | |
770 | f3e94e06 | Stavros Sachtouris | def main(self, image_id, key): |
771 | 236e7d08 | Stavros Sachtouris | super(self.__class__, self)._run() |
772 | b04288f7 | Stavros Sachtouris | self._run(image_id=image_id, key=key) |