This repository has been archived by the owner on Jan 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathbuild.sh
executable file
·191 lines (165 loc) · 5.36 KB
/
build.sh
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
#!/bin/sh
GOLANG_BUILDER_IMAGE=golang:1.7
DEFAULT_LOGSPOUT_VERSION=v3.1
BASE_IMAGE=scratch
IMAGE=rtoma/logspout-redis-logstash
usage() {
echo "Usage: $0 [-c] [-d] [-n] [-b <base image>] [-g <golang builder image>] <app version> [<logspout version>]"
echo
echo "Parameters:"
echo " -c : Don't use cache for Golang sources (will slow down building)."
echo " -d : Enable development mode. Local sourcefile will be used instead of Github."
echo " -n : Skip building of Docker image, to allow manual building."
echo " -b <image> : Set different Docker base image."
echo " -g <image> : Set different Golang builder image."
exit "${1:-0}"
}
DEVMODE=0
BUILDMODE=1
USECACHE=1
while getopts "cdnhg:b:" opt; do
case $opt in
c ) USECACHE=0;;
d ) DEVMODE=1;;
n ) BUILDMODE=0;;
g ) GOLANG_BUILDER_IMAGE="$OPTARG";;
b ) BASE_IMAGE="$OPTARG";;
h ) usage;;
\?) usage 1;;
: ) usage 1;;
esac
done
shift "$((OPTIND - 1))"
app_version=$1
logspout_version=${2:-$DEFAULT_LOGSPOUT_VERSION}
if [ -z "$app_version" ]; then
echo "Missing <app version>"
usage 1
fi
if [ "$DEVMODE" -eq 1 ]; then
echo "[*] Running in DEV mode - using local sourcefile"
app_version="${app_version}-dev"
fi
set -e
# setup clean target dir
targetdir=$PWD/target
[ ! -d "$targetdir" ] && mkdir -p "$targetdir"
if [ "$USECACHE" -eq 1 ]; then
echo "[*] Using local cachedir for Golang sources"
cachedir=$PWD/.cache
[ ! -d "$cachedir" ] && mkdir -p "$cachedir"
docker_cacheopts="-v $cachedir:/go/src"
else
echo "[*] Not using local cachedir for Golang sources (this will slow down builds)"
docker_cacheopts=
fi
# remove old artifact
artifact=linux.bin
[ -e "$targetdir/$artifact" ] && rm -f "$targetdir/$artifact"
golangbuilder=$PWD/.golangbuilder.sh
dockerfile=$targetdir/Dockerfile
trap 'echo "[*] Cleaning up"; rm -f "$golangbuilder"; [ "$BUILDMODE" -eq 1 ] && rm -rf "$dockerfile"; exit' EXIT
# create script to run inside of golang builder
cat > "$golangbuilder" <<EOF
#!/bin/sh
set -ex
cd \$GOPATH
# fix for internal gobuilder image with bad src
#rm -rf src/golang.org/x/net
if [ ! -d "src/github.com/docker/docker" ]; then
# minimize download
git clone --single-branch --depth 1 https://github.com/docker/docker src/github.com/docker/docker
fi
repo1=github.com/gliderlabs/logspout
repo2=github.com/rtoma/logspout-redis-logstash
# ensure we get the current logspout version
if [ ! -d "src/\$repo1" ]; then
# not cached, so get fresh
git clone --single-branch -b "$logspout_version" --depth 1 https://\$repo1 src/\$repo1
# give detached head a name so we can check later
cd src/\$repo1
git checkout -b "$logspout_version"
cd -
# save file for later
cp src/\$repo1/modules.go src/\$repo1/modules.go.bak
elif [ "\$(cd src/\$repo1 && git rev-parse --abbrev-ref HEAD | cut -d/ -f2-)" != "$logspout_version" ]; then
# if already in cache but different version, rm and get required version
rm -rf src/\$repo1
git clone --single-branch -b "$logspout_version" --depth 1 https://\$repo1 src/\$repo1
# give detached head a name so we can check later
cd src/\$repo1
git checkout -b "$logspout_version"
cd -
# save file for later
cp src/\$repo1/modules.go src/\$repo1/modules.go.bak
else
# use saved file to overwrite our custom file
cp src/\$repo1/modules.go.bak src/\$repo1/modules.go
fi
# get deps
go get -v \$repo1
# always start clean, wether dev mode or not
rm -rf src/\$repo2
# in dev mode: mkdir and copy source from local repo
if [ "$DEVMODE" -eq 1 ]; then
mkdir -p src/\$repo2
cp -rp /localrepo/* src/\$repo2
else
# not in dev mode: get our version tag from github
git clone --single-branch -b "$app_version" --depth 1 https://\$repo2 src/\$repo2
fi
# get deps for build + testing
go get -v -t \$repo2
cat > src/\$repo1/modules.go <<EOM
package main
import (
_ "github.com/gliderlabs/logspout/httpstream"
_ "github.com/gliderlabs/logspout/routesapi"
_ "github.com/rtoma/logspout-redis-logstash"
)
EOM
cd src/\$repo2
go test -v
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /target/linux.bin -ldflags "-X main.Version=v${app_version}-${logspout_version}" github.com/gliderlabs/logspout
EOF
chmod a+x "$golangbuilder"
# exec builder
echo "[*] Running Golang builder to compile v$app_version ..."
echo "[*] Golang image used: $GOLANG_BUILDER_IMAGE"
docker run --rm \
-v "$golangbuilder":/builder.sh:ro \
-v "$PWD":/localrepo:ro \
-v "$targetdir":/target \
-e "http_proxy=${http_proxy:-}" \
-e "https_proxy=${https_proxy:-}" \
-e "HTTP_PROXY=$HTTP_PROXY" \
-e "HTTPS_PROXY=$HTTPS_PROXY" \
$docker_cacheopts \
"$GOLANG_BUILDER_IMAGE" /builder.sh
echo
if [ ! -e "$targetdir/$artifact" ]; then
echo "Building artifact failed. Stopping here..."
exit 2
fi
cat > "$dockerfile" <<EOF
FROM $BASE_IMAGE
COPY $artifact /$artifact
ENTRYPOINT ["/$artifact"]
EOF
if [ "$BUILDMODE" -eq 1 ]; then
echo "[*] Building Docker image $IMAGE:$app_version ..."
docker build -f "$dockerfile" \
--build-arg "http_proxy=$http_proxy" \
--build-arg "https_proxy=$https_proxy" \
--build-arg "HTTP_PROXY=$HTTP_PROXY" \
--build-arg "HTTPS_PROXY=$HTTPS_PROXY" \
-t "$IMAGE:$app_version" target/
echo
echo "[*] Built $IMAGE image:"
docker images | grep "^$IMAGE"
else
echo "[*] We're in manual build mode: no Docker image will be build"
echo "Dockerfile: $dockerfile"
echo "Artifact : $targetdir/$artifact"
fi
echo