Beispiel #1
0
Datei: kub.py Projekt: coreos/kpm
    def _process_deploy(self, dry=False, force=False, fmt="txt", proxy=None, action="create",
                        dest="/tmp/kpm"):

        def output_progress(kubsource, status, fmt="text"):
            if fmt == 'text':
                print " --> %s (%s): %s" % (kubsource.name, kubsource.kind, colorize(status))

        dest = os.path.join(dest, self.name, self.version)
        mkdir_p(dest)
        table = []
        results = []
        if fmt == "text":
            print "%s %s " % (action, self.name)
        i = 0
        for kub in self.dependencies:
            package = self._dep_build(kub)
            i += 1
            pname = package["package"]
            version = package["version"]
            namespace = package["namespace"]
            if fmt == "text":
                print "\n %02d - %s:" % (i, package["package"])
            for resource in package["resources"]:
                body = resource["body"]
                endpoint = resource["endpoint"]
                # Use API instead of kubectl
                with open(
                        os.path.join(dest, "%s-%s" % (resource['name'],
                                                      resource['file'].replace("/", "_"))),
                        'wb') as f:
                    f.write(body)
                kubresource = Kubernetes(namespace=namespace, body=body, endpoint=endpoint,
                                         proxy=proxy)
                status = getattr(kubresource, action)(force=force, dry=dry, strategy=resource.get(
                    'update_mode', 'update'))
                if fmt == "text":
                    output_progress(kubresource, status)
                result_line = OrderedDict([("package", pname), ("version", version), (
                    "kind", kubresource.kind), ("dry", dry), ("name", kubresource.name), (
                        "namespace", kubresource.namespace), ("status", status)])

                if status != 'ok' and action == 'create':
                    kubresource.wait(3)
                results.append(result_line)
                if fmt == "text":
                    header = ["package", "version", "kind", "name", "namespace", "status"]
                    display_line = []
                    for k in header:
                        display_line.append(result_line[k])
                    table.append(display_line)
        if fmt == "text":
            print_deploy_result(table)
        return results
Beispiel #2
0
def test_wait(svc_resource, monkeypatch):
    import time
    monkeypatch.setattr(time, 'sleep', lambda s: None)
    proxy = "http://localhost:8001"
    k = Kubernetes(body=svc_resource['body'], proxy=proxy, endpoint=svc_resource['endpoint'])
    url = "%s/%s/%s" % (proxy, svc_resource['endpoint'][1:-1], svc_resource['name'])
    url2 = "%s/%s/%s" % (k.proxy.geturl(), k.endpoint, k.name)
    assert url == url2
    with requests_mock.mock() as m:
        response = get_response(svc_resource["name"], svc_resource["kind"])
        m.get(url, [{'status_code': 404}, {'text': response, 'status_code': 200}])
        assert json.dumps(k.wait()) == json.dumps(json.loads(response))
        assert m.call_count == 2
Beispiel #3
0
def test_wait(svc_resource, monkeypatch):
    import time
    monkeypatch.setattr(time, 'sleep', lambda s: None)
    proxy = "http://localhost:8001"
    k = Kubernetes(body=svc_resource['body'],
                   proxy=proxy,
                   endpoint=svc_resource['endpoint'])
    url = "%s/%s/%s" % (proxy, svc_resource['endpoint'][1:-1],
                        svc_resource['name'])
    url2 = "%s/%s/%s" % (k.proxy.geturl(), k.endpoint, k.name)
    assert url == url2
    with requests_mock.mock() as m:
        response = get_response(svc_resource["name"], svc_resource["kind"])
        m.get(url, [{
            'status_code': 404
        }, {
            'text': response,
            'status_code': 200
        }])
        assert json.dumps(k.wait()) == json.dumps(json.loads(response))
        assert m.call_count == 2
Beispiel #4
0
Datei: kub.py Projekt: zjinys/kpm
    def _process_deploy(self,
                        dry=False,
                        force=False,
                        fmt="txt",
                        proxy=None,
                        action="create",
                        dest="/tmp/kpm"):
        def output_progress(kubsource, status, fmt="text"):
            if fmt == 'text':
                print " --> %s (%s): %s" % (kubsource.name, kubsource.kind,
                                            colorize(status))

        dest = os.path.join(dest, self.name, self.version)
        mkdir_p(dest)
        table = []
        results = []
        if fmt == "text":
            print "%s %s " % (action, self.name)
        i = 0
        for kub in self.dependencies:
            package = self._dep_build(kub)
            i += 1
            pname = package["package"]
            version = package["version"]
            namespace = package["namespace"]
            if fmt == "text":
                print "\n %02d - %s:" % (i, package["package"])
            for resource in package["resources"]:
                body = resource["body"]
                endpoint = resource["endpoint"]
                # Use API instead of kubectl
                with open(
                        os.path.join(
                            dest,
                            "%s-%s" % (resource['name'],
                                       resource['file'].replace("/", "_"))),
                        'wb') as f:
                    f.write(body)
                kubresource = Kubernetes(namespace=namespace,
                                         body=body,
                                         endpoint=endpoint,
                                         proxy=proxy)
                status = getattr(kubresource, action)(force=force,
                                                      dry=dry,
                                                      strategy=resource.get(
                                                          'update_mode',
                                                          'update'))
                if fmt == "text":
                    output_progress(kubresource, status)
                result_line = OrderedDict([("package", pname),
                                           ("version", version),
                                           ("kind", kubresource.kind),
                                           ("dry", dry),
                                           ("name", kubresource.name),
                                           ("namespace",
                                            kubresource.namespace),
                                           ("status", status)])

                if status != 'ok' and action == 'create':
                    kubresource.wait(3)
                results.append(result_line)
                if fmt == "text":
                    header = [
                        "package", "version", "kind", "name", "namespace",
                        "status"
                    ]
                    display_line = []
                    for k in header:
                        display_line.append(result_line[k])
                    table.append(display_line)
        if fmt == "text":
            print_deploy_result(table)
        return results