-
Notifications
You must be signed in to change notification settings - Fork 25
/
run.sh
executable file
·129 lines (114 loc) · 2.86 KB
/
run.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
#!/bin/bash -e
function compose() {
# if docker-compose is not installed, use it
if ! command -v docker-compose &> /dev/null; then
docker compose "$@"
else
docker-compose "$@"
fi
}
function usage()
{
echo "--image: specify imagebuilder docker image, find it in https://hub.docker.com/r/openwrt/imagebuilder/tags or https://hub.docker.com/r/immortalwrt/imagebuilder/tags"
echo "--profile: specify profile"
echo "--with-pull: pull image before build"
echo "--rm-first: remove container before build"
echo "--use-mirror: use mirror"
echo "--mirror: specify mirror url, like mirrors.jlu.edu.cn, do not add http:// or https://"
echo "-h|--help: print this help"
exit 1
}
while [ "$1" != "" ]; do
PARAM=`echo $1 | awk -F= '{print $1}'`
VALUE=`echo $1 | awk -F= '{print $2}'`
case $PARAM in
--with-pull)
WITH_PULL=1
;;
--rm-first)
RM_FIRST=1
;;
--use-mirror)
USE_MIRROR=$VALUE
;;
--mirror)
MIRROR=$VALUE
;;
--profile)
PROFILE=$VALUE
;;
--image)
IMAGEBUILDER_IMAGE=$VALUE
;;
-h | --help)
usage
;;
*)
echo "ERROR: unknown parameter \"$PARAM\""
usage
exit 1
;;
esac
shift
done
if [ -z "$IMAGEBUILDER_IMAGE" ]; then
echo "ERROR: no image specified"
usage
exit 1
fi
if [ -z "$USE_MIRROR" ]; then
USE_MIRROR=1
fi
if [ -z "$MIRROR" ]; then
MIRROR="mirrors.pku.edu.cn"
fi
echo "IMAGEBUILDER_IMAGE: $IMAGEBUILDER_IMAGE PROFILE: $PROFILE"
if [[ $IMAGEBUILDER_IMAGE =~ "immortalwrt" ]]; then
BUILD_DIR=/home/build/immortalwrt
else
BUILD_DIR=/builder
fi
docker_compose_file_content=$(cat <<-END
version: "3.5"
services:
imagebuilder:
image: "$IMAGEBUILDER_IMAGE"
container_name: imagebuilder
environment:
- PROFILE=$PROFILE
- USE_MIRROR=$USE_MIRROR
- MIRROR=$MIRROR
env_file:
- ./.env
volumes:
- ./bin:$BUILD_DIR/bin
- ./build.sh:$BUILD_DIR/build.sh
- ./modules:$BUILD_DIR/modules_in_container
- ./user_modules:$BUILD_DIR/user_modules_in_container
- ./.env:$BUILD_DIR/.env
command: "./build.sh"
END
)
echo "$docker_compose_file_content" > docker-compose.yml
if [ ! -z $WITH_PULL ]; then
compose pull
fi
if [ ! -z $RM_FIRST ]; then
compose rm -f
fi
mkdir -p bin
# macOS no need to change the owner
# change the owner of bin to 1000:1000 when running on linux
if [[ $(uname) =~ "Linux" ]]; then
sudo chown -R 1000:1000 bin
fi
compose up --exit-code-from imagebuilder --remove-orphans
build_status=$?
compose rm -f
rm docker-compose.yml
if [ $build_status -ne 0 ]; then
echo "build failed with exit code $build_status"
exit 1
else
ls -R bin
fi