Exemple #1
0
def _extend_reservation(name, namespace, requester, duration):
    def _err_handler(err):
        msg = f"reservation extension failed: {str(err)}"
        _error(msg)

    try:
        res = get_reservation(name, namespace, requester)
        if res:
            res_config = process_reservation(
                res["metadata"]["name"],
                res["spec"]["requester"],
                duration,
            )

            log.debug("processed reservation:\n%s", res_config)

            apply_config(None, list_resource=res_config)
        else:
            raise FatalError("Reservation lookup failed")
    except KeyboardInterrupt as err:
        log.error("aborted by keyboard interrupt!")
        _err_handler(err)
    except TimedOutError as err:
        log.error("hit timeout error: %s", err)
        _err_handler(err)
    except FatalError as err:
        log.error("hit fatal error: %s", err)
        _err_handler(err)
    except Exception as err:
        log.exception("hit unexpected error!")
        _err_handler(err)
    else:
        log.info("reservation '%s' extended by '%s'", res["metadata"]["name"],
                 duration)
Exemple #2
0
def _cmd_config_deploy(
    apps,
    src_env,
    ref_env,
    set_template_ref,
    set_image_tag,
    get_dependencies,
    namespace,
    local_config,
    local_config_path,
    duration,
    retries,
    timeout,
):
    """Reserve a namespace, get config for app(s), and deploy to OpenShift"""
    if local_config:
        local_config_data = _load_file(local_config_path)

    requested_ns = namespace

    log.info("logging into OpenShift...")
    oc_login()
    log.info(
        "reserving ephemeral namespace%s...",
        f" '{requested_ns}'" if requested_ns else "",
    )
    ns = _reserve_namespace(duration, retries, requested_ns)

    try:
        if local_config:
            config = process_local_config(ns, local_config_data,
                                          apps.split(","), get_dependencies)
        else:
            log.info("getting app configs from qontract-server...")
            config = _get_app_config(apps, src_env, ref_env, set_template_ref,
                                     set_image_tag, get_dependencies, ns)

        log.debug("app configs:\n%s", json.dumps(config, indent=2))
        if not config["items"]:
            log.warning("no configurations found to apply!")
        else:
            log.info("applying app configs...")
            apply_config(ns, config)
            log.info("waiting on resources...")
            _wait_on_namespace_resources(ns, timeout)
    except (Exception, KeyboardInterrupt):
        log.exception("hit unexpected error!")
        try:
            if not requested_ns:
                log.info("releasing namespace '%s'", ns)
                release_namespace(ns)
        finally:
            _error("deploy failed")
    else:
        log.info("successfully deployed to %s", ns)
        print(ns)
Exemple #3
0
def _cmd_deploy_iqe_cji(
    namespace,
    clowd_app_name,
    debug,
    marker,
    filter,
    env,
    image_tag,
    cji_name,
    template_file,
    timeout,
    requirements,
    requirements_priority,
    test_importance,
):
    """Process IQE CJI template, apply it, and wait for it to start running."""
    _warn_if_unsafe(namespace)

    def _err_handler(err):
        msg = f"deploy failed: {str(err)}"
        _error(msg)

    try:
        cji_config = process_iqe_cji(clowd_app_name, debug, marker, filter,
                                     env, image_tag, cji_name, template_file)

        log.debug("processed CJI config:\n%s", cji_config)

        try:
            cji_name = cji_config["items"][0]["metadata"]["name"]
        except (KeyError, IndexError):
            raise Exception(
                "error parsing name of CJI from processed template, check CJI template"
            )

        apply_config(namespace, cji_config)

        log.info("waiting on CJI '%s' for max of %dsec...", cji_name, timeout)
        pod_name = wait_on_cji(namespace, cji_name, timeout)
    except KeyboardInterrupt as err:
        log.error("aborted by keyboard interrupt!")
        _err_handler(err)
    except TimedOutError as err:
        log.error("hit timeout error: %s", err)
        _err_handler(err)
    except FatalError as err:
        log.error("hit fatal error: %s", err)
        _err_handler(err)
    except Exception as err:
        log.exception("hit unexpected error!")
        _err_handler(err)
    else:
        log.info("pod '%s' related to CJI '%s' in ns '%s' is running",
                 pod_name, cji_name, namespace)
        click.echo(pod_name)
Exemple #4
0
def _create_new_reservation(bot, name, requester, duration, timeout):
    def _err_handler(err):
        msg = f"reservation failed: {str(err)}"
        _error(msg)

    try:
        res = get_reservation(name)
        # Name should be unique on reservation creation.
        if res:
            raise FatalError(f"Reservation with name {name} already exists")

        res_config = process_reservation(name, requester, duration)

        log.debug("processed reservation:\n%s", res_config)

        if not bot:
            if check_for_existing_reservation(
                    res_config["items"][0]["spec"]["requester"]):
                _warn_of_existing(res_config["items"][0]["spec"]["requester"])

        try:
            res_name = res_config["items"][0]["metadata"]["name"]
        except (KeyError, IndexError):
            raise Exception(
                "error parsing name of Reservation from processed template, "
                "check Reservation template")

        apply_config(None, list_resource=res_config)

        ns_name = wait_on_reservation(res_name, timeout)
    except KeyboardInterrupt as err:
        log.error("aborted by keyboard interrupt!")
        _err_handler(err)
    except TimedOutError as err:
        log.error("hit timeout error: %s", err)
        _err_handler(err)
    except FatalError as err:
        log.error("hit fatal error: %s", err)
        _err_handler(err)
    except Exception as err:
        log.exception("hit unexpected error!")
        _err_handler(err)
    else:
        log.info(
            "namespace '%s' is reserved by '%s' for '%s'",
            ns_name,
            res_config["items"][0]["spec"]["requester"],
            duration,
        )
        click.echo(ns_name)
Exemple #5
0
def _cmd_deploy_clowdenv(namespace, quay_user, clowd_env, template_file,
                         timeout, import_secrets, secrets_dir):
    """Process ClowdEnv template and deploy to a cluster"""
    _warn_if_unsafe(namespace)

    def _err_handler(err):
        msg = f"deploy failed: {str(err)}"
        _error(msg)

    try:
        if import_secrets:
            import_secrets_from_dir(secrets_dir)

        clowd_env_config = _process_clowdenv(namespace, quay_user, clowd_env,
                                             template_file)

        log.debug("ClowdEnvironment config:\n%s", clowd_env_config)

        apply_config(None, clowd_env_config)

        if not namespace:
            # wait for Clowder to tell us what target namespace it created
            namespace = wait_for_clowd_env_target_ns(clowd_env)

        log.info("waiting on resources for max of %dsec...", timeout)
        _wait_on_namespace_resources(namespace, timeout)

        clowd_env_name = find_clowd_env_for_ns(namespace)["metadata"]["name"]
    except KeyboardInterrupt as err:
        log.error("aborted by keyboard interrupt!")
        _err_handler(err)
    except TimedOutError as err:
        log.error("hit timeout error: %s", err)
        _err_handler(err)
    except FatalError as err:
        log.error("hit fatal error: %s", err)
        _err_handler(err)
    except Exception as err:
        log.exception("hit unexpected error!")
        _err_handler(err)
    else:
        log.info("ClowdEnvironment '%s' using ns '%s' is ready",
                 clowd_env_name, namespace)
        click.echo(namespace)
Exemple #6
0
def _cmd_deploy_clowdenv(namespace, clowd_env, template_file, timeout):
    """Process ClowdEnv template and deploy to a cluster"""
    oc_login()

    clowd_env_config = _process_clowdenv(namespace, clowd_env, template_file)

    log.debug("ClowdEnvironment config:\n%s", clowd_env_config)

    apply_config(None, clowd_env_config)

    if not namespace:
        # wait for Clowder to tell us what target namespace it created
        namespace = wait_for_clowd_env_target_ns(clowd_env)

    _wait_on_namespace_resources(namespace, timeout)

    clowd_env_name = find_clowd_env_for_ns(namespace)["metadata"]["name"]
    log.info("ClowdEnvironment '%s' using ns '%s' is ready", clowd_env_name, namespace)
    print(namespace)
Exemple #7
0
def _cmd_config_deploy(
    app_names,
    source,
    get_dependencies,
    set_image_tag,
    ref_env,
    target_env,
    set_template_ref,
    set_parameter,
    clowd_env,
    local_config_path,
    remove_resources,
    single_replicas,
    namespace,
    duration,
    retries,
    timeout,
    no_release_on_fail,
):
    """Process app templates and deploy them to a cluster"""
    requested_ns = namespace
    ns = None

    oc_login()

    successfully_reserved_ns = False
    reservable_namespaces = get_namespaces()

    if reservable_namespaces:
        # check if we're on a cluster that has reservable namespaces
        log.info(
            "reserving ephemeral namespace%s...",
            f" '{requested_ns}'" if requested_ns else "",
        )
        ns = _reserve_namespace(duration, retries, requested_ns)
        successfully_reserved_ns = True

    else:
        # we're not, user will have to specify namespace to deploy to
        if not requested_ns:
            _error("no reservable namespaces found on this cluster.  '--namespace' is required")

        # make sure namespace exists on the cluster
        cluster_namespaces = get_all_namespaces()
        for cluster_ns in cluster_namespaces:
            if cluster_ns["metadata"]["name"] == requested_ns:
                ns = requested_ns
                break
        else:
            _error(f"namespace '{requested_ns}' not found on cluster")

    if not clowd_env:
        # if no ClowdEnvironment name provided, see if a ClowdEnvironment is associated with this ns
        match = find_clowd_env_for_ns(ns)
        if not match:
            _error(
                f"could not find a ClowdEnvironment tied to ns '{ns}'.  Specify one with "
                "'--clowd-env' or apply one with 'bonfire deploy-env'"
            )
        clowd_env = match["metadata"]["name"]
        log.debug("inferred clowd_env: '%s'", clowd_env)

    try:
        log.info("processing app templates...")
        apps_config = _process(
            app_names,
            source,
            get_dependencies,
            set_image_tag,
            ref_env,
            target_env,
            set_template_ref,
            set_parameter,
            clowd_env,
            local_config_path,
            remove_resources,
            single_replicas,
        )
        log.debug("app configs:\n%s", json.dumps(apps_config, indent=2))
        if not apps_config["items"]:
            log.warning("no configurations found to apply!")
        else:
            log.info("applying app configs...")
            apply_config(ns, apps_config)
            log.info("waiting on resources...")
            _wait_on_namespace_resources(ns, timeout)
    except (Exception, KeyboardInterrupt):
        log.exception("hit unexpected error!")
        try:
            if not no_release_on_fail and not requested_ns and successfully_reserved_ns:
                # if we auto-reserved this ns, auto-release it on failure unless
                # --no-release-on-fail was requested
                log.info("releasing namespace '%s'", ns)
                release_namespace(ns)
        finally:
            _error("deploy failed")
    else:
        log.info("successfully deployed to %s", ns)
        print(ns)
Exemple #8
0
def _cmd_config_deploy(
    app_names,
    source,
    get_dependencies,
    set_image_tag,
    ref_env,
    target_env,
    set_template_ref,
    set_parameter,
    clowd_env,
    local_config_path,
    remove_resources,
    no_remove_resources,
    single_replicas,
    namespace,
    duration,
    retries,
    timeout,
    no_release_on_fail,
    component_filter,
    import_secrets,
    secrets_dir,
):
    """Process app templates and deploy them to a cluster"""
    requested_ns = namespace
    used_ns_reservation_system, ns = _get_target_namespace(
        duration, retries, requested_ns)

    if import_secrets:
        import_secrets_from_dir(secrets_dir)

    if not clowd_env:
        # if no ClowdEnvironment name provided, see if a ClowdEnvironment is associated with this ns
        match = find_clowd_env_for_ns(ns)
        if not match:
            _error(
                f"could not find a ClowdEnvironment tied to ns '{ns}'.  Specify which one "
                "if you have already deployed one with '--clowd-env' or deploy one with "
                "'bonfire deploy-env'")
        clowd_env = match["metadata"]["name"]
        log.debug("inferred clowd_env: '%s'", clowd_env)

    def _err_handler(err):
        try:
            if not no_release_on_fail and not requested_ns and used_ns_reservation_system:
                # if we auto-reserved this ns, auto-release it on failure unless
                # --no-release-on-fail was requested
                log.info("releasing namespace '%s'", ns)
                release_namespace(ns)
        finally:
            msg = f"deploy failed: {str(err)}"
            _error(msg)

    try:
        log.info("processing app templates...")
        apps_config = _process(
            app_names,
            source,
            get_dependencies,
            set_image_tag,
            ref_env,
            target_env,
            set_template_ref,
            set_parameter,
            clowd_env,
            local_config_path,
            remove_resources,
            no_remove_resources,
            single_replicas,
            component_filter,
        )
        log.debug("app configs:\n%s", json.dumps(apps_config, indent=2))
        if not apps_config["items"]:
            log.warning("no configurations found to apply!")
        else:
            log.info("applying app configs...")
            apply_config(ns, apps_config)
            log.info("waiting on resources for max of %dsec...", timeout)
            _wait_on_namespace_resources(ns, timeout)
    except KeyboardInterrupt as err:
        log.error("aborted by keyboard interrupt!")
        _err_handler(err)
    except TimedOutError as err:
        log.error("hit timeout error: %s", err)
        _err_handler(err)
    except FatalError as err:
        log.error("hit fatal error: %s", err)
        _err_handler(err)
    except Exception as err:
        log.exception("hit unexpected error!")
        _err_handler(err)
    else:
        log.info("successfully deployed to namespace '%s'", ns)
        click.echo(ns)