|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Simple packaging of freno |
| 4 | +# |
| 5 | +# Requires fpm: https://github.com/jordansissel/fpm |
| 6 | +# |
| 7 | + |
| 8 | +# This script is based off https://github.com/github/orchestrator/blob/master/build.sh |
| 9 | +# hence licensed under the Apache 2.0 license. |
| 10 | + |
| 11 | +# Copyright 2017 Shlomi Noach, GitHub Inc. |
| 12 | +# |
| 13 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 14 | +# you may not use this file except in compliance with the License. |
| 15 | +# You may obtain a copy of the License at |
| 16 | +# |
| 17 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | +# |
| 19 | +# Unless required by applicable law or agreed to in writing, software |
| 20 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 21 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | +# See the License for the specific language governing permissions and |
| 23 | +# limitations under the License. |
| 24 | + |
| 25 | +set -e |
| 26 | + |
| 27 | +mydir=$(dirname $0) |
| 28 | +GIT_COMMIT=$(git rev-parse HEAD) |
| 29 | +RELEASE_VERSION= |
| 30 | +TOPDIR=/tmp/freno-release |
| 31 | +export RELEASE_VERSION TOPDIR |
| 32 | + |
| 33 | +usage() { |
| 34 | + echo |
| 35 | + echo "Usage: $0 [-t target ] [-a arch ] [ -p prefix ] [-h] [-d] [-r]" |
| 36 | + echo "Options:" |
| 37 | + echo "-h Show this screen" |
| 38 | + echo "-t (linux|darwin) Target OS Default:(linux)" |
| 39 | + echo "-a (amd64|386) Arch Default:(amd64)" |
| 40 | + echo "-d debug output" |
| 41 | + echo "-b build only, do not generate packages" |
| 42 | + echo "-p build prefix Default:(/usr/bin)" |
| 43 | + echo "-r build with race detector" |
| 44 | + echo "-v release version (optional; default: content of RELEASE_VERSION file)" |
| 45 | + echo "-s release subversion (optional; default: empty)" |
| 46 | + echo |
| 47 | +} |
| 48 | + |
| 49 | +function precheck() { |
| 50 | + local target="$1" |
| 51 | + local build_only="$2" |
| 52 | + local ok=0 # return err. so shell exit code |
| 53 | + |
| 54 | + if [[ "$target" == "linux" ]]; then |
| 55 | + if [[ $build_only -eq 0 ]] && [[ ! -x "$( which fpm )" ]]; then |
| 56 | + echo "Please install fpm and ensure it is in PATH (typically: 'gem install fpm')" |
| 57 | + ok=1 |
| 58 | + fi |
| 59 | + |
| 60 | + if [[ $build_only -eq 0 ]] && [[ ! -x "$( which rpmbuild )" ]]; then |
| 61 | + echo "rpmbuild not in PATH, rpm will not be built (OS/X: 'brew install rpm')" |
| 62 | + fi |
| 63 | + fi |
| 64 | + |
| 65 | + if [[ -z "$GOPATH" ]]; then |
| 66 | + echo "GOPATH not set" |
| 67 | + ok=1 |
| 68 | + fi |
| 69 | + |
| 70 | + if [[ ! -x "$( which go )" ]]; then |
| 71 | + echo "go binary not found in PATH" |
| 72 | + ok=1 |
| 73 | + fi |
| 74 | + |
| 75 | + if [[ $(go version | egrep "go1[.][0123456]") ]]; then |
| 76 | + echo "go version is too low. Must use 1.7 or above" |
| 77 | + ok=1 |
| 78 | + fi |
| 79 | + |
| 80 | + return $ok |
| 81 | +} |
| 82 | + |
| 83 | +function setuptree() { |
| 84 | + local b prefix |
| 85 | + prefix="$1" |
| 86 | + |
| 87 | + mkdir -p $TOPDIR |
| 88 | + rm -rf ${TOPDIR:?}/* |
| 89 | + b=$( mktemp -d $TOPDIR/frenoXXXXXX ) || return 1 |
| 90 | + mkdir -p $b/freno |
| 91 | + mkdir -p $b/freno${prefix} |
| 92 | + mkdir -p $b/freno/etc/init.d |
| 93 | + mkdir -p $b/freno/var/lib |
| 94 | + echo $b |
| 95 | +} |
| 96 | + |
| 97 | +function oinstall() { |
| 98 | + local builddir prefix |
| 99 | + builddir="$1" |
| 100 | + prefix="$2" |
| 101 | + |
| 102 | + cd $mydir |
| 103 | + gofmt -s -w go/ |
| 104 | + cp ./resources/etc/init.d/freno $builddir/freno/etc/init.d/freno |
| 105 | + chmod +x $builddir/freno/etc/init.d/freno |
| 106 | + cp ./resources/freno.conf.skeleton.json $builddir/freno/etc/freno.conf.json |
| 107 | +} |
| 108 | + |
| 109 | +function package() { |
| 110 | + local target builddir prefix packages |
| 111 | + target="$1" |
| 112 | + builddir="$2" |
| 113 | + prefix="$3" |
| 114 | + |
| 115 | + cd $TOPDIR |
| 116 | + |
| 117 | + echo "Release version is ${RELEASE_VERSION} ( ${GIT_COMMIT} )" |
| 118 | + |
| 119 | + case $target in |
| 120 | + 'linux') |
| 121 | + echo "Creating Linux Tar package" |
| 122 | + tar -C $builddir/freno -czf $TOPDIR/freno-"${RELEASE_VERSION}"-$target-$arch.tar.gz ./ |
| 123 | + |
| 124 | + echo "Creating Distro full packages" |
| 125 | + fpm -v "${RELEASE_VERSION}" --epoch 1 -f -s dir -n freno -m shlomi-noach --description "Cooperative, highly available throttler service" --url "https://github.com/github/freno" --vendor "GitHub" --license "Apache 2.0" -C $builddir/freno --prefix=/ -t rpm . |
| 126 | + fpm -v "${RELEASE_VERSION}" --epoch 1 -f -s dir -n freno -m shlomi-noach --description "Cooperative, highly available throttler service" --url "https://github.com/github/freno" --vendor "GitHub" --license "Apache 2.0" -C $builddir/freno --prefix=/ -t deb --deb-no-default-config-files . |
| 127 | + ;; |
| 128 | + 'darwin') |
| 129 | + echo "Creating Darwin full Package" |
| 130 | + tar -C $builddir/freno -czf $TOPDIR/freno-"${RELEASE_VERSION}"-$target-$arch.tar.gz ./ |
| 131 | + ;; |
| 132 | + esac |
| 133 | + |
| 134 | + echo "---" |
| 135 | + echo "Done. Find releases in $TOPDIR" |
| 136 | +} |
| 137 | + |
| 138 | +function build() { |
| 139 | + local target arch builddir gobuild prefix |
| 140 | + os="$1" |
| 141 | + arch="$2" |
| 142 | + builddir="$3" |
| 143 | + prefix="$4" |
| 144 | + ldflags="-X main.AppVersion=${RELEASE_VERSION} -X main.GitCommit=${GIT_COMMIT}" |
| 145 | + echo "Building via $(go version)" |
| 146 | + gobuild="go build -i ${opt_race} -ldflags \"$ldflags\" -o $builddir/freno${prefix}/freno go/cmd/freno/main.go" |
| 147 | + |
| 148 | + case $os in |
| 149 | + 'linux') |
| 150 | + echo "GOOS=$os GOARCH=$arch $gobuild" | bash |
| 151 | + ;; |
| 152 | + 'darwin') |
| 153 | + echo "GOOS=darwin GOARCH=amd64 $gobuild" | bash |
| 154 | + ;; |
| 155 | + esac |
| 156 | + [[ $(find $builddir/freno${prefix}/ -type f -name freno) ]] && echo "freno binary created" || (echo "Failed to generate freno binary" ; exit 1) |
| 157 | +} |
| 158 | + |
| 159 | +function main() { |
| 160 | + local target="$1" |
| 161 | + local arch="$2" |
| 162 | + local prefix="$3" |
| 163 | + local build_only=$4 |
| 164 | + local builddir |
| 165 | + |
| 166 | + if [ -z "${RELEASE_VERSION}" ] ; then |
| 167 | + RELEASE_VERSION=$(git describe --abbrev=0 --tags | tr -d 'v') |
| 168 | + fi |
| 169 | + |
| 170 | + precheck "$target" "$build_only" |
| 171 | + builddir=$( setuptree "$prefix" ) |
| 172 | + oinstall "$builddir" "$prefix" |
| 173 | + build "$target" "$arch" "$builddir" "$prefix" |
| 174 | + [[ $? == 0 ]] || return 1 |
| 175 | + if [[ $build_only -eq 0 ]]; then |
| 176 | + package "$target" "$builddir" "$prefix" |
| 177 | + fi |
| 178 | +} |
| 179 | + |
| 180 | +build_only=0 |
| 181 | +opt_race= |
| 182 | +while getopts a:t:p:s:v:dbhr flag; do |
| 183 | + case $flag in |
| 184 | + a) |
| 185 | + arch="${OPTARG}" |
| 186 | + ;; |
| 187 | + t) |
| 188 | + target="${OPTARG}" |
| 189 | + ;; |
| 190 | + h) |
| 191 | + usage |
| 192 | + exit 0 |
| 193 | + ;; |
| 194 | + d) |
| 195 | + debug=1 |
| 196 | + ;; |
| 197 | + b) |
| 198 | + echo "Build only; no packaging" |
| 199 | + build_only=1 |
| 200 | + ;; |
| 201 | + p) |
| 202 | + prefix="${OPTARG}" |
| 203 | + ;; |
| 204 | + r) |
| 205 | + opt_race="-race" |
| 206 | + ;; |
| 207 | + v) |
| 208 | + RELEASE_VERSION="${OPTARG}" |
| 209 | + ;; |
| 210 | + s) |
| 211 | + ;; |
| 212 | + ?) |
| 213 | + usage |
| 214 | + exit 2 |
| 215 | + ;; |
| 216 | + esac |
| 217 | +done |
| 218 | + |
| 219 | +shift $(( OPTIND - 1 )); |
| 220 | + |
| 221 | +if [ -z "$target" ]; then |
| 222 | + uname=$(uname) |
| 223 | + case $uname in |
| 224 | + Linux) target=linux;; |
| 225 | + Darwin) target=darwin;; |
| 226 | + *) echo "Unexpected OS from uname: $uname. Exiting" |
| 227 | + exit 1 |
| 228 | + esac |
| 229 | +fi |
| 230 | +arch=${arch:-"amd64"} # default for arch is amd64 but should take from environment |
| 231 | +prefix=${prefix:-"/usr/bin"} |
| 232 | + |
| 233 | +[[ $debug -eq 1 ]] && set -x |
| 234 | +main "$target" "$arch" "$prefix" "$build_only" |
| 235 | + |
| 236 | +echo "freno build done; exit status is $?" |
0 commit comments