Ejemplo n.º 1
0
def has_plugin(command, plugin):
    """
    Check if helm plugin is installed.
    """

    cmd = command + " plugin"
    rc, output, err = get_helm_plugin_list(module, helm_bin=cmd)
    out = parse_helm_plugin_list(module, output=output.splitlines())

    if not out:
        return False

    for line in out:
        if line[0] == plugin:
            return True
    return False
Ejemplo n.º 2
0
def get_plugin_version(command, plugin):
    """
    Check if helm plugin is installed and return corresponding version
    """

    cmd = command + " plugin"
    rc, output, err = get_helm_plugin_list(module, helm_bin=cmd)
    out = parse_helm_plugin_list(module, output=output.splitlines())

    if not out:
        return None

    for line in out:
        if line[0] == plugin:
            return line[1]
    return None
def main():
    module = AnsibleModule(
        argument_spec=dict(
            binary_path=dict(type="path"),
            plugin_name=dict(type="str",),
            # Helm options
            context=dict(
                type="str",
                aliases=["kube_context"],
                fallback=(env_fallback, ["K8S_AUTH_CONTEXT"]),
            ),
            kubeconfig=dict(
                type="path",
                aliases=["kubeconfig_path"],
                fallback=(env_fallback, ["K8S_AUTH_KUBECONFIG"]),
            ),
            # Generic auth key
            host=dict(type="str", fallback=(env_fallback, ["K8S_AUTH_HOST"])),
            ca_cert=dict(
                type="path",
                aliases=["ssl_ca_cert"],
                fallback=(env_fallback, ["K8S_AUTH_SSL_CA_CERT"]),
            ),
            validate_certs=dict(
                type="bool",
                default=True,
                aliases=["verify_ssl"],
                fallback=(env_fallback, ["K8S_AUTH_VERIFY_SSL"]),
            ),
            api_key=dict(
                type="str", no_log=True, fallback=(env_fallback, ["K8S_AUTH_API_KEY"])
            ),
        ),
        mutually_exclusive=[
            ("context", "ca_cert"),
            ("context", "validate_certs"),
            ("kubeconfig", "ca_cert"),
            ("kubeconfig", "validate_certs"),
        ],
        supports_check_mode=True,
    )

    bin_path = module.params.get("binary_path")

    if bin_path is not None:
        helm_cmd_common = bin_path
    else:
        helm_cmd_common = "helm"

    helm_cmd_common = module.get_bin_path(helm_cmd_common, required=True)

    helm_cmd_common += " plugin"

    plugin_name = module.params.get("plugin_name")

    plugin_list = []

    rc, output, err = get_helm_plugin_list(module, helm_bin=helm_cmd_common)

    out = parse_helm_plugin_list(module, output=output.splitlines())

    for line in out:
        if plugin_name is None:
            plugin_list.append(
                {"name": line[0], "version": line[1], "description": line[2]}
            )
            continue

        if plugin_name == line[0]:
            plugin_list.append(
                {"name": line[0], "version": line[1], "description": line[2]}
            )
            break

    module.exit_json(
        changed=True,
        command=helm_cmd_common + " list",
        stdout=output,
        stderr=err,
        rc=rc,
        plugin_list=plugin_list,
    )
Ejemplo n.º 4
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            binary_path=dict(type="path"),
            state=dict(type="str",
                       default="present",
                       choices=["present", "absent", "latest"]),
            plugin_path=dict(type="str", ),
            plugin_name=dict(type="str", ),
            plugin_version=dict(type="str", ),
            # Helm options
            context=dict(
                type="str",
                aliases=["kube_context"],
                fallback=(env_fallback, ["K8S_AUTH_CONTEXT"]),
            ),
            kubeconfig=dict(
                type="path",
                aliases=["kubeconfig_path"],
                fallback=(env_fallback, ["K8S_AUTH_KUBECONFIG"]),
            ),
            # Generic auth key
            host=dict(type="str", fallback=(env_fallback, ["K8S_AUTH_HOST"])),
            ca_cert=dict(
                type="path",
                aliases=["ssl_ca_cert"],
                fallback=(env_fallback, ["K8S_AUTH_SSL_CA_CERT"]),
            ),
            validate_certs=dict(
                type="bool",
                default=True,
                aliases=["verify_ssl"],
                fallback=(env_fallback, ["K8S_AUTH_VERIFY_SSL"]),
            ),
            api_key=dict(type="str",
                         no_log=True,
                         fallback=(env_fallback, ["K8S_AUTH_API_KEY"])),
        ),
        supports_check_mode=True,
        required_if=[
            ("state", "present", ("plugin_path", )),
            ("state", "absent", ("plugin_name", )),
            ("state", "latest", ("plugin_name", )),
        ],
        mutually_exclusive=[
            ("plugin_name", "plugin_path"),
            ("context", "ca_cert"),
            ("context", "validate_certs"),
            ("kubeconfig", "ca_cert"),
            ("kubeconfig", "validate_certs"),
        ],
    )

    bin_path = module.params.get("binary_path")
    state = module.params.get("state")

    if bin_path is not None:
        helm_cmd_common = bin_path
    else:
        helm_cmd_common = "helm"

    helm_cmd_common = module.get_bin_path(helm_cmd_common, required=True)

    helm_cmd_common += " plugin"

    if state == "present":
        helm_cmd_common += " install %s" % module.params.get("plugin_path")
        plugin_version = module.params.get("plugin_version")
        if plugin_version is not None:
            helm_cmd_common += " --version=%s" % plugin_version
        if not module.check_mode:
            rc, out, err = run_helm(module,
                                    helm_cmd_common,
                                    fails_on_error=False)
        else:
            rc, out, err = (0, "", "")

        if rc == 1 and "plugin already exists" in err:
            module.exit_json(
                failed=False,
                changed=False,
                msg="Plugin already exists",
                command=helm_cmd_common,
                stdout=out,
                stderr=err,
                rc=rc,
            )
        elif rc == 0:
            module.exit_json(
                failed=False,
                changed=True,
                msg="Plugin installed successfully",
                command=helm_cmd_common,
                stdout=out,
                stderr=err,
                rc=rc,
            )
        else:
            module.fail_json(
                msg="Failure when executing Helm command.",
                command=helm_cmd_common,
                stdout=out,
                stderr=err,
                rc=rc,
            )
    elif state == "absent":
        plugin_name = module.params.get("plugin_name")
        rc, output, err = get_helm_plugin_list(module,
                                               helm_bin=helm_cmd_common)
        out = parse_helm_plugin_list(module, output=output.splitlines())

        if not out:
            module.exit_json(
                failed=False,
                changed=False,
                msg="Plugin not found or is already uninstalled",
                command=helm_cmd_common + " list",
                stdout=output,
                stderr=err,
                rc=rc,
            )

        found = False
        for line in out:
            if line[0] == plugin_name:
                found = True
                break
        if not found:
            module.exit_json(
                failed=False,
                changed=False,
                msg="Plugin not found or is already uninstalled",
                command=helm_cmd_common + " list",
                stdout=output,
                stderr=err,
                rc=rc,
            )

        helm_uninstall_cmd = "%s uninstall %s" % (helm_cmd_common, plugin_name)
        if not module.check_mode:
            rc, out, err = run_helm(module,
                                    helm_uninstall_cmd,
                                    fails_on_error=False)
        else:
            rc, out, err = (0, "", "")

        if rc == 0:
            module.exit_json(
                changed=True,
                msg="Plugin uninstalled successfully",
                command=helm_uninstall_cmd,
                stdout=out,
                stderr=err,
                rc=rc,
            )
        module.fail_json(
            msg="Failed to get Helm plugin uninstall",
            command=helm_uninstall_cmd,
            stdout=out,
            stderr=err,
            rc=rc,
        )
    elif state == "latest":
        plugin_name = module.params.get("plugin_name")
        rc, output, err = get_helm_plugin_list(module,
                                               helm_bin=helm_cmd_common)
        out = parse_helm_plugin_list(module, output=output.splitlines())

        if not out:
            module.exit_json(
                failed=False,
                changed=False,
                msg="Plugin not found",
                command=helm_cmd_common + " list",
                stdout=output,
                stderr=err,
                rc=rc,
            )

        found = False
        for line in out:
            if line[0] == plugin_name:
                found = True
                break
        if not found:
            module.exit_json(
                failed=False,
                changed=False,
                msg="Plugin not found",
                command=helm_cmd_common + " list",
                stdout=output,
                stderr=err,
                rc=rc,
            )

        helm_update_cmd = "%s update %s" % (helm_cmd_common, plugin_name)
        if not module.check_mode:
            rc, out, err = run_helm(module,
                                    helm_update_cmd,
                                    fails_on_error=False)
        else:
            rc, out, err = (0, "", "")

        if rc == 0:
            module.exit_json(
                changed=True,
                msg="Plugin updated successfully",
                command=helm_update_cmd,
                stdout=out,
                stderr=err,
                rc=rc,
            )
        module.fail_json(
            msg="Failed to get Helm plugin update",
            command=helm_update_cmd,
            stdout=out,
            stderr=err,
            rc=rc,
        )
Ejemplo n.º 5
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            binary_path=dict(type='path'),
            state=dict(type='str',
                       default='present',
                       choices=['present', 'absent']),
            plugin_path=dict(type='str', ),
            plugin_name=dict(type='str', ),
            # Helm options
            context=dict(type='str',
                         aliases=['kube_context'],
                         fallback=(env_fallback, ['K8S_AUTH_CONTEXT'])),
            kubeconfig=dict(type='path',
                            aliases=['kubeconfig_path'],
                            fallback=(env_fallback, ['K8S_AUTH_KUBECONFIG'])),

            # Generic auth key
            host=dict(type='str', fallback=(env_fallback, ['K8S_AUTH_HOST'])),
            ca_cert=dict(type='path',
                         aliases=['ssl_ca_cert'],
                         fallback=(env_fallback, ['K8S_AUTH_SSL_CA_CERT'])),
            validate_certs=dict(type='bool',
                                default=True,
                                aliases=['verify_ssl'],
                                fallback=(env_fallback,
                                          ['K8S_AUTH_VERIFY_SSL'])),
            api_key=dict(type='str',
                         no_log=True,
                         fallback=(env_fallback, ['K8S_AUTH_API_KEY']))),
        supports_check_mode=True,
        required_if=[
            ("state", "present", ("plugin_path", )),
            ("state", "absent", ("plugin_name", )),
        ],
        mutually_exclusive=[('plugin_name', 'plugin_path'),
                            ("context", "ca_cert"),
                            ("context", "validate_certs"),
                            ("kubeconfig", "ca_cert"),
                            ("kubeconfig", "validate_certs")],
    )

    bin_path = module.params.get('binary_path')
    state = module.params.get('state')

    if bin_path is not None:
        helm_cmd_common = bin_path
    else:
        helm_cmd_common = 'helm'

    helm_cmd_common = module.get_bin_path(helm_cmd_common, required=True)

    helm_cmd_common += " plugin"

    if state == 'present':
        helm_cmd_common += " install %s" % module.params.get('plugin_path')
        if not module.check_mode:
            rc, out, err = run_helm(module,
                                    helm_cmd_common,
                                    fails_on_error=False)
        else:
            rc, out, err = (0, '', '')

        if rc == 1 and 'plugin already exists' in err:
            module.exit_json(failed=False,
                             changed=False,
                             msg="Plugin already exists",
                             command=helm_cmd_common,
                             stdout=out,
                             stderr=err,
                             rc=rc)
        elif rc == 0:
            module.exit_json(
                failed=False,
                changed=True,
                msg="Plugin installed successfully",
                command=helm_cmd_common,
                stdout=out,
                stderr=err,
                rc=rc,
            )
        else:
            module.fail_json(
                msg="Failure when executing Helm command.",
                command=helm_cmd_common,
                stdout=out,
                stderr=err,
                rc=rc,
            )
    elif state == 'absent':
        plugin_name = module.params.get('plugin_name')
        rc, output, err = get_helm_plugin_list(module,
                                               helm_bin=helm_cmd_common)
        out = parse_helm_plugin_list(module, output=output.splitlines())

        if not out:
            module.exit_json(failed=False,
                             changed=False,
                             msg="Plugin not found or is already uninstalled",
                             command=helm_cmd_common + " list",
                             stdout=output,
                             stderr=err,
                             rc=rc)

        found = False
        for line in out:
            if line[0] == plugin_name:
                found = True
                break
        if not found:
            module.exit_json(failed=False,
                             changed=False,
                             msg="Plugin not found or is already uninstalled",
                             command=helm_cmd_common + " list",
                             stdout=output,
                             stderr=err,
                             rc=rc)

        helm_uninstall_cmd = "%s uninstall %s" % (helm_cmd_common, plugin_name)
        if not module.check_mode:
            rc, out, err = run_helm(module,
                                    helm_uninstall_cmd,
                                    fails_on_error=False)
        else:
            rc, out, err = (0, '', '')

        if rc == 0:
            module.exit_json(changed=True,
                             msg="Plugin uninstalled successfully",
                             command=helm_uninstall_cmd,
                             stdout=out,
                             stderr=err,
                             rc=rc)
        module.fail_json(
            msg="Failed to get Helm plugin uninstall",
            command=helm_uninstall_cmd,
            stdout=out,
            stderr=err,
            rc=rc,
        )