-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·166 lines (146 loc) · 4.13 KB
/
run.sh
File metadata and controls
executable file
·166 lines (146 loc) · 4.13 KB
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
#!/bin/bash -e
# 设置 Docker 路径(macOS)
export PATH="/usr/local/bin:/opt/homebrew/bin:/Applications/Docker.app/Contents/Resources/bin:/usr/bin:/bin:/usr/sbin:/sbin:$PATH"
# 验证 Docker 是否可用
if ! command -v docker &> /dev/null; then
echo "错误: 未找到 Docker,请确保 Docker Desktop 已安装并运行"
echo "尝试查找的路径:"
echo " - /usr/local/bin/docker (Intel Mac Homebrew)"
echo " - /opt/homebrew/bin/docker (Apple Silicon Homebrew)"
echo " - /Applications/Docker.app/Contents/Resources/bin/docker (Docker Desktop)"
exit 1
fi
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 "--output: specify output directory for build results (default: ./bin)"
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
;;
--output)
OUTPUT_DIR=$VALUE
;;
--custom-modules)
CUSTOM_MODULES_PATH=$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
if [ -z "$OUTPUT_DIR" ]; then
OUTPUT_DIR="./bin"
fi
if [ -z "$CUSTOM_MODULES_PATH" ]; then
CUSTOM_MODULES_PATH="./custom_modules"
fi
echo "IMAGEBUILDER_IMAGE: $IMAGEBUILDER_IMAGE PROFILE: $PROFILE"
echo "CUSTOM_MODULES_PATH: $CUSTOM_MODULES_PATH"
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
- IMAGEBUILDER_IMAGE=$IMAGEBUILDER_IMAGE
env_file:
- ./.env
volumes:
- $OUTPUT_DIR:$BUILD_DIR/bin
- ./build.sh:$BUILD_DIR/build.sh
- ./setup:$BUILD_DIR/setup
- ./modules:$BUILD_DIR/modules_in_container
- $CUSTOM_MODULES_PATH:$BUILD_DIR/custom_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
docker stop imagebuilder && docker rm imagebuilder
fi
mkdir -p "$OUTPUT_DIR"
# macOS no need to change the owner
# change the owner of output directory to 1000:1000 when running on linux
if [[ $(uname) =~ "Linux" ]]; then
sudo chown -R 1000:1000 "$OUTPUT_DIR"
fi
if [ ! -f .env ]; then
echo "WARNING: .env file not found, using default values"
echo "" > .env
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 "$OUTPUT_DIR"
fi