-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuilder
More file actions
176 lines (150 loc) · 5.25 KB
/
Copy pathbuilder
File metadata and controls
176 lines (150 loc) · 5.25 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
#!/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
HOST=`ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'`
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)
REPO_DIR="${ROOT_DIR}/${REPO}"
BUILD_DIR="${REPO_DIR}/build"
CACHE_DIR="${REPO_DIR}/cache"
# define image names
SLUG_NAME="$APP_NAME:git-$SHORT_SHA"
META_NAME=`echo ${SLUG_NAME}| tr ":" "-"`
TMP_IMAGE="$DEIS_REGISTRY_SERVICE_HOST:$DEIS_REGISTRY_SERVICE_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
# use Procfile if provided, otherwise try default process types from ./release
git archive --format=tar.gz ${GIT_SHA} > ${APP_NAME}.tar.gz
tar -xzf ${APP_NAME}.tar.gz -C $TMP_DIR/
USING_DOCKERFILE=true
if [ -f $TMP_DIR/Procfile ]; then
PROCFILE=$(cat $TMP_DIR/Procfile | yaml2json-procfile)
USING_DOCKERFILE=false
else
PROCFILE="{}"
fi
if [[ ! -f /var/run/secrets/object/store/access-key-id ]]; then
if $USING_DOCKERFILE ; then
l1=`grep -n "object-store" /etc/deis-dockerbuilder.yaml | head -n1 |cut -d ":" -f1`
l2=$(($l1+3))
sed "$l1,$l2 d" /etc/deis-dockerbuilder.yaml > /etc/${SLUG_NAME}.yaml.tmp
l1=`grep -n "object-store" /etc/deis-dockerbuilder.yaml.tmp | head -n1 |cut -d ":" -f1`
l2=$(($l1+3))
sed "$l1,$l2 d" /etc/${SLUG_NAME}.yaml.tmp > /etc/${SLUG_NAME}.yaml
sed -i -- "s#repo_name#$TMP_IMAGE#g" /etc/${SLUG_NAME}.yaml
else
head -n 21 /etc/deis-slugbuilder.yaml > /etc/${SLUG_NAME}.yaml
fi
else
if $USING_DOCKERFILE ; then
cp /etc/deis-dockerbuilder.yaml /etc/${SLUG_NAME}.yaml
sed -i -- "s#repo_name#$TMP_IMAGE#g" /etc/${SLUG_NAME}.yaml
else
cp /etc/deis-slugbuilder.yaml /etc/${SLUG_NAME}.yaml
fi
fi
git archive --format=tar.gz ${GIT_SHA} > ${APP_NAME}.tar.gz
HTTP_PREFIX="http"
# if minio is in the cluster, use it. otherwise use fetcher
# TODO: figure out something for using S3 also
if [[ -n "$DEIS_MINIO_SERVICE_HOST" && -n "$DEIS_MINIO_SERVICE_PORT" ]]; then
S3EP=${DEIS_MINIO_SERVICE_HOST}:${DEIS_MINIO_SERVICE_PORT}
elif [[ -n "$DEIS_OUTSIDE_STORAGE_HOST" && -n "$DEIS_OUTSIDE_STORAGE_HOST" ]]; then
HTTP_PREFIX="https"
S3EP=${DEIS_OUTSIDE_STORAGE_HOST}:${DEIS_OUTSIDE_STORAGE_HOST}
elif [ -z "$S3EP" ]; then
S3EP=${HOST}:3000
fi
TAR_URL=$HTTP_PREFIX://$S3EP/git/home/${SLUG_NAME}/tar
PUSH_URL=$HTTP_PREFIX://$S3EP/git/home/${SLUG_NAME}/push
sed -i -- "s#repo_name#$META_NAME#g" /etc/${SLUG_NAME}.yaml
sed -i -- "s#puturl#$PUSH_URL#g" /etc/${SLUG_NAME}.yaml
sed -i -- "s#tar-url#$TAR_URL#g" /etc/${SLUG_NAME}.yaml
ACCESS_KEY=`cat /var/run/secrets/object/store/access-key-id`
ACCESS_SECRET=`cat /var/run/secrets/object/store/access-secret-key`
# copy the self signed cert into the CA directory for alpine.
# note: we're not running minio with SSL at all right now, so no need for this.
# future SSL rollouts for in-cluster storage may not need it either if we set up an intermediate CA
# CERT_FILE="/var/run/secrets/object/ssl/access-cert"
# cp $CERT_FILE /etc/ssl/certs/deis-minio-self-signed-cert.crt
mkdir -p /var/minio-conf
CONFIG_DIR=/var/minio-conf
MC_PREFIX="mc -C $CONFIG_DIR --quiet"
$MC_PREFIX config host all "$HTTP_PREFIX://$S3EP" $ACCESS_KEY $ACCESS_SECRET
$MC_PREFIX mb "$HTTP_PREFIX://${S3EP}/git"
$MC_PREFIX cp ${APP_NAME}.tar.gz $TAR_URL
echo "stored tarfile in $TAR_URL"
kubectl create -f /etc/${SLUG_NAME}.yaml
#check for image creation or slug existence in S3EP
while [ ! -f /apps/${SLUG_NAME}/slug.tgz ]
do
sleep 2
echo "no file"
done
URL="http://$DEIS_WORKFLOW_SERVICE_HOST:$DEIS_WORKFLOW_SERVICE_PORT/v2/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
# use Procfile if provided, otherwise try default process types from ./release
puts-step "Launching... "
URL="http://$DEIS_WORKFLOW_SERVICE_HOST:$DEIS_WORKFLOW_SERVICE_PORT/v2/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