def download_git_repos(): hostname = socket.gethostname() uris = {} for t in oapi.get("/namespaces/openshift/templates")._items: for o in t.objects: if o.kind == "BuildConfig": uri = resolve_values(t, o.spec.source.git.uri) if uri and not uri.startswith("git://" + hostname): uris[uri] = "git://%s/%s" % (hostname, uri.split("://", 1)[1]) for uri in uris: print uri root = "/var/lib/git/" + uri.split("://", 1)[1] if not os.path.exists(root): system("git clone --bare " + uri + " " + root) system("chown -R nobody:nobody /var/lib/git") for t in oapi.get("/namespaces/openshift/templates")._items: for o in t.objects: if o.kind == "BuildConfig": m = re.match("^\${([^}]+)}$", o.spec.source.git.uri) if not m: raise Exception for p in t.parameters: if p.name == m.group(1) and "value" in p and \ not p.value.startswith("git://" + hostname): p.value = uris[p.value] if o.spec.strategy.type != "Source": raise Exception env = o.spec.strategy.sourceStrategy.get("env", []) env = [ x for x in env if x.name not in ["http_proxy", "https_proxy"] ] env.append( k8s.AttrDict({ "name": "http_proxy", "value": "http://%s:8080/" % hostname })) env.append( k8s.AttrDict({ "name": "https_proxy", "value": "http://%s:8080/" % hostname })) o.spec.strategy.sourceStrategy.env = env t.kind = "Template" t.apiVersion = "v1" oapi.put("/namespaces/openshift/templates/" + t.metadata.name, t)
def resolve(x): if isinstance(x, list): return [resolve(v) for v in x] elif isinstance(x, dict): return k8s.AttrDict({k: resolve(x[k]) for k in x}) elif isinstance(x, bool): return x return re.sub("\${([^}]+)}", lambda m: parameters[m.group(1)], x)
def set_env(c, k, v): c.env = [e for e in c.env if e.name != k] c.env.append(k8s.AttrDict({"name": k, "value": v}))
def download_referenced_images_imagestreams(repo): images = set() istrmap = {} for istr in oapi.get("/namespaces/openshift/imagestreams")._items: for t in istr.spec.tags: srctag = t.name if "_from" in t and t._from.kind == "ImageStreamTag": srctag = t._from.name istrmap[istr.metadata.name + ":" + t.name] = istr.spec.dockerImageRepository + ":" + srctag for t in oapi.get("/namespaces/openshift/templates")._items: for o in t.objects: if o.kind == "DeploymentConfig": # return container images triggered by imageChange, if in # openshift namespace for tr in o.spec.triggers: if "imageChangeParams" in tr: oo = tr.imageChangeParams._from if oo.kind == "ImageStreamTag" and "namespace" in oo \ and oo.namespace == "openshift": images.add(istrmap[resolve_values(t, oo.name)]) elif o["kind"] == "BuildConfig": # return builder images in openshift namespace oo = o.spec.strategy.sourceStrategy._from if oo.kind == "ImageStreamTag" and oo.namespace == "openshift": images.add(istrmap[resolve_values(t, oo.name)]) for i in images: newi = repo + "/" + i.split("/", 1)[1] if i != newi: if os.path.exists(i.split("/", 1)[1].split(":")[0]): system("docker build -t " + newi + " " + i.split("/", 1)[1].split(":")[0]) system("docker push " + newi, 5) system("docker rmi " + newi) system("docker rmi " + i, 0) else: system("docker pull " + i) system("docker tag " + i + " " + newi) system("docker push " + newi, 5) system("docker rmi " + newi) system("docker rmi " + i, 0) for im in oapi.get("/images")._items: oapi.delete("/images/" + im.metadata.name) for istr in oapi.get("/namespaces/openshift/imagestreams")._items: istr.kind = "ImageStream" istr.apiVersion = "v1" istr.metadata = k8s.AttrDict({ "name": istr.metadata.name, "annotations": k8s.AttrDict({"openshift.io/image.insecureRepository": "true"}) }) istr.spec.dockerImageRepository = repo + "/" + istr.spec.dockerImageRepository.split( "/", 1)[1] del istr.status oapi.delete("/namespaces/openshift/imagestreams/" + istr.metadata.name) oapi.post("/namespaces/openshift/imagestreams", istr)