1#!/bin/bash
2
3set -e
4
5function printHelp() {
6  >&2 echo "\
7USAGE: $0 -O OS -A ARCH
8
9Creates packages for the given OS/ARCH, using the influxdb source tarball mounted at
10/influxdb-src.tar.gz and the binaries tarball mounted at /influxdb-bin.tar.gz .
11"
12}
13
14if [ $# -eq 0 ]; then
15  printHelp
16  exit 1
17fi
18
19OS=""
20ARCH=""
21STATIC=""
22
23while getopts hO:A:s arg; do
24  case "$arg" in
25    h) printHelp; exit 1;;
26    O) OS="$OPTARG";;
27    # For backwards compatibility, ensure the packages say i386 if using GOARCH=386.
28    A) ARCH="$(echo "$OPTARG" | sed 's/386/i386/')";;
29    s) STATIC="1";;
30  esac
31done
32
33if [ -z "$OS" ] || [ -z "$ARCH" ]; then
34  printHelp
35  exit 1
36fi
37
38mkdir -p /go
39tar x -C /go -zf /influxdb-src.tar.gz
40ln -s /go/src/github.com/influxdata/influxdb /isrc # Shorthand for influxdb source.
41SHA=$(jq -r .sha < "/isrc/.metadata.json")
42VERSION=$(jq -r .version < "/isrc/.metadata.json")
43ARCHIVE_ROOT_NAME="influxdb-${VERSION}-1"
44PKG_ROOT="/pkg/$ARCHIVE_ROOT_NAME"
45
46# Extract the respective binaries to dedicated folders.
47mkdir -p /ibin
48(cd /ibin && tar xzf /influxdb-bin.tar.gz)
49
50if [ "$OS" == "linux" ] && [ "$STATIC" == "1" ]; then
51  # Static linux packages get only the binaries and the conf file in the root directory,
52  # plus the man pages in the full path.
53  rm -rf "$PKG_ROOT"
54  mkdir -p "$PKG_ROOT"
55
56  cp /ibin/* "$PKG_ROOT/"
57  cp /isrc/etc/config.sample.toml "$PKG_ROOT/influxdb.conf"
58
59  mkdir -p "$PKG_ROOT/usr/share/man/man1"
60  cp /isrc/man/*.1.gz "$PKG_ROOT/usr/share/man/man1"
61
62  # Creating tarball from /pkg, NOT from $PKG_ROOT, so that influxdb-$VERSION-1 directory is present in archive.
63  (cd /pkg && tar czf "/out/influxdb-${VERSION}-static_${OS}_${ARCH}.tar.gz" ./*)
64
65  (cd /out && for f in *.tar.gz; do
66    md5sum "$f" > "$f.md5"
67    sha256sum "$f" > "$f.sha256"
68  done)
69elif [ "$OS" == "linux" ] || [ "$OS" == "darwin" ]; then
70  #############################
71  ####### Data packages #######
72  #############################
73
74  # Create layout for packaging under $PKG_ROOT.
75  rm -rf "$PKG_ROOT"
76  mkdir -p "$PKG_ROOT/usr/bin" \
77           "$PKG_ROOT/var/log/influxdb" \
78           "$PKG_ROOT/var/lib/influxdb" \
79           "$PKG_ROOT/usr/lib/influxdb/scripts" \
80           "$PKG_ROOT/usr/share/man/man1" \
81           "$PKG_ROOT/etc/influxdb" \
82           "$PKG_ROOT/etc/logrotate.d"
83  chmod -R 0755 /pkg
84
85  # Copy service scripts.
86  cp /isrc/scripts/init.sh "$PKG_ROOT/usr/lib/influxdb/scripts/init.sh"
87  chmod 0644 "$PKG_ROOT/usr/lib/influxdb/scripts/init.sh"
88  cp /isrc/scripts/influxdb.service "$PKG_ROOT/usr/lib/influxdb/scripts/influxdb.service"
89  chmod 0644 "$PKG_ROOT/usr/lib/influxdb/scripts/influxdb.service"
90
91  # Copy logrotate script.
92  cp /isrc/scripts/logrotate "$PKG_ROOT/etc/logrotate.d/influxdb"
93  chmod 0644 "$PKG_ROOT/etc/logrotate.d/influxdb"
94
95  # Copy sample config.
96  cp /isrc/etc/config.sample.toml "$PKG_ROOT/etc/influxdb/influxdb.conf"
97
98  # Copy data binaries.
99  cp /ibin/* "$PKG_ROOT/usr/bin/"
100
101  # Copy man pages.
102  cp /isrc/man/*.1.gz "$PKG_ROOT/usr/share/man/man1"
103
104  # Make tarball of files in packaging.
105  BIN_GZ_NAME="/out/influxdb-${VERSION}_${OS}_${ARCH}.tar.gz"
106  if [ "$STATIC" == "1" ]; then
107    BIN_GZ_NAME="/out/influxdb-${VERSION}-static_${OS}_${ARCH}.tar.gz"
108  fi
109
110  # Creating tarball from /pkg, NOT from $PKG_ROOT, so that influxdb-$VERSION-1 directory is present in archive.
111  (cd /pkg && tar czf $BIN_GZ_NAME ./*)
112
113  if [ "$OS" == "linux" ] ; then
114    # Call fpm to build .deb and .rpm packages.
115    for typeargs in "-t deb" "-t rpm --depends coreutils --depends shadow-utils"; do
116      FPM_NAME=$(
117      fpm \
118        -s dir \
119        $typeargs \
120        --log error \
121        --vendor InfluxData \
122        --url "https://influxdata.com" \
123        --after-install /isrc/scripts/post-install.sh \
124        --before-install /isrc/scripts/pre-install.sh \
125        --after-remove /isrc/scripts/post-uninstall.sh \
126        --license Proprietary \
127        --maintainer "support@influxdb.com" \
128        --directories /var/log/influxdb \
129        --directories /var/lib/influxdb \
130        --description 'Distributed time-series database.' \
131        --config-files /etc/influxdb/influxdb.conf \
132        --config-files /etc/logrotate.d/influxdb \
133        --name "influxdb" \
134        --architecture "$ARCH" \
135        --version "$VERSION" \
136        --iteration 1 \
137        -C "$PKG_ROOT" \
138        -p /out \
139         | ruby -e 'puts (eval ARGF.read)[:path]' )
140
141        echo "fpm created $FPM_NAME"
142        NEW_NAME=$(echo "$FPM_NAME" | rev | sed "s/1-//" | rev)
143        echo "renaming to ${NEW_NAME}"
144        mv "${FPM_NAME}" "${NEW_NAME}"
145    done
146  fi
147
148  #############################
149  ######### Checksums #########
150  #############################
151  (cd /out && find . \( -name '*.deb' -o -name '*.rpm' -o -name '*.tar.gz' \) -exec sh -c 'md5sum {} > {}.md5 && sha256sum {} > {}.sha256' \;)
152elif [ "$OS" == "windows" ]; then
153  # Windows gets the binaries and the sample config file.
154  rm -rf "$PKG_ROOT"
155  mkdir -p "$PKG_ROOT"
156  cp /ibin/*.exe "$PKG_ROOT"
157  cp /isrc/etc/config.sample.toml "$PKG_ROOT/influxdb.conf"
158
159  (cd /pkg && zip -9 -r "/out/influxdb-${VERSION}_${OS}_${ARCH}.zip" ./*)
160  (cd /out && for f in *.zip; do
161    md5sum "$f" > "$f.md5"
162    sha256sum "$f" > "$f.sha256"
163  done)
164else
165  >&2 echo "Unrecognized OS: $OS"
166  exit 1
167fi
168