1#!/bin/bash
2
3set -e
4
5function printHelp() {
6  >&2 echo "USAGE: $0 [-r]
7
8Untars the influxdb source tarball mounted at /influxdb-src.tar.gz,
9then emits a tarball of influxdb binaries to /out,
10which must be a mounted volume if you want to access the file.
11
12Relies upon environment variables GOOS and GOARCH to determine what to build.
13Respects CGO_ENABLED.
14
15To build with race detection enabled, pass the -r flag.
16"
17}
18
19RACE_FLAG=""
20STATIC=""
21
22while getopts w:hrs arg; do
23  case "$arg" in
24    h) printHelp; exit 1;;
25    r) RACE_FLAG="-race";;
26    s) STATIC=1;;
27  esac
28done
29
30if [ -z "$GOOS" ] || [ -z "$GOARCH" ]; then
31  >&2 echo 'The environment variables $GOOS and $GOARCH must both be set.'
32  exit 1
33fi
34
35# Control the compiler for go. Rust linker is set in $HOME/.cargo/config
36if [[ "$GOOS" == darwin ]] ; then
37  export CC=x86_64-apple-darwin15-clang
38elif [[ "$GOOS" == windows ]] ; then
39  export CC=x86_64-w64-mingw32-gcc
40elif [[ "$GOARCH" == arm64 ]] ;then
41  export CC=aarch64-unknown-linux-musl-gcc
42fi
43
44WORKSPACE=/influxdata
45
46mkdir -p ${WORKSPACE}
47
48echo "Extracting influxdb tarball"
49# Extract tarball into WORKSPACE.
50
51tar -vxz -C ${WORKSPACE} -f /influxdb-src.tar.gz
52
53SHA=$(jq -r .sha < "${WORKSPACE}/influxdb/.metadata.json")
54
55OUTDIR=$(mktemp -d)
56(
57	cd ${WORKSPACE}/influxdb
58
59	BINARY_PACKAGES="
60		github.com/influxdata/influxdb/cmd/influxd
61		github.com/influxdata/influxdb/cmd/influx
62		github.com/influxdata/influxdb/cmd/influx_inspect
63		github.com/influxdata/influxdb/cmd/influx_tsm"
64
65	for cmd in $BINARY_PACKAGES; do
66		export CGO_ENABLED=1
67		echo "env for go build: GOOS=$GOOS GOARCH=$GOARCH CGO_ENABLED=$CGO_ENABLED"
68		# Note that we only do static builds for arm, to be consistent with influxdb 2.x
69		if [[ "$GOARCH" == arm64 ]] ; then
70			echo go build -i -o "$OUTDIR/$(basename $cmd)" -tags "netgo osusergo static_build noasm" $cmd
71			go build -i -o "$OUTDIR/$(basename $cmd)" -tags "netgo osusergo static_build noasm" $cmd
72		elif [[ -n "$STATIC" ]]; then
73			echo go build -i -o "$OUTDIR/$(basename $cmd)" -tags "netgo osusergo static_build" $cmd
74			go build -i -o "$OUTDIR/$(basename $cmd)" -tags "netgo osusergo static_build" $cmd
75		elif [[ "$GOOS" == windows ]] ; then
76			echo go build $RACE_FLAG -buildmode=exe -i -o "$OUTDIR/$(basename $cmd).exe" $cmd
77			go build $RACE_FLAG -buildmode=exe -i -o "$OUTDIR/$(basename $cmd).exe" $cmd
78		else
79			echo go build $RACE_FLAG -i -o "$OUTDIR/$(basename $cmd)" $cmd
80			go build $RACE_FLAG -i -o "$OUTDIR/$(basename $cmd)" $cmd
81		fi
82	done
83)
84
85SUFFIX=
86if [[ -n "$STATIC" ]]; then
87  # Only add the static suffix to the filename when explicitly requested.
88  SUFFIX=_static
89elif [ -n "$RACE_FLAG" ]; then
90  # -race depends on cgo, so this option is exclusive from CGO_ENABLED.
91  SUFFIX=_race
92fi
93
94TARBALL_NAME="influxdb_bin_${GOOS}_${GOARCH}${SUFFIX}-${SHA}.tar.gz"
95TARBALL_PATH="/out/${TARBALL_NAME}"
96echo tar -C ${OUTDIR} -cvzf ${TARBALL_PATH} .
97tar -C ${OUTDIR} -cvzf ${TARBALL_PATH} .
98(cd /out && md5sum "$TARBALL_NAME" > "$TARBALL_NAME.md5")
99(cd /out && sha256sum "$TARBALL_NAME" > "$TARBALL_NAME.sha256")
100