-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuilder
More file actions
130 lines (104 loc) · 3.08 KB
/
builder
File metadata and controls
130 lines (104 loc) · 3.08 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
#!/usr/bin/env bash
#
# builder hook called on every git receive-pack
# NOTE: this script must be run as root (for docker access)
#
set -eo pipefail
ARGS=3
indent() {
echo " $@"
}
puts-step() {
echo "-----> $@"
}
puts-step-sameline() {
echo -n "-----> $@"
}
puts-warn() {
echo " ! $@"
}
usage() {
echo "Usage: $0 <user> <repo> <sha>"
}
parse-string(){
# helper to avoid the single quote escape
# occurred in command substitution
local args=() idx=0 IFS=' ' c
for c; do printf -v args[idx++] '%s ' "$c"; done
printf "%s\n" "${args[*]}"
}
if [ $# -ne $ARGS ]; then
usage
exit 1
fi
USER=$1
REPO=$2
GIT_SHA=$3
SHORT_SHA=${GIT_SHA:0:8}
APP_NAME="${REPO%.*}"
cd $(dirname $0) # ensure we are in the root dir
ROOT_DIR=$(pwd)
DOCKERFILE_SHIM="/usr/local/share/shim.dockerfile"
REPO_DIR="${ROOT_DIR}/${REPO}"
BUILD_DIR="${REPO_DIR}/build"
CACHE_DIR="${REPO_DIR}/cache"
# define image names
IMAGE_NAME="$APP_NAME:git-$SHORT_SHA"
TMP_IMAGE="{{ getv "/deis/registry/host" }}:{{ getv "/deis/registry/port" }}/$IMAGE_NAME"
# create app directories
mkdir -p $BUILD_DIR $CACHE_DIR
# create temporary directory inside the build dir for this push
TMP_DIR=$(mktemp -d -p $BUILD_DIR)
cd $REPO_DIR
git archive $GIT_SHA | tar -xmC $TMP_DIR
# switch to app context
cd $TMP_DIR
USING_DOCKERFILE=true
# pull config from controller to be used during build
URL="{{ getv "/deis/controller/protocol" }}://{{ getv "/deis/controller/host" }}:{{ getv "/deis/controller/port" }}/v1/hooks/config"
RESPONSE=$(get-app-config -url="$URL" -key="{{ getv "/deis/controller/builderKey" }}" -user=$USER -app=$APP_NAME)
CODE=$?
if [ $CODE -ne 0 ]; then
puts-warn $RESPONSE
exit 1
fi
BUILD_OPTS=()
BUILD_OPTS+='/usr/bin/docker'
BUILD_OPTS+=' run -v /etc/environment_proxy:/etc/environment_proxy'
# get application configuration
BUILD_OPTS+=$(echo $RESPONSE | get-app-values)
# force newline
echo "" >> Dockerfile
# inject builder-specific environment variables into the application environment
echo "ENV GIT_SHA $GIT_SHA" >> Dockerfile
echo
puts-step "Building Docker image"
docker -H ${HOST}:2375 build -t $TMP_IMAGE . 2>&1
puts-step "Pushing image to private registry"
docker -H ${HOST}:2375 push $TMP_IMAGE &>/dev/null
echo
PROCFILE="{}"
puts-step "Launching... "
URL="{{ getv "/deis/controller/protocol" }}://{{ getv "/deis/controller/host" }}:{{ getv "/deis/controller/port" }}/v1/hooks/build"
DATA=$(generate-buildhook "$SHORT_SHA" "$USER" "$APP_NAME" "$APP_NAME" "$PROCFILE" "$USING_DOCKERFILE")
PUBLISH_RELEASE=$(echo "$DATA" | publish-release-controller -url=$URL -key={{ getv "/deis/controller/builderKey" }})
CODE=$?
if [ $CODE -ne 0 ]; then
puts-warn "ERROR: Failed to launch container"
puts-warn $PUBLISH_RELEASE
exit 1
fi
RELEASE=$(echo $PUBLISH_RELEASE | extract-version)
DOMAIN=$(echo $PUBLISH_RELEASE | extract-domain)
indent "done, $APP_NAME:v$RELEASE deployed to Deis"
echo
indent "http://$DOMAIN"
echo
indent "To learn more, use \`deis help\` or visit http://deis.io"
echo
# cleanup
cd $REPO_DIR
git gc &>/dev/null
if [ -n "$JOB" ]; then
docker rm -f $JOB &>/dev/null
fi