Files
deb-murano/murano/cmd/manage.py
Stan Lagun 35979b1e02 Preliminary support for HOT packages
Adds support for packages consisting of single HOT template.
Most of HOT features are supported with exception of
environments, attachments and JSON parameter type.

Implements: blueprint hot-packages

Change-Id: I927af0e96f1613e8843ac47844e9c19fa00fdaa6
2014-05-30 12:27:01 +04:00

184 lines
5.7 KiB
Python

# Copyright (c) 2014 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
CLI interface for murano management.
"""
import sys
import traceback
from oslo.config import cfg
import murano
from murano.common import consts
from murano.db.catalog import api as db_catalog_api
from murano.db import session as db_session
from murano.openstack.common.db import exception as db_exception
from murano.openstack.common import log as logging
from murano.packages import load_utils
CONF = cfg.CONF
LOG = logging.getLogger(__name__)
# TODO(ruhe): proper error handling
def do_db_sync():
"""
Place a database under migration control and upgrade,
creating first if necessary.
"""
db_session.db_sync()
class AdminContext(object):
def __init__(self):
self.is_admin = True
def _do_import_package(_dir, categories, update=False):
LOG.info("Going to import Murano package from {0}".format(_dir))
pkg = load_utils.load_from_dir(_dir)
LOG.info("Checking for existing")
existing = db_catalog_api.package_search(
{'fqn': pkg.full_name},
AdminContext())
if existing:
existing_pkg = existing[0]
if update:
LOG.info("Deleting existing package {0}".format(existing_pkg.id))
db_catalog_api.package_delete(existing_pkg.id, AdminContext())
else:
LOG.error("Package '{0}' exists ({1}). Use --update.".format(
pkg.full_name,
existing_pkg.id))
return
package = {
'fully_qualified_name': pkg.full_name,
'type': pkg.package_type,
'author': pkg.author,
'name': pkg.display_name,
'description': pkg.description,
# note: we explicitly mark all the imported packages as public,
# until a parameter added to control visibility scope of a package
'is_public': True,
'tags': pkg.tags,
'logo': pkg.logo,
'ui_definition': pkg.raw_ui,
'class_definitions': pkg.classes,
'archive': pkg.blob,
'categories': categories or []
}
# note(ruhe): the second parameter is tenant_id
# it is a required field in the DB, that's why we pass an empty string
result = db_catalog_api.package_upload(package, '')
LOG.info("Finished import of package {0}".format(result.id))
# TODO(ruhe): proper error handling
def do_import_package():
"""
Import Murano package from local directory.
"""
_do_import_package(
CONF.command.directory,
CONF.command.categories,
CONF.command.update)
def do_list_categories():
categories = db_catalog_api.category_get_names()
if categories:
print(">> Murano package categories:")
for c in categories:
print("* {0}".format(c))
else:
print("No categories were found")
def do_add_category():
category_name = CONF.command.category_name
try:
db_catalog_api.category_add(category_name)
print(">> Successfully added category {0}".format(category_name))
except db_exception.DBDuplicateEntry:
print(">> ERROR: Category '{0}' already exists".format(category_name))
def add_command_parsers(subparsers):
parser = subparsers.add_parser('db-sync')
parser.set_defaults(func=do_db_sync)
parser.add_argument('version', nargs='?')
parser.add_argument('current_version', nargs='?')
parser = subparsers.add_parser('import-package')
parser.set_defaults(func=do_import_package)
parser.add_argument('directory',
help='A directory with Murano package.')
parser.add_argument('-u', '--update',
action="store_true",
default=False,
help='If a package already exists, delete and update')
parser.add_argument('-c', '--categories',
choices=consts.CATEGORIES,
nargs='*',
help='An optional list of categories this package '
'to be assigned to.')
parser = subparsers.add_parser('category-list')
parser.set_defaults(func=do_list_categories)
parser = subparsers.add_parser('category-add')
parser.set_defaults(func=do_add_category)
parser.add_argument('category_name',
help='Name of the new category.')
command_opt = cfg.SubCommandOpt('command',
title='Commands',
help='Show available commands.',
handler=add_command_parsers)
def main():
CONF.register_cli_opt(command_opt)
try:
default_config_files = cfg.find_config_files('murano', 'murano')
CONF(sys.argv[1:], project='murano', prog='murano-manage',
version=murano.__version__,
default_config_files=default_config_files)
logging.setup("murano")
except RuntimeError as e:
LOG.error("failed to initialize murano-manage: %s" % e)
sys.exit("ERROR: %s" % e)
try:
CONF.command.func()
except Exception as e:
tb = traceback.format_exc()
err_msg = "murano-manage command failed: {0}\n{1}".format(e, tb)
LOG.error(err_msg)
sys.exit(err_msg)
if __name__ == '__main__':
main()