Esempio n. 1
0
    def _deploy_new_container(self):
        """Substitutes image, app, run data into k8s-template selected.
           Can also launch user into interactive shell with --interactive flag
        """
        app_name = self.config['name']
        self.namespace = self.config['namespace']
        remote_container_name = files.fetch_action_arg(
            'push', 'last_remote_container')
        if remote_container_name is None:
            raise ValueError("No image found to deploy with. Run a plain "
                             "`mlt deploy` to fix this. Most common reason "
                             "for this is a --no-push was used before "
                             "any image was available to use.")

        print("Deploying {}".format(remote_container_name))
        kubernetes_helpers.ensure_namespace_exists(self.namespace)

        app_run_id = str(uuid.uuid4())

        """
        we'll keep track of the number of containers that would be deployed
        so we know if we should exec into 1 or not (only auto-exec if 1 made)
        if we have replicas (with value > 1) then we automatically won't
        go into most recent pod, because there will be > 1 container made
        if we find > 1 container regardless of replica, same logic applies
        """
        self._replicas_found = False
        self._total_containers = 0

        # deploy our normal template sub logic, then if `deploy` in Makefile
        # add whatever custom stuff is desired
        self._default_deploy(app_name=app_name,
                             app_run_id=app_run_id,
                             remote_container_name=remote_container_name)
        if files.is_custom("deploy:"):
            # execute the custom deploy code
            self._custom_deploy(app_name=app_name,
                                app_run_id=app_run_id,
                                remote_container_name=remote_container_name)

        self._update_app_run_id(app_run_id)
        print("\nInspect created objects by running:\n"
              "$ kubectl get --namespace={} all\n"
              "or \n$ mlt status\n".format(self.namespace))

        if self.args["--interactive"] and not self._replicas_found \
                and self._total_containers == 1:
            self._exec_into_pod(self._get_most_recent_podname())
        elif self.args["--interactive"]:
            print("More than one container created."
                  ".\nCall `kubectl exec -it {{pod_name_here}} "
                  "--namespace={} /bin/bash` on a `Running` pod NAME "
                  "below.\nIf no pods are running yet, run `mlt status` "
                  "occasionally, or `watch -n1 mlt status` to watch until "
                  "pods are `Running`.\n".format(self.namespace))

            for line in self._get_pods_by_start_time():
                print(line)
Esempio n. 2
0
    def _deploy_new_container(self):
        """Substitutes image, app, run data into k8s-template selected.
           Can also launch user into interactive shell with --interactive flag
        """
        app_name = self.config['name']
        self.namespace = self.config['namespace']
        remote_container_name = files.fetch_action_arg(
            'push', 'last_remote_container')
        if remote_container_name is None:
            raise ValueError("No image found to deploy with. Run a plain "
                             "`mlt deploy` to fix this. Most common reason "
                             "for this is a --no-push was used before "
                             "any image was available to use.")

        print("Deploying {}".format(remote_container_name))
        kubernetes_helpers.ensure_namespace_exists(self.namespace)

        # do template substitution across everything in `k8s-templates` dir
        # replaces things with $ with the vars from template.substitute
        # also patches deployment if interactive mode is set
        self.interactive_deployment_found = False
        app_run_id = str(uuid.uuid4())
        for path, dirs, filenames in os.walk("k8s-templates"):
            self.file_count = len(filenames)
            for filename in filenames:
                with open(os.path.join(path, filename)) as f:
                    template = Template(f.read())
                out = template.substitute(
                    image=remote_container_name,
                    app=app_name,
                    run=app_run_id,
                    **config_helpers.get_template_parameters(self.config))

                interactive, out = self._check_for_interactive_deployment(
                    out, filename)
                self._apply_template(out, filename)
                if interactive:
                    interactive_podname = self._get_most_recent_podname()

            print("\nInspect created objects by running:\n"
                  "$ kubectl get --namespace={} all\n".format(self.namespace))

        self._update_app_run_id(app_run_id)
        # After everything is deployed we'll make a kubectl exec
        # call into our debug container if interactive mode
        if self.args["--interactive"] and self.interactive_deployment_found:
            self._exec_into_pod(interactive_podname)
        elif not self.interactive_deployment_found and \
                self.args['--interactive']:
            raise ValueError("Unable to find deployment to run interactively. "
                             "Multiple deployment files found and bad "
                             "<kube_spec> argument passed.")
def test_ensure_namespace_already_exists(proc_helpers, open_mock, call):
    call.return_value = 1

    ensure_namespace_exists(str(uuid.uuid4()))
    proc_helpers.run.assert_called_once()
def test_ensure_namespace_no_exist(proc_helpers, open_mock, call):
    call.return_value = 0

    ensure_namespace_exists(str(uuid.uuid4()))
    proc_helpers.run.assert_not_called()