|
5 | 5 | import requests |
6 | 6 | import subprocess |
7 | 7 |
|
8 | | - |
9 | 8 | DEBUG = os.environ.get('DEIS_DEBUG') in ('true', '1') |
| 9 | +regsitryLocation = os.getenv('DEIS_REGISTRY_LOCATION', 'on-cluster') |
10 | 10 |
|
11 | 11 |
|
12 | 12 | def log_output(stream, decode): |
@@ -36,6 +36,40 @@ def log(msg): |
36 | 36 | print(msg) |
37 | 37 |
|
38 | 38 |
|
| 39 | +def get_registry_name(): |
| 40 | + hostname = os.getenv('DEIS_REGISTRY_HOSTNAME', "").replace("https://", "").replace("http://", "") |
| 41 | + if regsitryLocation == "off-cluster": |
| 42 | + organization = os.getenv('DEIS_REGISTRY_ORGANIZATION') |
| 43 | + regName = "" |
| 44 | + # empty hostname means dockerhub and hence no need to prefix the image |
| 45 | + if hostname != "": |
| 46 | + regName = hostname + "/" |
| 47 | + # Registries may have organizations/namespaces under them which needs to |
| 48 | + # be prefixed to the image |
| 49 | + if organization != "": |
| 50 | + regName = regName + organization |
| 51 | + return regName |
| 52 | + elif regsitryLocation == "ecr": |
| 53 | + return hostname |
| 54 | + elif regsitryLocation == "gcr": |
| 55 | + return hostname+"/"+os.getenv('DEIS_REGISTRY_GCS_PROJ_ID') |
| 56 | + else: |
| 57 | + return os.getenv("DEIS_REGISTRY_SERVICE_HOST") + ":" + os.getenv("DEIS_REGISTRY_SERVICE_PORT") |
| 58 | + |
| 59 | + |
| 60 | +def docker_push(client, repo, tag): |
| 61 | + if regsitryLocation != "on-cluster": |
| 62 | + hostname = os.getenv('DEIS_REGISTRY_HOSTNAME', 'https://index.docker.io/v1/') |
| 63 | + auth_config = { |
| 64 | + 'username': os.getenv('DEIS_REGISTRY_USERNAME'), |
| 65 | + 'password': os.getenv('DEIS_REGISTRY_PASSWORD'), |
| 66 | + 'serveraddress': hostname, |
| 67 | + } |
| 68 | + return client.push(repo, tag=tag, stream=True, auth_config=auth_config) |
| 69 | + else: |
| 70 | + return client.push(repo, tag=tag, stream=True) |
| 71 | + |
| 72 | + |
39 | 73 | def download_file(tar_path): |
40 | 74 | os.putenv('BUCKET_FILE', "/var/run/secrets/deis/objectstore/creds/builder-bucket") |
41 | 75 | if os.getenv('BUILDER_STORAGE') == "minio": |
@@ -63,11 +97,11 @@ def download_file(tar_path): |
63 | 97 | tar.extractall("/app/") |
64 | 98 | log("extracting tar file complete") |
65 | 99 | client = docker.Client(version='auto') |
66 | | -registry = os.getenv("DEIS_REGISTRY_SERVICE_HOST") + ":" + os.getenv("DEIS_REGISTRY_SERVICE_PORT") |
| 100 | +registry = get_registry_name() |
67 | 101 | imageName, imageTag = os.getenv('IMG_NAME').split(":", 1) |
68 | 102 | repo = registry + "/" + os.getenv('IMG_NAME') |
69 | 103 | stream = client.build(tag=repo, stream=True, decode=True, rm=True, path='/app') |
70 | 104 | log_output(stream, True) |
71 | 105 | print("Pushing to registry") |
72 | | -stream = client.push(registry+'/'+imageName, tag=imageTag, stream=True) |
| 106 | +stream = docker_push(client, registry+'/'+imageName, imageTag) |
73 | 107 | log_output(stream, False) |
0 commit comments