Esempio n. 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)
Esempio n. 2
0
def _list_namespaces(available, mine, output):
    """Get list of ephemeral namespaces"""
    namespaces = get_namespaces(available=available, mine=mine)
    if not available and not mine and not namespaces:
        _error(NO_RESERVATION_SYS)
    elif not namespaces:
        if output == "json":
            click.echo("{}")
        else:
            click.echo("no namespaces found")
    else:
        if output == "json":
            data = {}
            for ns in namespaces:
                data[ns.name] = {
                    "reserved": ns.reserved,
                    "ready": ns.ready,
                    "requester": ns.requester_name,
                    "expires_in": ns.expires_in,
                }
            click.echo(json.dumps(data, indent=2))
        else:
            data = {
                "NAME": [ns.name for ns in namespaces],
                "RESERVED": [str(ns.reserved).lower() for ns in namespaces],
                "READY": [str(ns.ready).lower() for ns in namespaces],
                "REQUESTER": [ns.requester_name for ns in namespaces],
                "EXPIRES IN": [ns.expires_in for ns in namespaces],
            }
            tabulated = tabulate(data, headers="keys")
            click.echo(tabulated)
Esempio n. 3
0
def _get_target_namespace(duration, retries, namespace=None):
    """Determine the namespace to deploy to.

    Use ns reservation system if on a cluster that has reservable namespaces. Otherwise the user
    must specify a namespace with '--namespace' and we assume they have ownership of it.

    Returns tuple of:
    (bool indicating whether ns reservation system was used, namespace name)
    """
    # check if we're on a cluster that has reservable namespaces
    reservable_namespaces = get_namespaces()
    if reservable_namespaces:
        ns = _reserve_namespace(duration, retries, namespace)
        return (True, ns.name)
    else:
        # we're not, user has to namespace to deploy to
        if not namespace:
            _error(NO_RESERVATION_SYS +
                   ".  Use -n/--namespace to specify target namespace")

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

        return (False, ns)
Esempio n. 4
0
def _list_namespaces(available, mine):
    """Get list of ephemeral namespaces"""
    namespaces = get_namespaces(available_only=available, mine=mine)
    if not namespaces:
        click.echo("no namespaces found")
    else:
        data = {
            "NAME": [ns.name for ns in namespaces],
            "RESERVED": [str(ns.reserved).lower() for ns in namespaces],
            "READY": [str(ns.ready).lower() for ns in namespaces],
            "REQUESTER": [ns.requester_name for ns in namespaces],
            "EXPIRES IN": [ns.expires_in for ns in namespaces],
        }
        tabulated = tabulate(data, headers="keys")
        click.echo(tabulated)
Esempio n. 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)
Esempio n. 6
0
def _cmd_namespace_reserve(duration, retries, namespace):
    """Reserve an ephemeral namespace (specific or random)"""
    if not get_namespaces():
        _error(NO_RESERVATION_SYS)
    ns = _reserve_namespace(duration, retries, namespace)
    click.echo(ns.name)