Revision 3f36ba1d

b/kamaki/cli/commands/image.py
446 446
            'Load metadata from a json-formated file <img-file>.meta :'
447 447
            '{"key1": "val1", "key2": "val2", ..., "properties: {...}"}',
448 448
            ('--metafile')),
449
        metafile_force=FlagArgument(
450
            'Overide remote metadata file', ('-f', '--force')),
449
        force_upload=FlagArgument(
450
            'Overwrite remote files (image file, metadata file)',
451
            ('-f', '--force')),
451 452
        no_metafile_upload=FlagArgument(
452 453
            'Do not store metadata in remote meta file',
453 454
            ('--no-metafile-upload')),
......
471 472
    required = ('name', 'pithos_location')
472 473

  
473 474
    def _get_pithos_client(self, locator):
474
        if self['no_metafile_upload']:
475
            return None
476 475
        ptoken = self.client.token
477 476
        if getattr(self, 'auth_base', False):
478 477
            pithos_endpoints = self.auth_base.get_service_endpoints(
......
518 517
        for k, v in self['properties'].items():
519 518
            properties[k.upper().replace('-', '_')] = v
520 519

  
520
    def _assert_remote_file_not_exist(self, pithos, path):
521
        if pithos and not self['force_upload']:
522
            try:
523
                pithos.get_object_info(path)
524
                raiseCLIError(
525
                    'Remote file /%s/%s already exists' % (
526
                        pithos.container, path),
527
                    importance=2,
528
                    details=[
529
                        'Registration ABORTED',
530
                        'Use %s to force upload' % self.arguments[
531
                            'force_upload'].lvalue])
532
            except ClientError as ce:
533
                if ce.status != 404:
534
                    raise
535

  
521 536
    @errors.generic.all
522 537
    @errors.plankton.connection
523 538
    def _run(self, name, location):
524
        locator = self.arguments['pithos_location']
539
        locator, pithos = self.arguments['pithos_location'], None
525 540
        if self['local_image_path']:
526 541
            with open(self['local_image_path']) as f:
527 542
                pithos = self._get_pithos_client(locator)
543
                self._assert_remote_file_not_exist(pithos, locator.path)
528 544
                (pbar, upload_cb) = self._safe_progress_bar('Uploading')
529 545
                if pbar:
530 546
                    hash_bar = pbar.clone()
......
539 555
        if location != new_loc:
540 556
            locator.value = new_loc
541 557
        self._load_params_from_args(params, properties)
542
        pclient = self._get_pithos_client(locator)
543 558

  
544
        #check if metafile exists
545
        meta_path = '%s.meta' % locator.path
546
        if pclient and not self['metafile_force']:
547
            try:
548
                pclient.get_object_info(meta_path)
549
                raiseCLIError(
550
                    'Metadata file /%s/%s already exists, abort' % (
551
                        locator.container, meta_path),
552
                    details=['Registration ABORTED', 'Try -f to overwrite'])
553
            except ClientError as ce:
554
                if ce.status != 404:
555
                    raise
559
        if not self['no_metafile_upload']:
560
            #check if metafile exists
561
            pithos = pithos or self._get_pithos_client(locator)
562
            meta_path = '%s.meta' % locator.path
563
            self._assert_remote_file_not_exist(pithos, meta_path)
556 564

  
557 565
        #register the image
558 566
        try:
......
570 578
        self._print(r, self.print_dict)
571 579

  
572 580
        #upload the metadata file
573
        if pclient:
581
        if not self['no_metafile_upload']:
574 582
            try:
575
                meta_headers = pclient.upload_from_string(
583
                meta_headers = pithos.upload_from_string(
576 584
                    meta_path, dumps(r, indent=2),
577 585
                    container_info_cache=self.container_info_cache)
578 586
            except TypeError:

Also available in: Unified diff