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
38WORK=/influxdata
39mkdir -p ${WORK}
40tar x -C ${WORK} -zf /influxdb-src.tar.gz
41ln -s ${WORK}/influxdb /isrc # Shorthand for influxdb source.
42SHA=$(jq -r .sha < "/isrc/.metadata.json")
43VERSION=$(jq -r .version < "/isrc/.metadata.json")
44ARCHIVE_ROOT_NAME="influxdb-${VERSION}-1"
45PKG_ROOT="/pkg/$ARCHIVE_ROOT_NAME"
46
47# Extract the respective binaries to dedicated folders.
48mkdir -p /ibin
49(cd /ibin && tar xzf /influxdb-bin.tar.gz)
50
51if [ "$OS" == "linux" ] && [ "$STATIC" == "1" ]; then
52  # Static linux packages get only the binaries and the conf file in the root directory,
53  # plus the man pages in the full path.
54  rm -rf "$PKG_ROOT"
55  mkdir -p "$PKG_ROOT"
56
57  cp /ibin/* "$PKG_ROOT/"
58  cp /isrc/etc/config.sample.toml "$PKG_ROOT/influxdb.conf"
59
60  mkdir -p "$PKG_ROOT/usr/share/man/man1"
61  cp /isrc/man/*.1.gz "$PKG_ROOT/usr/share/man/man1"
62
63  # Creating tarball from /pkg, NOT from $PKG_ROOT, so that influxdb-$VERSION-1 directory is present in archive.
64  (cd /pkg && tar czf "/out/influxdb-${VERSION}-static_${OS}_${ARCH}.tar.gz" ./*)
65
66  (cd /out && for f in *.tar.gz; do
67    md5sum "$f" > "$f.md5"
68    sha256sum "$f" > "$f.sha256"
69  done)
70elif [ "$OS" == "linux" ] || [ "$OS" == "darwin" ]; then
71  #############################
72  ####### Data packages #######
73  #############################
74
75  # Create layout for packaging under $PKG_ROOT.
76  rm -rf "$PKG_ROOT"
77  mkdir -p "$PKG_ROOT/usr/bin" \
78           "$PKG_ROOT/var/log/influxdb" \
79           "$PKG_ROOT/var/lib/influxdb" \
80           "$PKG_ROOT/usr/lib/influxdb/scripts" \
81           "$PKG_ROOT/usr/share/man/man1" \
82           "$PKG_ROOT/etc/influxdb" \
83           "$PKG_ROOT/etc/logrotate.d"
84  chmod -R 0755 /pkg
85
86  # Copy service scripts.
87  cp /isrc/scripts/init.sh "$PKG_ROOT/usr/lib/influxdb/scripts/init.sh"
88  chmod 0644 "$PKG_ROOT/usr/lib/influxdb/scripts/init.sh"
89  cp /isrc/scripts/influxdb.service "$PKG_ROOT/usr/lib/influxdb/scripts/influxdb.service"
90  chmod 0644 "$PKG_ROOT/usr/lib/influxdb/scripts/influxdb.service"
91
92  # Copy logrotate script.
93  cp /isrc/scripts/logrotate "$PKG_ROOT/etc/logrotate.d/influxdb"
94  chmod 0644 "$PKG_ROOT/etc/logrotate.d/influxdb"
95
96  # Copy sample config.
97  cp /isrc/etc/config.sample.toml "$PKG_ROOT/etc/influxdb/influxdb.conf"
98
99  # Copy data binaries.
100  cp /ibin/* "$PKG_ROOT/usr/bin/"
101
102  # Copy man pages.
103  cp /isrc/man/*.1.gz "$PKG_ROOT/usr/share/man/man1"
104
105  # Make tarball of files in packaging.
106  BIN_GZ_NAME="/out/influxdb-${VERSION}_${OS}_${ARCH}.tar.gz"
107  if [ "$STATIC" == "1" ]; then
108    BIN_GZ_NAME="/out/influxdb-${VERSION}-static_${OS}_${ARCH}.tar.gz"
109  fi
110
111  # Creating tarball from /pkg, NOT from $PKG_ROOT, so that influxdb-$VERSION-1 directory is present in archive.
112  (cd /pkg && tar czf $BIN_GZ_NAME ./*)
113
114  if [ "$OS" == "linux" ] ; then
115    # Call fpm to build .deb and .rpm packages.
116    for typeargs in "-t deb" "-t rpm --depends coreutils --depends shadow-utils"; do
117      ARCH_CONVERTED=$ARCH
118      pkg_t=$(echo $typeargs | cut -d ' ' -f2)
119      if [ "$pkg_t" == "rpm" ] && [ $"$ARCH" == "armhf" ]; then
120        ARCH_CONVERTED="armv7hl"
121      elif [ "$pkg_t" == "rpm" ] && [ $"$ARCH" == "arm64" ]; then
122        ARCH_CONVERTED="aarch64"
123      fi
124      FPM_NAME=$(
125      fpm \
126        -s dir \
127        $typeargs \
128        --log error \
129        --vendor InfluxData \
130        --url "https://influxdata.com" \
131        --after-install /isrc/scripts/post-install.sh \
132        --before-install /isrc/scripts/pre-install.sh \
133        --after-remove /isrc/scripts/post-uninstall.sh \
134        --license Proprietary \
135        --maintainer "support@influxdb.com" \
136        --directories /var/log/influxdb \
137        --directories /var/lib/influxdb \
138        --rpm-attr 755,influxdb,influxdb:/var/log/influxdb \
139        --rpm-attr 755,influxdb,influxdb:/var/lib/influxdb \
140        --description 'Distributed time-series database.' \
141        --config-files /etc/influxdb/influxdb.conf \
142        --config-files /etc/logrotate.d/influxdb \
143        --name "influxdb" \
144        --architecture "$ARCH_CONVERTED" \
145        --version "$VERSION" \
146        --iteration 1 \
147        -C "$PKG_ROOT" \
148        -p /out \
149         | ruby -e 'puts (eval ARGF.read)[:path]' )
150
151        echo "fpm created $FPM_NAME"
152        NEW_NAME=$(echo "$FPM_NAME" | rev | sed "s/1-//" | rev)
153        echo "renaming to ${NEW_NAME}"
154        mv "${FPM_NAME}" "${NEW_NAME}"
155    done
156  fi
157
158  #############################
159  ######### Checksums #########
160  #############################
161  (cd /out && find . \( -name '*.deb' -o -name '*.rpm' -o -name '*.tar.gz' \) -exec sh -c 'md5sum {} > {}.md5 && sha256sum {} > {}.sha256' \;)
162elif [ "$OS" == "windows" ]; then
163  # Windows gets the binaries and the sample config file.
164  rm -rf "$PKG_ROOT"
165  mkdir -p "$PKG_ROOT"
166  cp /ibin/*.exe "$PKG_ROOT"
167  cp /isrc/etc/config.sample.toml "$PKG_ROOT/influxdb.conf"
168
169  (cd /pkg && zip -9 -r "/out/influxdb-${VERSION}_${OS}_${ARCH}.zip" ./*)
170  (cd /out && for f in *.zip; do
171    md5sum "$f" > "$f.md5"
172    sha256sum "$f" > "$f.sha256"
173  done)
174else
175  >&2 echo "Unrecognized OS: $OS"
176  exit 1
177fi
178