Revision 921355f8 snf-app/synnefo/tools/admin.py

b/snf-app/synnefo/tools/admin.py
56 56
from synnefo.util.dictconfig import dictConfig
57 57

  
58 58

  
59
SYSTEM_USER = 'okeanos'
60

  
61

  
62 59
def get_user(uid):
63 60
    try:
64 61
        uid = int(uid)
......
299 296
        parser.add_option('-p', action='store_true', dest='pithos',
300 297
                default=False, help='show images stored in Pithos')
301 298
        parser.add_option('--user', dest='user',
302
                default=SYSTEM_USER,
299
                default=settings.SYSTEM_IMAGES_OWNER,
303 300
                metavar='USER',
304 301
                help='list images accessible to USER')
305 302
    
......
334 331
class RegisterImage(Command):
335 332
    group = 'image'
336 333
    name = 'register'
337
    syntax = '<name> <backend id> <format>'
334
    syntax = '<name> <Backend ID or Pithos URL> <disk format>'
338 335
    description = 'register an image'
339 336
    
340 337
    def add_options(self, parser):
341 338
        parser.add_option('--meta', dest='meta', action='append',
342
                            metavar='KEY=VAL',
343
                            help='add metadata (can be used multiple times)')
339
                metavar='KEY=VAL',
340
                help='add metadata (can be used multiple times)')
344 341
        parser.add_option('--public', action='store_true', dest='public',
345
                            default=False, help='make image public')
342
                default=False, help='make image public')
346 343
        parser.add_option('-u', dest='uid', metavar='UID',
347
                            help='assign image to user with id UID')
344
                help='assign image to user with id UID')
348 345
    
349 346
    def main(self, name, backend_id, format):
347
        if backend_id.startswith('pithos://'):
348
            return self.main_pithos(name, backend_id, format)
349
        
350 350
        formats = [x[0] for x in models.Image.FORMATS]
351 351
        if format not in formats:
352 352
            valid = ', '.join(formats)
......
377 377
                    print 'WARNING: Ignoring meta', m
378 378
        
379 379
        print_item(image)
380
    
381
    def main_pithos(self, name, url, disk_format):
382
        if disk_format not in settings.ALLOWED_DISK_FORMATS:
383
            print 'Invalid disk format'
384
            return
385
        
386
        params = {
387
            'disk_format': disk_format,
388
            'is_public': self.public,
389
            'properties': {}}
390
        
391
        if self.meta:
392
            for m in self.meta:
393
                key, sep, val = m.partition('=')
394
                if key and val:
395
                    params['properties'][key] = val
396
                else:
397
                    print 'WARNING: Ignoring meta', m
398
        
399
        backend = ImageBackend(self.uid or settings.SYSTEM_IMAGES_OWNER)
400
        backend.register(name, url, params)
401
        backend.close()
380 402

  
381 403

  
382 404
class UploadImage(Command):
......
401 423
                metavar='KEY=VAL',
402 424
                help='add metadata (can be used multiple times)')
403 425
        parser.add_option('--owner', dest='owner',
404
                default=SYSTEM_USER,
426
                default=settings.SYSTEM_IMAGES_OWNER,
405 427
                metavar='USER',
406 428
                help='set owner to USER')
407 429
        parser.add_option('--public', action='store_true', dest='public',
......
456 478
        parser.add_option('--public', action='store_true', dest='public',
457 479
                help='make image public')
458 480
        parser.add_option('--user', dest='user',
459
                default=SYSTEM_USER,
481
                default=settings.SYSTEM_IMAGES_OWNER,
460 482
                metavar='USER',
461
                help='assign image to USER')
483
                help='connect as USER')
462 484
    
463 485
    def main(self, image_id):
464 486
        backend = ImageBackend(self.user)
......
558 580
    syntax = '<image id> [key[=val]]'
559 581
    description = 'get and manipulate image metadata'
560 582
    
583
    def add_options(self, parser):
584
        parser.add_option('--user', dest='user',
585
                default=settings.SYSTEM_IMAGES_OWNER,
586
                metavar='USER',
587
                help='connect as USER')
588

  
561 589
    def main(self, image_id, arg=''):
590
        if not image_id.isdigit():
591
            return self.main_pithos(image_id, arg)
592
        
562 593
        try:
563 594
            image = models.Image.objects.get(id=image_id)
564 595
        except:
......
595 626
            # Delete if val is empty
596 627
            if meta:
597 628
                meta.delete()
629
    
630
    def main_pithos(self, image_id, arg=''):
631
        backend = ImageBackend(self.user)
632
                
633
        try:
634
            image = backend.get_meta(image_id)
635
            if not image:
636
                print 'Image not found'
637
                return
638
            
639
            key, sep, val = arg.partition('=')
640
            if not sep:
641
                val = None
642
            
643
            properties = image.get('properties', {})
644
            
645
            if not key:
646
                print_dict(properties)
647
                return
648
            
649
            if val is None:
650
                if key in properties:
651
                    print_dict({key: properties[key]})
652
                return
653
            
654
            if val:
655
                properties[key] = val        
656
                params = {'properties': properties}
657
                backend.update(image_id, params)
658
        finally:
659
            backend.close()
598 660

  
599 661

  
600 662
# Flavor commands

Also available in: Unified diff