forked from fullstaq-ruby/server-edition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-ruby
executable file
·164 lines (151 loc) · 4.43 KB
/
build-ruby
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
#!/bin/bash
set -e
SELFDIR=$(dirname "$0")
SELFDIR=$(cd "$SELFDIR" && pwd)
# shellcheck source=lib/library.sh
source "$SELFDIR/lib/library.sh"
ENVIRONMENT_NAME=
SOURCE_PATH=
PACKAGE_VERSION=
OUTPUT_PATH=
VARIANT=normal
JEMALLOC_BIN_PATH=
BUILD_CONCURRENCY=1
CACHE_PATH=
function usage()
{
echo "Usage: ./build-ruby <OPTIONS>"
echo "Build a Ruby binary tarball from its source tarball."
echo
echo "Required options:"
echo " -n NAME Name of environment to build in (one of: $(list_environment_names "$SELFDIR/environments"))"
echo " -s PATH Path to Ruby source tarball"
echo " -v VERSION Package version number"
echo " -o PATH Path to output tarball"
echo
echo "Build variant (required):"
echo " -m PATH Build with Jemalloc support, using this Jemalloc binary tarball (as built by ./build-jemalloc)"
echo " -t Build with malloc_trim support"
echo
echo "Optional options:"
echo " -j NUM Build concurrency (default: $BUILD_CONCURRENCY)"
echo " -c PATH Use given directory as cache"
echo " -h Show usage"
}
function parse_options()
{
local OPTIND=1
local ORIG_ARGV
local opt
while getopts "n:s:v:o:m:tj:c:h" opt; do
case "$opt" in
n)
ENVIRONMENT_NAME="$OPTARG"
;;
s)
SOURCE_PATH=$(absolute_path "$OPTARG")
;;
v)
PACKAGE_VERSION="$OPTARG"
;;
o)
OUTPUT_PATH=$(absolute_path "$OPTARG")
;;
m)
VARIANT=jemalloc
JEMALLOC_BIN_PATH=$(absolute_path "$OPTARG")
;;
t)
VARIANT=malloctrim
;;
j)
BUILD_CONCURRENCY="$OPTARG"
;;
c)
CACHE_PATH=$(absolute_path "$OPTARG")
;;
h)
usage
exit
;;
*)
return 1
;;
esac
done
(( OPTIND -= 1 )) || true
shift $OPTIND || true
ORIG_ARGV=("$@")
if [[ "$ENVIRONMENT_NAME" = "" ]]; then
echo 'ERROR: please specify an environment name with -n.' >&2
exit 1
fi
if [[ "$SOURCE_PATH" = "" ]]; then
echo 'ERROR: please specify a Ruby source tarball path with -s.' >&2
exit 1
fi
if [[ ! -e "$SOURCE_PATH" ]]; then
echo "ERROR: $SOURCE_PATH does not exist." >&2
exit 1
fi
if [[ "$PACKAGE_VERSION" = "" ]]; then
echo 'ERROR: please specify a package version number with -v.' >&2
exit 1
fi
if [[ "$VARIANT" = jemalloc ]]; then
if [[ "$JEMALLOC_BIN_PATH" = "" ]]; then
echo 'ERROR: please specify a Jemalloc binary tarball path with -m.' >&2
exit 1
fi
if [[ "$JEMALLOC_BIN_PATH" != "" && ! -e "$JEMALLOC_BIN_PATH" ]]; then
echo "ERROR: $JEMALLOC_BIN_PATH does not exist." >&2
exit 1
fi
fi
if [[ "$OUTPUT_PATH" = "" ]]; then
echo 'ERROR: please specify an output tarball path with -o.' >&2
exit 1
fi
if [[ "$CACHE_PATH" != "" && ! -e "$CACHE_PATH" ]]; then
echo "ERROR: $CACHE_PATH does not exist." >&2
exit 1
fi
}
parse_options "$@"
if tty -s; then
TTY_ARGS=(-t -i)
else
TTY_ARGS=()
fi
MOUNT_ARGS=()
if [[ "$VARIANT" = jemalloc ]]; then
MOUNT_ARGS+=(-v "$JEMALLOC_BIN_PATH:/input/jemalloc-bin.tar.gz:ro")
fi
if [[ "$CACHE_PATH" != "" ]]; then
MOUNT_ARGS+=(-v "$CACHE_PATH:/cache:delegated")
fi
IMAGE_VERSION=$(read_single_value_file "$SELFDIR/environments/$ENVIRONMENT_NAME/image_tag")
OUTPUT_TEMP_PATH=$(mktemp "$OUTPUT_PATH.XXXXXX")
create_file_if_missing "$OUTPUT_TEMP_PATH"
echo '--- Entering Docker container ---'
function _cleanup() {
echo
echo '--- Exited Docker container ---'
run rm -f "$OUTPUT_TEMP_PATH"
}
verbose_run docker run --rm --init "${TTY_ARGS[@]}" \
-v "$SELFDIR:/system:ro" \
-v "$SOURCE_PATH:/input/ruby-src.tar.gz:ro" \
-v "$OUTPUT_TEMP_PATH:/output/ruby-bin.tar.gz" \
"${MOUNT_ARGS[@]}" \
-e "VARIANT=$VARIANT" \
-e "BUILD_CONCURRENCY=$BUILD_CONCURRENCY" \
-e "PACKAGE_VERSION=$PACKAGE_VERSION" \
-e "ENVIRONMENT_NAME=$ENVIRONMENT_NAME" \
--user "$(id -u):$(id -g)" \
"fullstaq/ruby-build-env-$ENVIRONMENT_NAME:$IMAGE_VERSION" \
/system/container-entrypoints/build-ruby
function _cleanup() {
echo '--- Exited Docker container ---'
run mv "$OUTPUT_TEMP_PATH" "$OUTPUT_PATH"
}