-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuilder
More file actions
executable file
·153 lines (147 loc) · 6.4 KB
/
builder
File metadata and controls
executable file
·153 lines (147 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python
#
# builder hook called on every git receive-pack
# NOTE: this script must be run as root (for docker access)
#
import hashlib
import json
import os
import requests
import shutil
import subprocess
import sys
import tarfile
import tempfile
import uuid
import yaml
def parse_args():
if len(sys.argv) < 2:
print('Usage: {} [user] [repo]'.format(sys.argv[0]))
sys.exit(1)
user, repo = sys.argv[1], sys.argv[2]
app = repo.split('.')[0]
return user, repo, app
DOCKERFILE_SHIM = """FROM deis/slugrunner
RUN mkdir -p /app
ADD slug.tgz /app
ENTRYPOINT ["/runner/init"]
"""
if __name__ == '__main__':
user, repo, app = parse_args()
# define image names
_id = uuid.uuid4().hex[:8]
tmp_image = "{user}/{app}:temp_{_id}".format(**locals())
target_image = "{{ .deis_registry_host }}:{{ .deis_registry_port }}/{user}/{app}".format(**locals())
# create required directories
repo_dir = os.path.join(os.getcwd(), repo)
build_dir = os.path.join(repo_dir, 'build')
cache_dir = os.path.join(repo_dir, 'cache')
for d in (cache_dir, build_dir):
if not os.path.exists(d):
os.mkdir(d)
try:
# create temporary directory
temp_dir = tempfile.mkdtemp(dir=build_dir)
# extract git master
p = subprocess.Popen(
'git archive master | tar -x -C {temp_dir}'.format(**locals()),
shell=True, cwd=repo_dir)
rc = p.wait()
if rc != 0:
raise Exception('Could not extract git archive')
# check for Procfile
dockerfile = os.path.join(temp_dir, 'Dockerfile')
procfile = os.path.join(temp_dir, 'Procfile')
if not os.path.exists(dockerfile) and os.path.exists(procfile):
if os.path.exists('/buildpacks'):
build_cmd = "docker run -i -a stdin -v {cache_dir}:/tmp/cache:rw -v /buildpacks:/tmp/buildpacks deis/slugbuilder".format(**locals())
else:
build_cmd = "docker run -i -a stdin -v {cache_dir}:/tmp/cache:rw deis/slugbuilder".format(**locals())
# run slugbuilder in the background
p = subprocess.Popen("git archive master | " + build_cmd, shell=True, cwd=repo_dir, stdout=subprocess.PIPE)
container = p.stdout.read().strip('\n')
# attach to slugbuilder output
p = subprocess.Popen('docker attach {container}'.format(**locals()), shell=True, cwd=temp_dir)
rc = p.wait()
if rc != 0:
raise Exception('Slugbuilder returned error code')
# extract slug
p = subprocess.Popen('docker cp {container}:/tmp/slug.tgz .'.format(**locals()), shell=True, cwd=temp_dir)
rc = p.wait()
if rc != 0:
raise Exception('Could not extract slug from container')
slug_path = os.path.join(temp_dir, 'slug.tgz')
# write out a dockerfile shim for slugbuilder
with open(dockerfile, 'w') as f:
f.write(DOCKERFILE_SHIM)
# build the docker image
print('-----> Building Docker image')
sys.stdout.flush(), sys.stderr.flush()
p = subprocess.Popen('docker build -t {tmp_image} .'.format(**locals()), shell=True, cwd=temp_dir)
rc = p.wait()
if rc != 0:
raise Exception('Could not build Docker image')
# tag the image
p = subprocess.Popen('docker tag {tmp_image} {target_image}'.format(**locals()), shell=True)
rc = p.wait()
if rc != 0:
raise Exception('Could not tag Docker image')
# push the image, output to /dev/null
print('-----> Pushing image to private registry')
sys.stdout.flush(), sys.stderr.flush()
p = subprocess.Popen('docker push {target_image}'.format(**locals()), shell=True,
stdout=open(os.devnull, 'w'), stderr=open(os.devnull, 'w'))
rc = p.wait()
if rc != 0:
raise Exception('Could not push Docker image')
# construct json body for posting to the build hook
body = {}
body['receive_user'] = user
body['receive_repo'] = app
body['image'] = target_image
body['url'] = 'http://localhost/to/be/deprecated'
# extract the Procfile and convert to JSON
if os.path.exists(procfile):
with open(procfile) as f:
raw_procfile = f.read()
body['procfile'] = json.dumps(yaml.safe_load(raw_procfile))
body['size'] = os.stat(slug_path).st_size
# calculate sha256
sha256 = hashlib.sha256()
with open(slug_path) as f:
for chunk in iter(lambda: f.read(128), b''):
sha256.update(chunk)
# use sha of master
with open(os.path.join(repo_dir, 'refs/heads/master')) as f:
body['sha'] = f.read().strip('\n')
# trigger build hook
sys.stdout.write('\n Launching... ')
sys.stdout.flush()
url = "{{ .deis_controller_protocol }}://{{ .deis_controller_host }}:{{ .deis_controller_port }}/api/hooks/build"
headers = {'Content-Type': 'application/json', 'X-Deis-Builder-Auth': '{{ .deis_controller_builderKey }}'}
r = requests.post(url, headers=headers, data=json.dumps(body))
if r.status_code != 200:
raise Exception('Build hook error: {} {}'.format(r.status_code, r.text))
# write out results for git user
databag = r.json()
sys.stdout.write('done, v{version}\n\n'.format(**databag['release']))
print("-----> {app} deployed to Deis".format(**locals()))
domains = databag.get('domains', [])
if domains:
for domain in domains:
print(" http://{domain}".format(**locals()))
else:
print(' No proxy nodes found for this formation.\n')
print('\n To learn more, use `deis help` or visit http://deis.io\n')
except Exception as e:
print(e.message)
sys.exit(1)
finally:
l = locals()
shutil.rmtree(temp_dir)
if 'container' in l:
subprocess.Popen('docker rm {container}'.format(**l), shell=True,
stdout=open(os.devnull, 'w'), stderr=open(os.devnull, 'w'))
if 'tmp_image' in l:
subprocess.Popen('docker rmi {tmp_image}'.format(**l), shell=True,
stdout=open(os.devnull, 'w'), stderr=open(os.devnull, 'w'))