This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
builder.sh
416 lines (380 loc) · 14.3 KB
/
builder.sh
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#!/bin/bash
# Docker based OpenWRT package build environment
RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
#############################################
PLATFORM=$(uname -s)
SCRIPT_DIR=$(pwd)
DOCKER_VERSION=""
#MY_LANG="${LANG:-en_US.UTF-8}"
MY_TERM="${TERM:-xterm-256color}"
MY_HOSTNAME="docker_pb"
MY_TZ="UTC"
SUDO=sudo
ECHOX="echo -e"
SYSTEMD=0
# base Tag to use for docker image
IMAGE_TAG="andy2222/docker-openwrt-sdk-base"
IMAGE_TAG_HOST="pierrezemb/gostatic"
CPREFIX="openwrt_pb"
#############################################
if [ "$PLATFORM" == "Darwin" ]; then
ECHOX="echo"
#sudo has 5 min timeout by default...
#sudo sh -c 'echo "\nDefaults timestamp_timeout=20">>/etc/sudoers'
fi
if ! (sudo -v &>/dev/null); then
SUDO=
fi
if (timedatectl --version &>/dev/null); then
MY_TZ=$(timedatectl 2>/dev/null | grep "Time zone" | awk '{print $3}')
elif [ -f /etc/timezone ]; then
MY_TZ=$(cat /etc/timezone)
fi
if (systemctl --version &>/dev/null); then
SYSTEMD=1
fi
if (docker -v &>/dev/null); then
DOCKER_VERSION=$(docker version -f '{{.Client.Version}}' 2>/dev/null)
fi
############### stop on error ###############
set -e
#############################################
function fail {
$ECHOX "****** ${RED}ERROR: $*${NC}" >&2
exit 1
}
function usage_and_exit {
cat<<EOT
Dockerized automatic Openwrt Package-Builder.
Usage: $1 COMMAND [FILE]
build - build packages defined via [FILE]
build_quick - quickly build packages defined via [FILE], no (sdk, feeds, patches) updates are performed and build cache is reused.
shell - start shell in docker container (allows manual build/debug)
host - host the build packages locally via a tiny web server
clean - clean/remove the associated container
FILE:
[FILE].txt - the config file to-be used (must contain valid VERSION, TARGET, SUBTARGET)
Usage: $1 COMMAND
cleanall - remove ALL package builder docker containers
update - update docker images (aka pull)
bundle_patches - create per feed patches-[feed name]-[sdk version].tar.gz via '.patches' dir
- structure: .patches/[feed name]/[sdk version]/[patchfile].patch
- example: .patches/packages/18.06.0/mypatch.patch
Examples:
$1 build example.txt
$1 host example.txt
$1 cleanall
EOT
exit 0
}
######## early exit #####
if [ $# -lt 1 ]; then
usage_and_exit $0
fi
#########################
# all args = "$@", exit code = $?
# $1 = full container namme
# use /.cmd file to allow dynamic cmds on a already started container
function prepare_container {
check_images
local cid=$($SUDO docker ps -a -q -f name="$1")
if [ -z "$cid" ]; then
#-e GOSU_USER=`id -u`:`id -g`
#-e LANG="$MY_LANG"
$SUDO docker create -ti \
--log-driver=none \
--name "$1" \
-h "${MY_HOSTNAME}_build" \
-e TERM="$MY_TERM" \
-e TZ="$my_tz" \
--mount type=bind,source="$SCRIPT_DIR",target=/hostdir \
$IMAGE_TAG /bin/bash -C /.cmd
cid=$($SUDO docker ps -a -q -f name="$1")
fi
if [ -n "$cid" ]; then
$SUDO docker stop "$1" 2>/dev/null
$SUDO docker cp "$SCRIPT_DIR/build.in" "${1}:/workdir/build.sh"
$SUDO docker cp "$FILE" "${1}:/workdir/"
$SUDO docker cp "$SCRIPT_DIR/keys.tar.gz" "${1}:/workdir/"
local patches="$(ls "$SCRIPT_DIR"/patches-*-${VERSION}.tar.gz 2>/dev/null)"
local file
for file in ${patches[@]}; do
if [ -f "$file" ]; then
$SUDO docker cp "$file" "${1}:/workdir/"
fi
done
else
fail "Something went wrong, for $1"
fi
}
# run a shell in the container, useful for debugging.
function run_shell {
prepare_container "$1"
local filename=$(basename $FILE)
echo "echo -e \"************* Host script dir ${YELLOW} '$SCRIPT_DIR' ${NC} has been mounted to ${GREEN} '/hostdir' ${NC} inside the container, for your convenience! *************\"" > cmd.in
echo "echo -e \"************* available tools: mc, nano, edit \"" >> cmd.in
echo "chmod 777 /workdir/ /workdir/build.sh && chmod 666 /workdir/$filename && dos2unix -q /workdir/build.sh /workdir/$filename && exec bash" >> cmd.in
$SUDO docker cp cmd.in "${1}:/.cmd"
$SUDO rm -f cmd.in
$SUDO docker start -ai "$1"
$SUDO docker stop "$1"
}
function run_build_packages {
prepare_container "$1"
local filename=$(basename $FILE)
echo "chmod 777 /workdir/ /workdir/build.sh && chmod 666 /workdir/$filename && dos2unix -q /workdir/build.sh /workdir/$filename && exec /workdir/build.sh $filename" > cmd.in
$SUDO docker cp cmd.in "${1}:/.cmd"
$SUDO rm -f cmd.in
$SUDO docker start -ai "$1"
$SUDO docker stop "$1"
}
function run_build_packages_quick {
prepare_container "$1"
local filename=$(basename $FILE)
echo "chmod 777 /workdir/ /workdir/build.sh && chmod 666 /workdir/$filename && dos2unix -q /workdir/build.sh /workdir/$filename && echo BUILD_QUICK=1 >> /workdir/$filename && exec /workdir/build.sh $filename" > cmd.in
$SUDO docker cp cmd.in "${1}:/.cmd"
$SUDO rm -f cmd.in
$SUDO docker start -ai "$1"
$SUDO docker stop "$1"
}
function run_cleanall {
echo "Cleaning ALL"
local cids=$($SUDO docker ps -a -q -f name="$CPREFIX" | tr '\n' ' ')
if [ -n "$cids" ]; then
$SUDO docker rm -f $cids
fi
# full cleanup
$SUDO docker image prune -f
}
function run_clean {
echo "Cleaning $1"
$SUDO docker rm -f "$1"
}
function run_update_images {
$ECHOX "${YELLOW}updating Docker images${NC}"
$SUDO docker pull $IMAGE_TAG
$SUDO docker pull $IMAGE_TAG_HOST
$SUDO docker image prune -f
}
function check_images {
cid1=$(docker image ls -a -q -f reference="$IMAGE_TAG_HOST")
cid2=$(docker image ls -a -q -f reference="$IMAGE_TAG")
if [ -z "$cid1" -o -z "$cid2" ]; then
run_update_images
fi
}
function run_host_packages {
check_images
local bin_dir="bin-$VERSION-$TARGET-$SUBTARGET"
local host_dir="$SCRIPT_DIR/$bin_dir"/bin/
# macos ? $HOSTNAME
local hostname="$(hostname -f)"
if [ ! -d "$host_dir" ];then
fail "$host_dir does not exist!"
fi
local has_core=$([ -f "$host_dir/targets/$TARGET/$SUBTARGET/packages/Packages.manifest" ] && echo 1)
local custom_feeds=$(ls -d $host_dir/packages/*/* | grep -vE '/base$|/luci$|/routing$|/telephony$|/packages$')
local official_feeds=$(ls -d $host_dir/packages/*/* | grep -E '/base$|/luci$|/routing$|/telephony$|/packages$')
local arch=$(basename $(ls -d $host_dir/packages/*))
if [ -z "$official_feeds" -a -z "$custom_feeds" -a "$has_core" -ne 1 ]; then
fail "Nothing to host found in $host_dir"
fi
$ECHOX "Hosting ${YELLOW} $host_dir ${NC} via URL ${GREEN} http://${hostname}:8043/$VERSION/ ${NC}"
$ECHOX "These lines can be used via your ${YELLOW} router luci UI ${NC} under ${YELLOW} 'System/Software/Configuration' ${NC} in the ${YELLOW} 'Custom feeds' ${NC} field."
$ECHOX "${YELLOW}IMPORTANT${NC}: Make sure you ${YELLOW}disable 'Distribution feeds'${NC} that contain packages with the ${YELLOW}same name.${NC} (add '#' at the beginning)"
$ECHOX "\t Otherwise your rebuild packages will NOT be listed and overwritten by the default 'Distribution feeds'!"
$ECHOX "------------------------------------------------------------------------------------------------------------------------------------------------------"
if [ -n "$official_feeds" ] || [ "$has_core" -eq 1 ]; then
$ECHOX "${YELLOW}If you ${RED}did${YELLOW} customize or patch${NC} official packages, use these lines, otherwise not needed."
$ECHOX "${YELLOW}NOTE:${NC} Those should also be added via the ${YELLOW} 'Custom feeds' ${NC} field."
$ECHOX "${YELLOW}Custom official feeds entries${NC}"
if [ "$has_core" -eq 1 ]; then
$ECHOX "\t${GREEN}src/gz local_core http://${hostname}:8043/$VERSION/targets/$TARGET/$SUBTARGET/packages${NC}"
fi
local ofeed
for ofeed in ${official_feeds[@]}; do
if [ -f "$ofeed/Packages.manifest" ]; then
local feed_name="$(basename $ofeed)"
$ECHOX "\t${GREEN}src/gz local_${feed_name} http://${hostname}:8043/$VERSION/packages/$arch/$feed_name${NC}"
fi
done
$ECHOX "------------------------------------------------------------------------------------------------------------------------------------------------------"
fi
if [ -n "$custom_feeds" ]; then
$ECHOX "${YELLOW}Custom feeds entries${NC}"
local cfeed
for cfeed in ${custom_feeds[@]}; do
if [ -f "$cfeed/Packages.manifest" ]; then
local feed_name="$(basename $cfeed)"
$ECHOX "\t${GREEN}src/gz local_${feed_name} http://${hostname}:8043/$VERSION/packages/$arch/$feed_name${NC}"
fi
done
$ECHOX "------------------------------------------------------------------------------------------------------------------------------------------------------"
fi
$ECHOX "${YELLOW}NOTE:${NC} Make sure to disable 'Distribution feeds' that contain packages with the same names, or you wont see your updated versions!"
$ECHOX "use ${RED} CTRL-C ${NC} to stop hosting!"
$SUDO docker run --rm -ti \
--name "$1" \
-h "${MY_HOSTNAME}_host" \
-p 8043:8043 \
--mount type=bind,source="$host_dir",target="/srv/http/$VERSION",readonly \
$IMAGE_TAG_HOST
$SUDO docker rm -f "$1" 2>/dev/null
}
function copy_packages {
local sdk_dir="sdk-$VERSION-$TARGET-$SUBTARGET"
local bin_dir="bin-$VERSION-$TARGET-$SUBTARGET"
local dst_dir="/workdir/$sdk_dir/bin/"
$SUDO rm -f build_result.out
set +e
$SUDO docker cp "${1}:/workdir/.build_result" build_result.out 2>/dev/null
set -e
if [ -f build_result.out ] && [ $(cat build_result.out) -eq 0 ]; then
$SUDO rm -fR "$SCRIPT_DIR/$bin_dir/"
$SUDO mkdir -p -m 777 "$SCRIPT_DIR/$bin_dir/"
$SUDO docker cp "${1}:$dst_dir" "$SCRIPT_DIR/$bin_dir/"
#$SUDO chmod -fR `id -u`:`id -g` "$SCRIPT_DIR/$bin_dir/"
$ECHOX "************* ${GREEN}Valid build result${NC} found, copying packages to ${GREEN} $SCRIPT_DIR/$bin_dir/ ${NC}"
$ECHOX "************* Build results can now be locally hosted for installation via: ${GREEN} $0 host $FILE ${NC}"
else
$ECHOX "Problem detected you can debug by setting ${GREEN}'DEBUG=2'${NC} in ${YELLOW}$FILE${NC} or open a shell via: ${GREEN} $0 shell $FILE ${NC}"
fail "No .build_result found in $1 or last build failed, skipping copy!"
fi
$SUDO rm -f build_result.out
}
# HELP: per feed patches can be added via a '.patches' dir, with subdirs of the feednames (base, luci, packages ...) and subdirs of the SDK version (snapshots, 18.06.0, 18.06.1)
# Examples:
# .patches/packages/18.06.0/mypatch.patch
# .patches/base/snapshots/mypatch2.patch
# The generated files will be automatically used by 'builder.sh build' command and applied to the downloaded feeds
function create_patches {
if [ -d "$SCRIPT_DIR/.patches" ]; then
rm -f patches-*.tar.gz
else
fail "Error: no .patches dir found."
fi
local patch_feeds="$(ls -d $SCRIPT_DIR/.patches/* 2>/dev/null)"
if [ -z "$patch_feeds" ]; then
fail "Nothing todo empty .patches dir!"
fi
local feed_dir
for feed_dir in ${patch_feeds[@]}; do
local patch_versions="$(ls -d $feed_dir/*)"
local version_dir
for version_dir in ${patch_versions[@]}; do
local feed_name="$(basename $feed_dir)"
local version_name="$(basename $version_dir)"
echo "Creating patches-$feed_name-$version_name from $version_dir"
chmod 644 "$version_dir"/*.patch
(cd "$version_dir" && tar -zcf "$SCRIPT_DIR/patches-$feed_name-$version_name".tar.gz *.patch)
done
done
}
# assume mixed or windows CRLF!
function import_config {
if [ -f "$1" ]; then
VERSION=$(sed -n -E -e '/^VERSION ?=/ s/.*\= *//p' "$1" | tr -d '"[:cntrl:]')
TARGET=$(sed -n -E -e '/^TARGET ?=/ s/.*\= *//p' "$1" | tr -d '"[:cntrl:]')
SUBTARGET=$(sed -n -E -e '/^SUBTARGET ?=/ s/.*\= *//p' "$1" | tr -d '"[:cntrl:]')
else
fail "file does not exist: $1"
fi
# check for required VARS
[ -n "$VERSION" ] || fail "missing VERSION in $1"
[ -n "$TARGET" ] || fail "missing TARGET in $1"
[ -n "$SUBTARGET" ] || fail "missing SUBTARGET in $1"
$ECHOX "${GREEN}Config imported${NC} with:"
$ECHOX "SDK ${YELLOW}$VERSION${NC}"
$ECHOX "Target ${GREEN}$TARGET${NC}"
$ECHOX "Subtarget ${GREEN}$SUBTARGET${NC}"
echo "---------------------------"
}
function has_docker {
if [ -z "$DOCKER_VERSION" ]; then
$ECHOX "Install Docker first, check ${GREEN} https://docs.docker.com/install/ ${NC}"
case $PLATFORM in
Linux)
$ECHOX "Alternatively you can quickly install via the official ${YELLOW}Docker Edge convenience script:${NC}"
$ECHOX "> ${GREEN}curl -fsSL https://get.docker.com -o get-docker.sh ${NC}"
$ECHOX "> ${GREEN}sudo sh get-docker.sh ${NC}"
;;
Darwin)
$ECHOX "Direct download link: ${GREEN} https://download.docker.com/mac/stable/Docker.dmg ${NC}"
$ECHOX "Make sure you meet the requirements, see: https://docs.docker.com/docker-for-mac/install/#what-to-know-before-you-install"
$ECHOX "\t macOS El Capitan 10.11 and newer"
$ECHOX "\t ${YELLOW}Virtualisation capable CPU${NC} and enabled in Bios (Intel VT or AMD-V)"
local hv_support=$($SUDO sysctl kern.hv_support | awk '{print $2}')
if [ $hv_support -eq 1 ]; then
$ECHOX "\t\t ${GREEN} Virtualisation detected ${NC}"
else
$ECHOX "\t\t ${RED} Virtualisation not detected, incompatible CPU or disabled via Bios. ${NC}"
fi
;;
*)
$ECHOX "${YELLOW}untested OS detected!${NC}"
;;
esac
fail "could not find Docker."
fi
}
function start_docker {
DOCKER_STARTED=0
if [ $SYSTEMD -eq 1 ]; then
if [ $($SUDO systemctl show --property ActiveState docker 2>/dev/null) = 'ActiveState=inactive' ]; then
echo "Starting Docker daemon."
$SUDO systemctl start docker
DOCKER_STARTED=1
fi
elif ! (docker ps -q); then
fail "Docker daemon is not running. Please start Docker on your computer."
fi
}
######################################### Main ############################################
has_docker
start_docker
CMD="$1"
FILE="$2"
if [ $# -eq 2 ]; then
import_config $2
CONTAINER_NAME="$CPREFIX-build-$VERSION-$TARGET-$SUBTARGET"
CONTAINER_NAME_HOSTING="$CPREFIX-host-$VERSION-$TARGET-$SUBTARGET"
case $1 in
build)
run_build_packages "$CONTAINER_NAME"
copy_packages "$CONTAINER_NAME"
;;
build_quick)
run_build_packages_quick "$CONTAINER_NAME"
copy_packages "$CONTAINER_NAME"
;;
shell)
run_shell "$CONTAINER_NAME"
;;
host)
run_host_packages "$CONTAINER_NAME_HOSTING"
;;
clean)
run_clean "$CONTAINER_NAME"
;;
*) usage_and_exit $0
esac
elif [ $# -eq 1 ]; then
case $1 in
cleanall)
run_cleanall ;;
update)
run_update_images ;;
bundle_patches)
create_patches ;;
*) usage_and_exit $0
esac
else
fail "Invalid number of arguments given: $#"
fi
if [ $DOCKER_STARTED -eq 1 ]; then
$SUDO systemctl stop docker
echo "Stopped docker daemon."
fi