on engine-data help sign in
searx/utils/lxc.sh
560 lines 18 kB history raw
1#!/usr/bin/env bash
2# -*- coding: utf-8; mode: sh indent-tabs-mode: nil -*-
3# SPDX-License-Identifier: AGPL-3.0-or-later
5# shellcheck source=utils/lib.sh
6source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
7source_dot_config
9# load environment of the LXC suite
10LXC_ENV="${LXC_ENV:-${REPO_ROOT}/utils/lxc-searx.env}"
11source "$LXC_ENV"
12lxc_set_suite_env
14# ----------------------------------------------------------------------------
15# config
16# ----------------------------------------------------------------------------
18# read also:
19# - https://lxd.readthedocs.io/en/latest/
21LXC_HOST_PREFIX="${LXC_HOST_PREFIX:-test}"
23# Location in the container where all folders from HOST are mounted
24LXC_SHARE_FOLDER="/share"
25LXC_REPO_ROOT="${LXC_SHARE_FOLDER}/$(basename "${REPO_ROOT}")"
27ubu1804_boilerplate="
28export DEBIAN_FRONTEND=noninteractive
29apt-get update -y
30apt-get upgrade -y
31apt-get install -y git curl wget
33ubu1904_boilerplate="$ubu1804_boilerplate"
35# shellcheck disable=SC2034
36ubu2004_boilerplate="
37$ubu1904_boilerplate
38echo 'Set disable_coredump false' >> /etc/sudo.conf
41# shellcheck disable=SC2034
42ubu2010_boilerplate="$ubu1904_boilerplate"
44# shellcheck disable=SC2034
45archlinux_boilerplate="
46pacman -Syu --noconfirm
47pacman -S --noconfirm inetutils git curl wget sudo
48echo 'Set disable_coredump false' >> /etc/sudo.conf
51# shellcheck disable=SC2034
52fedora33_boilerplate="
53dnf update -y
54dnf install -y git curl wget hostname
55echo 'Set disable_coredump false' >> /etc/sudo.conf
58# shellcheck disable=SC2034
59centos7_boilerplate="
60yum update -y
61yum install -y git curl wget hostname sudo which
62echo 'Set disable_coredump false' >> /etc/sudo.conf
65REMOTE_IMAGES=()
66CONTAINERS=()
67LOCAL_IMAGES=()
69for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
70 REMOTE_IMAGES=("${REMOTE_IMAGES[@]}" "${LXC_SUITE[i]}")
71 CONTAINERS=("${CONTAINERS[@]}" "${LXC_HOST_PREFIX}-${LXC_SUITE[i+1]}")
72 LOCAL_IMAGES=("${LOCAL_IMAGES[@]}" "${LXC_SUITE[i+1]}")
73done
75HOST_USER="${SUDO_USER:-$USER}"
76HOST_USER_ID=$(id -u "${HOST_USER}")
77HOST_GROUP_ID=$(id -g "${HOST_USER}")
79# ----------------------------------------------------------------------------
80usage() {
81# ----------------------------------------------------------------------------
82 _cmd="$(basename "$0")"
83 cat <<EOF
84usage::
85 $_cmd build [containers|<name>]
86 $_cmd copy [images]
87 $_cmd remove [containers|<name>|images]
88 $_cmd [start|stop] [containers|<name>]
89 $_cmd show [images|suite|info|config [<name>]]
90 $_cmd cmd [--|<name>] '...'
91 $_cmd install [suite|base [<name>]]
93build
94 :containers: build, launch all containers and 'install base' packages
95 :<name>: build, launch container <name> and 'install base' packages
96copy:
97 :images: copy remote images of the suite into local storage
98remove
99 :containers: delete all 'containers' or only <container-name>
100 :images: delete local images of the suite
101start/stop
102 :containers: start/stop all 'containers' from the suite
103 :<name>: start/stop container <name> from suite
104show
105 :info: show info of all (or <name>) containers from LXC suite
106 :config: show config of all (or <name>) containers from the LXC suite
107 :suite: show services of all (or <name>) containers from the LXC suite
108 :images: show information of local images
109cmd
110 use single qoutes to evaluate in container's bash, e.g.: 'echo \$(hostname)'
111 -- run command '...' in all containers of the LXC suite
112 :<name>: run command '...' in container <name>
113install
114 :base: prepare LXC; install basic packages
115 :suite: install LXC ${LXC_SUITE_NAME} suite into all (or <name>) containers
117EOF
118 usage_containers
119 [ -n "${1+x}" ] && err_msg "$1"
122usage_containers() {
123 lxc_suite_install_info
124 [ -n "${1+x}" ] && err_msg "$1"
127lxd_info() {
129 cat <<EOF
131LXD is needed, to install run::
133 snap install lxd
134 lxd init --auto
136EOF
139main() {
141 local exit_val
142 local _usage="unknown or missing $1 command $2"
144 # don't check prerequisite when in recursion
145 if [[ ! $1 == __* ]]; then
146 if ! in_container; then
147 ! required_commands lxc && lxd_info && exit 42
148 fi
149 [[ -z $LXC_SUITE ]] && err_msg "missing LXC_SUITE" && exit 42
150 fi
152 case $1 in
153 --getenv) var="$2"; echo "${!var}"; exit 0;;
154 -h|--help) usage; exit 0;;
156 build)
157 sudo_or_exit
158 case $2 in
159 ${LXC_HOST_PREFIX}-*) build_container "$2" ;;
160 ''|--|containers) build_all_containers ;;
161 *) usage "$_usage"; exit 42;;
162 esac
163 ;;
164 copy)
165 case $2 in
166 ''|images) lxc_copy_images_localy;;
167 *) usage "$_usage"; exit 42;;
168 esac
169 ;;
170 remove)
171 sudo_or_exit
172 case $2 in
173 ''|--|containers) remove_containers ;;
174 images) lxc_delete_images_localy ;;
175 ${LXC_HOST_PREFIX}-*)
176 ! lxc_exists "$2" && warn_msg "container not yet exists: $2" && exit 0
177 if ask_yn "Do you really want to delete container $2"; then
178 lxc_delete_container "$2"
179 fi
180 ;;
181 *) usage "uknown or missing container <name> $2"; exit 42;;
182 esac
183 ;;
184 start|stop)
185 sudo_or_exit
186 case $2 in
187 ''|--|containers) lxc_cmd "$1" ;;
188 ${LXC_HOST_PREFIX}-*)
189 ! lxc_exists "$2" && usage_containers "unknown container: $2" && exit 42
190 info_msg "lxc $1 $2"
191 lxc "$1" "$2" | prefix_stdout "[${_BBlue}${i}${_creset}] "
192 ;;
193 *) usage "uknown or missing container <name> $2"; exit 42;;
194 esac
195 ;;
196 show)
197 sudo_or_exit
198 case $2 in
199 suite)
200 case $3 in
201 ${LXC_HOST_PREFIX}-*)
202 lxc exec -t "$3" -- "${LXC_REPO_ROOT}/utils/lxc.sh" __show suite \
203 | prefix_stdout "[${_BBlue}$3${_creset}] "
204 ;;
205 *) show_suite;;
206 esac
207 ;;
208 images) show_images ;;
209 config)
210 case $3 in
211 ${LXC_HOST_PREFIX}-*)
212 ! lxc_exists "$3" && usage_containers "unknown container: $3" && exit 42
213 lxc config show "$3" | prefix_stdout "[${_BBlue}${3}${_creset}] "
214 ;;
215 *)
216 rst_title "container configurations"
217 echo
218 lxc list "$LXC_HOST_PREFIX-"
219 echo
220 lxc_cmd config show
221 ;;
222 esac
223 ;;
224 info)
225 case $3 in
226 ${LXC_HOST_PREFIX}-*)
227 ! lxc_exists "$3" && usage_containers "unknown container: $3" && exit 42
228 lxc info "$3" | prefix_stdout "[${_BBlue}${3}${_creset}] "
229 ;;
230 *)
231 rst_title "container info"
232 echo
233 lxc_cmd info
234 ;;
235 esac
236 ;;
237 *) usage "$_usage"; exit 42;;
238 esac
239 ;;
240 __show)
241 # wrapped show commands, called once in each container
242 case $2 in
243 suite) lxc_suite_info ;;
244 esac
245 ;;
246 cmd)
247 sudo_or_exit
248 shift
249 case $1 in
250 --) shift; lxc_exec "$@" ;;
251 ${LXC_HOST_PREFIX}-*)
252 ! lxc_exists "$1" && usage_containers "unknown container: $1" && exit 42
253 local name=$1
254 shift
255 lxc_exec_cmd "${name}" "$@"
256 ;;
257 *) usage_containers "unknown container: $1" && exit 42
258 esac
259 ;;
260 install)
261 sudo_or_exit
262 case $2 in
263 suite|base)
264 case $3 in
265 ${LXC_HOST_PREFIX}-*)
266 ! lxc_exists "$3" && usage_containers "unknown container: $3" && exit 42
267 lxc_exec_cmd "$3" "${LXC_REPO_ROOT}/utils/lxc.sh" __install "$2"
268 ;;
269 ''|--) lxc_exec "${LXC_REPO_ROOT}/utils/lxc.sh" __install "$2" ;;
270 *) usage_containers "unknown container: $3" && exit 42
271 esac
272 ;;
273 *) usage "$_usage"; exit 42 ;;
274 esac
275 ;;
276 __install)
277 # wrapped install commands, called once in each container
278 # shellcheck disable=SC2119
279 case $2 in
280 suite) lxc_suite_install ;;
281 base) FORCE_TIMEOUT=0 lxc_install_base_packages ;;
282 esac
283 ;;
284 doc)
285 echo
286 echo ".. generic utils/lxc.sh documentation"
287 ;;
288 -*) usage "unknown option $1"; exit 42;;
289 *) usage "unknown or missing command $1"; exit 42;;
290 esac
294build_all_containers() {
295 rst_title "Build all LXC containers of suite"
296 echo
297 usage_containers
298 lxc_copy_images_localy
299 lxc_init_all_containers
300 lxc_config_all_containers
301 lxc_boilerplate_all_containers
302 rst_title "install LXC base packages" section
303 echo
304 lxc_exec "${LXC_REPO_ROOT}/utils/lxc.sh" __install base
305 echo
306 lxc list "$LXC_HOST_PREFIX"
309build_container() {
310 rst_title "Build container $1"
312 local remote_image
313 local container
314 local image
315 local boilerplate_script
317 for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
318 if [ "${LXC_HOST_PREFIX}-${LXC_SUITE[i+1]}" = "$1" ]; then
319 remote_image="${LXC_SUITE[i]}"
320 container="${LXC_HOST_PREFIX}-${LXC_SUITE[i+1]}"
321 image="${LXC_SUITE[i+1]}"
322 boilerplate_script="${image}_boilerplate"
323 boilerplate_script="${!boilerplate_script}"
324 break
325 fi
326 done
327 echo
328 if [ -z "$container" ]; then
329 err_msg "container $1 unknown"
330 usage_containers
331 return 42
332 fi
333 lxc_image_copy "${remote_image}" "${image}"
334 rst_title "init container" section
335 lxc_init_container "${image}" "${container}"
336 rst_title "configure container" section
337 lxc_config_container "${container}"
338 rst_title "run LXC boilerplate scripts" section
339 lxc_install_boilerplate "${container}" "$boilerplate_script"
340 echo
341 rst_title "install LXC base packages" section
342 lxc_exec_cmd "${container}" "${LXC_REPO_ROOT}/utils/lxc.sh" __install base \
343 | prefix_stdout "[${_BBlue}${container}${_creset}] "
344 echo
345 lxc list "$container"
348remove_containers() {
349 rst_title "Remove all LXC containers of suite"
350 rst_para "existing containers matching ${_BGreen}$LXC_HOST_PREFIX-*${_creset}"
351 echo
352 lxc list "$LXC_HOST_PREFIX-"
353 echo -en "\\n${_BRed}LXC containers to delete::${_creset}\\n\\n ${CONTAINERS[*]}\\n" | $FMT
354 local default=Ny
355 [[ $FORCE_TIMEOUT = 0 ]] && default=Yn
356 if ask_yn "Do you really want to delete these containers" $default; then
357 for i in "${CONTAINERS[@]}"; do
358 lxc_delete_container "$i"
359 done
360 fi
361 echo
362 lxc list "$LXC_HOST_PREFIX-"
365# images
366# ------
368lxc_copy_images_localy() {
369 rst_title "copy images" section
370 for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
371 lxc_image_copy "${LXC_SUITE[i]}" "${LXC_SUITE[i+1]}"
372 done
373 # lxc image list local: && wait_key
376lxc_delete_images_localy() {
377 rst_title "Delete LXC images"
378 rst_para "local existing images"
379 echo
380 lxc image list local:
381 echo -en "\\n${_BRed}LXC images to delete::${_creset}\\n\\n ${LOCAL_IMAGES[*]}\\n"
382 if ask_yn "Do you really want to delete these images"; then
383 for i in "${LOCAL_IMAGES[@]}"; do
384 lxc_delete_local_image "$i"
385 done
386 fi
388 for i in $(lxc image list --format csv | grep '^,' | sed 's/,\([^,]*\).*$/\1/'); do
389 if ask_yn "Image $i has no alias, do you want to delete the image?" Yn; then
390 lxc_delete_local_image "$i"
391 fi
392 done
394 echo
395 lxc image list local:
398show_images(){
399 rst_title "local images"
400 echo
401 lxc image list local:
402 echo -en "\\n${_Green}LXC suite images::${_creset}\\n\\n ${LOCAL_IMAGES[*]}\\n"
403 wait_key
404 for i in "${LOCAL_IMAGES[@]}"; do
405 if lxc_image_exists "$i"; then
406 info_msg "lxc image info ${_BBlue}${i}${_creset}"
407 lxc image info "$i" | prefix_stdout "[${_BBlue}${i}${_creset}] "
408 else
409 warn_msg "image ${_BBlue}$i${_creset} does not yet exists"
410 fi
411 done
416# container
417# ---------
419show_suite(){
420 rst_title "LXC suite ($LXC_HOST_PREFIX-*)"
421 echo
422 lxc list "$LXC_HOST_PREFIX-"
423 echo
424 for i in "${CONTAINERS[@]}"; do
425 if ! lxc_exists "$i"; then
426 warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
427 else
428 lxc exec -t "${i}" -- "${LXC_REPO_ROOT}/utils/lxc.sh" __show suite \
429 | prefix_stdout "[${_BBlue}${i}${_creset}] "
430 echo
431 fi
432 done
435lxc_cmd() {
436 for i in "${CONTAINERS[@]}"; do
437 if ! lxc_exists "$i"; then
438 warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
439 else
440 info_msg "lxc $* $i"
441 lxc "$@" "$i" | prefix_stdout "[${_BBlue}${i}${_creset}] "
442 fi
443 done
446lxc_exec_cmd() {
447 local name="$1"
448 shift
449 exit_val=
450 info_msg "[${_BBlue}${name}${_creset}] ${_BGreen}${*}${_creset}"
451 lxc exec -t --cwd "${LXC_REPO_ROOT}" "${name}" -- bash -c "$*"
452 exit_val=$?
453 if [[ $exit_val -ne 0 ]]; then
454 warn_msg "[${_BBlue}${name}${_creset}] exit code (${_BRed}${exit_val}${_creset}) from ${_BGreen}${*}${_creset}"
455 else
456 info_msg "[${_BBlue}${name}${_creset}] exit code (${exit_val}) from ${_BGreen}${*}${_creset}"
457 fi
460lxc_exec() {
461 for i in "${CONTAINERS[@]}"; do
462 if ! lxc_exists "$i"; then
463 warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
464 else
465 lxc_exec_cmd "${i}" "$@" | prefix_stdout "[${_BBlue}${i}${_creset}] "
466 fi
467 done
470lxc_init_all_containers() {
471 rst_title "init all containers" section
473 local image_name
474 local container_name
476 for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
477 lxc_init_container "${LXC_SUITE[i+1]}" "${LXC_HOST_PREFIX}-${LXC_SUITE[i+1]}"
478 done
481lxc_config_all_containers() {
482 rst_title "configure all containers" section
484 for i in "${CONTAINERS[@]}"; do
485 lxc_config_container "${i}"
486 done
489lxc_config_container() {
490 info_msg "[${_BBlue}$1${_creset}] configure container ..."
492 info_msg "[${_BBlue}$1${_creset}] map uid/gid from host to container"
493 # https://lxd.readthedocs.io/en/latest/userns-idmap/#custom-idmaps
494 echo -e -n "uid $HOST_USER_ID 0\\ngid $HOST_GROUP_ID 0"\
495 | lxc config set "$1" raw.idmap -
497 info_msg "[${_BBlue}$1${_creset}] share ${REPO_ROOT} (repo_share) from HOST into container"
498 # https://lxd.readthedocs.io/en/latest/instances/#type-disk
499 lxc config device add "$1" repo_share disk \
500 source="${REPO_ROOT}" \
501 path="${LXC_REPO_ROOT}" &>/dev/null
502 # lxc config show "$1" && wait_key
505lxc_boilerplate_all_containers() {
506 rst_title "run LXC boilerplate scripts" section
508 local boilerplate_script
509 local image_name
511 for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
513 image_name="${LXC_SUITE[i+1]}"
514 boilerplate_script="${image_name}_boilerplate"
515 boilerplate_script="${!boilerplate_script}"
517 lxc_install_boilerplate "${LXC_HOST_PREFIX}-${image_name}" "$boilerplate_script"
519 if [[ -z "${boilerplate_script}" ]]; then
520 err_msg "[${_BBlue}${container_name}${_creset}] no boilerplate for image '${image_name}'"
521 fi
522 done
525lxc_install_boilerplate() {
527 # usage: lxc_install_boilerplate <container-name> <string: shell commands ..>
528 #
529 # usage: lxc_install_boilerplate searx-archlinux "${archlinux_boilerplate}"
531 local container_name="$1"
532 local boilerplate_script="$2"
534 info_msg "[${_BBlue}${container_name}${_creset}] init .."
535 if lxc start -q "${container_name}" &>/dev/null; then
536 sleep 5 # guest needs some time to come up and get an IP
537 fi
538 lxc_init_container_env "${container_name}"
539 info_msg "[${_BBlue}${container_name}${_creset}] install /.lxcenv.mk .."
540 cat <<EOF | lxc exec "${container_name}" -- bash | prefix_stdout "[${_BBlue}${container_name}${_creset}] "
541rm -f "/.lxcenv.mk"
542ln -s "${LXC_REPO_ROOT}/utils/makefile.lxc" "/.lxcenv.mk"
543ls -l "/.lxcenv.mk"
544EOF
546 info_msg "[${_BBlue}${container_name}${_creset}] run LXC boilerplate scripts .."
547 if lxc start -q "${container_name}" &>/dev/null; then
548 sleep 5 # guest needs some time to come up and get an IP
549 fi
550 if [[ -n "${boilerplate_script}" ]]; then
551 echo "${boilerplate_script}" \
552 | lxc exec "${container_name}" -- bash \
553 | prefix_stdout "[${_BBlue}${container_name}${_creset}] "
554 fi
558# ----------------------------------------------------------------------------
559main "$@"
560# ----------------------------------------------------------------------------