|
1 | 1 | #!/usr/bin/env bash |
2 | 2 |
|
3 | | -set -euo pipefail |
4 | | -IFS=$'\n\t' |
| 3 | +set -eo pipefail |
5 | 4 |
|
6 | 5 | if [ -n "${DEBUG:-}" ]; then |
7 | 6 | set -x |
8 | 7 | fi |
9 | 8 |
|
| 9 | +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) |
| 10 | +export $(grep -v '^#' $SCRIPT_DIR/.env | xargs) |
| 11 | + |
10 | 12 | function main() { |
11 | | - rm -rf /tmp/build/* && uname -a >> /tmp/build/out |
| 13 | + with_backoff curl --fail -sSLo pcre.tar.gz "https://downloads.sourceforge.net/project/pcre/pcre/${PCRE_VERSION}/pcre-${PCRE_VERSION}.tar.gz" |
| 14 | + tar -xzvf pcre.tar.gz |
| 15 | + ln -s pcre-${PCRE_VERSION} pcre |
| 16 | + |
| 17 | + with_backoff curl --fail -sSLo openresty.tar.gz "https://openresty.org/download/openresty-${OPENRESTY_VERSION}.tar.gz" |
| 18 | + tar -xzvf openresty.tar.gz |
| 19 | + ln -s openresty-${OPENRESTY_VERSION} openresty |
| 20 | + |
| 21 | + with_backoff curl --fail -sSLo luarocks.tar.gz "https://luarocks.org/releases/luarocks-${LUAROCKS_VERSION}.tar.gz" |
| 22 | + tar -xzvf luarocks.tar.gz |
| 23 | + ln -s luarocks-${LUAROCKS_VERSION} luarocks |
| 24 | + |
| 25 | + with_backoff git clone --single-branch --branch ${KONG_NGINX_MODULE_VERSION} https://github.com/Kong/lua-kong-nginx-module |
| 26 | + |
| 27 | + with_backoff git clone --single-branch --branch ${LUA_RESTY_LMDB_VERSION} https://github.com/Kong/lua-resty-lmdb --recursive |
| 28 | + |
| 29 | + with_backoff git clone --single-branch --branch ${LUA_RESTY_EVENTS_VERSION} https://github.com/Kong/lua-resty-events --recursive |
| 30 | + |
| 31 | + pushd openresty-${OPENRESTY_VERSION}/bundle |
| 32 | + for patch_file in $(ls /tmp/patches/*.patch); do |
| 33 | + patch -p1 < $patch_file |
| 34 | + done |
| 35 | + |
| 36 | + lj_dir=$(ls -d LuaJIT*) |
| 37 | + lj_release_date=$(echo ${lj_dir} | sed -e 's/LuaJIT-[[:digit:]]\+.[[:digit:]]\+-\([[:digit:]]\+\)/\1/') |
| 38 | + lj_version_tag="LuaJIT\ 2.1.0-${lj_release_date}" |
| 39 | + popd |
| 40 | + |
| 41 | + pushd openresty-${OPENRESTY_VERSION} |
| 42 | + OPENRESTY_OPTS=( |
| 43 | + "--prefix=/usr/local/openresty" |
| 44 | + "--with-pcre-jit" |
| 45 | + "--with-http_ssl_module" |
| 46 | + "--with-http_sub_module" |
| 47 | + "--with-http_realip_module" |
| 48 | + "--with-http_stub_status_module" |
| 49 | + "--with-http_v2_module" |
| 50 | + "--without-http_encrypted_session_module" |
| 51 | + "--with-luajit-xcflags='-DLUAJIT_VERSION=\\\"${lj_version_tag}\\\"'" |
| 52 | + "-j2" |
| 53 | + ) |
| 54 | + |
| 55 | + OPENRESTY_OPTS+=("--add-module=/tmp/lua-kong-nginx-module") |
| 56 | + OPENRESTY_OPTS+=("--add-module=/tmp/lua-kong-nginx-module/stream") |
| 57 | + OPENRESTY_OPTS+=("--add-module=/tmp/lua-resty-lmdb") |
| 58 | + OPENRESTY_OPTS+=("--add-module=/tmp/lua-resty-events") |
| 59 | + OPENRESTY_OPTS+=('--with-stream_realip_module') |
| 60 | + OPENRESTY_OPTS+=('--with-stream_ssl_preread_module') |
| 61 | + OPENRESTY_OPTS+=('--with-pcre=/tmp/pcre') |
| 62 | + OPENRESTY_OPTS+=("--with-ld-opt='-L/tmp/build/usr/local/kong/lib -Wl,--disable-new-dtags,-rpath,/usr/local/kong/lib'") |
| 63 | + OPENRESTY_OPTS+=("--with-cc-opt='-I/tmp/build/usr/local/kong/include'") |
| 64 | + |
| 65 | + eval ./configure ${OPENRESTY_OPTS[*]} || tee /tmp/00-openresty-configure.log |
| 66 | + |
| 67 | + make -j2 || tee /tmp/01-openresty-build.log |
| 68 | + make -j2 install DESTDIR=/tmp/build || tee /tmp/02-openresty-install.log |
| 69 | + popd |
| 70 | + |
| 71 | + pushd /tmp/lua-kong-nginx-module |
| 72 | + make install LUA_LIB_DIR=/tmp/build/usr/local/openresty/lualib |
| 73 | + popd |
| 74 | + |
| 75 | + pushd /tmp/lua-resty-lmdb |
| 76 | + make install LUA_LIB_DIR=/tmp/build/usr/local/openresty/lualib |
| 77 | + popd |
| 78 | + |
| 79 | + pushd /tmp/lua-resty-events |
| 80 | + make install LUA_LIB_DIR=/tmp/build/usr/local/openresty/lualib |
| 81 | + popd |
| 82 | + |
| 83 | + pushd /tmp/luarocks-${LUAROCKS_VERSION} |
| 84 | + ./configure \ |
| 85 | + --prefix=/usr/local \ |
| 86 | + --with-lua=/tmp/build/usr/local/openresty/luajit \ |
| 87 | + --with-lua-include=/tmp/build/usr/local/openresty/luajit/include/luajit-2.1 |
| 88 | + |
| 89 | + make build -j2 |
| 90 | + make install DESTDIR=/tmp/build |
| 91 | + popd |
| 92 | + |
| 93 | + arch=$(uname -m) |
| 94 | + |
| 95 | + package_architecture=x86_64 |
| 96 | + if [ "$(arch)" == "aarch64" ]; then |
| 97 | + package_architecture=aarch64 |
| 98 | + fi |
| 99 | + |
| 100 | + curl -fsSLo atc-router.tar.gz https://github.com/hutchic/atc-router/releases/download/$ATC_ROUTER_VERSION/$package_architecture-unknown-$OSTYPE.tar.gz |
| 101 | + tar -C /tmp/build -xvf atc-router.tar.gz |
| 102 | +} |
| 103 | + |
| 104 | +# Retries a command a configurable number of times with backoff. |
| 105 | +# |
| 106 | +# The retry count is given by ATTEMPTS (default 5), the initial backoff |
| 107 | +# timeout is given by TIMEOUT in seconds (default 1.) |
| 108 | +# |
| 109 | +# Successive backoffs double the timeout. |
| 110 | +function with_backoff { |
| 111 | + local max_attempts=${ATTEMPTS-5} |
| 112 | + local timeout=${TIMEOUT-5} |
| 113 | + local attempt=1 |
| 114 | + local exitCode=0 |
| 115 | + |
| 116 | + while (( $attempt < $max_attempts )) |
| 117 | + do |
| 118 | + if "$@" |
| 119 | + then |
| 120 | + return 0 |
| 121 | + else |
| 122 | + exitCode=$? |
| 123 | + fi |
| 124 | + |
| 125 | + echo "Failure! Retrying in $timeout.." 1>&2 |
| 126 | + sleep $timeout |
| 127 | + attempt=$(( attempt + 1 )) |
| 128 | + timeout=$(( timeout * 2 )) |
| 129 | + done |
| 130 | + |
| 131 | + if [[ $exitCode != 0 ]] |
| 132 | + then |
| 133 | + echo "You've failed me for the last time! ($@)" 1>&2 |
| 134 | + fi |
| 135 | + |
| 136 | + return $exitCode |
12 | 137 | } |
13 | 138 |
|
14 | 139 | main |
0 commit comments