forked from sorintlab/stolon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build
executable file
·109 lines (90 loc) · 3.6 KB
/
build
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
#!/usr/bin/env bash
set -e
# Cross compatibility with osx
# origin source: https://github.com/kubernetes/kubernetes/blob/master/hack/lib/init.sh#L102
function readlinkdashf() {
# run in a subshell for simpler 'cd'
(
if [[ -d "$1" ]]; then # This also catch symlinks to dirs.
cd "$1"
pwd -P
else
cd $(dirname "$1")
local f
f=$(basename "$1")
if [[ -L "$f" ]]; then
readlink "$f"
else
echo "$(pwd -P)/${f}"
fi
fi
)
}
BASEDIR=$(readlinkdashf $(dirname $0))
BINDIR=${BASEDIR}/bin
if [ $PWD != $BASEDIR ]; then
cd $BASEDIR
fi
ORG_PATH="github.com/sorintlab"
REPO_PATH="${ORG_PATH}/stolon"
export GOPATH=${PWD}/gopath
rm -f $GOPATH/src/${REPO_PATH}
mkdir -p $GOPATH/src/${ORG_PATH}
ln -s ${PWD} $GOPATH/src/${REPO_PATH}
mkdir -p ${BINDIR}
export GO15VENDOREXPERIMENT=1
VERSION=${STOLON_VERSION:-$(${BASEDIR}/scripts/git-version.sh)}
LD_FLAGS="-s -X ${REPO_PATH}/cmd.Version=$VERSION"
# for static compilation we need to compile with cgo disabled (CGO_ENABLED=0),
# this will trigger a rebuild of all the packages and also the go std library
# (using a different install suffix called `cgo`, will use this since it's the
# name used by many projects but IMHO it's misleading...).
# If the user has write access to the go distribution pkg dir we can use "go
# install". If not possible we have to use `go build` which is slower since
# it'll rebuild every needed package everytime.
use_go_install=
go_root_dir=$(go env GOROOT)
go_host_os=$(go env GOHOSTOS)
go_host_arch=$(go env GOHOSTARCH)
cgodisabled_pkg_dir=${go_root_dir}/pkg/${go_host_os}_${go_host_arch}_cgo
if [ -e ${cgodisabled_pkg_dir} ]; then
use_go_install=1
fi
if [ -w ${go_root_dir}/pkg ]; then
use_go_install=1
fi
if [ -z $use_go_install ]; then
echo "Cannot find/create a go stdlib created with cgo disabled for the go release installed at ${go_root_dir} since ${go_root_dir}/pkg is not writable by `whoami`"
echo "The build will use \"go build\" instead of \"go install\". This is slower since every run will need to rebuild all the needed packages."
echo "To speed up the build you should make ${go_root_dir}/pkg writable for `whoami` for at least the first build"
echo "or manually rebuild stdlib executing the command 'CGO_ENABLED=0 go install -a -installsuffix cgo std' from a user with write access to ${go_root_dir}/pkg"
for cmd in sentinel proxy; do
CGO_ENABLED=0 go build -a -installsuffix cgo -ldflags "$LD_FLAGS" -o ${BINDIR}/stolon-${cmd} ${REPO_PATH}/cmd/${cmd}
done
CGO_ENABLED=0 go build -a -installsuffix cgo -ldflags "$LD_FLAGS" -o ${BINDIR}/stolonctl ${REPO_PATH}/cmd/stolonctl
else
for cmd in sentinel proxy; do
CGO_ENABLED=0 go install -installsuffix cgo -ldflags "$LD_FLAGS" ${REPO_PATH}/cmd/${cmd}
rm -f ${BINDIR}/stolon-${cmd}
cp ${GOPATH}/bin/${cmd} ${BINDIR}/stolon-${cmd}
done
CGO_ENABLED=0 go install -installsuffix cgo -ldflags "$LD_FLAGS" ${REPO_PATH}/cmd/stolonctl
rm -f ${BINDIR}/stolonctl
cp ${GOPATH}/bin/stolonctl ${BINDIR}/
fi
# stolon-keeper cannot be statically built since it needs to get its current
# running user and this is not available with cgo disabled
go install -ldflags "$LD_FLAGS" ${REPO_PATH}/cmd/keeper
rm -f ${BINDIR}/stolon-keeper
cp ${GOPATH}/bin/keeper ${BINDIR}/stolon-keeper
# Copy binaries to Dockerfile image directories
declare -a DOCKERFILE_PATHS
DOCKERFILE_PATHS=(${BASEDIR}/examples/kubernetes/image/docker ${BASEDIR}/examples/docker)
for path in "${DOCKERFILE_PATHS[@]}"
do
rm -rf $path/bin/
mkdir -p $path/bin/
for cmd in stolon-keeper stolon-sentinel stolon-proxy stolonctl; do
cp ${BINDIR}/${cmd} $path/bin/
done
done