Allow importing bundles of packages

Add bundle-import command to allow importing
bundles of packages from local files,
urls or base murano repository.

Change-Id: Ia51fdb242f5730af69dc6825fd48bc7e2e695968
Partially implements: blueprint muranoclient-marketplace-support
This commit is contained in:
Kirill Zaitsev 2015-02-26 18:29:09 +03:00 committed by Kirill
parent cc9dc07353
commit 45ea1baf98
2 changed files with 75 additions and 10 deletions

View File

@ -219,15 +219,7 @@ def to_url(filename, base_url, version='', path='/', extension=''):
return urlparse.urljoin(base_url, path + filename + version + extension)
class Package(object):
"""Represents murano package contents."""
@staticmethod
def fromFile(file_obj):
if not isinstance(file_obj, File):
file_obj = File(file_obj)
return Package(file_obj)
class FileWrapperMixin(object):
def __init__(self, file_wrapper):
self.file_wrapper = file_wrapper
try:
@ -249,6 +241,50 @@ class Package(object):
self.close()
class Package(FileWrapperMixin):
"""Represents murano package contents."""
@staticmethod
def fromFile(file_obj):
if not isinstance(file_obj, File):
file_obj = File(file_obj)
return Package(file_obj)
class Bundle(FileWrapperMixin):
"""Represents murano bundle contents."""
@staticmethod
def fromFile(file_obj):
if not isinstance(file_obj, File):
file_obj = File(file_obj)
return Bundle(file_obj)
def packages(self):
"""Returns a generator, yielding packages found in the bundle."""
self._file.seek(0)
bundle = None
try:
bundle = jsonutils.load(self._file)
except ValueError:
pass
if bundle is None:
try:
bundle = yaml.load(self._file)
except yaml.error.YAMLError:
pass
if bundle is None:
raise ValueError("Can't parse bundle contents")
if 'Packages' not in bundle:
return
for package in bundle['Packages']:
if 'Name' not in package:
continue
yield package
class YaqlExpression(object):
def __init__(self, expression):
self._expression = str(expression)

View File

@ -199,7 +199,7 @@ def do_package_delete(mc, args):
help='Make package available for user from other tenants')
@utils.arg('--version', default='',
help='Version of the package to use from repository')
def do_package_import(mc, args):
def do_package_import(mc, args, list_packages=True):
"""Import a package.
`FILE` can be either a path to a zip file, url or a FQPN.
`categories` could be separated by a comma
@ -216,6 +216,35 @@ def do_package_import(mc, args):
mc.packages.create(data, ((args.filename, filename),),
version=args.version,
murano_repo_url=args.murano_repo_url)
if list_packages:
do_package_list(mc)
@utils.arg('filename', metavar='<FILE>',
help='Bundle url, bundle name, or path to the bundle file')
@utils.arg('--is-public', action='store_true', default=False,
help='Make packages available to users from other tenants')
def do_bundle_import(mc, args):
"""Import a bundle.
`FILE` can be either a path to a zip file, url or name from repo.
"""
if os.path.isfile(args.filename):
bundle_file = utils.Bundle.fromFile(args.filename)
else:
bundle_file = utils.Bundle.fromFile(utils.to_url(
args.filename, base_url=args.murano_repo_url, path='/bundles/'))
data = {"is_public": args.is_public}
for package in bundle_file.packages():
try:
mc.packages.create(data, ((package['Name'], package['Name']),),
version=package.get('Version'),
murano_repo_url=args.murano_repo_url)
except Exception as e:
print("Error '{0}' occurred during import of {1} package".format(
e, package['Name']))
do_package_list(mc)