forked from tsaarni/docker-deb-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build
executable file
·211 lines (177 loc) · 6.27 KB
/
build
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
#!/bin/bash
set -eo pipefail
PROG=$(basename "$0")
PROG_DIR=$(dirname "$0")
CDEBB_DIR='/opt/cdebb'
if [ -t 0 ] && [ -t 1 ]; then
Red='\033[0;31m'
Cyan='\033[0;36m'
UnderlineON='\033[4m'
UnderlineOFF='\033[24m'
Bold='\033[1m'
Reset='\033[0m'
else
Red=
Cyan=
UnderlineON='`'
UnderlineOFF='`'
Bold=
Reset=
fi
function log {
echo -e "${Cyan}[+] $1${Reset}"
}
function usage {
cat <<EOF 1>&2
usage: $PROG [options...] SOURCEDIR
Options:
-i IMAGE Name of the docker image (including tag) to use as package build environment.
-c PROGRAM Use a custom container engine.
-o DIR Destination directory to store packages to.
-d DIR Directory that contains other deb packages that need to be installed before build.
-p profiles Specify the profiles to build (e.g. nocheck). Takes a comma separated list.
-C Use ccache to cache compiled objects.
-L Run Lintian after a successful build.
-B Run blhc after a successful build.
-t Reset file modification timestamps to changelog entry.
EOF
exit 1
}
function fatal {
echo -e "${Red}[!]${Reset} ${Bold}$PROG${Reset}: ${Red}${1:-"Unknown Error"}${Reset}" 1>&2
exit 1
}
function abspath {
cd "$1" && pwd
}
function sanitize_string {
local str=${1//_/}
str=${str// /_}
echo "${str//[^a-zA-Z0-9_]/-}"
}
###########################################################################
[[ $# -eq 0 ]] && usage
while getopts "c:i:o:p:d:htBCL" opt; do
case $opt in
c)
[[ "$tool" ]] && fatal "Container engine specified multiple times"
tool="$OPTARG"
;;
i)
[[ "$image" ]] && fatal "Build image specified multiple times"
image="$OPTARG"
;;
o)
[[ "$outdir" ]] && fatal "Destination directory specified multiple times"
outdir="$OPTARG"
;;
p)
[[ "$profiles" ]] && fatal "Build profiles specified multiple times"
profiles="$OPTARG"
;;
d)
[[ "$depdir" ]] && fatal "Dependency directory specified multiple times"
depdir="$OPTARG"
;;
t)
[[ "$reset_timestamps" ]] && fatal "Timestamp reset option specified multiple times"
reset_timestamps=1
;;
B)
[[ "$run_blhc" ]] && fatal "blhc option specified multiple times"
run_blhc=1
;;
C)
[[ "$use_ccache" ]] && fatal "ccache option specified multiple times"
use_ccache=1
;;
L)
[[ "$run_lintian" ]] && fatal "Lintian option specified multiple times"
run_lintian=1
;;
h)
usage
;;
*)
usage
;;
esac
done
shift $((OPTIND - 1))
[[ $# -eq 0 ]] && fatal "source directory not specified"
srcdir=$1
shift
extra_args=("$@")
[[ $# -ne 0 ]] && fatal "invalid trailing command line argument(s): ${extra_args[*]}"
if [[ ! "$tool" ]]; then
if command -v podman >/dev/null 2>&1; then
tool='podman'
elif command -v docker >/dev/null 2>&1; then
tool='docker'
elif command -v distrobox-host-exec >/dev/null 2>&1; then
if distrobox-host-exec podman --version >/dev/null 2>&1; then
tool='distrobox-host-exec podman'
elif distrobox-host-exec docker --version >/dev/null 2>&1; then
tool='distrobox-host-exec docker'
fi
fi
fi
[[ ! "$tool" ]] && fatal "neither podman nor docker found"
tool_version=$($tool --version)
tool_args="--interactive --tty "
# Check that mandatory parameters are valid
[[ ! "$outdir" ]] && fatal "output directory was not given (-o DIR)"
[[ ! -d "$outdir" ]] && fatal "output directory ${UnderlineON}${outdir}${UnderlineOFF} does not exist"
[[ ! "$srcdir" ]] && fatal "source directory not given"
[[ ! -r "$srcdir/debian" ]] && fatal "source directory ${UnderlineON}${srcdir}${UnderlineOFF} does not contain debian sub directory"
[[ ! "$image" ]] && fatal "docker image name not given (-i IMAGE)"
# Check that optional parameters are valid
if [[ "$depdir" ]]; then
[[ ! -e "$depdir" ]] && fatal "dependency directory ${UnderlineON}${depdir}${UnderlineOFF} given but does not exist"
[[ ! -d "$depdir" ]] && fatal "dependency directory ${UnderlineON}${depdir}${UnderlineOFF} given but is not a directory"
tool_args+="--volume $(abspath "$depdir"):${CDEBB_DIR}/dependencies:ro "
fi
tool_args+="--volume $(abspath "$srcdir"):${CDEBB_DIR}/source-ro:ro "
tool_args+="--volume $(abspath "$outdir"):${CDEBB_DIR}/output "
tool_args+="--volume $(abspath "$PROG_DIR")/build-helper.sh:${CDEBB_DIR}/build-helper.sh:ro "
tool_args+="--mount type=tmpfs,destination=/tmp "
# Pass current UID and GID to container, so that it can change the
# ownership of output files which are otherwise written to outdir as
# root
CURR_UID=$(id -u)
CURR_GID=$(id -g)
[[ "$tool_version" =~ "Docker" ]] && tool_args+="--env USER=${CURR_UID} --env GROUP=${CURR_GID} "
# Comment following out if you want to keep container after execution
# for debugging
tool_args+="--rm "
# pass build profiles to use
if [[ "$profiles" ]]; then
tool_args+="--env DEB_BUILD_PROFILES=$profiles --env DEB_BUILD_OPTIONS=$profiles "
fi
sanitized_image_name=$(sanitize_string "$image")
# share apt package cache
tool_args+="--volume cdebb__${sanitized_image_name}__apt:/var/cache/apt/archives "
# pass whether to use ccache
if [[ "$use_ccache" ]]; then
tool_args+="--env USE_CCACHE=1 --volume cdebb__${sanitized_image_name}__ccache:${CDEBB_DIR}/ccache_dir "
fi
# pass whether to run Lintian
if [[ "$run_lintian" ]]; then
tool_args+="--env RUN_LINTIAN=1 "
fi
# pass whether to run blhc
if [[ "$run_blhc" ]]; then
tool_args+="--env RUN_BLHC=1 "
fi
# pass whether to reset timestamps
if [[ "$reset_timestamps" ]]; then
tool_args+="--env RESET_TIMESTAMPS=1 "
fi
# disable any selinux stuff while using rh and derivates with podman
[[ "$tool_version" =~ "podman" ]] && tool_args+="--security-opt label=disable "
# run in RAM
[[ "$tool_version" =~ "podman" ]] && tool_args+="--image-volume=tmpfs "
cmd="$tool run $tool_args $image /bin/bash ${CDEBB_DIR}/build-helper.sh"
log "Running '$tool':"
log "$cmd"
exec $cmd