Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / commands / image_cli.py @ c5b9380c

History | View | Annotate | Download (13.2 kB)

1
# Copyright 2012 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.command
33

    
34
from kamaki.cli import command
35
from kamaki.cli.command_tree import CommandTree
36
from kamaki.cli.utils import print_dict, print_items
37
from kamaki.clients.image import ImageClient
38
from kamaki.cli.argument import FlagArgument, ValueArgument, KeyValueArgument
39
from kamaki.cli.argument import IntArgument
40
from kamaki.cli.commands.cyclades_cli import _init_cyclades
41
from kamaki.cli.commands import _command_init, errors
42

    
43

    
44
image_cmds = CommandTree('image', 'Plankton (and Compute) Image API commands')
45
_commands = [image_cmds]
46

    
47

    
48
about_image_id = [
49
    'To see a list of available image ids: /image list']
50

    
51

    
52
class _init_image(_command_init):
53
    @errors.generic.all
54
    def _run(self):
55
        token = self.config.get('image', 'token')\
56
            or self.config.get('compute', 'token')\
57
            or self.config.get('global', 'token')
58
        base_url = self.config.get('image', 'url')\
59
            or self.config.get('compute', 'url')\
60
            or self.config.get('global', 'url')
61
        self.client = ImageClient(base_url=base_url, token=token)
62
        self._update_low_level_log()
63
        self._update_max_threads()
64

    
65
    def main(self):
66
        self._run()
67

    
68

    
69
# Plankton Image Commands
70

    
71

    
72
@command(image_cmds)
73
class image_list(_init_image):
74
    """List images accessible by user"""
75

    
76
    arguments = dict(
77
        detail=FlagArgument('show detailed output', ('-l', '--details')),
78
        container_format=ValueArgument(
79
            'filter by container format',
80
            '--container-format'),
81
        disk_format=ValueArgument('filter by disk format', '--disk-format'),
82
        name=ValueArgument('filter by name', '--name'),
83
        size_min=IntArgument('filter by minimum size', '--size-min'),
84
        size_max=IntArgument('filter by maximum size', '--size-max'),
85
        status=ValueArgument('filter by status', '--status'),
86
        order=ValueArgument(
87
            'order by FIELD ( - to reverse order)',
88
            '--order',
89
            default=''),
90
        limit=IntArgument('limit number of listed images', ('-n', '--number')),
91
        more=FlagArgument(
92
            'output results in pages (-n to set items per page, default 10)',
93
            '--more')
94
    )
95

    
96
    @errors.generic.all
97
    @errors.cyclades.connection
98
    def _run(self):
99
        super(self.__class__, self)._run()
100
        filters = {}
101
        for arg in set([
102
                'container_format',
103
                'disk_format',
104
                'name',
105
                'size_min',
106
                'size_max',
107
                'status']).intersection(self.arguments):
108
            filters[arg] = self[arg]
109

    
110
        order = self['order']
111
        detail = self['detail']
112
        images = self.client.list_public(detail, filters, order)
113
        if self['more']:
114
            print_items(
115
                images,
116
                title=('name',),
117
                with_enumeration=True,
118
                page_size=self['limit'] or 10)
119
        elif self['limit']:
120
            print_items(
121
                images[:self['limit']],
122
                title=('name',),
123
                with_enumeration=True)
124
        else:
125
            print_items(images, title=('name',), with_enumeration=True)
126

    
127
    def main(self):
128
        super(self.__class__, self)._run()
129
        self._run()
130

    
131

    
132
@command(image_cmds)
133
class image_meta(_init_image):
134
    """Get image metadata
135
    Image metadata include:
136
    - image file information (location, size, etc.)
137
    - image information (id, name, etc.)
138
    - image os properties (os, fs, etc.)
139
    """
140

    
141
    @errors.generic.all
142
    @errors.plankton.connection
143
    @errors.plankton.id
144
    def _run(self, image_id):
145
        image = self.client.get_meta(image_id)
146
        print_dict(image)
147

    
148
    def main(self, image_id):
149
        super(self.__class__, self)._run()
150
        self._run(image_id=image_id)
151

    
152

    
153
@command(image_cmds)
154
class image_register(_init_image):
155
    """(Re)Register an image"""
156

    
157
    arguments = dict(
158
        checksum=ValueArgument('set image checksum', '--checksum'),
159
        container_format=ValueArgument(
160
            'set container format',
161
            '--container-format'),
162
        disk_format=ValueArgument('set disk format', '--disk-format'),
163
        #id=ValueArgument('set image ID', '--id'),
164
        owner=ValueArgument('set image owner (admin only)', '--owner'),
165
        properties=KeyValueArgument(
166
            'add property in key=value form (can be repeated)',
167
            ('-p, --property')),
168
        is_public=FlagArgument('mark image as public', '--public'),
169
        size=IntArgument('set image size', '--size'),
170
        update=FlagArgument(
171
            'update existing image properties',
172
            ('-u', '--update'))
173
    )
174

    
175
    @errors.generic.all
176
    @errors.plankton.connection
177
    def _run(self, name, location):
178
        if not location.startswith('pithos://'):
179
            account = self.config.get('store', 'account') \
180
                or self.config.get('global', 'account')
181
            assert account, 'No user account provided'
182
            if account[-1] == '/':
183
                account = account[:-1]
184
            container = self.config.get('store', 'container') \
185
                or self.config.get('global', 'container')
186
            if not container:
187
                location = 'pithos://%s/%s' % (account, location)
188
            else:
189
                location = 'pithos://%s/%s/%s' % (account, container, location)
190

    
191
        params = {}
192
        for key in set([
193
                'checksum',
194
                'container_format',
195
                'disk_format',
196
                'owner',
197
                'size',
198
                'is_public']).intersection(self.arguments):
199
            params[key] = self[key]
200

    
201
            properties = self['properties']
202
        if self['update']:
203
            self.client.reregister(location, name, params, properties)
204
        else:
205
            self.client.register(name, location, params, properties)
206

    
207
    def main(self, name, location):
208
        super(self.__class__, self)._run()
209
        self._run(name, location)
210

    
211

    
212
@command(image_cmds)
213
class image_members(_init_image):
214
    """Get image members"""
215

    
216
    @errors.generic.all
217
    @errors.plankton.connection
218
    @errors.plankton.id
219
    def _run(self, image_id):
220
        members = self.client.list_members(image_id)
221
        print_items(members)
222

    
223
    def main(self, image_id):
224
        super(self.__class__, self)._run()
225
        self._run(image_id=image_id)
226

    
227

    
228
@command(image_cmds)
229
class image_shared(_init_image):
230
    """List images shared by a member"""
231

    
232
    @errors.generic.all
233
    @errors.plankton.connection
234
    def _run(self, member):
235
        images = self.client.list_shared(member)
236
        print_items(images)
237

    
238
    def main(self, member):
239
        super(self.__class__, self)._run()
240
        self._run(member)
241

    
242

    
243
@command(image_cmds)
244
class image_addmember(_init_image):
245
    """Add a member to an image"""
246

    
247
    @errors.generic.all
248
    @errors.plankton.connection
249
    @errors.plankton.id
250
    def _run(self, image_id=None, member=None):
251
            self.client.add_member(image_id, member)
252

    
253
    def main(self, image_id, member):
254
        super(self.__class__, self)._run()
255
        self._run(image_id=image_id, member=member)
256

    
257

    
258
@command(image_cmds)
259
class image_delmember(_init_image):
260
    """Remove a member from an image"""
261

    
262
    @errors.generic.all
263
    @errors.plankton.connection
264
    @errors.plankton.id
265
    def _run(self, image_id=None, member=None):
266
            self.client.remove_member(image_id, member)
267

    
268
    def main(self, image_id, member):
269
        super(self.__class__, self)._run()
270
        self._run(image_id=image_id, member=member)
271

    
272

    
273
@command(image_cmds)
274
class image_setmembers(_init_image):
275
    """Set the members of an image"""
276

    
277
    @errors.generic.all
278
    @errors.plankton.connection
279
    @errors.plankton.id
280
    def _run(self, image_id, members):
281
            self.client.set_members(image_id, members)
282

    
283
    def main(self, image_id, *members):
284
        super(self.__class__, self)._run()
285
        self._run(image_id=image_id, members=members)
286

    
287

    
288
# Compute Image Commands
289

    
290

    
291
@command(image_cmds)
292
class image_compute(_init_cyclades):
293
    """Compute Image API commands"""
294

    
295

    
296
@command(image_cmds)
297
class image_compute_list(_init_cyclades):
298
    """List images"""
299

    
300
    arguments = dict(
301
        detail=FlagArgument('show detailed output', ('-l', '--details')),
302
        limit=IntArgument('limit number listed images', ('-n', '--number')),
303
        more=FlagArgument(
304
            'output results in pages (-n to set items per page, default 10)',
305
            '--more')
306
    )
307

    
308
    def _make_results_pretty(self, images):
309
        for img in images:
310
            if 'metadata' in img:
311
                img['metadata'] = img['metadata']['values']
312

    
313
    @errors.generic.all
314
    @errors.cyclades.connection
315
    def _run(self):
316
        images = self.client.list_images(self['detail'])
317
        if self['detail']:
318
            self._make_results_pretty(images)
319
        if self['more']:
320
            print_items(images, page_size=self['limit'] or 10)
321
        else:
322
            print_items(images[:self['limit']])
323

    
324
    def main(self):
325
        super(self.__class__, self)._run()
326
        self._run()
327

    
328

    
329
@command(image_cmds)
330
class image_compute_info(_init_cyclades):
331
    """Get detailed information on an image"""
332

    
333
    @errors.generic.all
334
    @errors.cyclades.connection
335
    @errors.plankton.id
336
    def _run(self, image_id):
337
        image = self.client.get_image_details(image_id)
338
        if 'metadata' in image:
339
            image['metadata'] = image['metadata']['values']
340
        print_dict(image)
341

    
342
    def main(self, image_id):
343
        super(self.__class__, self)._run()
344
        self._run(image_id=image_id)
345

    
346

    
347
@command(image_cmds)
348
class image_compute_delete(_init_cyclades):
349
    """Delete an image (WARNING: image file is also removed)"""
350

    
351
    @errors.generic.all
352
    @errors.cyclades.connection
353
    @errors.plankton.id
354
    def _run(self, image_id):
355
        self.client.delete_image(image_id)
356

    
357
    def main(self, image_id):
358
        super(self.__class__, self)._run()
359
        self._run(image_id=image_id)
360

    
361

    
362
@command(image_cmds)
363
class image_compute_properties(_init_cyclades):
364
    """Get properties related to OS installation in an image"""
365

    
366
    @errors.generic.all
367
    @errors.cyclades.connection
368
    @errors.plankton.id
369
    @errors.plankton.metadata
370
    def _run(self, image_id, key):
371
        r = self.client.get_image_metadata(image_id, key)
372
        print_dict(r)
373

    
374
    def main(self, image_id, key=''):
375
        super(self.__class__, self)._run()
376
        self._run(image_id=image_id, key=key)
377

    
378

    
379
@command(image_cmds)
380
class image_addproperty(_init_cyclades):
381
    """Add an OS-related property to an image"""
382

    
383
    @errors.generic.all
384
    @errors.cyclades.connection
385
    @errors.plankton.id
386
    @errors.plankton.metadata
387
    def _run(self, image_id, key, val):
388
        r = self.client.create_image_metadata(image_id, key, val)
389
        print_dict(r)
390

    
391
    def main(self, image_id, key, val):
392
        super(self.__class__, self)._run()
393
        self._run(image_id=image_id, key=key, val=val)
394

    
395

    
396
@command(image_cmds)
397
class image_compute_setproperty(_init_cyclades):
398
    """Update an existing property in an image"""
399

    
400
    @errors.generic.all
401
    @errors.cyclades.connection
402
    @errors.plankton.id
403
    @errors.plankton.metadata
404
    def _run(self, image_id, key, val):
405
        metadata = {key: val}
406
        r = self.client.update_image_metadata(image_id, **metadata)
407
        print_dict(r)
408

    
409
    def main(self, image_id, key, val):
410
        super(self.__class__, self)._run()
411
        self._run(image_id=image_id, key=key, val=val)
412

    
413

    
414
@command(image_cmds)
415
class image_compute_delproperty(_init_cyclades):
416
    """Delete a property of an image"""
417

    
418
    @errors.generic.all
419
    @errors.cyclades.connection
420
    @errors.plankton.id
421
    @errors.plankton.metadata
422
    def _run(self, image_id, key):
423
        self.client.delete_image_metadata(image_id, key)
424

    
425
    def main(self, image_id, key):
426
        super(self.__class__, self)._run()
427
        self._run(image_id=image_id, key=key)