1#!/bin/bash
2
3function printHelp() {
4  >&2 echo "USAGE: $0 [-r]
5
6Untars the influxdb source tarball mounted at /influxdb-src.tar.gz,
7then emits a tarball of influxdb binaries to /out,
8which must be a mounted volume if you want to access the file.
9
10Relies upon environment variables GOOS and GOARCH to determine what to build.
11Respects CGO_ENABLED.
12
13To build with race detection enabled, pass the -r flag.
14"
15}
16
17RACE_FLAG=""
18
19while getopts hr arg; do
20  case "$arg" in
21    h) printHelp; exit 1;;
22    r) RACE_FLAG="-race";;
23  esac
24done
25
26
27if [ -z "$GOOS" ] || [ -z "$GOARCH" ]; then
28  >&2 echo 'The environment variables $GOOS and $GOARCH must both be set.'
29  exit 1
30fi
31
32
33# Extract tarball into GOPATH.
34tar xz -C "$GOPATH" -f /influxdb-src.tar.gz
35
36SHA=$(jq -r .sha < "$GOPATH/src/github.com/influxdata/influxdb/.metadata.json")
37
38
39SUFFIX=
40if [ "$CGO_ENABLED" == "0" ]; then
41  # Only add the static suffix to the filename when explicitly requested.
42  SUFFIX=_static
43elif [ -n "$RACE_FLAG" ]; then
44  # -race depends on cgo, so this option is exclusive from CGO_ENABLED.
45  SUFFIX=_race
46fi
47
48TARBALL_NAME="influxdb_bin_${GOOS}_${GOARCH}${SUFFIX}-${SHA}.tar.gz"
49
50# note: according to https://github.com/golang/go/wiki/GoArm
51# we want to support armel using GOARM=5
52# and we want to support armhf using GOARM=6
53# no GOARM setting is necessary for arm64
54if [ $GOARCH == "armel" ]; then
55  GOARCH=arm
56  GOARM=5
57fi
58
59if [ $GOARCH == "armhf" ]; then
60  GOARCH=arm
61  GOARM=6
62fi
63
64
65
66OUTDIR=$(mktemp -d)
67for cmd in \
68  influxdb/cmd/influxd \
69  influxdb/cmd/influx_stress \
70  influxdb/cmd/influx \
71  influxdb/cmd/influx_inspect \
72  influxdb/cmd/influx_tsm \
73  ; do
74    # Build all the binaries into $OUTDIR.
75    # Windows binaries will get the .exe suffix as expected.
76    (cd "$OUTDIR" && go build $RACE_FLAG -i "github.com/influxdata/$cmd")
77done
78
79
80(cd "$OUTDIR" && tar czf "/out/$TARBALL_NAME" ./*)
81(cd /out && md5sum "$TARBALL_NAME" > "$TARBALL_NAME.md5")
82(cd /out && sha256sum "$TARBALL_NAME" > "$TARBALL_NAME.sha256")
83