@@ -5,7 +5,11 @@
55# shellcheck source=utils/lib.sh
66source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
77source_dot_config
8−source "${REPO_ROOT}/utils/lxc.env"
8+
9+# load environment of the LXC suite
10+LXC_ENV="${LXC_ENV:-${REPO_ROOT}/utils/lxc-searx.env}"
11+source "$LXC_ENV"
12+lxc_set_suite_env
913
1014# ----------------------------------------------------------------------------
1115# config
@@ -14,33 +18,27 @@ source "${REPO_ROOT}/utils/lxc.env"
1418# read also:
1519# - https://lxd.readthedocs.io/en/latest/
1620
17−# name of https://images.linuxcontainers.org
18−LINUXCONTAINERS_ORG_NAME="${LINUXCONTAINERS_ORG_NAME:-images}"
19−HOST_PREFIX="${HOST_PREFIX:-searx}"
21+LXC_HOST_PREFIX="${LXC_HOST_PREFIX:-test}"
2022
2123# where all folders from HOST are mounted
2224LXC_SHARE_FOLDER="/share"
2325LXC_REPO_ROOT="${LXC_SHARE_FOLDER}/$(basename "${REPO_ROOT}")"
2426
25−TEST_IMAGES=(
26− "$LINUXCONTAINERS_ORG_NAME:ubuntu/18.04" "ubu1804"
27− "$LINUXCONTAINERS_ORG_NAME:ubuntu/19.04" "ubu1904"
28− "$LINUXCONTAINERS_ORG_NAME:ubuntu/19.10" "ubu1910"
29− "$LINUXCONTAINERS_ORG_NAME:ubuntu/20.04" "ubu2004"
30− "$LINUXCONTAINERS_ORG_NAME:archlinux" "archlinux"
31− "$LINUXCONTAINERS_ORG_NAME:fedora/31" "fedora31"
32−)
33−
34−ubu1804_boilerplate="
27+ubu1604_boilerplate="
3528export DEBIAN_FRONTEND=noninteractive
3629apt-get update -y
3730apt-get upgrade -y
3831apt-get install -y git curl wget
3932"
33+ubu1804_boilerplate="$ubu1604_boilerplate"
4034ubu1904_boilerplate="$ubu1804_boilerplate"
4135ubu1910_boilerplate="$ubu1904_boilerplate"
36+
4237# shellcheck disable=SC2034
43−ubu2004_boilerplate="$ubu1910_boilerplate"
38+ubu2004_boilerplate="
39+$ubu1910_boilerplate
40+echo 'Set disable_coredump false' >> /etc/sudo.conf
41+"
4442
4543# shellcheck disable=SC2034
4644archlinux_boilerplate="
@@ -57,11 +55,13 @@ echo 'Set disable_coredump false' >> /etc/sudo.conf
5755"
5856
5957REMOTE_IMAGES=()
58+CONTAINERS=()
6059LOCAL_IMAGES=()
6160
62−for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do
63− REMOTE_IMAGES=("${REMOTE_IMAGES[@]}" "${TEST_IMAGES[i]}")
64− LOCAL_IMAGES=("${LOCAL_IMAGES[@]}" "${HOST_PREFIX}-${TEST_IMAGES[i+1]}")
61+for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
62+ REMOTE_IMAGES=("${REMOTE_IMAGES[@]}" "${LXC_SUITE[i]}")
63+ CONTAINERS=("${CONTAINERS[@]}" "${LXC_HOST_PREFIX}-${LXC_SUITE[i+1]}")
64+ LOCAL_IMAGES=("${LOCAL_IMAGES[@]}" "${LXC_SUITE[i+1]}")
6565done
6666
6767HOST_USER="${SUDO_USER:-$USER}"
@@ -71,36 +71,48 @@ HOST_GROUP_ID=$(id -g "${HOST_USER}")
7171# ----------------------------------------------------------------------------
7272usage() {
7373# ----------------------------------------------------------------------------
74−
74+ _cmd="$(basename "$0")"
7575 cat <<EOF
7676
7777usage::
7878
79− $(basename "$0") build [containers]
80− $(basename "$0") install [searx-suite]
81− $(basename "$0") remove [containers|subordinate]
82− $(basename "$0") [start|stop] [containers|<container-name>]
83− $(basename "$0") show [info|config|searx-suite]
84− $(basename "$0") cmd ...
85−
86−build / remove
87− :containers: build & launch (or remove) all LXC containers
79+ $_cmd build [containers]
80+ $_cmd copy [images]
81+ $_cmd remove [containers|<name>|images|subordinate]
82+ $_cmd add [subordinate]
83+ $_cmd [start|stop] [containers|<name>]
84+ $_cmd show [info|config|suite|images]
85+ $_cmd cmd [--|<name>] ...
86+ $_cmd install [suite]
87+
88+build
89+ :containers: build & launch all LXC containers of the suite
90+copy:
91+ :images: copy remote images of the suite into local storage
92+remove
93+ :containers: delete all 'containers' or only <container-name>
94+ :images: delete local images of the suite
8895add / remove
89− :subordinate: lxd permission to map ${HOST_USER}'s user/group id through
96+ :subordinate: LXD permission to map ${HOST_USER}'s user/group id through
9097start/stop
91− :containers: start/stop of all 'containers' or only <container-name>
98+ :containers: start/stop all 'containers' from the suite
99+ :<name>: start/stop conatiner <name> from suite
92100show
93− :info: show info of all containers
94− :config: show config of all containers
95− :searx-suite: show searx-suite services of all containers
96−cmd ...
97− run commandline ... in all containers
101+ :info: show info of all the containers from LXC suite
102+ :config: show config of all the containers from the LXC suite
103+ :suite: show services of all the containers from the LXC suite
104+ :images: show information of local images
105+cmd
106+ -- run command ... in all containers of the LXC suite
107+ :<name>: run command ... in container <name>
98108install
99− :searx-suite: install searx suite, includes morty & filtron
109+ :suite: install LXC suite, includes morty & filtron
100110
101−all LXC containers:
102− ${LOCAL_IMAGES[@]}
111+Images of the LXC suite:
112+$(echo " ${LOCAL_IMAGES[*]}" | $FMT)
103113
114+Containers of the LXC suite:
115+$(echo " ${CONTAINERS[*]}" | $FMT)
104116EOF
105117 [ -n "${1+x}" ] && err_msg "$1"
106118}
@@ -122,10 +134,12 @@ main() {
122134 local exit_val
123135 local _usage="unknown or missing $1 command $2"
124136
125− if [[ ! $1 == __* ]] && ! required_commands lxc; then
126− lxd_info
127− exit 42
137+ # don't check prerequisite when in recursion
138+ if [[ ! $1 == __* ]]; then
139+ ! required_commands lxc && lxd_info && exit 42
140+ [[ -z $LXC_SUITE ]] && err_msg "missing LXC_SUITE" && exit 42
128141 fi
142+
129143 case $1 in
130144 --source-only) ;;
131145 -h|--help) usage; exit 0;;
@@ -133,16 +147,28 @@ main() {
133147 build)
134148 sudo_or_exit
135149 case $2 in
136− containers) build_instances ;;
150+ ''|containers) build_instances ;;
151+ *) usage "$_usage"; exit 42;;
152+ esac
153+ ;;
154+ copy)
155+ case $2 in
156+ ''|images) lxc_copy_images_localy;;
137157 *) usage "$_usage"; exit 42;;
138158 esac
139159 ;;
140160 remove)
141161 sudo_or_exit
142162 case $2 in
143− containers) remove_instances ;;
163+ ''|containers) remove_instances ;;
164+ images) lxc_delete_images_localy ;;
144165 subordinate) echo; del_subordinate_ids ;;
145− *) usage "$_usage"; exit 42;;
166+ ${LXC_HOST_PREFIX}-*)
167+ if ask_yn "Do you really want to delete conatiner $2"; then
168+ lxc_delete_container "$2"
169+ fi
170+ ;;
171+ *) usage "unknown (or mising) container <name> $2"; exit 42;;
146172 esac
147173 ;;
148174 add)
@@ -155,116 +181,86 @@ main() {
155181 start|stop)
156182 sudo_or_exit
157183 case $2 in
158− containers) lxc_cmd "$1" ;;
159− *)
184+ ''|containers) lxc_cmd "$1" ;;
185+ ${LXC_HOST_PREFIX}-*)
160186 info_msg "lxc $1 $2"
161187 lxc "$1" "$2" | prefix_stdout "[${_BBlue}${i}${_creset}] "
162188 ;;
189+ *) usage "ukknown or missing container <name> $2"; exit 42;;
163190 esac
164191 ;;
165192 show)
166193 sudo_or_exit
167194 case $2 in
168− config) lxc_cmd config show;;
169− info) lxc_cmd info;;
170− searx-suite)
171− for i in "${LOCAL_IMAGES[@]}"; do
172− info_msg "[${_BBlue}${i}${_creset}] ${_BGreen}${LXC_REPO_ROOT}/utils/lxc.sh install $2${_creset}"
173− lxc exec -t "${i}" -- "${LXC_REPO_ROOT}/utils/lxc.sh" __show "$2" | prefix_stdout "[${i}] "
174− done
195+ suite) show_suite ;;
196+ images) show_images ;;
197+ config)
198+ rst_title "container configurations"
199+ echo
200+ lxc list "$LXC_HOST_PREFIX-"
201+ echo
202+ lxc_cmd config show
203+ ;;
204+ info)
205+ rst_title "container info"
206+ echo
207+ lxc_cmd info
175208 ;;
176209 *) usage "$_usage"; exit 42;;
177210 esac
178211 ;;
179212 __show)
180213 case $2 in
181− searx-suite) searx_suite_info ;;
214+ suite) lxc_suite_info ;;
182215 esac
183216 ;;
184217 cmd)
185218 sudo_or_exit
186219 shift
187− for i in "${LOCAL_IMAGES[@]}"; do
188− exit_val=
189− info_msg "[${_BBlue}${i}${_creset}] ${_BGreen}${*}${_creset}"
190− lxc exec "${i}" -- "$@"
191− exit_val=$?
192− if [[ $exit_val -ne 0 ]]; then
193− warn_msg "[${_BBlue}${i}${_creset}] exit code (${_BRed}${exit_val}${_creset}) from ${_BGreen}${*}${_creset}"
194− else
195− info_msg "[${_BBlue}${i}${_creset}] exit code (${exit_val}) from ${_BGreen}${*}${_creset}"
196− fi
197− echo
198− done
220+ case $1 in
221+ --)
222+ shift
223+ for name in "${CONTAINERS[@]}"; do
224+ lxc_exec_cmd "${name}" "$@"
225+ done
226+ ;;
227+ ${LXC_HOST_PREFIX}-*)
228+ local name=$1
229+ shift
230+ lxc_exec_cmd "${name}" "$@"
231+ ;;
232+
233+ *) usage "unknown <name>: $1"; exit 42
234+ ;;
235+ esac
199236 ;;
200237 install)
201238 sudo_or_exit
202239 case $2 in
203− searx-suite)
204− for i in "${LOCAL_IMAGES[@]}"; do
205− info_msg "[${_BBlue}${i}${_creset}] ${_BGreen}${LXC_REPO_ROOT}/utils/lxc.sh install $2${_creset}"
206− lxc exec -t "${i}" -- "${LXC_REPO_ROOT}/utils/lxc.sh" __install "$2" | prefix_stdout "[${i}] "
207− done
208− ;;
209− *) usage "$_usage"; exit 42;;
240+ suite) install_suite ;;
241+ *) usage "$_usage"; exit 42 ;;
210242 esac
211243 ;;
212244 __install)
213245 case $2 in
214− searx-suite) searx_suite_install ;;
246+ suite) lxc_suite_install ;;
215247 esac
216248 ;;
217249 doc)
218250 echo
219251 echo ".. generic utils/lxc.sh documentation"
220252 ;;
221−
222− *)
223− usage "unknown or missing command $1"; exit 42;;
253+ -*) usage "unknown option $1"; exit 42;;
254+ *) usage "unknown or missing command $1"; exit 42;;
224255 esac
225256}
226257
227−searx_suite_install() {
228− (
229− searx_suite_set_env
230− export FORCE_TIMEOUT=0
231− "${LXC_REPO_ROOT}/utils/searx.sh" install all
232− "${LXC_REPO_ROOT}/utils/morty.sh" install all
233− "${LXC_REPO_ROOT}/utils/filtron.sh" install all
234−
235− rst_title "searx-suite installation finished ($(hostname))" part
236− searx_suite_info
237− echo
238− )
239−}
240−
241−searx_suite_info() {
242− (
243− searx_suite_set_env
244− rst_para "Services of the container $(hostname)"
245− for ip in $(hostname -I); do
246− echo
247− if [[ $ip =~ .*:.* ]]; then
248− :
249− # IPv6: not yet implemented / tested
250− # echo " searx (filtron) --> http://[$ip]:4004/"
251− # echo " morty --> http://[$ip]:3000/"
252− else
253− # IPv4:
254− echo " searx (filtron) --> http://$ip:4004/"
255− echo " morty --> http://$ip:3000/"
256− fi
257− done
258− )
259−}
260258
261259build_instances() {
262260 rst_title "Build LXC instances"
263−
264− rst_title "copy images" section
265261 echo
262+ add_subordinate_ids
266263 lxc_copy_images_localy
267− # lxc image list local: && wait_key
268264 echo
269265 rst_title "build containers" section
270266 echo
@@ -272,64 +268,141 @@ build_instances() {
272268 lxc_config_containers
273269 lxc_boilerplate_containers
274270 echo
275− lxc list "$HOST_PREFIX"
271+ lxc list "$LXC_HOST_PREFIX"
276272}
277273
278274remove_instances() {
279275 rst_title "Remove LXC instances"
280− lxc list "$HOST_PREFIX"
281− echo -en "\\nLXC containers(s)::\\n\\n ${LOCAL_IMAGES[*]}\\n" | $FMT
282− if ask_yn "Do you really want to delete all images"; then
283− lxc_delete_containers
276+ rst_para "existing containers matching ${_BGreen}$LXC_HOST_PREFIX-*${_creset}"
277+ echo
278+ lxc list "$LXC_HOST_PREFIX-"
279+ echo -en "\\n${_BRed}LXC containers to delete::${_creset}\\n\\n ${CONTAINERS[*]}\\n" | $FMT
280+ if ask_yn "Do you really want to delete these conatiners"; then
281+ for i in "${CONTAINERS[@]}"; do
282+ lxc_delete_container "$i"
283+ done
284284 fi
285285 echo
286− lxc list "$HOST_PREFIX"
287− # lxc image list local: && wait_key
286+ lxc list "$LXC_HOST_PREFIX-"
288287}
289288
290289# images
291290# ------
292291
293292lxc_copy_images_localy() {
294− for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do
295− if lxc image info "local:${TEST_IMAGES[i+1]}" &>/dev/null; then
296− info_msg "image ${TEST_IMAGES[i]} already copied --> ${TEST_IMAGES[i+1]}"
293+ rst_title "copy images" section
294+ echo
295+ for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
296+ if lxc_image_exists "local:${LXC_SUITE[i+1]}"; then
297+ info_msg "image ${LXC_SUITE[i]} already copied --> ${LXC_SUITE[i+1]}"
297298 else
298− info_msg "copy image locally ${TEST_IMAGES[i]} --> ${TEST_IMAGES[i+1]}"
299− lxc image copy "${TEST_IMAGES[i]}" local: \
300− --alias "${TEST_IMAGES[i+1]}" | prefix_stdout
299+ info_msg "copy image locally ${LXC_SUITE[i]} --> ${LXC_SUITE[i+1]}"
300+ lxc image copy "${LXC_SUITE[i]}" local: \
301+ --alias "${LXC_SUITE[i+1]}" | prefix_stdout
301302 fi
302303 done
304+ # lxc image list local: && wait_key
303305}
304306
305307lxc_delete_images_localy() {
308+ rst_title "Delete LXC images"
309+ rst_para "local existing images"
306310 echo
311+ lxc image list local:
312+ echo -en "\\n${_BRed}LXC images to delete::${_creset}\\n\\n ${LOCAL_IMAGES[*]}\\n"
313+ if ask_yn "Do you really want to delete these images"; then
314+ for i in "${LOCAL_IMAGES[@]}"; do
315+ lxc_delete_local_image "$i"
316+ done
317+ fi
318+ echo
319+ lxc image list local:
320+}
321+
322+show_images(){
323+ rst_title "local images"
324+ echo
325+ lxc image list local:
326+ echo -en "\\n${_Green}LXC suite images::${_creset}\\n\\n ${LOCAL_IMAGES[*]}\\n"
327+ wait_key
307328 for i in "${LOCAL_IMAGES[@]}"; do
308− info_msg "delete image 'local:$i'"
309− lxc image delete "local:$i"
329+ if lxc_image_exists "$i"; then
330+ info_msg "lxc image info ${_BBlue}${i}${_creset}"
331+ lxc image info "$i" | prefix_stdout "[${_BBlue}${i}${_creset}] "
332+ else
333+ warn_msg "image ${_BBlue}$i${_creset} does not yet exists"
334+ fi
310335 done
311− #lxc image list local:
336+
312337}
313338
339+
314340# container
315341# ---------
316342
343+show_suite(){
344+ rst_title "LXC suite ($LXC_HOST_PREFIX-*)"
345+ echo
346+ lxc list "$LXC_HOST_PREFIX-"
347+ echo
348+ for i in "${CONTAINERS[@]}"; do
349+ if ! lxc_exists "$i"; then
350+ warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
351+ else
352+ lxc exec -t "${i}" -- "${LXC_REPO_ROOT}/utils/lxc.sh" __show suite \
353+ | prefix_stdout "[${_BBlue}${i}${_creset}] "
354+ fi
355+ done
356+}
357+
358+install_suite() {
359+ for i in "${CONTAINERS[@]}"; do
360+ if ! lxc_exists "$i"; then
361+ warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
362+ else
363+ info_msg "[${_BBlue}${i}${_creset}] ${_BGreen}${LXC_REPO_ROOT}/utils/lxc.sh install suite${_creset}"
364+ lxc exec -t "${i}" -- "${LXC_REPO_ROOT}/utils/lxc.sh" __install suite \
365+ | prefix_stdout "[${_BBlue}${i}${_creset}] "
366+ fi
367+ done
368+}
369+
317370lxc_cmd() {
318− for i in "${LOCAL_IMAGES[@]}"; do
319− info_msg "lxc $* $i"
320− lxc "$@" "$i" | prefix_stdout "[${_BBlue}${i}${_creset}] "
371+ for i in "${CONTAINERS[@]}"; do
372+ if ! lxc_exists "$i"; then
373+ warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
374+ else
375+ info_msg "lxc $* $i"
376+ lxc "$@" "$i" | prefix_stdout "[${_BBlue}${i}${_creset}] "
377+ echo
378+ fi
321379 done
322380}
323381
382+lxc_exec_cmd() {
383+ local name="$1"
384+ shift
385+ exit_val=
386+ info_msg "[${_BBlue}${name}${_creset}] ${_BGreen}${*}${_creset}"
387+ lxc exec "${name}" -- "$@"
388+ exit_val=$?
389+ if [[ $exit_val -ne 0 ]]; then
390+ warn_msg "[${_BBlue}${i}${_creset}] exit code (${_BRed}${exit_val}${_creset}) from ${_BGreen}${*}${_creset}"
391+ else
392+ info_msg "[${_BBlue}${i}${_creset}] exit code (${exit_val}) from ${_BGreen}${*}${_creset}"
393+ fi
394+ echo
395+}
396+
324397lxc_init_containers() {
325398
326399 local image_name
327400 local container_name
328401
329− for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do
402+ for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
330403
331− image_name="${TEST_IMAGES[i+1]}"
332− container_name="${HOST_PREFIX}-${image_name}"
404+ image_name="${LXC_SUITE[i+1]}"
405+ container_name="${LXC_HOST_PREFIX}-${image_name}"
333406
334407 if lxc info "${container_name}" &>/dev/null; then
335408 info_msg "container '${container_name}' already exists"
@@ -341,7 +414,7 @@ lxc_init_containers() {
341414}
342415
343416lxc_config_containers() {
344− for i in "${LOCAL_IMAGES[@]}"; do
417+ for i in "${CONTAINERS[@]}"; do
345418 info_msg "[${_BBlue}${i}${_creset}] configure container ..."
346419
347420 info_msg "[${_BBlue}${i}${_creset}] map uid/gid from host to container"
@@ -364,10 +437,10 @@ lxc_boilerplate_containers() {
364437 local container_name
365438 local boilerplate_script
366439
367− for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do
440+ for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
368441
369− image_name="${TEST_IMAGES[i+1]}"
370− container_name="${HOST_PREFIX}-${image_name}"
442+ image_name="${LXC_SUITE[i+1]}"
443+ container_name="${LXC_HOST_PREFIX}-${image_name}"
371444 boilerplate_script="${image_name}_boilerplate"
372445 boilerplate_script="${!boilerplate_script}"
373446
@@ -386,18 +459,6 @@ lxc_boilerplate_containers() {
386459 done
387460}
388461
389−lxc_delete_containers() {
390− for i in "${LOCAL_IMAGES[@]}"; do
391− if lxc info "$i" &>/dev/null; then
392− info_msg "stop & delete instance ${_BBlue}${i}${_creset}"
393− lxc stop "$i" &>/dev/null
394− lxc delete "$i" | prefix_stdout
395− else
396− warn_msg "instance '$i' does not exist / can't delete :o"
397− fi
398− done
399−}
400−
401462# subordinates
402463# ------------
403464#