예제 #1
0
def get_active_context(backend: BackendFacade):
    """
    Set current model environment workspace.
    """
    try:
        active_context = backend.get_active_context()

        if active_context:
            click.echo("{} - Active context is - {}".
                       format(click.style("Info", bold=True, fg='green'),
                              click.style(active_context, bold=True, fg='green')))
        else:
            available_contexts = backend.list()
            if available_contexts:
                click.echo("{} - No {} context set. Set an active context using `{}`".
                           format(click.style("Warning", bold=True, fg='yellow'),
                                  click.style("active", bold=True, fg='green'),
                                  click.style("kaos build set", bold=True, fg='white')))
            else:
                click.echo("{} - No active builds found. Please run {} to deploy an environment".format(
                    click.style("Warning", bold=True, fg='yellow'),
                    click.style('kaos build deploy', bold=True, fg='green')))

    except Exception as e:
        handle_specific_exception(e)
        handle_exception(e)
예제 #2
0
def set_active_context(backend: BackendFacade, context: Optional[str] = None, ind: Optional[int] = None):
    """
    Set current model environment workspace.
    """
    try:
        # ensure arguments are correctly defined
        validate_inputs([context, ind], ['context', 'ind'])
        # selection by index
        available_contexts = backend.list()
        if available_contexts:
            if context:
                is_context_set = backend.set_context_by_context(context)
                if not is_context_set:
                    click.echo('Context {} invalid. It is not one of the existing deployments in {} '
                               .format(click.style(context, bold=True, fg='red'), click.style("kaos", bold=True)))
                    sys.exit(1)
                click.echo("{} - Successfully set to context - {}".
                           format(click.style("Info", bold=True, fg='green'), click.style(context, bold=True, fg='green')))
            if ind or ind == 0:
                is_context_set, context = backend.set_context_by_index(ind)
                if not is_context_set:
                    click.echo('Index {} invalid. It is not one of the existing deployments in {} '
                               .format(click.style(str(ind), bold=True, fg='red'), click.style("kaos", bold=True)))
                    sys.exit(1)
                click.echo("{} - Successfully set to context - {}".
                           format(click.style("Info", bold=True, fg='green'), click.style(context, bold=True, fg='green')))
        else:
            click.echo("{} - No active builds found. Please run {} to deploy an environment".
                       format(click.style("Warning", bold=True, fg='yellow'),
                              click.style('kaos build deploy', bold=True, fg='green')))

    except Exception as e:
        handle_specific_exception(e)
        handle_exception(e)
예제 #3
0
def list_all(backend: BackendFacade):
    """
    List all deployed kaos backend infrastructures.
    """
    try:
        available_contexts = backend.list()

        if available_contexts:
            backend.cache(available_contexts)
            table = render_table(available_contexts, include_ind=False)
            click.echo(table)
        else:
            click.echo("{} - No active builds found. Please run {} to deploy an environment".format(
                click.style("Warning", bold=True, fg='yellow'),
                click.style('kaos build deploy', bold=True, fg='green')))

    except Exception as e:
        handle_specific_exception(e)
        handle_exception(e)
예제 #4
0
def destroy(backend: BackendFacade, cloud, env, verbose, yes):
    """
    Destroy kaos backend infrastructure based on selected provider.
    """

    # validate ENV
    env = validate_build_env(cloud, env)

    if not yes:
        # confirm creation of backend
        if env:
            click.confirm(
                '{} - Are you sure about destroying {} [{}] backend in {}?'.
                format(click.style("Warning", bold=True, fg='yellow'),
                       click.style('kaos', bold=True),
                       click.style(env, bold=True, fg='blue'),
                       click.style(cloud, bold=True, fg='red')),
                abort=True)
        else:
            click.confirm(
                '{} - Are you sure about destroying {} backend in {}?'.format(
                    click.style("Warning", bold=True, fg='yellow'),
                    click.style('kaos', bold=True),
                    click.style(cloud, bold=True, fg='red')),
                abort=True)
    try:

        backend.destroy(cloud, env, verbose=verbose)

        if env:
            click.echo(
                "{} - Successfully destroyed {} [{}] environment".format(
                    click.style("Info", bold=True, fg='green'),
                    click.style('kaos', bold=True),
                    click.style(env, bold=True, fg='blue')))
        else:
            click.echo("{} - Successfully destroyed {} environment".format(
                click.style("Info", bold=True, fg='green'),
                click.style('kaos', bold=True)))

    except Exception as e:
        handle_specific_exception(e)
        handle_exception(e)
예제 #5
0
def init(backend: BackendFacade, endpoint, token, force):
    """
    Configure and connect kaos with deployed backend infrastructure.
    """

    # handle "force" STATE creation
    if force:
        click.echo("{} - Force initialization of {}".format(
            click.style("Warning", bold=True, fg='yellow'),
            click.style("kaos", bold=True)))

    elif backend.is_created():
        click.confirm(
            '{} - {} is already initialized in this directory. Do you want to overwrite?'
            .format(click.style("Warning", bold=True, fg='yellow'),
                    click.style("kaos", bold=True)),
            abort=True)

    backend.init(endpoint, token)

    click.echo('{} - {} successfully connected to {}'.format(
        click.style("Info", bold=True, fg='green'),
        click.style("kaos", bold=True),
        click.style(endpoint, bold=True, fg='green')))
예제 #6
0
    def _create_facades(state=None, terraform=None):
        template = TemplateFacade()
        backend = BackendFacade(state, terraform)
        workspace = WorkspaceFacade(state)
        train = TrainFacade(state)
        serve = ServeFacade(state)
        notebook = NotebookFacade(state)

        facades = {
            BackendFacade: backend,
            WorkspaceFacade: workspace,
            TemplateFacade: template,
            TrainFacade: train,
            ServeFacade: serve,
            NotebookFacade: notebook
        }
        return facades
예제 #7
0
def deploy(backend: BackendFacade, cloud: str, env: str, force: bool, verbose: bool, yes: bool, local_backend: bool):
    """
    Deploy kaos backend infrastructure based on selected provider.
    """

    env_state = EnvironmentState.initialize(cloud, env)

    if env_state.if_tfstate_exists and not force:
        click.echo('{} - {} backend is already built.'.format(click.style("Aborting", bold=True, fg='red'),
                                                              click.style("kaos", bold=True)))
        sys.exit(1)

    elif env_state.if_tfstate_exists and force:
        click.echo('{} - Performing {} build of the backend'.format(
            click.style("Warning", bold=True, fg='yellow'),
            click.style("force", bold=True)))
        env_state.remove_terraform_files()

    # set env variable appropriately
    env_state.set_build_env()
    cloud = env_state.cloud
    env = env_state.env

    if not yes:
        # confirm creation of backend
        if env_state.env:
            click.confirm(
                '{} - Are you sure about building {} [{}] backend in {}?'.format(
                    click.style("Warning", bold=True, fg='yellow'),
                    click.style('kaos', bold=True),
                    click.style(env_state.env, bold=True, fg='blue'),
                    click.style(env_state.cloud, bold=True, fg='red')),
                abort=True)
        else:
            click.confirm(
                '{} - Are you sure about building {} backend in {}?'.format(
                    click.style("Warning", bold=True, fg='yellow'),
                    click.style('kaos', bold=True),
                    click.style(env_state.cloud, bold=True, fg='red')),
                abort=True)

    if local_backend:
        click.echo('{} - Building with {} terraform backend state'.format(
            click.style("Info", bold=True, fg='green'),
            click.style("local", bold=True)))

    if local_backend and env_state.cloud in [DOCKER, MINIKUBE]:
        click.echo('{} - local backend (-l/--local_backend) has no effect for {}'.format(
            click.style("Info", bold=True, fg='green'),
            click.style(env_state.cloud, bold=True, fg='red')))

    # validate unused port for DOCKER
    if env_state.cloud == DOCKER and not validate_unused_port(80):
        # If the force build flag was set to True and the kaos backend 
        # was already built then skip the warning; otherwise, warn
        # the user that the port is already taken by another service
        # and issue a sys exit
        if not (force and env_state.if_tfstate_exists):
            click.echo(
                "{} - Network port {} is used but is needed for building {} backend in {}".format(
                    click.style("Warning", bold=True, fg='yellow'),
                    click.style("80", bold=True),
                    click.style("kaos", bold=True),
                    click.style(env_state.cloud, bold=True, fg='red')))
            sys.exit(1)
    
    try:
        is_built_successfully, env_state = backend.build(env_state.cloud,
                                                         env_state.env,
                                                         local_backend=local_backend,
                                                         verbose=verbose)

        if is_built_successfully:
            if verbose:
                click.echo("\n{} - Endpoint successfully set to {}".format(
                    click.style("Info", bold=True, fg='green'),
                    click.style(backend.url, bold=True, fg='green')))

            if is_cloud_provider(env_state.cloud):
                kubeconfig = os.path.abspath(backend.kubeconfig)
                click.echo("\n{} - To interact with the Kubernetes cluster:\n {}"
                           .format(click.style("Info", bold=True, fg='green'),
                                   click.style("export KUBECONFIG=" + kubeconfig,
                                               bold=True, fg='red')))

            if env:
                click.echo("{} - Successfully built {} [{}] environment".format(
                    click.style("Info", bold=True, fg='green'),
                    click.style('kaos', bold=True),
                    click.style(env, bold=True, fg='blue')))
            else:
                click.echo("{} - Successfully built {} environment".format(
                    click.style("Info", bold=True, fg='green'),
                    click.style('kaos', bold=True)))

        else:
            click.echo("{} - Deployment Unsuccessful while creating {} [{} {}] environment".format(
                click.style("Error", bold=True, fg='red'),
                click.style('kaos', bold=True),
                click.style(cloud, bold=True, fg='red'),
                click.style(env, bold=True, fg='red'))),
            sys.exit(1)

    except Exception as e:
        handle_specific_exception(e)
        handle_exception(e)
예제 #8
0
def destroy(backend: BackendFacade, cloud, env, verbose, yes):
    """
    Destroy kaos backend infrastructure based on selected provider.
    """
    env_state = EnvironmentState.initialize(cloud, env)

    # set env variable appropriately
    env_state.set_build_env()

    # Ensure that appropriate warnings are displayed
    env_state.validate_if_tf_state_exits()

    if not yes:
        # confirm creation of backend
        if env_state.env:
            click.confirm(
                '{} - Are you sure about destroying {} [{}] backend in {}?'.format(
                    click.style("Warning", bold=True, fg='yellow'),
                    click.style('kaos', bold=True),
                    click.style(env_state.env, bold=True, fg='blue'),
                    click.style(env_state.cloud, bold=True, fg='red')),
                abort=True)
        else:
            click.confirm(
                '{} - Are you sure about destroying {} backend in {}?'.format(
                    click.style("Warning", bold=True, fg='yellow'),
                    click.style('kaos', bold=True),
                    click.style(env_state.cloud, bold=True, fg='red')),
                abort=True)
    try:

        env_state = backend.destroy(env_state, verbose=verbose)

        if not env_state.if_tfstate_exists:
            if env_state.env:
                click.echo(
                    "{} - Successfully destroyed {} [{}] environment".format(click.style("Info", bold=True, fg='green'),
                                                                             click.style('kaos', bold=True),
                                                                             click.style(env_state.env, bold=True, fg='blue')))
            else:
                click.echo(
                    "{} - Successfully destroyed {} environment".format(click.style("Info", bold=True, fg='green'),
                                                                        click.style('kaos', bold=True)))
        else:
            if env_state.env:
                click.echo("{} - Destroy operation unsuccessful for {} [{} {}] environment".format(
                    click.style("Error", bold=True, fg='red'),
                    click.style('kaos', bold=True),
                    click.style(env_state.cloud, bold=True, fg='red'),
                    click.style(env_state.env, bold=True, fg='red'))),
                sys.exit(1)
            else:
                click.echo("{} - Destroy operation unsuccessful for {} [{}] environment".format(
                    click.style("Error", bold=True, fg='red'),
                    click.style('kaos', bold=True),
                    click.style(env_state.cloud, bold=True, fg='red'))),
                sys.exit(1)

    except Exception as e:
        handle_specific_exception(e)
        handle_exception(e)
예제 #9
0
def build(backend: BackendFacade, cloud, env, force, verbose, yes,
          local_backend):
    """
    Deploy kaos backend infrastructure based on selected provider.
    """

    is_created = backend.is_created()

    if is_created and not force:
        click.echo('{} - {} backend is already built.'.format(
            click.style("Aborting", bold=True, fg='red'),
            click.style("kaos", bold=True)))
        sys.exit(1)

    elif is_created and force:
        click.echo('{} - Performing {} build of the backend'.format(
            click.style("Warning", bold=True, fg='yellow'),
            click.style("force", bold=True)))

    # validate ENV
    env = validate_build_env(cloud, env)

    if not yes:
        # confirm creation of backend
        if env:
            click.confirm(
                '{} - Are you sure about building {} [{}] backend in {}?'.
                format(click.style("Warning", bold=True, fg='yellow'),
                       click.style('kaos', bold=True),
                       click.style(env, bold=True, fg='blue'),
                       click.style(cloud, bold=True, fg='red')),
                abort=True)
        else:
            click.confirm(
                '{} - Are you sure about building {} backend in {}?'.format(
                    click.style("Warning", bold=True, fg='yellow'),
                    click.style('kaos', bold=True),
                    click.style(cloud, bold=True, fg='red')),
                abort=True)

    if local_backend:
        click.echo('{} - Building with {} terraform backend state'.format(
            click.style("Info", bold=True, fg='green'),
            click.style("local", bold=True)))

    if local_backend and cloud in [DOCKER, MINIKUBE]:
        click.echo(
            '{} - local backend (-l/--local_backend) has no effect for {}'.
            format(click.style("Info", bold=True, fg='green'),
                   click.style(cloud, bold=True, fg='red')))

    try:

        backend.build(cloud, env, local_backend=local_backend, verbose=verbose)

        if verbose:
            click.echo("\n{} - Endpoint successfully set to {}".format(
                click.style("Info", bold=True, fg='green'),
                click.style(backend.url, bold=True, fg='green')))

        if is_cloud_provider(cloud):
            kubeconfig = os.path.abspath(backend.kubeconfig)
            click.echo(
                "\n{} - To interact with the Kubernetes cluster:\n {}".format(
                    click.style("Info", bold=True, fg='green'),
                    click.style("export KUBECONFIG=" + kubeconfig,
                                bold=True,
                                fg='red')))

        if env:
            click.echo("{} - Successfully built {} [{}] environment".format(
                click.style("Info", bold=True, fg='green'),
                click.style('kaos', bold=True),
                click.style(env, bold=True, fg='blue')))
        else:
            click.echo("{} - Successfully built {} environment".format(
                click.style("Info", bold=True, fg='green'),
                click.style('kaos', bold=True)))

    except Exception as e:
        handle_specific_exception(e)
        handle_exception(e)