Commit d77e33d4 authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Add upload local image abilities to register

Refs: #4206

New register args:

parent a0a61f30
......@@ -32,7 +32,7 @@
# or implied, of GRNET S.A.command
from json import load, dumps
from os.path import abspath
from os import path
from logging import getLogger
from kamaki.cli import command
......@@ -43,7 +43,7 @@ from kamaki.clients.pithos import PithosClient
from kamaki.clients.astakos import AstakosClient
from kamaki.clients import ClientError
from kamaki.cli.argument import FlagArgument, ValueArgument, KeyValueArgument
from kamaki.cli.argument import IntArgument
from kamaki.cli.argument import IntArgument, ProgressBarArgument
from kamaki.cli.commands.cyclades import _init_cyclades
from kamaki.cli.errors import raiseCLIError, CLIBaseUrlError
from kamaki.cli.commands import _command_init, errors, addLogSettings
......@@ -142,7 +142,7 @@ def _load_image_meta(filepath):
:raises AssertionError: Valid json but invalid image properties dict
with open(abspath(filepath)) as f:
with open(path.abspath(filepath)) as f:
meta_dict = load(f)
return _validate_image_meta(meta_dict)
......@@ -283,6 +283,8 @@ class image_meta(_init_image, _optional_json):
class image_register(_init_image, _optional_json):
"""(Re)Register an image"""
container_info_cache = {}
arguments = dict(
checksum=ValueArgument('set image checksum', '--checksum'),
......@@ -308,7 +310,13 @@ class image_register(_init_image, _optional_json):
'Pithos+ container containing the image file',
('-C', '--container')),
uuid=ValueArgument('Custom user uuid', '--uuid')
uuid=ValueArgument('Custom user uuid', '--uuid'),
'Local image file path to upload and register '
'(still need target file in the form container:remote-path )',
'Do not use progress bar', '--no-progress-bar', default=False)
def _get_user_id(self):
......@@ -339,7 +347,8 @@ class image_register(_init_image, _optional_json):
def _store_remote_metafile(self, pclient, remote_path, metadata):
return pclient.upload_from_string(
remote_path, _validate_image_meta(metadata, return_str=True))
remote_path, _validate_image_meta(metadata, return_str=True),
def _load_params_from_file(self, location):
params, properties = dict(), dict()
......@@ -413,6 +422,19 @@ class image_register(_init_image, _optional_json):
def _run(self, name, uuid, container, img_path):
if self['local_image_path']:
with open(self['local_image_path']) as f:
pithos = self._get_pithos_client(container)
(pbar, upload_cb) = self._safe_progress_bar('Uploading')
if pbar:
hash_bar = pbar.clone()
hash_cb = hash_bar.get_generator('Calculating hashes')
img_path, f,
hash_cb=hash_cb, upload_cb=upload_cb,
location = 'pithos://%s/%s/%s' % (uuid, container, img_path)
(params, properties, new_loc) = self._load_params_from_file(location)
if location != new_loc:
......@@ -449,7 +471,8 @@ class image_register(_init_image, _optional_json):
if pclient:
meta_headers = pclient.upload_from_string(
meta_path, dumps(r, indent=2))
meta_path, dumps(r, indent=2),
except TypeError:
print('Failed to dump metafile %s:%s' % (container, meta_path))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment