Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/packages/multibuild.sh

raw symbols SCIP
1 #!/bin/bash
2 # Multi-platform build system
3 # Copyright (C) 2016-2024 Davide Madrisan <[email protected]>
4
5 PROGNAME="${0##*/}"
6 PROGPATH="${0%/*}"
7 REVISION=3
8
9 die () { echo -e "$PROGNAME: error: $1" 1>&2; exit 1; }
10 msg () { echo "*** info: $1"; }
11
12 docker_helpers="$PROGPATH/docker-shell-helpers/docker-shell-helpers.sh"
13
14 [ -r "$docker_helpers" ] || die "no such file: $docker_helpers"
15 # shellcheck source=/dev/null
16 . "$docker_helpers"
17
18 usage () {
19 cat <<__EOF
20 Usage: $PROGNAME -o <os> -s <shared> [-d <distro>] [--spec <file>] \
21 [-t <folder>] -v <version>
22 $PROGNAME --help
23 $PROGNAME --version
24
25 Where:
26 -d|--distro : distribution name (default: no distribution set)
27 -o|--os : distribution (example: centos:centos6)
28 -s|--shared : shared folder that will be mounted on the docker instance
29 --spec : the specfile to be used for building the rpm packages
30 -t|--target : the directory where to copy the rpm packages
31 -v|--pckver : the package version
32 -g|--gid : group ID of the user 'developer' used for building the software
33 -u|--uid : user ID of the user 'developer' used for building the software
34
35 Supported distributions:
36 Alpine Linux 17-19
37 CentOS 5-8
38 CentOS Stream 8, 9
39 Debian 9-12
40 Fedora 33-38/rawhide
41 Rocky Linux 8, 9
42
43 Example:
44 $0 -s $PROGPATH/../../nagios-plugins-linux:/shared:rw \\
45 --spec specs/nagios-plugins-linux.spec \\
46 -t pcks -d mamba -g 100 -u 1000 -o centos:latest
47 $0 -s $PROGPATH/../../nagios-plugins-linux:/shared:rw \\
48 -t pcks -d mamba -v 20 -o debian:jessie
49
50 __EOF
51 }
52
53 help () {
54 cat <<__EOF
55 $PROGNAME v$REVISION - containerized software build checker
56 Copyright (C) 2016-2023 Davide Madrisan <[email protected]>
57
58 __EOF
59
60 usage
61 }
62
63 while test -n "$1"; do
64 case "$1" in
65 --help|-h) help; exit 0 ;;
66 --version|-V)
67 echo "$PROGNAME v$REVISION"
68 exit 0 ;;
69 --distro|-d)
70 usr_distro="$2"; shift ;;
71 --gid|-g)
72 usr_gid="$2"; shift ;;
73 --os|-o)
74 usr_os="$2"; shift ;;
75 --shared|-s)
76 usr_disk="$2"; shift ;;
77 --spec)
78 usr_specfile="$2"; shift ;;
79 --target|-t)
80 usr_targetdir="$2"; shift ;;
81 --pckver|-v)
82 usr_pckver="$2"; shift ;;
83 --uid|-u)
84 usr_uid="$2"; shift ;;
85 --*|-*) die "unknown argument: $1" ;;
86 *) die "unknown option: $1" ;;
87 esac
88 shift
89 done
90
91 [ "$usr_disk" ] || { usage; exit 1; }
92 [ "$usr_os" ] || { usage; exit 1; }
93 [ "$usr_specfile" ] &&
94 { [ -r "$usr_specfile" ] || die "no such file: $usr_specfile"; }
95 [ "$usr_pckver" ] || { usage; exit 1; }
96
97 # parse the shared disk string
98 IFS_save="$IFS"
99 IFS=":"; set -- $usr_disk
100 shared_disk_host="$(readlink -f "$1")"
101 shared_disk_container="$2"
102 IFS="$IFS_save"
103
104 ([ "$shared_disk_host" ] && [ "$shared_disk_container" ]) ||
105 die "bad syntax for --shared"
106
107 if [ "$usr_specfile" ]; then
108 specfile="$(readlink -f "$usr_specfile")"
109 case "$specfile" in
110 ${shared_disk_host}*)
111 specfile="./${specfile#$shared_disk_host}" ;;
112 *) die "the specfile must be in $shared_disk_host" ;;
113 esac
114 fi
115 if [ "$usr_targetdir" ]; then
116 targetdir="$(readlink -m "$usr_targetdir")"
117 case "$targetdir" in
118 ${shared_disk_host}*)
119 targetdir="$shared_disk_container/${targetdir#$shared_disk_host}" ;;
120 *) die "the target dir must be in $shared_disk_host" ;;
121 esac
122 fi
123
124 msg "instantiating a new container based on $usr_os ..."
125 container="$(container_create --random-name --os "$usr_os" \
126 --disk "$shared_disk_host:$shared_disk_container")" ||
127 die "failed to create a new container with os $usr_os"
128
129 container_start "$container"
130
131 ipaddr="$(container_property --ipaddr "$container")"
132 os="$(container_property --os "$container")"
133
134 case "$os" in
135 alpine-*)
136 pck_format="apk"
137 pck_install="apk add"
138 pcks_dev="alpine-sdk autoconf automake bzip2 curl-dev libtool linux-headers m4 xz"
139 have_libcurl="1"
140 have_libvarlink="0"
141 ;;
142 centos-*)
143 pck_format="rpm"
144 pck_install="yum install -y"
145 pck_dist=".elr"
146 pcks_dev="bzip2 make gcc libcurl-devel rpm-build util-linux xz"
147 have_libcurl="1"
148 have_libvarlink="0"
149 ;;
150 debian-*)
151 pck_format="deb"
152 pck_install="\
153 export DEBIAN_FRONTEND=noninteractive;
154 apt-get update && apt-get -y --no-install-recommends install"
155 pcks_dev="\
156 build-essential bzip2 debhelper devscripts fakeroot gcc make pkg-config xz-utils"
157 pcks_dev="$pcks_dev libcurl4-gnutls-dev" ;;
158 fedora-*)
159 pck_format="rpm"
160 pck_install="dnf install -y"
161 pck_dist=".fc${os:7:2}"
162 pcks_dev="bzip2 make gcc libcurl-devel libvarlink-devel xz rpm-build util-linux"
163 have_libcurl="1"
164 have_libvarlink="1" ;;
165 rockylinux-*)
166 pck_format="rpm"
167 pck_install="dnf install -y"
168 pck_dist=".el${os:11:1}"
169 pcks_dev="bzip2 make gcc libcurl-devel xz rpm-build util-linux"
170 have_libcurl="1"
171 have_libvarlink="0" ;;
172 *) die "unsupported os: $os" ;;
173 esac
174 pck_dist="${pck_dist}${usr_distro:+.$usr_distro}"
175
176 pckname="nagios-plugins-linux"
177
178 echo "\
179 Container \"$container\" status:running ipaddr:$ipaddr os:$os
180 "
181
182 if [ "$have_libcurl" = "0" ]; then
183 msg "the docker plugin will not be built because libcurl is too old."
184 else
185 msg "the available curl library allows the docker plugin to be built."
186 fi
187
188 msg "testing the build process inside $container ..."
189 container_exec_command "$container" "\
190 # fixes for debian 6 (squeeze)
191 [ -r /etc/debian_version ] &&
192 case \"\$(cat /etc/debian_version 2>/dev/null)\" in
193 6.*) sed -i 's,httpredir,archive,g;
194 /squeeze-updates/d;' /etc/apt/sources.list ;;
195 esac
196
197 # install the build prereqs
198 $pck_install $pcks_dev
199
200 # create a non-root user for building the software (developer) ...
201 case $os in
202 alpine-*)
203 addgroup -g 1000 developers
204 adduser -D -G developers -u 1000 -s /bin/sh developer
205 addgroup developer abuild
206 #abuild-keygen -a -i -n
207 ;;
208 *) groupadd -g $usr_gid developers
209 useradd -m -g $usr_gid -u $usr_uid -s /bin/bash developer
210 ;;
211 esac
212
213 # ... and switch to this user
214 su - developer -c '
215 msg () { echo \"*** info: \$1\"; }
216
217 mkdir -p $targetdir
218
219 if [ \"'$pck_format'\" = apk ]; then
220 mkdir -p ~/${pckname}
221 cp -p $shared_disk_container/${pckname}*.tar.* ~/${pckname}
222 cp -p $shared_disk_container/$specfile ~/${pckname}
223
224 msg \"creating the required abuild certificates ...\"
225 abuild-keygen -n -a
226 cat .abuild/abuild.conf
227 echo
228 find /home/developer/.abuild/ -name \"*rsa*\" -exec cat {} \\;
229 echo
230 source .abuild/abuild.conf
231 export PACKAGER_PRIVKEY=\"\$PACKAGER_PRIVKEY\"
232
233 msg \"creating the apk packages ...\"
234 cd ~/${pckname}
235 abuild checksum
236 abuild
237
238 if [ \"'$targetdir'\" ]; then
239 msg \"copying the apk packages to the target folder ...\"
240 cp -p ../packages/developer/$(arch)/*.apk $targetdir
241 fi
242 elif [ \"'$pck_format'\" = rpm ] && [ \"'$specfile'\" ]; then
243 mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
244 cp -p $shared_disk_container/$specfile ~/rpmbuild/SPECS/
245 cp -p $shared_disk_container/${pckname}*.tar.* ~/rpmbuild/SOURCES/
246
247 msg \"creating the rpm packages ...\"
248 pushd ~/rpmbuild/SPECS/ &>/dev/null
249 rpmbuild \
250 --define=\"dist $pck_dist\" \
251 --define=\"_topdir \$HOME/rpmbuild\" \
252 --define=\"have_libcurl $have_libcurl\" \
253 --define=\"have_libvarlink $have_libvarlink\" \
254 -ba ${pckname}.spec
255
256 msg \"testing the installation of the rpm packages ...\"
257 rpm --test -hiv ../RPMS/*/*.rpm || exit 1
258
259 if [ \"'$targetdir'\" ]; then
260 msg \"copying the rpm packages to the target folder ...\"
261 cp -p ../SRPMS/*.src.rpm ../RPMS/*/*.rpm $targetdir
262 fi
263 popd &>/dev/null
264 elif [ \"'$pck_format'\" = deb ]; then
265 msg \"creating the origin package ${pckname}_${usr_pckver}.orig.tar.xz...\"
266 mkdir -p ~/debian-build
267 cp $shared_disk_container/${pckname}-${usr_pckver}.tar.xz \
268 ~/debian-build/${pckname}_${usr_pckver}.orig.tar.xz
269
270 export PATH=\$PATH:/usr/sbin:/sbin
271
272 msg \"creating the deb package and build files for version ${usr_pckver}...\"
273 cd ~/debian-build
274 tar xf ${pckname}_${usr_pckver}.orig.tar.xz
275 cd ~/debian-build/${pckname}-${usr_pckver}
276 debuild -us -uc || exit 1
277 msg \"testing the installation of the deb package(s) ...\"
278 dpkg --dry-run -i ../*.deb || exit 1
279
280 if [ \"'$targetdir'\" ]; then
281 msg \"copying the debian packages to the target folder ...\"
282 cp -p ../*.{changes,deb,dsc,orig.tar.*} $targetdir
283 fi
284 fi
285 '"
286
287 msg "removing the temporary container ..."
288 container_remove "$container"