1#!/bin/bash
2
3set -e
4
5function printHelp() {
6  >&2 echo "USAGE: $0 -s INFLUXDB_SHA -b INFLUXDB_BRANCH -v INFLUXDB_VERSION
7
8Emits a tarball of influxdb source code and dependencies to /out,
9which must be a mounted volume if you want to access the file.
10
11If the directory /influxdb-git exists and is mounted,
12that will be used as the git repository used when cloning influxdb.
13"
14}
15
16if [ $# -eq 0 ]; then
17  printHelp
18  exit 1
19fi
20
21SHA=""
22BRANCH=""
23VERSION=""
24
25while getopts hs:b:v: arg; do
26  case "$arg" in
27    h) printHelp; exit 1;;
28    s) SHA="$OPTARG";;
29    b) BRANCH="$OPTARG";;
30    v) VERSION="$OPTARG";;
31  esac
32done
33
34if [ -z "$SHA" ] || [ -z "$BRANCH" ] || [ -z "$VERSION" ]; then
35  printHelp
36  exit 1
37fi
38
39IPATH=/influxdb-source
40mkdir -p "$IPATH" && cd "$IPATH"
41if [ -d /influxdb-git ]; then
42  git clone /influxdb-git "$IPATH/influxdb"
43else
44  echo "Influxdb .git directory required" >&2
45  exit 1
46fi
47
48(
49	cd influxdb
50	git checkout "$SHA"
51)
52
53# Emit version metadata to appropriate files.
54
55# Include machine-parseable metadata JSON file.
56printf '{
57"version": "%s",
58"branch": "%s",
59"sha": "%s"
60}' "$VERSION" "$BRANCH" "$SHA" > "./influxdb/.metadata.json"
61
62# Set version info for influxdb binaries.
63
64printf 'package main
65
66// Code generated by influxdata/releng tooling. DO NOT EDIT.
67
68func init() {
69	version = "%s"
70	branch = "%s"
71	commit = "%s"
72}' "$VERSION" "$BRANCH" "$SHA" > "./influxdb/cmd/influxd/version.generated.go"
73
74# influx uses just version.
75printf 'package main
76
77// Code generated by influxdata/releng tooling. DO NOT EDIT.
78
79func init() {
80	version = "%s"
81}' "$VERSION" > "./influxdb/cmd/influx/version.generated.go"
82
83# Prebuild the man pages so that consumers of the source tarball don't have to build it themselves.
84(cd "$IPATH"/influxdb/man && make build && gzip -9 ./*.1)
85
86TARBALL_NAME="influxdb-src-$SHA.tar.gz"
87tar -vC ${IPATH} -czf "/out/${TARBALL_NAME}" --exclude-vcs ./* # --exclude-vcs is a GNU tar option.
88(cd /out && md5sum "$TARBALL_NAME" > "$TARBALL_NAME.md5")
89(cd /out && sha256sum "$TARBALL_NAME" > "$TARBALL_NAME.sha256")
90