Ejemplo n.º 1
0
def run_playbooks(parsed_args,
                  playbooks,
                  extra_vars=None,
                  limit=None,
                  tags=None,
                  quiet=False,
                  check_output=False,
                  verbose_level=None,
                  check=None,
                  ignore_limit=False):
    """Run a Kayobe Ansible playbook."""
    _validate_args(parsed_args, playbooks)
    cmd = build_args(parsed_args,
                     playbooks,
                     extra_vars=extra_vars,
                     limit=limit,
                     tags=tags,
                     verbose_level=verbose_level,
                     check=check,
                     ignore_limit=ignore_limit)
    env = os.environ.copy()
    vault.update_environment(parsed_args, env)
    # If the configuration path has been specified via --config-path, ensure
    # the environment variable is set, so that it can be referenced by
    # playbooks.
    env.setdefault(CONFIG_PATH_ENV, parsed_args.config_path)
    try:
        utils.run_command(cmd, check_output=check_output, quiet=quiet, env=env)
    except subprocess.CalledProcessError as e:
        LOG.error("Kayobe playbook(s) %s exited %d", ", ".join(playbooks),
                  e.returncode)
        if check_output:
            LOG.error("The output was:\n%s", e.output)
        sys.exit(e.returncode)
Ejemplo n.º 2
0
def run_playbooks(parsed_args,
                  playbooks,
                  extra_vars=None,
                  limit=None,
                  tags=None,
                  quiet=False,
                  verbose_level=None,
                  check=None):
    """Run a Kayobe Ansible playbook."""
    _validate_args(parsed_args, playbooks)
    cmd = build_args(parsed_args,
                     playbooks,
                     extra_vars=extra_vars,
                     limit=limit,
                     tags=tags,
                     verbose_level=verbose_level,
                     check=check)
    env = os.environ.copy()
    # If the Vault password has been specified via --vault-password-file,
    # ensure the environment variable is set, so that it can be referenced by
    # playbooks to generate the kolla-ansible passwords.yml file.
    if vault.VAULT_PASSWORD_ENV not in env and parsed_args.vault_password_file:
        vault_password = _read_vault_password_file(
            parsed_args.vault_password_file)
        env[vault.VAULT_PASSWORD_ENV] = vault_password
    # If the configuration path has been specified via --config-path, ensure
    # the environment variable is set, so that it can be referenced by
    # playbooks.
    env.setdefault(CONFIG_PATH_ENV, parsed_args.config_path)
    try:
        utils.run_command(cmd, quiet=quiet, env=env)
    except subprocess.CalledProcessError as e:
        LOG.error("Kayobe playbook(s) %s exited %d", ", ".join(playbooks),
                  e.returncode)
        sys.exit(e.returncode)
Ejemplo n.º 3
0
def run_playbooks(parsed_args,
                  playbooks,
                  extra_vars=None,
                  limit=None,
                  tags=None,
                  quiet=False,
                  check_output=False,
                  verbose_level=None,
                  check=None,
                  ignore_limit=False,
                  list_tasks=None):
    """Run a Kayobe Ansible playbook."""
    _validate_args(parsed_args, playbooks)
    cmd = build_args(parsed_args,
                     playbooks,
                     extra_vars=extra_vars,
                     limit=limit,
                     tags=tags,
                     verbose_level=verbose_level,
                     check=check,
                     ignore_limit=ignore_limit,
                     list_tasks=list_tasks)
    env = _get_environment(parsed_args)
    try:
        utils.run_command(cmd, check_output=check_output, quiet=quiet, env=env)
    except subprocess.CalledProcessError as e:
        LOG.error("Kayobe playbook(s) %s exited %d", ", ".join(playbooks),
                  e.returncode)
        if check_output:
            LOG.error("The output was:\n%s", e.output)
        sys.exit(e.returncode)
Ejemplo n.º 4
0
def run(parsed_args,
        command,
        inventory_filename,
        extra_vars=None,
        tags=None,
        quiet=False,
        verbose_level=None,
        extra_args=None,
        limit=None):
    """Run a Kolla Ansible command."""
    _validate_args(parsed_args, inventory_filename)
    cmd = build_args(parsed_args,
                     command,
                     inventory_filename=inventory_filename,
                     extra_vars=extra_vars,
                     tags=tags,
                     verbose_level=verbose_level,
                     extra_args=extra_args,
                     limit=limit)
    env = _get_environment(parsed_args)
    try:
        utils.run_command(" ".join(cmd), quiet=quiet, shell=True, env=env)
    except subprocess.CalledProcessError as e:
        LOG.error("kolla-ansible %s exited %d", command, e.returncode)
        sys.exit(e.returncode)
Ejemplo n.º 5
0
def run_playbooks(parsed_args, playbooks,
                  extra_vars=None, limit=None, tags=None, quiet=False,
                  verbose_level=None, check=None):
    """Run a Kayobe Ansible playbook."""
    _validate_args(parsed_args, playbooks)
    cmd = build_args(parsed_args, playbooks,
                     extra_vars=extra_vars, limit=limit, tags=tags,
                     verbose_level=verbose_level, check=check)
    try:
        utils.run_command(cmd, quiet=quiet)
    except subprocess.CalledProcessError as e:
        LOG.error("Kayobe playbook(s) %s exited %d",
                  ", ".join(playbooks), e.returncode)
        sys.exit(e.returncode)
Ejemplo n.º 6
0
def run_playbooks(parsed_args, playbooks,
                  extra_vars=None, limit=None, tags=None, quiet=False,
                  verbose_level=None, check=None):
    """Run a Kayobe Ansible playbook."""
    _validate_args(parsed_args, playbooks)
    cmd = build_args(parsed_args, playbooks,
                     extra_vars=extra_vars, limit=limit, tags=tags,
                     verbose_level=verbose_level, check=check)
    try:
        utils.run_command(cmd, quiet=quiet)
    except subprocess.CalledProcessError as e:
        LOG.error("Kayobe playbook(s) %s exited %d",
                  ", ".join(playbooks), e.returncode)
        sys.exit(e.returncode)
Ejemplo n.º 7
0
 def test_run_command_quiet(self, mock_call, mock_open):
     mock_devnull = mock_open.return_value.__enter__.return_value
     output = utils.run_command(["command", "to", "run"], quiet=True)
     mock_call.assert_called_once_with(["command", "to", "run"],
                                       stdout=mock_devnull,
                                       stderr=mock_devnull)
     self.assertIsNone(output)
Ejemplo n.º 8
0
 def test_run_command_quiet(self, mock_call, mock_open):
     mock_devnull = mock_open.return_value.__enter__.return_value
     output = utils.run_command(["command", "to", "run"], quiet=True)
     mock_call.assert_called_once_with(["command", "to", "run"],
                                       stdout=mock_devnull,
                                       stderr=mock_devnull)
     self.assertIsNone(output)
Ejemplo n.º 9
0
def run(parsed_args, command, inventory_filename, extra_vars=None,
        tags=None, quiet=False, verbose_level=None, extra_args=None,
        limit=None):
    """Run a Kolla Ansible command."""
    _validate_args(parsed_args, inventory_filename)
    cmd = build_args(parsed_args, command,
                     inventory_filename=inventory_filename,
                     extra_vars=extra_vars, tags=tags,
                     verbose_level=verbose_level,
                     extra_args=extra_args,
                     limit=limit)
    try:
        utils.run_command(" ".join(cmd), quiet=quiet, shell=True)
    except subprocess.CalledProcessError as e:
        LOG.error("kolla-ansible %s exited %d", command, e.returncode)
        sys.exit(e.returncode)
Ejemplo n.º 10
0
def _get_vault_password_helper():
    """Return the path to the kayobe-vault-password-helper executable."""
    cmd = ["which", "kayobe-vault-password-helper"]
    try:
        output = utils.run_command(cmd, check_output=True)
    except subprocess.CalledProcessError:
        return None
    return output.strip()
Ejemplo n.º 11
0
def _get_vault_password_helper():
    """Return the path to the kayobe-vault-password-helper executable."""
    cmd = ["which", "kayobe-vault-password-helper"]
    try:
        # NOTE(mgoddard): universal_newlines ensures stdout is opened in text
        # mode, and we get a string rather than bytes.
        output = utils.run_command(cmd,
                                   check_output=True,
                                   universal_newlines=True)
    except subprocess.CalledProcessError:
        return None
    return output.strip()
Ejemplo n.º 12
0
def _get_default_vault_password_file():
    """Return the default value for the vault password file argument.

    It is possible to use an environment variable to avoid typing the vault
    password.
    """
    if not os.getenv(VAULT_PASSWORD_ENV):
        return None
    cmd = ["which", "kayobe-vault-password-helper"]
    try:
        output = utils.run_command(cmd, check_output=True)
    except subprocess.CalledProcessError:
        return None
    return output.strip()
Ejemplo n.º 13
0
def _get_default_vault_password_file():
    """Return the default value for the vault password file argument.

    It is possible to use an environment variable to avoid typing the vault
    password.
    """
    if not os.getenv(VAULT_PASSWORD_ENV):
        return None
    cmd = ["which", "kayobe-vault-password-helper"]
    try:
        output = utils.run_command(cmd, check_output=True)
    except subprocess.CalledProcessError:
        return None
    return output.strip()
Ejemplo n.º 14
0
 def test_run_command(self, mock_call):
     output = utils.run_command(["command", "to", "run"])
     mock_call.assert_called_once_with(["command", "to", "run"])
     self.assertIsNone(output)
Ejemplo n.º 15
0
 def test_run_command_check_output(self, mock_output):
     mock_output.return_value = "command output"
     output = utils.run_command(["command", "to", "run"], check_output=True)
     mock_output.assert_called_once_with(["command", "to", "run"])
     self.assertEqual(output, "command output")
Ejemplo n.º 16
0
 def test_run_command(self, mock_call):
     output = utils.run_command(["command", "to", "run"])
     mock_call.assert_called_once_with(["command", "to", "run"])
     self.assertIsNone(output)
Ejemplo n.º 17
0
 def test_run_command_check_output(self, mock_output):
     mock_output.return_value = "command output"
     output = utils.run_command(["command", "to", "run"], check_output=True)
     mock_output.assert_called_once_with(["command", "to", "run"])
     self.assertEqual(output, "command output")