-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
compile
executable file
·179 lines (153 loc) · 6.06 KB
/
compile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env bash
# bin/compile <build-dir> <cache-dir>
set -eo pipefail
[[ $TRACE ]] && set -x
NGINX_VERSION="1.23.1"
NGINX_TARBALL="nginx-${NGINX_VERSION}.tar.gz"
PCRE_VERSION="10.40"
PCRE_TARBALL="pcre2-${PCRE_VERSION}.tar.gz"
SIGIL_VERSION="0.9.0"
SIGIL_TARBALL="gliderlabs-sigil_${SIGIL_VERSION}_linux_amd64.tgz"
ZLIB_VERSION="1.2.12"
ZLIB_TARBALL="zlib-${ZLIB_VERSION}.tar.gz"
suppress() {
/bin/rm --force /tmp/surpress.out 2>/dev/null
# shellcheck disable=SC2069
"$@" 2>&1 >/tmp/surpress.out || cat /tmp/surpress.out
/bin/rm /tmp/surpress.out
}
# parse and derive params
BUILD_DIR=$1
CACHE_DIR=$2
CUR_DIR=$(cd "$(dirname "$0")" && cd .. && pwd)
mkdir -p "$BUILD_DIR" "$CACHE_DIR"
if [[ ! -e "${BUILD_DIR}/www" ]]; then
echo "-----> Copy static files to www"
rm -rf "${CACHE_DIR}/www"
mkdir -p "${CACHE_DIR}/www"
# shellcheck disable=SC2086
mv $BUILD_DIR/* "${CACHE_DIR}/www"
mkdir -p "${BUILD_DIR}/www"
# shellcheck disable=SC2086
mv ${CACHE_DIR}/www/* "${BUILD_DIR}/www"
# Check for a copy the nginx conf file override to the build dir
[[ -f "${BUILD_DIR}/www/nginx.conf.erb" ]] && mv "${BUILD_DIR}/www/nginx.conf.erb" "${BUILD_DIR}"
[[ -f "${BUILD_DIR}/www/nginx.conf.sigil" ]] && mv "${BUILD_DIR}/www/nginx.conf.sigil" "${BUILD_DIR}"
[[ -f "${BUILD_DIR}/www/app-nginx.conf.sigil" ]] && mv "${BUILD_DIR}/www/app-nginx.conf.sigil" "${BUILD_DIR}"
[[ -f "${BUILD_DIR}/www/mime.types" ]] && mv "${BUILD_DIR}/www/mime.types" "${BUILD_DIR}"
[[ -f "${BUILD_DIR}/www/CHECKS" ]] && mv "${BUILD_DIR}/www/CHECKS" "${BUILD_DIR}"
[[ -f "${BUILD_DIR}/www/app.json" ]] && mv "${BUILD_DIR}/www/app.json" "${BUILD_DIR}"
rm -rf "${CACHE_DIR}/www"
fi
cd "$CACHE_DIR"
if [[ ! -d "${NGINX_TARBALL%.tar.gz}" ]]; then
echo "-----> Download and unzip nginx ${NGINX_VERSION} via http"
curl -sSL "http://nginx.org/download/${NGINX_TARBALL}" -o "${NGINX_TARBALL}"
tar xzf "${NGINX_TARBALL}" && rm -f "${NGINX_TARBALL}"
fi
if [[ ! -d "${PCRE_TARBALL%.tar.gz}" ]]; then
echo "-----> Download and unzip pcre ${PCRE_VERSION} via http"
curl -sSL "https://github.com/PCRE2Project/pcre2/releases/download/pcre2-${PCRE_VERSION}/${PCRE_TARBALL}" -o "${PCRE_TARBALL}"
tar xzf "${PCRE_TARBALL}" && rm -f "${PCRE_TARBALL}"
fi
if [[ ! -d "${ZLIB_TARBALL%.tar.gz}" ]]; then
echo "-----> Download and unzip zlib ${ZLIB_VERSION} via http"
curl -sSL "https://github.com/madler/zlib/archive/v${ZLIB_VERSION}.tar.gz" -o "${ZLIB_TARBALL}"
tar xzf "${ZLIB_TARBALL}" && rm -rf "${ZLIB_TARBALL}"
fi
if [[ ! -f "sigil" ]]; then
echo "-----> Download and unzip sigil ${SIGIL_VERSION} via http"
curl -sSL "https://github.com/gliderlabs/sigil/releases/download/v${SIGIL_VERSION}/gliderlabs-sigil_${SIGIL_VERSION}_linux_amd64.tgz" -o "${SIGIL_TARBALL}"
tar xzf "${SIGIL_TARBALL}" && rm -rf "${SIGIL_TARBALL}"
fi
mkdir -p "$BUILD_DIR/sigil"
cp -r gliderlabs-sigil-amd64 "$BUILD_DIR/sigil/sigil"
cd "nginx-${NGINX_VERSION}"
if [[ ! -f "${CACHE_DIR}/bin/nginx" ]]; then
echo "-----> Compiling static nginx binary"
mkdir "$BUILD_DIR/nginx"
suppress ./configure \
--with-cpu-opt=generic \
--prefix="$BUILD_DIR/nginx" \
--with-pcre=../pcre2-${PCRE_VERSION} \
--sbin-path=. \
--pid-path=./nginx.pid \
--conf-path=./nginx.conf \
--with-ld-opt="-static" \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-file-aio \
--with-zlib=../zlib-${ZLIB_VERSION} \
--with-pcre \
--with-cc-opt="-O2 -static -static-libgcc" \
--without-http_ssi_module \
--without-http_userid_module \
--without-http_access_module \
--without-http_autoindex_module \
--without-http_geo_module \
--without-http_map_module \
--without-http_split_clients_module \
--without-http_referer_module \
--without-http_fastcgi_module \
--without-http_uwsgi_module \
--without-http_scgi_module \
--without-http_memcached_module \
--without-http_empty_gif_module \
--without-http_browser_module \
--without-http_upstream_ip_hash_module \
--without-http_upstream_least_conn_module \
--without-http_upstream_keepalive_module \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module \
--with-http_realip_module
sed -i "/CFLAGS/s/ \-O //g" objs/Makefile
suppress make && suppress make install
rm -rf "${CACHE_DIR:?}/bin" && mkdir -p "$CACHE_DIR/bin/"
# shellcheck disable=SC2086
cp -r $BUILD_DIR/nginx/* "$CACHE_DIR/bin/"
else
echo "-----> Reusing nginx binary from cache"
mkdir -p "$BUILD_DIR/nginx"
# shellcheck disable=SC2086
cp -r $CACHE_DIR/bin/* "$BUILD_DIR/nginx/"
fi
# Update the PATH
mkdir -p "$BUILD_DIR/.profile.d"
cat >"$BUILD_DIR/.profile.d/nginx.sh" <<"EOF"
export PATH="$PATH:$HOME/nginx"
EOF
cd "$CUR_DIR"
# Add support for app-nginx.conf.sigil
if [ -f "$BUILD_DIR/app-nginx.conf.sigil" ]; then
echo "-----> Using user provided app-nginx.conf.sigil"
cp "$BUILD_DIR/app-nginx.conf.sigil" "$BUILD_DIR/nginx/app-nginx.conf.sigil"
# Allow deprecated nginx.conf.erb
elif [ -f "$BUILD_DIR/nginx.conf.erb" ]; then
echo "-----> DEPRECATED: Using user provided nginx.conf.erb"
cp "$BUILD_DIR/nginx.conf.erb" "$BUILD_DIR/nginx/nginx.conf.erb"
# ...else, force default file
else
echo "-----> Using default app-nginx.conf.sigil"
cp conf/app-nginx.conf.sigil "$BUILD_DIR/nginx/app-nginx.conf.sigil"
fi
# build mime.types unless overridden by user
if [ -f "$BUILD_DIR/mime.types" ]; then
echo "-----> Using user provided mime.types"
cp "$BUILD_DIR/mime.types" "$BUILD_DIR/nginx/mime.types"
else
echo "-----> Using default mime.types"
cp conf/mime.types "$BUILD_DIR/nginx/mime.types"
fi
# build a startup script
cat <<EOF >"$BUILD_DIR/start_nginx"
#!/usr/bin/env bash
rm -f /app/nginx/nginx.conf
if [[ -f /app/nginx/app-nginx.conf.sigil ]]; then
/app/sigil/sigil -f /app/nginx/app-nginx.conf.sigil NGINX_ROOT="\$NGINX_ROOT" NGINX_DEFAULT_REQUEST="\$NGINX_DEFAULT_REQUEST" PORT="\$PORT" | cat -s > /app/nginx/nginx.conf
else
erb /app/nginx/nginx.conf.erb > /app/nginx/nginx.conf
fi
exec /app/nginx/nginx -p /app/nginx -c /app/nginx/nginx.conf
EOF
chmod +x "$BUILD_DIR/start_nginx"