def create_raid_configuration(clients, node_uuids, configuration,
                              verbosity=0):
    """Create RAID configuration on nodes.

    :param clients: application client object.
    :type clients: Object

    :param node_uuids: List of instance UUID(s).
    :type node_uuids: List

    :param configuration: RAID configuration object.
    :type configuration: Object

    :param verbosity: Verbosity level
    :type verbosity: Integer
    """

    with utils.TempDirs() as tmp:
        utils.run_ansible_playbook(
            playbook='cli-baremetal-raid.yaml',
            inventory='localhost,',
            workdir=tmp,
            playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
            verbosity=verbosity,
            extra_vars={
                'node_uuids': node_uuids,
                'raid_configuration': configuration
            }
        )

    print('Successfully configured RAID for nodes: {}'.format(node_uuids))
Esempio n. 2
0
    def take_action(self, parsed_args):
        self.log.debug("take_action(%s)" % parsed_args)

        with open(parsed_args.input, 'r') as fp:
            roles = yaml.safe_load(fp)

        key = self.get_key_pair(parsed_args)
        with open('{}.pub'.format(key), 'rt') as fp:
            ssh_key = fp.read()

        output_path = os.path.abspath(parsed_args.output)

        extra_vars = {
            "stack_name": parsed_args.stack,
            "baremetal_deployment": roles,
            "baremetal_deployed_path": output_path,
            "ssh_public_keys": ssh_key,
            "ssh_user_name": parsed_args.overcloud_ssh_user,
            "node_timeout": parsed_args.timeout,
            "concurrency": parsed_args.concurrency
        }

        with oooutils.TempDirs() as tmp:
            oooutils.run_ansible_playbook(
                playbook='cli-overcloud-node-provision.yaml',
                inventory='localhost,',
                workdir=tmp,
                playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
                verbosity=oooutils.playbook_verbosity(self=self),
                extra_vars=extra_vars,
            )

        print('Nodes deployed successfully, add %s to your deployment '
              'environment' % parsed_args.output)
    def take_action(self, parsed_args):
        self.log.debug("take_action({args})".format(args=parsed_args))

        self._validate_args(parsed_args)

        if not parsed_args.yes:
            confirm = utils.prompt_user_for_confirmation(message=_(
                "Are you sure you want to delete this overcloud "
                "[y/N]? "),
                                                         logger=self.log)
            if not confirm:
                raise oscexc.CommandError("Action not confirmed, exiting.")

        if parsed_args.skip_ipa_cleanup:
            playbooks = ["cli-overcloud-delete.yaml"]
        else:
            # Order is important, let's make sure we cleanup FreeIPA before we
            # start removing infrastructure.
            playbooks = ["cli-cleanup-ipa.yml", "cli-overcloud-delete.yaml"]

        with utils.TempDirs() as tmp:
            utils.run_ansible_playbook(
                playbooks,
                constants.ANSIBLE_INVENTORY,
                workdir=tmp,
                playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
                verbosity=utils.playbook_verbosity(self=self),
                extra_vars={"stack_name": parsed_args.stack})

        print("Success.")
def introspect(clients, node_uuids, run_validations, concurrency,
               verbosity=0):
    """Introspect Baremetal Nodes

    :param clients: Application client object.
    :type clients: Object

    :param node_uuids: List of instance UUID(s).
    :type node_uuids: List

    :param run_validations: Enable or disable validations
    :type run_validations: Boolean

    :param concurrency: concurrency level
    :type concurrency: Integer

    :param verbosity: Verbosity level
    :type verbosity: Integer
    """

    with utils.TempDirs() as tmp:
        utils.run_ansible_playbook(
            playbook='cli-baremetal-introspect.yaml',
            inventory='localhost,',
            workdir=tmp,
            playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
            verbosity=verbosity,
            extra_vars={
                "node_uuids": node_uuids,
                "run_validations": run_validations,
                "concurrency": concurrency,
            }
        )

    print('Successfully introspected nodes: {}'.format(node_uuids))
def deploy(container, run_validations, skip_deploy_identifier,
           timeout, verbosity=0):
    """Run the deployment playbook.

    :param container: Name of the container
    :type container: String

    :param run_validations: Enable or disable validations
    :type run_validations: Boolean

    :param skip_deploy_identifier: Enable or disable validations
    :type skip_deploy_identifier: Boolean

    :param timeout: Deployment timeout (minutes).
    :type timeout: Integer

    :param verbosity: Verbosity level
    :type verbosity: Integer
    """
    with utils.TempDirs() as tmp:
        utils.run_ansible_playbook(
            "cli-deploy-deployment-plan.yaml",
            'undercloud,',
            workdir=tmp,
            playbook_dir=ANSIBLE_TRIPLEO_PLAYBOOKS,
            verbosity=verbosity,
            timeout=timeout,
            extra_vars={
                "container": container,
                "run_validations": run_validations,
                "skip_deploy_identifier": skip_deploy_identifier,
            }
        )

    print("Success.")
def create_deployment_plan(container,
                           generate_passwords,
                           use_default_templates=False,
                           source_url=None,
                           validate_stack=True,
                           verbosity_level=0,
                           plan_env_file=None):
    extra_vars = {
        "container": container,
        "validate": validate_stack,
        "generate_passwords": generate_passwords
    }

    if plan_env_file:
        extra_vars['plan_environment'] = plan_env_file

    with utils.TempDirs() as tmp:
        utils.run_ansible_playbook(
            "cli-create-deployment-plan.yaml",
            'undercloud,',
            workdir=tmp,
            playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
            extra_vars=extra_vars,
            verbosity=verbosity_level)

    print("Success.")
def apply_bios_configuration(node_uuids, configuration, verbosity=0):
    """Apply BIOS settings on nodes.

    :param node_uuids: List of instance UUID(s).
    :type node_uuids: List

    :param configuration: BIOS configuration object.
    :type configuration: Object

    :param verbosity: Verbosity level
    :type verbosity: Integer
    """

    print('Applying BIOS settings for given nodes, this may take time')

    with utils.TempDirs() as tmp:
        utils.run_ansible_playbook(
            playbook='cli-baremetal-bios.yaml',
            inventory='localhost,',
            workdir=tmp,
            playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
            verbosity=verbosity,
            extra_vars={
                'node_uuids': node_uuids,
                'bios_configuration': configuration
            }
        )

    print('Successfully applied the BIOS for nodes: {}'.format(node_uuids))
    def take_action(self, parsed_args):
        self.log.debug("take_action(%s)" % parsed_args)
        clients = self.app.client_manager

        if parsed_args.baremetal_deployment:
            with open(parsed_args.baremetal_deployment, 'r') as fp:
                roles = yaml.safe_load(fp)

            nodes_text, nodes = self._nodes_to_delete(parsed_args, roles)
            if nodes_text:
                print(nodes_text)
            else:
                return
        else:
            nodes = parsed_args.nodes
            nodes_text = '\n'.join('- %s' % node for node in nodes)
        if not parsed_args.yes:
            confirm = oooutils.prompt_user_for_confirmation(message=_(
                "Are you sure you want to delete these overcloud "
                "nodes [y/N]? "),
                                                            logger=self.log)
            if not confirm:
                raise oscexc.CommandError("Action not confirmed, exiting.")

        orchestration_client = clients.orchestration

        stack = oooutils.get_stack(orchestration_client, parsed_args.stack)

        if not stack:
            raise InvalidConfiguration("stack {} not found".format(
                parsed_args.stack))

        print(
            "Deleting the following nodes from stack {stack}:\n{nodes}".format(
                stack=stack.stack_name, nodes=nodes_text))

        scale.scale_down(
            log=self.log,
            clients=clients,
            stack=stack,
            nodes=nodes,
            connection_timeout=parsed_args.overcloud_ssh_port_timeout,
            timeout=parsed_args.timeout,
            verbosity=oooutils.playbook_verbosity(self=self))

        if parsed_args.baremetal_deployment:
            with oooutils.TempDirs() as tmp:
                oooutils.run_ansible_playbook(
                    playbook='cli-overcloud-node-unprovision.yaml',
                    inventory='localhost,',
                    workdir=tmp,
                    playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
                    verbosity=oooutils.playbook_verbosity(self=self),
                    extra_vars={
                        "stack_name": parsed_args.stack,
                        "baremetal_deployment": roles,
                        "prompt": False,
                    })
Esempio n. 9
0
    def take_action(self, parsed_args):
        self.log.debug("take_action(%s)" % parsed_args)

        with open(parsed_args.input, 'r') as fp:
            roles = yaml.safe_load(fp)

        with oooutils.TempDirs() as tmp:
            unprovision_confirm = os.path.join(tmp, 'unprovision_confirm.json')

            if not parsed_args.yes:
                oooutils.run_ansible_playbook(
                    playbook='cli-overcloud-node-unprovision.yaml',
                    inventory='localhost,',
                    workdir=tmp,
                    playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
                    verbosity=oooutils.playbook_verbosity(self=self),
                    extra_vars={
                        "stack_name": parsed_args.stack,
                        "baremetal_deployment": roles,
                        "all": parsed_args.all,
                        "prompt": True,
                        "unprovision_confirm": unprovision_confirm
                    }
                )
                with open(unprovision_confirm) as f:
                    to_unprovision = json.load(f)
                    if not to_unprovision:
                        print('Nothing to unprovision, exiting')
                        return
                    self._print_nodes(to_unprovision)

                confirm = oooutils.prompt_user_for_confirmation(
                    message=_("Are you sure you want to unprovision these %s "
                              "nodes [y/N]? ") % parsed_args.stack,
                    logger=self.log)
                if not confirm:
                    raise oscexc.CommandError("Action not confirmed, exiting.")

            oooutils.run_ansible_playbook(
                playbook='cli-overcloud-node-unprovision.yaml',
                inventory='localhost,',
                workdir=tmp,
                playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
                verbosity=oooutils.playbook_verbosity(self=self),
                extra_vars={
                    "stack_name": parsed_args.stack,
                    "baremetal_deployment": roles,
                    "all": parsed_args.all,
                    "prompt": False,
                }
            )

        print('Unprovision complete')
def enable_ssh_admin(stack, hosts, ssh_user, ssh_key, timeout,
                     verbosity=0):
    """Run enable ssh admin access playbook.

    :param stack: Stack data.
    :type stack: Object

    :param hosts: Machines to connect to.
    :type hosts: List

    :param ssh_user: SSH access username.
    :type ssh_user: String

    :param ssh_key: SSH access key.
    :type ssh_key: String

    :param timeout: Ansible connection timeout in seconds
    :type timeout: int

    :param verbosity: Verbosity level
    :type verbosity: Integer
    """

    print(
        'Enabling ssh admin (tripleo-admin) for hosts: {}.'
        '\nUsing ssh user "{}" for initial connection.'
        '\nUsing ssh key at "{}" for initial connection.'
        '\n\nStarting ssh admin enablement playbook'.format(
            hosts,
            ssh_user,
            ssh_key
        )
    )
    with utils.TempDirs() as tmp:
        utils.run_ansible_playbook(
            playbook='cli-enable-ssh-admin.yaml',
            inventory=','.join(hosts),
            workdir=tmp,
            playbook_dir=ANSIBLE_TRIPLEO_PLAYBOOKS,
            key=ssh_key,
            ssh_user=ssh_user,
            verbosity=verbosity,
            extra_vars={
                "ssh_user": ssh_user,
                "ssh_servers": hosts,
                'tripleo_cloud_name': stack.stack_name
            },
            ansible_timeout=timeout
        )
    print("Enabling ssh admin - COMPLETE.")
def update_deployment_plan(clients, verbosity_level=0, **workflow_input):
    with utils.TempDirs() as tmp:
        utils.run_ansible_playbook(
            "cli-update-deployment-plan.yaml",
            'undercloud,',
            workdir=tmp,
            playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
            extra_vars={
                "container": workflow_input['container'],
                "validate": workflow_input['validate_stack'],
                "generate_passwords": workflow_input["generate_passwords"],
            },
            verbosity=verbosity_level)

    print("Success.")
    def take_action(self, parsed_args):
        self.log.debug('take_action({})'.format(parsed_args))

        extra_vars = {
            'server_name': parsed_args.server_name,
            'sos_destination': parsed_args.destination,
        }

        with utils.TempDirs() as tmp:
            utils.run_ansible_playbook(
                playbook='cli-support-collect-logs.yaml',
                inventory=constants.ANSIBLE_INVENTORY,
                workdir=tmp,
                playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
                verbosity=utils.playbook_verbosity(self=self),
                extra_vars=extra_vars)
Esempio n. 13
0
def create_overcloudrc(container="overcloud",
                       no_proxy='',
                       output_dir=CLOUD_HOME_DIR,
                       verbosity=0):
    with utils.TempDirs() as tmp:
        utils.run_ansible_playbook("cli-generate-overcloudrc.yaml",
                                   'undercloud,',
                                   workdir=tmp,
                                   playbook_dir=ANSIBLE_TRIPLEO_PLAYBOOKS,
                                   verbosity=verbosity,
                                   extra_vars={
                                       "container": container,
                                       "no_proxy": no_proxy,
                                       "output_dir": output_dir,
                                   })
    rcpath = os.path.join(output_dir, container + 'rc')
    return rcpath
    def _provision_baremetal(self, parsed_args, tht_root):

        if not parsed_args.baremetal_deployment:
            return []

        with open(parsed_args.baremetal_deployment, 'r') as fp:
            roles = yaml.safe_load(fp)

        key = self.get_key_pair(parsed_args)
        with open('{}.pub'.format(key), 'rt') as fp:
            ssh_key = fp.read()

        output_path = self._user_env_path(
            'baremetal-deployed.yaml',
            tht_root
        )
        extra_vars = {
            "stack_name": parsed_args.stack,
            "baremetal_deployment": roles,
            "baremetal_deployed_path": output_path,
            "ssh_public_keys": ssh_key,
            "ssh_user_name": parsed_args.overcloud_ssh_user,
        }

        with utils.TempDirs() as tmp:
            utils.run_ansible_playbook(
                playbook='cli-overcloud-node-provision.yaml',
                inventory='localhost,',
                workdir=tmp,
                playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
                verbosity=utils.playbook_verbosity(self=self),
                extra_vars=extra_vars,
            )

        with open(output_path, 'r') as fp:
            parameter_defaults = yaml.safe_load(fp)

        # TODO(sbaker) Remove this call when it is no longer necessary
        # to write to a swift object
        self._write_user_environment(
            parameter_defaults,
            'baremetal-deployed.yaml',
            tht_root,
            parsed_args.stack)

        return [output_path]
Esempio n. 15
0
    def take_action(self, parsed_args):
        self.log.debug("take_action(%s)" % parsed_args)

        nodes_config = oooutils.parse_env_file(parsed_args.env_file)
        parsed_args.env_file.close()

        if parsed_args.validate_only:
            return baremetal.validate_nodes(self.app.client_manager,
                                            nodes_json=nodes_config)

        # Look for *specific* deploy images and update the node data if
        # one is found.
        if not parsed_args.no_deploy_image:
            oooutils.update_nodes_deploy_data(nodes_config,
                                              http_boot=parsed_args.http_boot)
        nodes = baremetal.register_or_update(
            self.app.client_manager,
            nodes_json=nodes_config,
            instance_boot_option=parsed_args.instance_boot_option
        )

        nodes_uuids = [node.uuid for node in nodes]

        if parsed_args.introspect:
            extra_vars = {
                "node_uuids": nodes_uuids,
                "run_validations": parsed_args.run_validations,
                "concurrency": parsed_args.concurrency,
            }

            with oooutils.TempDirs() as tmp:
                oooutils.run_ansible_playbook(
                    playbook='cli-baremetal-introspect.yaml',
                    inventory='localhost,',
                    workdir=tmp,
                    playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
                    verbosity=oooutils.playbook_verbosity(self=self),
                    extra_vars=extra_vars
                )

        if parsed_args.provide:
            baremetal.provide(
                verbosity=oooutils.playbook_verbosity(self=self),
                node_uuids=nodes_uuids
            )
Esempio n. 16
0
    def _unprovision_baremetal(self, parsed_args):

        if not parsed_args.baremetal_deployment:
            return

        with open(parsed_args.baremetal_deployment, 'r') as fp:
            roles = yaml.safe_load(fp)

        with utils.TempDirs() as tmp:
            utils.run_ansible_playbook(
                playbook='cli-overcloud-node-unprovision.yaml',
                inventory='localhost,',
                workdir=tmp,
                playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
                verbosity=utils.playbook_verbosity(self=self),
                extra_vars={
                    "stack_name": parsed_args.stack,
                    "baremetal_deployment": roles,
                    "prompt": False,
                })
def provide(verbosity, node_uuids):
    """Provide Baremetal Nodes

    :param verbosity: Verbosity level
    :type verbosity: Integer

    :param node_uuids: List of instance UUID(s).
    :type node_uuids: List
    """

    with utils.TempDirs() as tmp:
        utils.run_ansible_playbook(
            playbook='cli-overcloud-node-provide.yaml',
            inventory='localhost,',
            workdir=tmp,
            playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
            verbosity=verbosity,
            extra_vars={
                'node_uuids': node_uuids
            }
        )

    print('Successfully provided nodes: {}'.format(node_uuids))
    def take_action(self, parsed_args):
        self.log.debug("take_action({args})".format(args=parsed_args))

        self._validate_args(parsed_args)

        if not parsed_args.yes:
            confirm = utils.prompt_user_for_confirmation(message=_(
                "Are you sure you want to delete this overcloud "
                "[y/N]? "),
                                                         logger=self.log)
            if not confirm:
                raise oscexc.CommandError("Action not confirmed, exiting.")

        with utils.TempDirs() as tmp:
            utils.run_ansible_playbook(
                "cli-overcloud-delete.yaml",
                'undercloud,',
                workdir=tmp,
                playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
                verbosity=utils.playbook_verbosity(self=self),
                extra_vars={"stack_name": parsed_args.stack})

        print("Success.")
def clean_nodes(node_uuids, verbosity=0):
    """Clean Baremetal Nodes

    :param node_uuids: List of instance UUID(s).
    :type node_uuids: List

    :param verbosity: Verbosity level
    :type verbosity: Integer
    """

    with utils.TempDirs() as tmp:
        utils.run_ansible_playbook(
            playbook='cli-baremetal-clean.yaml',
            inventory='localhost,',
            workdir=tmp,
            playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
            verbosity=verbosity,
            extra_vars={
                'node_uuids': node_uuids
            }
        )

    print('Successfully cleaned nodes: {}'.format(node_uuids))
def reset_bios_configuration(node_uuids, verbosity=0):
    """Reset BIOS settings on nodes.

    :param node_uuids: List of instance UUID(s).
    :type node_uuids: List

    :param verbosity: Verbosity level
    :type verbosity: Integer
    """

    with utils.TempDirs() as tmp:
        utils.run_ansible_playbook(
            playbook='cli-baremetal-bios-reset.yaml',
            inventory='localhost,',
            workdir=tmp,
            playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
            verbosity=verbosity,
            extra_vars={
                'node_uuids': node_uuids
            }
        )

    print('Successfully reset the BIOS for nodes: {}'.format(node_uuids))
Esempio n. 21
0
def get_horizon_url(stack, verbosity=0):
    """Return horizon URL string.

    :params stack: Stack name
    :type stack: string
    :returns: string
    """

    with utils.TempDirs() as tmp:
        horizon_tmp_file = os.path.join(tmp, 'horizon_url')
        utils.run_ansible_playbook(
            playbook='cli-undercloud-get-horizon-url.yaml',
            inventory='localhost,',
            workdir=tmp,
            playbook_dir=ANSIBLE_TRIPLEO_PLAYBOOKS,
            verbosity=verbosity,
            extra_vars={
                'stack_name': stack,
                'horizon_url_output_file': horizon_tmp_file
            })

        with open(horizon_tmp_file) as f:
            return f.read().strip()
    def _nodes_to_delete(self, parsed_args, roles):
        with oooutils.TempDirs() as tmp:
            unprovision_confirm = os.path.join(tmp, 'unprovision_confirm.json')

            oooutils.run_ansible_playbook(
                playbook='cli-overcloud-node-unprovision.yaml',
                inventory='localhost,',
                workdir=tmp,
                playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
                verbosity=oooutils.playbook_verbosity(self=self),
                extra_vars={
                    "stack_name": parsed_args.stack,
                    "baremetal_deployment": roles,
                    "prompt": True,
                    "unprovision_confirm": unprovision_confirm,
                })
            with open(unprovision_confirm) as f:
                nodes = json.load(f)
        if not nodes:
            print('No nodes to unprovision')
            return None, None
        TableArgs = collections.namedtuple('TableArgs',
                                           'print_empty max_width fit_width')
        args = TableArgs(print_empty=True, max_width=-1, fit_width=True)
        nodes_data = [(i.get('hostname', ''), i.get('name',
                                                    ''), i.get('id', ''))
                      for i in nodes]

        node_hostnames = [i['hostname'] for i in nodes if 'hostname' in i]

        formatter = table.TableFormatter()
        output = six.StringIO()
        formatter.emit_list(column_names=['hostname', 'name', 'id'],
                            data=nodes_data,
                            stdout=output,
                            parsed_args=args)
        return output.getvalue(), node_hostnames
Esempio n. 23
0
def _check_diskspace(upgrade=False, verbose_level=0):
    """Check undercloud disk space

    This runs a simple ansible playbook located in tripleo-validations
    There are currently two playbooks:
    - undercloud-disk-space.yaml
    - undercloud-disk-space-pre-upgrade.yaml
    First one checks minimal disk space for a brand new deploy.
    Second one checks minimal disk space for an upgrade.
    """
    if upgrade:
        playbook_args = constants.DEPLOY_ANSIBLE_ACTIONS['preflight-upgrade']
    else:
        playbook_args = constants.DEPLOY_ANSIBLE_ACTIONS['preflight-deploy']

    with utils.TempDirs() as tmp:
        utils.run_ansible_playbook(
            workdir=tmp,
            inventory='undercloud',
            connection='local',
            output_callback='validation_output',
            playbook_dir=constants.ANSIBLE_VALIDATION_DIR,
            verbosity=verbose_level,
            **playbook_args)
def _check_diskspace(upgrade=False):
    """Check undercloud disk space

    This runs a simple ansible playbook located in tripleo-validations
    There are currently two playbooks:
    - undercloud-disk-space.yaml
    - undercloud-disk-space-pre-upgrade.yaml
    First one checks minimal disk space for a brand new deploy.
    Second one checks minimal disk space for an upgrade.
    """
    if upgrade:
        playbook = 'undercloud-disk-space-pre-upgrade.yaml'
    else:
        playbook = 'undercloud-disk-space.yaml'

    python_interpreter = "/usr/bin/python{}".format(sys.version_info[0])
    utils.run_ansible_playbook(logger=LOG,
                               workdir=constants.ANSIBLE_VALIDATION_DIR,
                               playbook=playbook,
                               inventory='undercloud,',
                               retries=False,
                               connection='local',
                               output_callback='validation_output',
                               python_interpreter=python_interpreter)
Esempio n. 25
0
    def take_action(self, parsed_args):
        self.log.debug("take_action(%s)" % parsed_args)

        if parsed_args.file_in.name.endswith('.json'):
            params = simplejson.load(parsed_args.file_in)
        elif parsed_args.file_in.name.endswith('.yaml'):
            params = yaml.safe_load(parsed_args.file_in)
        else:
            raise exceptions.InvalidConfiguration(
                _("Invalid file extension for %s, must be json or yaml") %
                parsed_args.file_in.name)

        if 'parameter_defaults' in params:
            params = params['parameter_defaults']

        with utils.TempDirs() as tmp:
            utils.run_ansible_playbook(
                playbook='cli-update-params.yaml',
                inventory='localhost,',
                workdir=tmp,
                playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
                verbosity=utils.playbook_verbosity(self=self),
                extra_vars={"container": parsed_args.name},
                extra_vars_file={"parameters": params})
Esempio n. 26
0
 def _run_ansible(self, logger, plan, workdir, log_path_dir, playbook,
                  inventory, retries, output_callback, extra_vars,
                  python_interpreter, gathering_policy):
     rc, output = oooutils.run_ansible_playbook(
         logger=logger,
         plan=plan,
         workdir=workdir,
         log_path_dir=log_path_dir,
         playbook=playbook,
         inventory=inventory,
         retries=retries,
         output_callback=output_callback,
         extra_vars=extra_vars,
         python_interpreter=python_interpreter,
         gathering_policy=gathering_policy)
     return rc, output
Esempio n. 27
0
def invoke_plan_env_workflows(clients, stack_name, plan_env_file, verbosity=0):
    """Invokes the workflows in plan environment file"""

    try:
        with open(plan_env_file) as pf:
            plan_env_data = yaml.safe_load(pf.read())
    except IOError as exc:
        raise exceptions.PlanEnvWorkflowError('File (%s) is not found: '
                                              '%s' % (plan_env_file, exc))

    if plan_env_data and "playbook_parameters" in plan_env_data:
        static_inventory = utils.get_tripleo_ansible_inventory(
            ssh_user='******',
            stack=stack_name,
            undercloud_connection='local',
            return_inventory_file_path=True)
        with utils.TempDirs() as tmp:
            for pb, pb_vars in plan_env_data["playbook_parameters"].items():
                print('Invoking playbook ({}) specified in plan-environment'
                      ' file'.format(pb))
                LOG.debug('Running playbook "{}" with the'
                          ' following options {}.'.format(pb, pb_vars))
                playbook_dir = os.path.dirname(pb)
                if not playbook_dir:
                    playbook_dir = ANSIBLE_TRIPLEO_PLAYBOOKS

                utils.run_ansible_playbook(playbook=os.path.basename(pb),
                                           inventory=static_inventory,
                                           workdir=tmp,
                                           playbook_dir=playbook_dir,
                                           verbosity=verbosity,
                                           extra_vars=pb_vars)

    # NOTE(cloudnull): Remove this when mistral is gone.
    elif plan_env_data and "workflow_parameters" in plan_env_data:
        LOG.warning(
            'The workflow_parameters interface is deprecated, begin using'
            ' playbook_parameters instead.')
        for wf_name, wf_inputs in plan_env_data["workflow_parameters"].items():
            print('Invoking workflow (%s) specified in plan-environment '
                  'file' % wf_name)
            inputs = {'plan': stack_name, 'user_inputs': wf_inputs}
            workflow_client = clients.workflow_engine
            tripleoclients = clients.tripleoclient
            with tripleoclients.messaging_websocket() as ws:
                execution = base.start_workflow(workflow_client,
                                                wf_name,
                                                workflow_input=inputs)

                # Getting the derive parameters timeout after 600 seconds.
                for payload in base.wait_for_messages(workflow_client, ws,
                                                      execution, 600):
                    if ('message' in payload and
                        (payload.get('status', 'RUNNING') == "RUNNING")):
                        print(payload['message'])

            if payload.get('status', 'FAILED') == 'SUCCESS':
                result = payload.get('result', '')
                # Prints the workflow result
                if result:
                    print('Workflow execution is completed. result:')
                    print(yaml.safe_dump(result, default_flow_style=False))
            else:
                message = payload.get('message', '')
                msg = ('Workflow execution is failed: %s' % (message))
                raise exceptions.PlanEnvWorkflowError(msg)
Esempio n. 28
0
def config_download(log,
                    clients,
                    stack,
                    ssh_network=None,
                    output_dir=None,
                    override_ansible_cfg=None,
                    timeout=600,
                    verbosity=0,
                    deployment_options=None,
                    in_flight_validations=False,
                    ansible_playbook_name='deploy_steps_playbook.yaml',
                    limit_hosts=None,
                    extra_vars=None,
                    inventory_path=None,
                    ssh_user='******',
                    tags=None,
                    skip_tags=None,
                    deployment_timeout=None):
    """Run config download.

    :param log: Logging object
    :type log: Object

    :param clients: openstack clients
    :type clients: Object

    :param stack: Heat Stack object
    :type stack: Object

    :param ssh_network: Network named used to access the overcloud.
    :type ssh_network: String

    :param output_dir: Path to the output directory.
    :type output_dir: String

    :param override_ansible_cfg: Ansible configuration file location.
    :type override_ansible_cfg: String

    :param timeout: Ansible connection timeout in seconds.
    :type timeout: Integer

    :param verbosity: Ansible verbosity level.
    :type verbosity: Integer

    :param deployment_options: Additional deployment options.
    :type deployment_options: Dictionary

    :param in_flight_validations: Enable or Disable inflight validations.
    :type in_flight_validations: Boolean

    :param ansible_playbook_name: Name of the playbook to execute.
    :type ansible_playbook_name: String

    :param limit_hosts: String of hosts to limit the current playbook to.
    :type limit_hosts: String

    :param extra_vars: Set additional variables as a Dict or the absolute
                       path of a JSON or YAML file type.
    :type extra_vars: Either a Dict or the absolute path of JSON or YAML

    :param inventory_path: Inventory file or path, if None is provided this
                           function will perform a lookup
    :type inventory_path: String

    :param ssh_user: SSH user, defaults to tripleo-admin.
    :type ssh_user: String

    :param tags: Ansible inclusion tags.
    :type tags: String

    :param skip_tags: Ansible exclusion tags.
    :type skip_tags: String

    :param deployment_timeout: Deployment timeout in minutes.
    :type deployment_timeout: Integer

    """
    def _log_and_print(message, logger, level='info', print_msg=True):
        """Print and log a given message.

        :param message: Message to print and log.
        :type message: String

        :param log: Logging object
        :type log: Object

        :param level: Log level.
        :type level: String

        :param print_msg: Print messages to stdout.
        :type print_msg: Boolean
        """

        if print_msg:
            print(message)

        log = getattr(logger, level)
        log(message)

    if not output_dir:
        output_dir = DEFAULT_WORK_DIR

    if not deployment_options:
        deployment_options = dict()

    if not in_flight_validations:
        if skip_tags:
            skip_tags = 'opendev-validation,{}'.format(skip_tags)
        else:
            skip_tags = 'opendev-validation'

    with utils.TempDirs() as tmp:
        utils.run_ansible_playbook(playbook='cli-grant-local-access.yaml',
                                   inventory='localhost,',
                                   workdir=tmp,
                                   playbook_dir=ANSIBLE_TRIPLEO_PLAYBOOKS,
                                   verbosity=verbosity,
                                   extra_vars={
                                       'access_path': output_dir,
                                       'execution_user': getpass.getuser()
                                   })

    stack_work_dir = os.path.join(output_dir, stack.stack_name)
    context = clients.tripleoclient.create_mistral_context()
    _log_and_print(
        message='Checking for blacklisted hosts from stack: {}'.format(
            stack.stack_name),
        logger=log,
        print_msg=(verbosity == 0))
    blacklist_show = stack.output_show('BlacklistedHostnames')
    blacklist_stack_output = blacklist_show.get('output', dict())
    blacklist_stack_output_value = blacklist_stack_output.get('output_value')
    if blacklist_stack_output_value:

        if not limit_hosts:
            limit_hosts = ""

        limit_hosts += (':'.join(
            ['!{}'.format(i) for i in blacklist_stack_output_value if i]))

    _log_and_print(message='Retrieving configuration for stack: {}'.format(
        stack.stack_name),
                   logger=log,
                   print_msg=(verbosity == 0))
    container_config = '{}-config'.format(stack.stack_name)

    utils.get_config(clients,
                     container=stack.stack_name,
                     container_config=container_config)
    _log_and_print(message='Downloading configuration for stack: {}'.format(
        stack.stack_name),
                   logger=log,
                   print_msg=(verbosity == 0))
    download = config.DownloadConfigAction(work_dir=stack_work_dir,
                                           container_config=container_config)

    work_dir = download.run(context=context)
    _log_and_print(message='Retrieving keyfile for stack: {}'.format(
        stack.stack_name),
                   logger=log,
                   print_msg=(verbosity == 0))
    key_file = utils.get_key(stack=stack.stack_name)
    _log_and_print(message='Generating information for stack: {}'.format(
        stack.stack_name),
                   logger=log,
                   print_msg=(verbosity == 0))
    inventory_kwargs = {
        'ansible_ssh_user': ssh_user,
        'work_dir': work_dir,
        'plan_name': stack.stack_name,
        'undercloud_key_file': key_file
    }
    if ssh_network:
        inventory_kwargs['ssh_network'] = ssh_network
    python_interpreter = deployment_options.get('ansible_python_interpreter')
    if python_interpreter:
        inventory_kwargs['ansible_python_interpreter'] = python_interpreter
    if not inventory_path:
        inventory = ansible.AnsibleGenerateInventoryAction(**inventory_kwargs)
        inventory_path = inventory.run(context=context)
    _log_and_print(
        message='Executing deployment playbook for stack: {}'.format(
            stack.stack_name),
        logger=log,
        print_msg=(verbosity == 0))

    if isinstance(ansible_playbook_name, list):
        playbooks = [
            os.path.join(stack_work_dir, p) for p in ansible_playbook_name
        ]
    else:
        playbooks = os.path.join(stack_work_dir, ansible_playbook_name)

    with utils.TempDirs() as tmp:
        utils.run_ansible_playbook(
            playbook=playbooks,
            inventory=inventory_path,
            workdir=tmp,
            playbook_dir=work_dir,
            skip_tags=skip_tags,
            tags=tags,
            ansible_cfg=override_ansible_cfg,
            verbosity=verbosity,
            ssh_user=ssh_user,
            key=key_file,
            limit_hosts=limit_hosts,
            ansible_timeout=timeout,
            reproduce_command=True,
            extra_env_variables={
                'ANSIBLE_BECOME': True,
            },
            extra_vars=extra_vars,
            timeout=deployment_timeout,
        )

    _log_and_print(
        message='Overcloud configuration completed for stack: {}'.format(
            stack.stack_name),
        logger=log,
        print_msg=(verbosity == 0))
    def _process_and_upload_environment(self, container_name,
                                        env, moved_files, tht_root):
        """Process the environment and upload to Swift

        The environment at this point should be the result of the merged
        custom user environments. We need to look at the paths in the
        environment and update any that changed when they were uploaded to
        swift.
        """

        file_prefix = "file://"

        if env.get('resource_registry'):
            for name, path in env['resource_registry'].items():
                if not isinstance(path, six.string_types):
                    continue
                if path in moved_files:
                    new_path = moved_files[path]
                    env['resource_registry'][name] = new_path
                elif path.startswith(file_prefix):
                    path = path[len(file_prefix):]
                    if path.startswith(tht_root):
                        path = path[len(tht_root):]
                    # We want to make sure all the paths are relative.
                    if path.startswith("/"):
                        path = path[1:]
                    env['resource_registry'][name] = path

        # Parameters are removed from the environment
        params = env.pop('parameter_defaults', None)

        contents = yaml.safe_dump(env, default_flow_style=False)

        # Until we have a well defined plan update workflow in tripleo-common
        # we need to manually add an environment in swift and for users
        # custom environments passed to the deploy command.
        # See bug: https://bugs.launchpad.net/tripleo/+bug/1623431
        # Update plan env.
        swift_path = "user-environment.yaml"
        self.object_client.put_object(container_name, swift_path, contents)

        env = yaml.safe_load(self.object_client.get_object(
            container_name, constants.PLAN_ENVIRONMENT)[1])

        user_env = {'path': swift_path}
        if user_env not in env['environments']:
            env['environments'].append(user_env)
            yaml_string = yaml.safe_dump(env, default_flow_style=False)
            self.object_client.put_object(
                container_name, constants.PLAN_ENVIRONMENT, yaml_string)

        # Parameters are sent to the update parameters action, this stores them
        # in the plan environment and means the UI can find them.
        if params:
            with utils.TempDirs() as tmp:
                utils.run_ansible_playbook(
                    playbook='cli-update-params.yaml',
                    inventory='localhost,',
                    workdir=tmp,
                    playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
                    verbosity=utils.playbook_verbosity(self=self),
                    extra_vars={
                        "container": container_name
                    },
                    extra_vars_file={
                        "parameters": params
                    }
                )