Пример #1
0
    def run(self, image_tag, cmd, tty=False, env=(), constraint=()):
        if os.environ.get('USER') is not None:
            uuid = "%s-%s" % (os.environ.get('USER'), k8s_uuid())
        else:
            uuid = k8s_uuid()

        name = "%s-hokusai-run-%s" % (config.project_name, uuid)
        image_name = "%s:%s" % (config.aws_ecr_registry, image_tag)
        container = {
            "args":
            cmd.split(' '),
            "name":
            name,
            "image":
            image_name,
            "imagePullPolicy":
            "Always",
            'envFrom': [{
                'configMapRef': {
                    'name': "%s-environment" % config.project_name
                }
            }]
        }

        if tty:
            container.update({"stdin": True, "stdinOnce": True, "tty": True})

        if env:
            container['env'] = []
            for s in env:
                if '=' not in s:
                    raise HokusaiError(
                        "Error: environment variables must be of the form 'KEY=VALUE'"
                    )
                split = s.split('=', 1)
                container['env'].append({'name': split[0], 'value': split[1]})

        spec = {"containers": [container]}
        if constraint:
            spec['nodeSelector'] = {}
            for label in constraint:
                if '=' not in label:
                    raise HokusaiError(
                        "Error: Node selectors must of the form 'key=value'")
                split = label.split('=', 1)
                spec['nodeSelector'][split[0]] = split[1]

        overrides = {"apiVersion": "v1", "spec": spec}

        if tty:
            shout(self.kctl.command(
                "run %s -t -i --image=%s --restart=Never --overrides='%s' --rm"
                % (name, image_name, json.dumps(overrides))),
                  print_output=True)
        else:
            return returncode(
                self.kctl.command(
                    "run %s --attach --image=%s --overrides='%s' --restart=Never --rm"
                    % (name, image_name, json.dumps(overrides))))
Пример #2
0
    def run(self, tag_or_digest, cmd, tty=None, env=(), constraint=()):
        if os.environ.get('USER') is not None:
            # The regex used for the validation of name is '[a-z0-9]([-a-z0-9]*[a-z0-9])?'
            user = re.sub("[^0-9a-z]+", "-", os.environ.get('USER').lower())
            uuid = "%s-%s" % (user, k8s_uuid())
        else:
            uuid = k8s_uuid()

        name = "%s-hokusai-run-%s" % (config.project_name, uuid)
        separator = "@" if ":" in tag_or_digest else ":"
        image_name = "%s%s%s" % (self.ecr.project_repo, separator,
                                 tag_or_digest)

        container = {
            "args":
            cmd.split(' '),
            "name":
            name,
            "image":
            image_name,
            "imagePullPolicy":
            "Always",
            'envFrom': [{
                'configMapRef': {
                    'name': "%s-environment" % config.project_name
                }
            }]
        }

        run_tty = tty if tty is not None else config.run_tty
        if run_tty:
            container.update({"stdin": True, "stdinOnce": True, "tty": True})

        if env:
            container['env'] = []
            for s in env:
                if '=' not in s:
                    raise HokusaiError(
                        "Error: environment variables must be of the form 'KEY=VALUE'"
                    )
                split = s.split('=', 1)
                container['env'].append({'name': split[0], 'value': split[1]})

        spec = {"containers": [container]}
        constraints = constraint or config.run_constraints
        if constraints:
            spec['nodeSelector'] = {}
            for label in constraints:
                if '=' not in label:
                    raise HokusaiError(
                        "Error: Node selectors must of the form 'key=value'")
                split = label.split('=', 1)
                spec['nodeSelector'][split[0]] = split[1]

        overrides = {"apiVersion": "v1", "spec": spec}

        if run_tty:
            shout(self.kctl.command(
                "run %s -t -i --image=%s --restart=Never --overrides=%s --rm" %
                (name, image_name, pipes.quote(json.dumps(overrides)))),
                  print_output=True)
        else:
            return returncode(
                self.kctl.command(
                    "run %s --attach --image=%s --overrides=%s --restart=Never --rm"
                    % (name, image_name, pipes.quote(json.dumps(overrides)))))
Пример #3
0
 def test_k8s_uuid(self):
   self.assertEqual(len(k8s_uuid()), 5)
   for char in list(k8s_uuid()):
     self.assertIn(char, string.lowercase)