]>
Commit | Line | Data |
---|---|---|
ce96f77c | 1 | #!/bin/sh |
e2ea04e5 | 2 | set -eu |
ce96f77c | 3 | |
e2ea04e5 | 4 | PROGRAM=${0##*/} |
ce96f77c | 5 | |
e2ea04e5 ER |
6 | # defaults |
7 | : ${PACKAGE_NAME=''} | |
3ac42f8d | 8 | : ${NETWORKING=false} |
5d14e3f9 | 9 | : ${TRACING=false} |
60584f5d ER |
10 | : ${WITH=} |
11 | : ${WITHOUT=} | |
c31e256f | 12 | : ${KEEP_CONTAINER=true} |
2da31caa | 13 | : ${TMPFS="4G"} |
ce96f77c | 14 | |
ce96f77c | 15 | dir=$(pwd) |
47ec7a26 | 16 | image=registry.gitlab.com/pld-linux/cleanbuild |
ce96f77c | 17 | topdir=$dir/rpm |
8f52cf50 | 18 | home=/home/builder |
73d3710b | 19 | |
9ccc3554 ER |
20 | notice() { |
21 | echo >&2 "[cleanbuild:notice]: $*" | |
22 | } | |
23 | ||
e2ea04e5 | 24 | die() { |
20a616bf | 25 | local rc=${2:-1} |
9ccc3554 | 26 | echo >&2 "[cleanbuild:error]: $1" |
20a616bf | 27 | exit $rc |
e2ea04e5 ER |
28 | } |
29 | ||
2da31caa ER |
30 | is_no() { |
31 | # Test syntax | |
32 | if [ $# = 0 ]; then | |
33 | return 2 | |
34 | fi | |
35 | ||
36 | case "$1" in | |
37 | no|No|NO|false|False|FALSE|off|Off|OFF|N|n|0) | |
38 | # true returns zero | |
39 | return 0 | |
40 | ;; | |
41 | *) | |
42 | # false returns one | |
43 | return 1 | |
44 | ;; | |
45 | esac | |
46 | } | |
47 | ||
48 | tmpfs() { | |
8e5e409e | 49 | if is_no "${TMPFS:-true}" || [ "$TMPFS" = "0" ]; then |
2da31caa ER |
50 | return |
51 | fi | |
52 | ||
53 | echo "--tmpfs $home/rpm/BUILD:rw,exec,nosuid,size=$TMPFS" | |
54 | } | |
55 | ||
98e02557 ER |
56 | have_container() { |
57 | local name="$1" id | |
58 | ||
59 | id=$(docker ps -a -f "label=cleanbuild=$name" --format '{{.ID}}') | |
60 | ||
61 | test -n "$id" | |
62 | } | |
63 | ||
e2ea04e5 | 64 | create_container() { |
98e02557 ER |
65 | if ! $KEEP_CONTAINER; then |
66 | notice "Clean up old container: $name" | |
67 | docker kill $name >/dev/null 2>&1 || : | |
68 | docker rm $name >/dev/null 2>&1 || : | |
69 | fi | |
ce96f77c | 70 | |
e2ea04e5 | 71 | install -d $topdir/logs |
ce96f77c | 72 | |
e2ea04e5 | 73 | # start the container |
98e02557 ER |
74 | if ! have_container "$PACKAGE_NAME"; then |
75 | TMPFS_SIZE=$TMPFS \ | |
76 | PACKAGE_NAME=$PACKAGE_NAME \ | |
77 | docker-compose run --rm -d \ | |
78 | --name=$name \ | |
eb07c8d6 | 79 | --workdir=$home/rpm/packages/$PACKAGE_NAME \ |
98e02557 ER |
80 | --label=cleanbuild=$PACKAGE_NAME \ |
81 | cleanbuild | |
82 | fi | |
ce96f77c | 83 | |
9ccc3554 | 84 | notice "Setup the homedir" |
e2ea04e5 | 85 | docker exec --user=root -w / $name usermod -d $home builder |
621943d5 | 86 | |
9ccc3554 | 87 | notice "Setup permissions" |
ddb46f65 | 88 | docker exec --user=root -w / $name sh -c "cd $home && chown builder:builder . rpm rpm/logs rpm/BUILD rpm/RPMS .ccache" |
e2ea04e5 ER |
89 | |
90 | if [ ! -d $topdir/rpm-build-tools ]; then | |
9ccc3554 | 91 | notice "Initialize rpm-build-tools" |
d59399e8 | 92 | docker exec -w / $name builder --init-rpm-dir |
ce96f77c | 93 | fi |
e2ea04e5 | 94 | } |
ce96f77c | 95 | |
e2ea04e5 | 96 | package_prepare() { |
9ccc3554 | 97 | notice "Fetch sources and install dependencies" |
e2ea04e5 ER |
98 | if [ -d $topdir/packages/$PACKAGE_NAME ]; then |
99 | # chown, as it might be different owner (root) modified outside container | |
9ccc3554 | 100 | notice "Fix ownership of existing package directory" |
e2ea04e5 ER |
101 | docker exec --user=root -w / $name chown -R builder:builder $home/rpm/packages/$PACKAGE_NAME |
102 | fi | |
9ccc3554 ER |
103 | |
104 | notice "Fetch package sources" | |
98e02557 | 105 | docker exec --user=root -w / $name setfacl -x u:builder /etc/resolv.conf |
eb07c8d6 | 106 | docker exec -w / $name builder -g $PACKAGE_NAME |
e2ea04e5 | 107 | |
9ccc3554 ER |
108 | if ! $NETWORKING; then |
109 | notice "Disable networking: Prevent network access for user builder like PLD Linux builders" | |
110 | docker exec --user=root -w / $name setfacl -m u:builder:--- /etc/resolv.conf | |
111 | fi | |
e2ea04e5 | 112 | |
4fbca347 | 113 | notice "Find latest tag on the branch" |
eb07c8d6 | 114 | git_tag=$(docker exec -w / -e GIT_DIR=$home/rpm/packages/$PACKAGE_NAME/.git $name git describe --tags --always) |
e2ea04e5 | 115 | buildlog=rpm/logs/${git_tag#auto/*/}.log |
4fbca347 | 116 | notice "Build log: $buildlog" |
e2ea04e5 ER |
117 | } |
118 | ||
119 | package_build() { | |
60584f5d ER |
120 | # create default args for builder |
121 | set -- -nn ${WITH:+--with "${WITH# }"} ${WITHOUT:+--without "${WITHOUT# }"} "$PACKAGE_NAME" | |
122 | ||
e2ea04e5 | 123 | while true; do |
9ccc3554 | 124 | notice "Install dependencies" |
eb07c8d6 | 125 | docker exec -w / -t $name builder -g -R "$@" |
9ccc3554 | 126 | notice "Remove .la dependencies" |
60ca295b | 127 | docker exec --user=root -w / $name $home/cleanbuild/bin/cleanup-la |
9ccc3554 | 128 | notice "Reset findunusedbr state after deps install" |
60ca295b | 129 | docker exec --user=root -w / $name $home/cleanbuild/bin/findunusedbr -c / $home/rpm/packages/$PACKAGE_NAME/$PACKAGE_NAME.spec |
e2ea04e5 | 130 | |
9ccc3554 | 131 | notice "Build package" |
eb07c8d6 | 132 | docker exec -w $home $name cleanbuild/bin/teeboth $buildlog builder -bb --define '__spec_clean_body %{nil}' "$@" && rc=$? || rc=$? |
6ecf9aec ER |
133 | # Kill processes on Ctrl+C |
134 | if [ "$rc" = 255 ]; then | |
eb07c8d6 | 135 | docker exec -w / $name pkill -e -u builder |
6ecf9aec ER |
136 | die "Aborted" $rc |
137 | fi | |
e2ea04e5 ER |
138 | |
139 | findbr=$PACKAGE_NAME.findbr.log | |
eb07c8d6 | 140 | builddir=$(docker exec -w $home $name sh -c 'test ! -d rpm/BUILD/* || echo rpm/BUILD/*') |
e2ea04e5 | 141 | if [ -z "$builddir" ]; then |
20a616bf | 142 | die "No build dir. Build failed?" 6 |
e2ea04e5 | 143 | fi |
9ccc3554 | 144 | notice "Execute findbr" |
60ca295b | 145 | docker exec --user=root -w / $name sh -c "cd $home && cleanbuild/bin/findbr $builddir $buildlog" > $findbr |
e2ea04e5 ER |
146 | |
147 | installed_something=false | |
148 | while read pkg msg; do | |
03501aec | 149 | bin/addbr rpm/packages/$PACKAGE_NAME/$PACKAGE_NAME.spec "$pkg" "$msg" || continue |
e2ea04e5 ER |
150 | installed_something=true |
151 | done < $findbr | |
152 | rm -f $findbr | |
153 | ||
154 | # go for another try | |
155 | $installed_something && continue | |
156 | ||
9ccc3554 | 157 | notice "Execute findunusedbr" |
60ca295b | 158 | docker exec --user=root -w / $name $home/cleanbuild/bin/findunusedbr -c / $home/rpm/packages/$PACKAGE_NAME/$PACKAGE_NAME.spec |
e2ea04e5 | 159 | |
ad5ed75b | 160 | if [ $rc -eq 0 ] && ! $KEEP_CONTAINER; then |
9ccc3554 | 161 | notice "Finished ok, cleanup container" |
e2ea04e5 ER |
162 | docker kill $name >/dev/null && docker rm $name >/dev/null || : |
163 | fi | |
164 | ||
165 | # propagate error | |
166 | exit $rc | |
167 | done | |
168 | } | |
169 | ||
170 | parse_options() { | |
171 | local t | |
c31e256f | 172 | t=$(getopt -o 'x' --long 'network,no-tmpfs,notmpfs,tmpfs:,keep-container:,with:,without:' -n "$PROGRAM" -- "$@") |
e2ea04e5 ER |
173 | [ $? != 0 ] && exit $? |
174 | eval set -- "$t" | |
175 | ||
176 | while :; do | |
177 | case "$1" in | |
5d14e3f9 ER |
178 | -x) |
179 | TRACING=true | |
180 | ;; | |
3ac42f8d ER |
181 | --network) |
182 | NETWORKING=true | |
183 | ;; | |
8447721f ER |
184 | --no-tmpfs|--notmpfs) |
185 | TMPFS=false | |
186 | ;; | |
2da31caa ER |
187 | --tmpfs) |
188 | shift | |
189 | TMPFS="$1" | |
190 | ;; | |
ad5ed75b | 191 | --keep-container) |
c31e256f ER |
192 | shift |
193 | [ "$1" = "true" -o "$1" = "false" ] || die "Invalid boolean value: $1" | |
194 | KEEP_CONTAINER=$1 | |
ad5ed75b | 195 | ;; |
60584f5d ER |
196 | --with) |
197 | shift | |
9c5772e4 | 198 | WITH="$WITH,$1" |
60584f5d ER |
199 | ;; |
200 | --without) | |
201 | shift | |
9c5772e4 | 202 | WITHOUT="$WITHOUT,$1" |
60584f5d | 203 | ;; |
e2ea04e5 ER |
204 | --) |
205 | shift | |
206 | break | |
207 | ;; | |
208 | *) | |
209 | die "Internal error: [$1] not recognized!" | |
210 | ;; | |
211 | esac | |
212 | shift | |
213 | done | |
214 | ||
9ccc3554 | 215 | test "$#" -eq 1 || die "Package not specified or excess arguments" |
e2ea04e5 ER |
216 | PACKAGE_NAME="${1%.spec}" |
217 | } | |
218 | ||
219 | main() { | |
220 | parse_options "$@" | |
221 | ||
5d14e3f9 | 222 | $TRACING && set -x |
e2ea04e5 | 223 | local name="cleanbuild-$PACKAGE_NAME" |
e2ea04e5 ER |
224 | create_container |
225 | package_prepare | |
226 | package_build | |
227 | } | |
228 | ||
229 | main "$@" |