forked from kclejeune/system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·215 lines (186 loc) · 3.84 KB
/
build.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
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
212
213
214
215
#!/usr/bin/env bash
set -euv
###
# settings
# set cachix user
CACHIX_USER="bri"
DEFAULT_ARCH=x86_64
# only linux for now
OS=linux
# too lazy to write something better rn
_THISFILE(){
printf '%s' 'build.sh'
}
# server to scp artifacts to
UPLOAD_SERVER=ci-upload.ibeep.com
UPLOAD_USER=ci-upload
IMAGE_BUILD_FLAGS+=( --print-out-paths --show-trace --accept-flake-config )
# hashtable of destdirs
declare -A DESTDIRS=(
[proxmox]="dump/"
[raw-efi]="images/"
[iso]="template/iso/"
)
TARGETS=(
"server"
"bri"
)
FORMATS=(
"proxmox"
"raw-efi"
"iso"
)
declare -A EXTENSIONS=(
[raw-efi]="img"
[proxmox]="vma.zst"
[iso]="iso"
)
# filename prefixes (only proxmox needs this)
_PREFIX(){
case "${FORMAT}" in
"proxmox")
local PREFIX="vzdump-qemu-"
;;
*)
local PREFIX="" # null
esac
printf '%s' "${PREFIX}"
}
_NAME(){
printf 'build_image_%s@%s-%s_%s' \
"$(_TARGET)" \
"$(_ARCH)" \
"$(_OS)" \
"$(_FORMAT)" \
;
}
###
# Helper string functions
_TARGET(){
printf '%s' "${TARGET-${DEFAULT_TARGET}}"
}
_ARCH(){
printf '%s' "${ARCH-${DEFAULT_ARCH}}"
}
_OS(){
printf '%s' "${OS-${DEFAULT_OS}}"
}
_FORMAT(){
printf '%s' "${FORMAT-${DEFAULT_FORMAT}}"
}
_println(){
printf "%s\n" "${*}"
}
_die(){
set +x
exit_code=254
if [[ "${1}" =~ ^[0-9]+$ ]] ; then
exit_code="${1}"
shift
fi
exit "${exit_code}"
}
_LIST(){
KEEP_FUNCS='/(){/!d' # bug in bash lsp syntax highlighting
FMT_FUNCS='s/(){//'
LIST="$(<"$(_THISFILE)" sed -e '/^_/d' -e '/^ /d' -e "${KEEP_FUNCS}" -e "${FMT_FUNCS}")"
_println "Targets:"
_println "${LIST}" | sed -e's/^/ /'
}
INSTALL_CACHIX(){
nix profile install github:nixos/nixpkgs/nixpkgs-unstable#cachix --impure && cachix use $CACHIX_USER
}
WITH_CACHIX(){
cachix watch-exec "${CACHIX_USER}" -- "${@}"
}
BUILD_IMAGE(){
mkdir -p build
ARCH="$(_ARCH)"
BUILD_FILE="$(nix build ".#nixosConfigurations.$(_TARGET)@${ARCH//arm/aarch}-${OS}.config.formats.$(_FORMAT)" "${IMAGE_BUILD_FLAGS[@]}")"
BASE_FILE="$(basename "${BUILD_FILE}")"
CUT_FILE="$(cut -d- -f2- <<<"${BASE_FILE}")"
HASH=$(cut -b-5 <<<"${BASE_FILE}")
OUTNAME="build/$(_PREFIX)${HASH}-$(date -I)_${TARGET}_${CUT_FILE}"
#cp --sparse "${BUILD_FILE}" "${OUTNAME}"
ln -vs "${BUILD_FILE}" "${OUTNAME}"
}
#LIST_RENAME_BUILD_ARTIFACTS(){
# find build
# printf -v OUTFILE\
# %s%s.%s.%s \
# "$(_PREFIX)" \
# "$(_NAME)" \
# "$(date -I)" \
# "${EXTENSIONS[$(_FORMAT)]}"
#
# for i in build/*."${EXTENSIONS[$(_FORMAT)]}" ; do
# mv "$i" "build/${OUTFILE}"
# done
#}
SAVE_SSH_KEY(){
<<< "${UPLOAD_SSH_KEY_BASE64// /$'\n'}" base64 -d > /tmp/ci-upload.key
}
UPLOAD_ARTIFACTS(){
SSH_OPTIONS=(
"-i/tmp/ci-upload.key"
"-oStrictHostKeyChecking=no"
"-oport=222"
"-oidentitiesonly=true"
"-oPasswordAuthentication=no"
"-oUser=${UPLOAD_USER}"
)
chmod 600 /tmp/ci-upload.key
chmod -R 755 build
rsync \
-auvLzt \
--chmod=D2775,F664 -p \
-e "ssh ${SSH_OPTIONS[*]}" \
--info=progress2 \
"${OUTNAME}" \
"${UPLOAD_USER}@${UPLOAD_SERVER}:${DESTDIRS[$(_FORMAT)]}"
}
###
# META TARGETS
CLEAN(){
rm -fR build result
}
# tasks to build an image
BUILD_IMAGE_TASKS(){
#INSTALL_CACHIX
#WITH_CACHIX BUILD_IMAGE
BUILD_IMAGE
#LIST_RENAME_BUILD_ARTIFACTS
}
# tasks to run for upload
UPLOAD_TASKS(){
SAVE_SSH_KEY
UPLOAD_ARTIFACTS
}
# build and upload an image
BUILD_AND_UPLOAD(){
time BUILD_IMAGE_TASKS
time UPLOAD_TASKS
}
BUILD_IMAGES(){
for FORMAT in "${FORMATS[@]}"; do
for TARGET in "${TARGETS[@]}"; do
echo '{'
echo '"TARGET": "'"$(_TARGET)"'",'
echo '"FORMAT": "'"$(_FORMAT)"'"'
echo '}'
CLEAN
BUILD_AND_UPLOAD
done
done
}
CI(){
BUILD_IMAGES
}
_main(){
if [[ -z "${1-}" ]] ; then
_LIST
_die ''
fi
"${@}"
}
_main "${@}"