示例#1
0
def _cmd_namespace_release(namespace, force):
    """Remove reservation from an ephemeral namespace"""
    if not get_namespaces():
        _error(NO_RESERVATION_SYS)
    if not force:
        _warn_if_unsafe(namespace)
    release_namespace(namespace)
示例#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)
示例#3
0
 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)
示例#4
0
def _cmd_namespace_release(namespace):
    """Remove reservation from an ephemeral namespace"""
    _warn_if_not_available(namespace)
    release_namespace(namespace)
示例#5
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)