Example #1
0
    def execute(self):
        templates = self.args['<templates>']
        try:
            if not login_lib.check_env():
                print('you are not authorized.')
            else:
                login_lib.load_env_file()
                libs_dir = os.path.dirname(os.path.realpath(__file__))
                # os.chdir("{}/templates/{}".format(libs_dir,templates))
                template_file = "{}/templates/{}/index.yaml".format(
                    libs_dir, templates)
                template_env = "{}/templates/{}/env.yaml".format(
                    libs_dir, templates)
                heat_url = 'https://heat.wjv-1.neo.id:8004/v1/%s' % os.environ.get(
                    "OS_PROJECT_ID")

                # template = open(templates_index)
                env = open(template_env)
                files, template = template_utils.process_template_path(
                    template_file)

                heat = heat_client.Client('1',
                                          endpoint=heat_url,
                                          token=os.environ.get("OS_TOKEN"))

                heat.stacks.create(stack_name=templates,
                                   template=template,
                                   environment=env.read(),
                                   files=files)
        except Exception as e:
            print(e)
        else:
            pass
        finally:
            pass
Example #2
0
def do_create(initialize, session=None):
    try:
        heat = get_heat_client(session)
        for deploy in initialize:
            deploy_init_file = "{}/init.yml".format(deploy["dir"])
            deploy_file = utils.yaml_parser(deploy_init_file)["create"]
            """ template """
            deploy_template = "{}/{}".format(deploy["dir"], deploy_file)
            deploy_name = deploy["project"]
            files, template = template_utils.process_template_path(
                deploy_template)
            """Create Stack"""
            utils.log_info("Create {} stack....".format(deploy["project"]))
            if not deploy["env_file"]:
                heat.stacks.create(stack_name=deploy_name,
                                   template=template,
                                   files=files)
            else:
                deploy_env_file = open(deploy["env_file"])
                heat.stacks.create(
                    stack_name=deploy_name,
                    template=template,
                    environment=deploy_env_file.read(),
                    files=files,
                )
            if len(initialize) > 0:
                time.sleep(8)
    except Exception as e:
        utils.log_err(e)
    else:
        pass
    finally:
        pass
Example #3
0
def create_stack(heat_client,
                 stack_name,
                 template,
                 parameters,
                 environment=None):
    # process_template_path expects a path to a file object, create
    # a temporary named file and write the contents of template to it
    with tempfile.NamedTemporaryFile(mode='w+t') as fd:
        fd.write(template)
        fd.seek(0)
        files, processed_template = template_utils.process_template_path(
            fd.name)

    stack_params = {
        'stack_name': stack_name,
        'template': processed_template,
        'parameters': parameters,
        'environment': environment,
        'files': files,
    }

    stack = heat_client.stacks.create(**stack_params)['stack']
    LOG.info('New stack: %s', stack)

    wait_stack_completion(heat_client, stack['id'])

    return stack['id']
Example #4
0
def _validate(heat_client, args):
    tpl_files, template = template_utils.process_template_path(
        args.template, object_request=http.authenticated_fetcher(heat_client))

    env_files_list = []
    env_files, env = template_utils.process_multiple_environments_and_files(
        env_paths=args.environment, env_list_tracker=env_files_list)

    fields = {
        'template': template,
        'parameters': heat_utils.format_parameters(args.parameter),
        'files': dict(list(tpl_files.items()) + list(env_files.items())),
        'environment': env,
    }

    if args.ignore_errors:
        fields['ignore_errors'] = args.ignore_errors

    # If one or more environments is found, pass the listing to the server
    if env_files_list:
        fields['environment_files'] = env_files_list

    if args.show_nested:
        fields['show_nested'] = args.show_nested

    validation = heat_client.stacks.validate(**fields)
    data = list(six.itervalues(validation))
    columns = list(six.iterkeys(validation))
    return columns, data
Example #5
0
def deploy_app(new_sess, neutron, keystone, new_project):
    # Rebuilding app
    new_project_obj = [
        prj for prj in keystone.projects.list() if prj.name == new_project
    ]
    neutron_resources = neutron.list_networks(project_id=new_project_obj[0].id)

    # Redeployng App
    #Assuming App management netwrk will have MGMT in the name and DataPlane for dp network
    mgmt = [
        net for net in neutron_resources['networks'] if 'MGMT' in net['name']
    ]
    dp = [
        net for net in neutron_resources['networks']
        if 'DataPlane' in net['name']
    ]
    resources_dict = {}
    resources_dict['Management_net'] = mgmt[0]['id']
    resources_dict['Mgmt_subnet'] = mgmt[0]['subnets'][0]
    resources_dict['DataPlane_net'] = dp[0]['id']
    resources_dict['DP_subnet'] = dp[0]['subnets'][0]

    heat = heat_client('1', session=new_sess)
    files, template = template_utils.process_template_path(
        'service-deployment.yaml')
    res = heat.stacks.create(stack_name='App',
                             template=template,
                             files=files,
                             parameters=resources_dict)
    print "App redeployment was started"
Example #6
0
def _validate(heat_client, args):
    tpl_files, template = template_utils.process_template_path(
        args.template,
        object_request=http.authenticated_fetcher(heat_client))

    env_files_list = []
    env_files, env = template_utils.process_multiple_environments_and_files(
        env_paths=args.environment, env_list_tracker=env_files_list)

    fields = {
        'template': template,
        'parameters': heat_utils.format_parameters(args.parameter),
        'files': dict(list(tpl_files.items()) + list(env_files.items())),
        'environment': env,
    }

    if args.ignore_errors:
        fields['ignore_errors'] = args.ignore_errors

    # If one or more environments is found, pass the listing to the server
    if env_files_list:
        fields['environment_files'] = env_files_list

    if args.show_nested:
        fields['show_nested'] = args.show_nested

    validation = heat_client.stacks.validate(**fields)
    data = list(six.itervalues(validation))
    columns = list(six.iterkeys(validation))
    return columns, data
    def pre_expand_pnda(self, node_counts):
        ''' Use the Openstack heatclient API to expand a stack '''

        # Generate template files
        self._update_template_file(self._flavor, node_counts['datanodes'],
                                   node_counts['kafka_nodes'])

        stack_name = self._cluster
        heat_session = self._get_heat_session()
        templates_path = os.getcwd() + '/cli/' + '_resources_{}-{}'.format(
            self._flavor, self._cluster)
        template_file = templates_path + "/pnda.yaml"
        env_file = templates_path + "/pnda_env.yaml"
        env_param = [env_file]
        tpl_files, tpl_template = template_utils.process_template_path(
            template_file)
        env_files, env_template = template_utils.process_multiple_environments_and_files(
            env_paths=env_param)
        files_all = dict(list(tpl_files.items()) + list(env_files.items()))

        try:
            heat_session.stacks.update(stack_id=stack_name,
                                       template=tpl_template,
                                       files=files_all,
                                       environment=env_template,
                                       timeout_mins=120)
            stack_status_body = heat_session.stacks.get(stack_id=stack_name)
            stack_status = 'UPDATING'
            stack_status_new = None
            stack_id = stack_status_body.stack_name
        except exc.HTTPBadRequest as exp:
            error_state = exp.error
            CONSOLE.error("Bad request update stack failed: %s", error_state)
            sys.exit(1)

        while stack_status in [
                'UPDATE_IN_PROGRESS', 'UPDATING',
                'UPDATE_COMPLETE_CLEANUP_IN_PROGRESS'
        ]:
            time.sleep(5)
            if stack_status != stack_status_new:
                if stack_status_new is not None:
                    stack_status = stack_status_new
                CONSOLE.info('Stack is: %s', stack_status)
            else:
                CONSOLE.debug('Stack is: %s', stack_status)
            stack_status_body = heat_session.stacks.get(stack_id)
            stack_status_new = stack_status_body.stack_status

        if stack_status != 'UPDATE_COMPLETE':
            CONSOLE.error('Stack did not come up, status is: %s', stack_status)
            sys.exit(1)

        self.clear_instance_map_cache()
Example #8
0
 def get_heat_temp_files(self, template, env_file=None):
     try:
         env = None
         files = None
         temp = None
         files, temp = template_utils.process_template_path(template)
         if env_file:
             files, env = template_utils.process_environment_and_files(
                 env_file, template)
         return files, temp, env
     except Exception as err:
         logger.debug(err)
         raise
Example #9
0
    def stack_create(self, hc, HOT_path, env_path, stack_name):
        hot_path = self._get_resource_path(HOT_path)
        env_path = self._get_resource_path(env_path)
        with open(env_path, "r") as envFile:
            environment = yaml.load(envFile)

        files, template = template_utils.process_template_path(hot_path)
        stack = hc.stacks.create(stack_name=stack_name,
                                 template=template,
                                 environment=environment,
                                 files=files)

        return stack
Example #10
0
def do_create(initialize, session=None):
    try:
        heat = get_heat_client(session)
        for deploy in initialize:
            deploy_init_file = "{}/init.yml".format(deploy["dir"])
            deploy_file = utils.yaml_parser(deploy_init_file)["create"]
            """ template """
            deploy_template = "{}/{}".format(deploy["dir"], deploy_file)
            deploy_name = deploy["project"]
            files, template = template_utils.process_template_path(
                deploy_template)
            """Create Stack"""
            utils.log_info("Create {} stack....".format(deploy["project"]))
            if not deploy["env_file"]:
                heat.stacks.create(
                    stack_name=deploy_name, template=template, files=files)
            else:
                deploy_env_file = open(deploy["env_file"])
                heat.stacks.create(
                    stack_name=deploy_name,
                    template=template,
                    environment=deploy_env_file.read(),
                    files=files)
            if (len(initialize) > 0):
                time.sleep(8)
            # if deploy["stack"] == "clusters":
            #     utils.log_info("Generate {} private key...".format(
            #         deploy["project"]))
            #     wait_key = True
            #     private_key_file = None
            #     while wait_key:
            #         out = get_pkey_from_stack(
            #             deploy["project"])
            #         if out:
            #             private_key_file = "{}/private_key.pem".format(
            #                 deploy["dir"])
            #             with open(private_key_file, "w") as pkey:
            #                 pkey.write(out)
            #                 os.chmod(private_key_file, 0o600)
            #                 utils.log_info("Done...")
            #             wait_key = False
            #         else:
            #             time.sleep(5)

    except Exception as e:
        utils.log_err(e)
    else:
        pass
    finally:
        pass
Example #11
0
def start(stack_name):
    global stack_active
    global stackname
    stackname = stack_name

    template_name = 'Heat_template_start_instance.yml'
    files, template = template_utils.process_template_path(template_name)

    if stack_active:
        abort(400, 'A stack is already active')

    try:
        hc.stacks.create(stack_name=stack_name, template=template, files=files)
        time.sleep(10)

        stacks = hc.stacks.list(filters={'stack_name': stack_name})
        stack = next(stacks)

        stack_status = stack.status
        while stack_status == 'IN_PROGRESS':
            stacks = hc.stacks.list(filters={'stack_name': stack_name})
            stack = next(stacks)
            stack_output = hc.stacks.output_list(stack.id)
            print 'Build in progress, sleep for 5 seconds...'
            time.sleep(5)
            stack_status = stack.status

        stack_active = True
        result = {}
        for line in stack_output['outputs']:
            output_value = line['output_key']
            result[output_value] = hc.stacks.output_show(
                stack.id, output_value)

        write_to_hosts_file(result)
        write_to_ansible_hosts_file(result)
        write_hosts_to_master_and_worker(result)

        token = subprocess.check_output(
            'ssh -i group8key.pem -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -q ubuntu@'
            + result['ansible_ip']['output']['output_value'] +
            " 'sh ~/get_token.sh'")
        result['token'] = token

        return jsonify(result)
        # redirect('http://IP.TO.SPARK.MASTER:60060/', 302, jsonify(result))
    except heatclient.exc.HTTPConflict as e:
        abort(400, 'Stack already exists : %s %s' % (e.error, stack_name))
    except heatclient.exc.HTTPBadRequest as e:
        abort(400, 'Bad request : %s' % e.error)
Example #12
0
def do_create(initialize, session=None):
    try:
        heat = get_heat_client(session)
        for deploy in initialize:
            deploy_init_file = "{}/init.yml".format(deploy["dir"])
            deploy_file = utils.yaml_parser(deploy_init_file)["create"]
            """ template """
            deploy_template = "{}/{}".format(deploy["dir"], deploy_file)
            deploy_name = deploy["project"]
            files, template = template_utils.process_template_path(
                deploy_template)
            """Create Stack"""
            utils.log_info("Create {} stack....".format(deploy["project"]))
            if not deploy["env_file"]:
                heat.stacks.create(
                    stack_name=deploy_name, template=template, files=files)
            else:
                deploy_env_file = open(deploy["env_file"])
                heat.stacks.create(
                    stack_name=deploy_name,
                    template=template,
                    environment=deploy_env_file.read(),
                    files=files)
            if (len(initialize) > 0):
                time.sleep(8)
            # if deploy["stack"] == "clusters":
            #     utils.log_info("Generate {} private key...".format(
            #         deploy["project"]))
            #     wait_key = True
            #     private_key_file = None
            #     while wait_key:
            #         out = get_pkey_from_stack(
            #             deploy["project"])
            #         if out:
            #             private_key_file = "{}/private_key.pem".format(
            #                 deploy["dir"])
            #             with open(private_key_file, "w") as pkey:
            #                 pkey.write(out)
            #                 os.chmod(private_key_file, 0o600)
            #                 utils.log_info("Done...")
            #             wait_key = False
            #         else:
            #             time.sleep(5)

    except Exception as e:
        utils.log_err(e)
    else:
        pass
    finally:
        pass
Example #13
0
def create_stacks(num_stacks, nested, template_file):
    tpl_files, template = template_utils.process_template_path(
        template_file,
        object_request=http.authenticated_fetcher(TempestClients.heat()))

    for i in range(num_stacks):
        stack_name = 'stack_%s' % i + ('_nested' if nested else '')
        TempestClients.heat().stacks.create(stack_name=stack_name,
                                            template=template,
                                            files=tpl_files,
                                            parameters={})
        wait_for_status(45,
                        _check_num_stacks,
                        num_stacks=num_stacks,
                        state='CREATE_COMPLETE')
        time.sleep(2)
    def take_action(self, parsed_args):
        self.log.debug('take_action(%s)', parsed_args)

        client = self.app.client_manager.orchestration

        tpl_files, template = template_utils.process_template_path(
            parsed_args.template,
            object_request=_authenticated_fetcher(client))

        env_files, env = (
            template_utils.process_multiple_environments_and_files(
                env_paths=parsed_args.environment))

        parameters = heat_utils.format_all_parameters(
            parsed_args.parameter,
            parsed_args.parameter_file,
            parsed_args.template)

        if parsed_args.pre_create:
            template_utils.hooks_to_env(env, parsed_args.pre_create,
                                        'pre-create')

        fields = {
            'stack_name': parsed_args.name,
            'disable_rollback': not parsed_args.enable_rollback,
            'parameters': parameters,
            'template': template,
            'files': dict(list(tpl_files.items()) + list(env_files.items())),
            'environment': env
        }

        if parsed_args.tags:
            fields['tags'] = parsed_args.tags
        if parsed_args.timeout:
            fields['timeout_mins'] = parsed_args.timeout

        stack = client.stacks.create(**fields)['stack']
        if parsed_args.wait:
            if not utils.wait_for_status(client.stacks.get, parsed_args.name,
                                         status_field='stack_status',
                                         success_status='create_complete',
                                         error_status='create_failed'):

                msg = _('Stack %s failed to create.') % parsed_args.name
                raise exc.CommandError(msg)

        return _show_stack(client, stack['id'], format='table', short=True)
Example #15
0
 def create_stack(self, stack_name, stack_file_path, heat_parameters):
     files, template = template_utils.process_template_path(stack_file_path)
     stack_created = self.heatclient.stacks.create(
         stack_name=stack_name,
         template=template,
         parameters=heat_parameters,
         files=files)
     stack = self.heatclient.stacks.get(stack_created['stack']['id'],
                                        resolve_outputs=True)
     # Poll at 5 second intervals, until the status is no longer 'BUILD'
     while stack.stack_status == 'CREATE_IN_PROGRESS':
         print('waiting..')
         time.sleep(5)
         stack = self.heatclient.stacks.get(stack_created['stack']['id'],
                                            resolve_outputs=True)
     if stack.stack_status == 'CREATE_COMPLETE':
         return stack
     else:
         RapidLog.exception('Error in stack deployment')
Example #16
0
    def take_action(self, parsed_args):
        self.log.debug('take_action(%s)', parsed_args)

        client = self.app.client_manager.orchestration

        tpl_files, template = template_utils.process_template_path(
            parsed_args.template,
            object_request=_authenticated_fetcher(client),
            existing=parsed_args.existing)

        env_files, env = (
            template_utils.process_multiple_environments_and_files(
                env_paths=parsed_args.environment))

        parameters = heat_utils.format_all_parameters(
            parsed_args.parameter,
            parsed_args.parameter_file,
            parsed_args.template)

        if parsed_args.pre_update:
            template_utils.hooks_to_env(env, parsed_args.pre_update,
                                        'pre-update')

        fields = {
            'stack_id': parsed_args.stack,
            'parameters': parameters,
            'existing': parsed_args.existing,
            'template': template,
            'files': dict(list(tpl_files.items()) + list(env_files.items())),
            'environment': env
        }

        if parsed_args.tags:
            fields['tags'] = parsed_args.tags
        if parsed_args.timeout:
            fields['timeout_mins'] = parsed_args.timeout
        if parsed_args.clear_parameter:
            fields['clear_parameters'] = list(parsed_args.clear_parameter)

        if parsed_args.rollback:
            rollback = parsed_args.rollback.strip().lower()
            if rollback not in ('enabled', 'disabled', 'keep'):
                msg = _('--rollback invalid value: %s') % parsed_args.rollback
                raise exc.CommandError(msg)
            if rollback != 'keep':
                fields['disable_rollback'] = rollback == 'disabled'

        if parsed_args.dry_run:
            changes = client.stacks.preview_update(**fields)

            fields = ['state', 'resource_name', 'resource_type',
                      'resource_identity']

            columns = sorted(changes.get("resource_changes", {}).keys())
            data = [heat_utils.json_formatter(changes["resource_changes"][key])
                    for key in columns]

            return columns, data

        client.stacks.update(**fields)
        if parsed_args.wait:
            if not utils.wait_for_status(client.stacks.get, parsed_args.stack,
                                         status_field='stack_status',
                                         success_status='update_complete',
                                         error_status='update_failed'):

                msg = _('Stack %s failed to update.') % parsed_args.stack
                raise exc.CommandError(msg)

        return _show_stack(client, parsed_args.stack, format='table',
                           short=True)
Example #17
0
Created on 2018/6/25 10:31
@author: mengph
reach
"""
import yaml
from heatclient import client as htclient
from heatclient.common import template_utils
from keystoneauth1 import loading, session

yaml_name = "nic_qos.yaml"
keystone_url_200 = "http://172.16.90.2:5000/v2.0"
keystone_url_180_11 = "http://10.121.137.50:5000/v2.0"
keystone_url_180_12 = "http://10.121.137.80:5000/v2.0"
keystone_url_136_132 = "http://10.121.136.132:5000/v2.0"
loader = loading.get_plugin_loader('password')
auth = loader.load_from_options(auth_url=keystone_url_180_11,
                                username="******",
                                password="******",
                                project_name="admin")
sess = session.Session(auth=auth, verify=False)
heat = htclient.Client(version="1", session=sess)
files, template = template_utils.process_template_path(yaml_name)
heat_parameters = open(yaml_name)
# temp_params = yaml.load(heat_parameters)
# heat_parameters.close()ls

# print files,template
# print temp_params["parameters"]
# heat.stacks.create(stack_name="cmcc", template=template, parameters=temp_params["parameters"], files=files)
heat.stacks.create(stack_name=yaml_name, template=template, files=files)
print heat.stacks.list()
Example #18
0
    def take_action(self, parsed_args):
        self.log.debug('take_action(%s)', parsed_args)

        client = self.app.client_manager.orchestration

        tpl_files, template = template_utils.process_template_path(
            parsed_args.template,
            object_request=http.authenticated_fetcher(client))

        env_files, env = (
            template_utils.process_multiple_environments_and_files(
                env_paths=parsed_args.environment))

        parameters = heat_utils.format_all_parameters(
            parsed_args.parameter,
            parsed_args.parameter_file,
            parsed_args.template)

        if parsed_args.pre_create:
            template_utils.hooks_to_env(env, parsed_args.pre_create,
                                        'pre-create')

        fields = {
            'stack_name': parsed_args.name,
            'disable_rollback': not parsed_args.enable_rollback,
            'parameters': parameters,
            'template': template,
            'files': dict(list(tpl_files.items()) + list(env_files.items())),
            'environment': env
        }

        if parsed_args.tags:
            fields['tags'] = parsed_args.tags
        if parsed_args.timeout:
            fields['timeout_mins'] = parsed_args.timeout

        if parsed_args.dry_run:
            stack = client.stacks.preview(**fields)

            formatters = {
                'description': heat_utils.text_wrap_formatter,
                'template_description': heat_utils.text_wrap_formatter,
                'stack_status_reason': heat_utils.text_wrap_formatter,
                'parameters': heat_utils.json_formatter,
                'outputs': heat_utils.json_formatter,
                'resources': heat_utils.json_formatter,
                'links': heat_utils.link_formatter,
            }

            columns = []
            for key in stack.to_dict():
                columns.append(key)
            columns.sort()

            return (
                columns,
                utils.get_item_properties(stack, columns,
                                          formatters=formatters)
            )

        stack = client.stacks.create(**fields)['stack']
        if parsed_args.wait:
            stack_status, msg = event_utils.poll_for_events(
                client, parsed_args.name, action='CREATE')
            if stack_status == 'CREATE_FAILED':
                raise exc.CommandError(msg)

        return _show_stack(client, stack['id'], format='table', short=True)
    def pre_install_pnda(self, node_counts):
        '''
        Use the Openstack heatclient API to launch a stack that PNDA can be installed on
        The stack is defined in template files in the flavor specific heat-template directory
        '''
        # Generate template files
        self._generate_template_file(self._flavor, node_counts['datanodes'],
                                     node_counts['opentsdb_nodes'],
                                     node_counts['kafka_nodes'],
                                     node_counts['zk_nodes'])

        stack_name = self._cluster

        heat_session = self._get_heat_session()
        templates_path = os.getcwd() + '/cli/' + '_resources_{}-{}'.format(
            self._flavor, self._cluster)
        template_file = templates_path + "/pnda.yaml"
        env_file = templates_path + "/pnda_env.yaml"
        config, ind, bsi = load_yaml_guess_indent(open(env_file))
        parameters = config['parameters']
        parameters['keyName'] = node_counts['keyname']
        # remove extra parmeters for heat template
        exclude_section = [
            'INFRASTRUCTURE_TYPE', 'SSH_KEY', 'OS_USER', 'networkCidr',
            'KEYSTONE_AUTH_URL', 'KEYSTONE_USER', 'KEYSTONE_PASSWORD',
            'KEYSTONE_TENANT', 'KEYSTONE_AUTH_URL', 'KEYSTONE_AUTH_VERSION',
            'KEYSTONE_REGION_NAME'
        ]
        for param in exclude_section:
            if param in parameters:
                del parameters[param]

        ruamel.yaml.round_trip_dump(config,
                                    open(env_file, 'w'),
                                    indent=ind,
                                    block_seq_indent=bsi)
        env_param = [env_file]
        tpl_files, tpl_template = template_utils.process_template_path(
            template_file)
        env_files, env_template = template_utils.process_multiple_environments_and_files(
            env_paths=env_param)
        files_all = dict(list(tpl_files.items()) + list(env_files.items()))

        try:
            status = heat_session.stacks.create(stack_name=stack_name,
                                                template=tpl_template,
                                                files=files_all,
                                                environment=env_template,
                                                timeout_mins=120)
            stack_id = status['stack']['id']
            stack_status = 'CREATING'
            stack_status_new = None
        except exc.HTTPConflict as exp:
            error_state = exp.error
            CONSOLE.error("Stack already exist : %s %s", error_state,
                          stack_name)
            sys.exit(1)

        except exc.HTTPBadRequest as exp:
            error_state = exp.error
            CONSOLE.error("Bad request stack creation failed: %s", error_state)
            sys.exit(1)

        while stack_status in ['CREATE_IN_PROGRESS', 'CREATING']:
            time.sleep(5)
            if stack_status != stack_status_new:
                if stack_status_new is not None:
                    stack_status = stack_status_new
                CONSOLE.info('Stack is: %s', stack_status)
            else:
                CONSOLE.debug('Stack is: %s', stack_status)
            stack_status_body = heat_session.stacks.get(stack_id)
            stack_status_new = stack_status_body.stack_status

        if stack_status != 'CREATE_COMPLETE':
            CONSOLE.error('Stack did not come up, status is: %s', stack_status)
            sys.exit(1)

        self.fill_instance_map()
Example #20
0
    def take_action(self, parsed_args):
        self.log.debug('take_action(%s)', parsed_args)

        client = self.app.client_manager.orchestration

        tpl_files, template = template_utils.process_template_path(
            parsed_args.template,
            object_request=http.authenticated_fetcher(client),
            existing=parsed_args.existing)

        env_files, env = (
            template_utils.process_multiple_environments_and_files(
                env_paths=parsed_args.environment))

        parameters = heat_utils.format_all_parameters(
            parsed_args.parameter,
            parsed_args.parameter_file,
            parsed_args.template)

        if parsed_args.pre_update:
            template_utils.hooks_to_env(env, parsed_args.pre_update,
                                        'pre-update')

        fields = {
            'stack_id': parsed_args.stack,
            'parameters': parameters,
            'existing': parsed_args.existing,
            'template': template,
            'files': dict(list(tpl_files.items()) + list(env_files.items())),
            'environment': env
        }

        if parsed_args.tags:
            fields['tags'] = parsed_args.tags
        if parsed_args.timeout:
            fields['timeout_mins'] = parsed_args.timeout
        if parsed_args.clear_parameter:
            fields['clear_parameters'] = list(parsed_args.clear_parameter)

        if parsed_args.rollback:
            rollback = parsed_args.rollback.strip().lower()
            if rollback not in ('enabled', 'disabled', 'keep'):
                msg = _('--rollback invalid value: %s') % parsed_args.rollback
                raise exc.CommandError(msg)
            if rollback != 'keep':
                fields['disable_rollback'] = rollback == 'disabled'

        if parsed_args.dry_run:
            changes = client.stacks.preview_update(**fields)

            fields = ['state', 'resource_name', 'resource_type',
                      'resource_identity']

            columns = sorted(changes.get("resource_changes", {}).keys())
            data = [heat_utils.json_formatter(changes["resource_changes"][key])
                    for key in columns]

            return columns, data

        if parsed_args.wait:
            # find the last event to use as the marker
            events = event_utils.get_events(client,
                                            stack_id=parsed_args.stack,
                                            event_args={'sort_dir': 'desc',
                                                        'limit': 1})
            marker = events[0].id if events else None

        client.stacks.update(**fields)

        if parsed_args.wait:
            stack = client.stacks.get(parsed_args.stack)
            stack_status, msg = event_utils.poll_for_events(
                client, stack.stack_name, action='UPDATE', marker=marker)
            if stack_status == 'UPDATE_FAILED':
                raise exc.CommandError(msg)

        return _show_stack(client, parsed_args.stack, format='table',
                           short=True)
Example #21
0
    def take_action(self, parsed_args):
        self.log.debug('take_action(%s)', parsed_args)

        client = self.app.client_manager.orchestration

        tpl_files, template = template_utils.process_template_path(
            parsed_args.template,
            object_request=http.authenticated_fetcher(client),
            existing=parsed_args.existing,
            fetch_child=parsed_args.files_container is None)

        env_files_list = []
        env_files, env = (
            template_utils.process_multiple_environments_and_files(
                env_paths=parsed_args.environment,
                env_list_tracker=env_files_list,
                fetch_env_files=parsed_args.files_container is None))

        parameters = heat_utils.format_all_parameters(
            parsed_args.parameter, parsed_args.parameter_file,
            parsed_args.template)

        if parsed_args.pre_update:
            template_utils.hooks_to_env(env, parsed_args.pre_update,
                                        'pre-update')

        fields = {
            'stack_id': parsed_args.stack,
            'parameters': parameters,
            'existing': parsed_args.existing,
            'template': template,
            'files': dict(list(tpl_files.items()) + list(env_files.items())),
            'environment': env
        }

        # If one or more environments is found, pass the listing to the server
        if env_files_list:
            fields['environment_files'] = env_files_list

        if parsed_args.files_container:
            fields['files_container'] = parsed_args.files_container

        if parsed_args.tags:
            fields['tags'] = parsed_args.tags
        if parsed_args.timeout:
            fields['timeout_mins'] = parsed_args.timeout
        if parsed_args.clear_parameter:
            fields['clear_parameters'] = list(parsed_args.clear_parameter)

        if parsed_args.rollback:
            rollback = parsed_args.rollback.strip().lower()
            if rollback not in ('enabled', 'disabled', 'keep'):
                msg = _('--rollback invalid value: %s') % parsed_args.rollback
                raise exc.CommandError(msg)
            if rollback != 'keep':
                fields['disable_rollback'] = rollback == 'disabled'

        if parsed_args.dry_run:
            if parsed_args.show_nested:
                fields['show_nested'] = parsed_args.show_nested

            changes = client.stacks.preview_update(**fields)

            fields = [
                'state', 'resource_name', 'resource_type', 'resource_identity'
            ]

            columns = sorted(changes.get("resource_changes", {}).keys())
            data = [
                heat_utils.json_formatter(changes["resource_changes"][key])
                for key in columns
            ]

            return columns, data

        if parsed_args.wait:
            # find the last event to use as the marker
            events = event_utils.get_events(client,
                                            stack_id=parsed_args.stack,
                                            event_args={'sort_dir': 'desc'},
                                            limit=1)
            marker = events[0].id if events else None

        if parsed_args.converge:
            fields['converge'] = True

        client.stacks.update(**fields)

        if parsed_args.wait:
            stack = client.stacks.get(parsed_args.stack)
            stack_status, msg = event_utils.poll_for_events(client,
                                                            stack.stack_name,
                                                            action='UPDATE',
                                                            marker=marker)
            if stack_status == 'UPDATE_FAILED':
                raise exc.CommandError(msg)

        return _show_stack(client,
                           parsed_args.stack,
                           format='table',
                           short=True)
Example #22
0
    def take_action(self, parsed_args):
        self.log.debug('take_action(%s)', parsed_args)

        client = self.app.client_manager.orchestration

        tpl_files, template = template_utils.process_template_path(
            parsed_args.template,
            object_request=http.authenticated_fetcher(client),
            fetch_child=parsed_args.files_container is None)

        env_files_list = []
        env_files, env = (
            template_utils.process_multiple_environments_and_files(
                env_paths=parsed_args.environment,
                env_list_tracker=env_files_list,
                fetch_env_files=parsed_args.files_container is None))

        parameters = heat_utils.format_all_parameters(
            parsed_args.parameter, parsed_args.parameter_file,
            parsed_args.template)

        if parsed_args.pre_create:
            template_utils.hooks_to_env(env, parsed_args.pre_create,
                                        'pre-create')

        fields = {
            'stack_name': parsed_args.name,
            'disable_rollback': not parsed_args.enable_rollback,
            'parameters': parameters,
            'template': template,
            'files': dict(list(tpl_files.items()) + list(env_files.items())),
            'environment': env
        }

        # If one or more environments is found, pass the listing to the server
        if env_files_list:
            fields['environment_files'] = env_files_list

        if parsed_args.files_container:
            fields['files_container'] = parsed_args.files_container

        if parsed_args.tags:
            fields['tags'] = parsed_args.tags
        if parsed_args.timeout:
            fields['timeout_mins'] = parsed_args.timeout

        if parsed_args.dry_run:
            stack = client.stacks.preview(**fields)

            formatters = {
                'description': heat_utils.text_wrap_formatter,
                'template_description': heat_utils.text_wrap_formatter,
                'stack_status_reason': heat_utils.text_wrap_formatter,
                'parameters': heat_utils.json_formatter,
                'outputs': heat_utils.json_formatter,
                'resources': heat_utils.json_formatter,
                'links': heat_utils.link_formatter,
            }

            columns = []
            for key in stack.to_dict():
                columns.append(key)
            columns.sort()

            return (columns,
                    utils.get_item_properties(stack,
                                              columns,
                                              formatters=formatters))

        stack = client.stacks.create(**fields)['stack']
        if parsed_args.wait:
            stack_status, msg = event_utils.poll_for_events(
                client,
                parsed_args.name,
                action='CREATE',
                poll_period=parsed_args.poll)
            if stack_status == 'CREATE_FAILED':
                raise exc.CommandError(msg)

        return _show_stack(client, stack['id'], format='table', short=True)
Example #23
0
def stack_up(conn, stack_name):
    for stack_dir in _stack_dir():
        stack_file = os.path.join(stack_dir, "%s.yaml" % (stack_name))
        if os.path.exists(stack_file):
            break
    else:
        raise Exception("Cannot find '%s' stack." % (stack_name))

    dependencies = []

    with open(stack_file) as fp:
        for l in fp:
            l = l.strip()
            if len(l) > 2 and l[0] == '#':
                if l[2:].startswith('Depends-On: '):
                    dependencies.append(l[14:])

    for dep in dependencies:
        stack_up(conn, dep)

    with open(stack_file) as fp:
        config = yaml.safe_load(fp)

        if 'heat_template_version' in config:
            d = config['heat_template_version'].strftime('%Y-%m-%d')
            config['heat_template_version'] = d

    has_image = config.get('parameters', {}).get('image', None) is not None
    has_key_pair_path = config.get('parameters',
                                   {}).get('key_pair_path', None) is not None
    has_key_pair = config.get('parameters', {}).get('key_pair',
                                                    None) is not None

    files, template = template_utils.process_template_path(stack_file)

    args = {
        'files': files,
        'template': template,
        'parameters': {},
        'name': stack_name,
        'tags': 'stack',
    }
    if has_image:
        args['parameters']['image'] = 'cirros-0.4.0-x86_64-disk'

    if has_key_pair_path:
        key_filename = os.path.expanduser('~/.ssh/id_rsa.pub')
        args['parameters']['key_pair_path'] = 'keys/id_rsa.pub'
        with open(key_filename) as fp:
            files['keys/id_rsa.pub'] = fp.read()

    if has_key_pair:
        args['parameters']['key_pair'] = 'keypair0'

    cur_stack = conn.orchestration.find_stack(stack_name)
    if cur_stack is None:
        print("Creating stack %s" % (stack_name))
        conn.orchestration.create_stack(**args)
    else:
        print("Updating stack %s" % (stack_name))
        conn.orchestration.update_stack(cur_stack.id, **args)

    while True:
        stack = conn.orchestration.get_stack(stack_name)
        if stack.status not in ('CREATE_IN_PROGRESS', 'UPDATE_IN_PROGRESS'):
            break

        time.sleep(1)

    print("Stack %s %s (%s)" % (stack_name, stack.status, stack.status_reason))

    print(
        yaml.safe_dump(stack.outputs,
                       default_flow_style=False,
                       explicit_start=True))