-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuilder
More file actions
executable file
·201 lines (162 loc) · 5.45 KB
/
builder
File metadata and controls
executable file
·201 lines (162 loc) · 5.45 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env bash
#
# builder hook called on every git receive-pack
# NOTE: this script must be run as root (for docker access)
#
set -e
ARGS=3
get_app_name() {
echo $1 | awk -F"." '{print $1}'
}
get_git_sha() {
repo=$1
branch=$2
branch_file="${repo}/${branch}"
cat $branch_file
}
indent() {
echo " $@"
}
puts-step() {
echo "-----> $@"
}
puts-step-sameline() {
echo -n "-----> $@"
}
puts-warn() {
echo " ! $@"
}
usage() {
echo "Usage: $0 <user> <repo> <branch>"
}
if [ $# -ne $ARGS ]; then
usage
exit 1
fi
USER=$1
REPO=$2
BRANCH=$3
APP_NAME=$(get_app_name $REPO)
cd $(dirname $0) # ensure we are in the root dir
ROOT_DIR=$(pwd)
DOCKERFILE_SHIM="$ROOT_DIR/shim.dockerfile"
REPO_DIR="${ROOT_DIR}/${REPO}"
BUILD_DIR="${REPO_DIR}/build"
CACHE_DIR="${REPO_DIR}/cache"
# get git sha of branch
GIT_SHA=$(get_git_sha $REPO_DIR $BRANCH)
SHORT_SHA=${GIT_SHA:0:8}
# define image names
TMP_IMAGE="${APP_NAME}:git-${SHORT_SHA}"
TARGET_IMAGE="{{ .deis_registry_host }}:{{ .deis_registry_port }}/${APP_NAME}"
IMAGE_REFERENCE=$APP_NAME
# create app directories
mkdir -p $BUILD_DIR $CACHE_DIR
# create temporary directory inside the build dir for this push
TMP_DIR=$(mktemp -d --tmpdir=$BUILD_DIR)
cd $REPO_DIR
# extract git branch
git archive $BRANCH | tar -xC $TMP_DIR
# switch to app context
cd $TMP_DIR
if [ -f Dockerfile ]; then
DOCKERFILE=$(cat Dockerfile)
fi
if [ -f Procfile ]; then
PROCFILE=$(cat Procfile)
fi
# pull config from controller to be used during build
URL="{{ .deis_controller_protocol }}://{{ .deis_controller_host }}:{{ .deis_controller_port }}/api/hooks/config"
RESPONSE=$(curl -s -XPOST \
-H "Content-Type: application/json" \
-H "X-Deis-Builder-Auth: {{ .deis_controller_builderKey }}" \
--data "{\"receive_user\":\"$USER\",\"receive_repo\":\"$APP_NAME\"}" \
$URL)
# massage response for the environment variables in form HELLO=world
CONFIG=$(echo $RESPONSE | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["values"]' | jq -c -M "to_entries|map(\"\(.key)=\(.value|tostring)\")|.[]" 2> /dev/null || echo $RESPONSE)
if [ "$CONFIG" == "$RESPONSE" ];
then
puts-warn "failed retrieving config from controller"
puts-warn $RESPONSE
exit 1
fi
# build option string to send to slugbuilder
BUILD_OPTS=""
echo $CONFIG > tmp
while read envvar; do
if [ "$envvar" != "" ]; then
BUILD_OPTS+="-e $envvar "
fi
done < tmp
rm tmp
# if no Dockerfile is present, use slugbuilder to compile a heroku slug
# and write out a Dockerfile to use that slug
if [ ! -f Dockerfile ]; then
if [ -f /buildpacks ]; then
BUILD_OPTS+="-v /buildpacks:/tmp/buildpacks:rw "
# give non-root slubuilder user R/W perms for docker volumes
chown -R 2000:2000 /buildpacks
fi
# run in the background, we'll attach to it to retrieve logs
BUILD_OPTS+="-d "
BUILD_OPTS+="-v $TMP_DIR:/tmp/app "
BUILD_OPTS+="-v $CACHE_DIR:/tmp/cache:rw "
# give non-root slubuilder user R/W perms for docker volumes
chown -R 2000:2000 $TMP_DIR $CACHE_DIR
# build the application and attach to the process
JOB=$(docker run $BUILD_OPTS deis/slugbuilder)
docker attach $JOB
# copy out the compiled slug
docker cp $JOB:/tmp/slug.tgz .
# copy over the Dockerfile shim to the build dir
cp $DOCKERFILE_SHIM ./Dockerfile
fi
puts-step "Building Docker image"
docker build -t $TMP_IMAGE . 2>&1
docker tag $TMP_IMAGE $TARGET_IMAGE
puts-step "Pushing image to private registry"
docker push $TARGET_IMAGE &>/dev/null
if [ -f $TMP_DIR/slug.tgz ]; then
RELEASE_INFO=$(tar --to-stdout -xf $TMP_DIR/slug.tgz ./.release | python -c 'import sys,yaml,json;print json.dumps(yaml.safe_load(sys.stdin).get("default_process_types", {}))')
else
RELEASE_INFO="{}"
fi
if [ -f $TMP_DIR/Procfile ]; then
# update release info with data from the Procfile
RELEASE_INFO=$(echo $RELEASE_INFO | python -c "import sys,json,os,yaml;obj=json.load(sys.stdin);procfile=open('Procfile').read();obj.update(yaml.safe_load(procfile));print json.dumps(obj)")
fi
if [ -f $TMP_DIR/Dockerfile ]; then
DOCKERFILE=$(cat $TMP_DIR/Dockerfile)
fi
# safely escape double quotes
RELEASE_INFO=$(echo $RELEASE_INFO | sed -e 's/\"/\\\"/g')
DOCKERFILE=$(echo $DOCKERFILE | sed -e 's/\"/\\\"/g')
puts-step-sameline "Launching... "
URL="{{ .deis_controller_protocol }}://{{ .deis_controller_host }}:{{ .deis_controller_port }}/api/hooks/build"
DATA="{\"sha\":\"$GIT_SHA\",\"receive_user\":\"$USER\",\"receive_repo\":\"$APP_NAME\",\"image\":\"$IMAGE_REFERENCE\",\"procfile\":\"$RELEASE_INFO\",\"dockerfile\":\"$DOCKERFILE\"}"
# notify the controller that the push was successful
RESPONSE=$(curl -s -XPOST \
-H "Content-Type: application/json" \
-H "X-Deis-Builder-Auth: {{ .deis_controller_builderKey }}" \
--data "$DATA" \
$URL)
RELEASE=$(echo $RESPONSE | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["release"]["version"]' 2> /dev/null || echo $RESPONSE)
if [ "$RELEASE" == "$RESPONSE" ];
then
# we're printing inline with "Launching..."
echo "failed"
puts-warn $RESPONSE
exit 1
fi
DOMAIN=$(echo $RESPONSE | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["domains"][0]')
echo "done, v$RELEASE"
puts-step "$APP_NAME deployed to Deis"
echo
indent "http://$DOMAIN"
echo
indent "To learn more, use \`deis help\` or visit http://deis.io"
# cleanup
cd $REPO_DIR
git gc &>/dev/null
docker rm -f $JOB &>/dev/null
docker rmi -f $TMP_IMAGE $TARGET_IMAGE &>/dev/null