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"
9# load environment of the LXC suite
10LXC_ENV="${LXC_ENV:-${REPO_ROOT}/utils/lxc-searx.env}"
14# ----------------------------------------------------------------------------
16# ----------------------------------------------------------------------------
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}")"
28export DEBIAN_FRONTEND=noninteractive
31apt-get install -y git curl wget
33ubu1904_boilerplate="$ubu1804_boilerplate"
35# shellcheck disable=SC2034
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
54dnf install -y git curl wget hostname
55echo 'Set disable_coredump false' >> /etc/sudo.conf
58# shellcheck disable=SC2034
61yum install -y git curl wget hostname sudo which
62echo 'Set disable_coredump false' >> /etc/sudo.conf
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]}")
75HOST_USER="${SUDO_USER:-$USER}"
76HOST_USER_ID=$(id -u "${HOST_USER}")
77HOST_GROUP_ID=$(id -g "${HOST_USER}")
79# ----------------------------------------------------------------------------
81# ----------------------------------------------------------------------------
82 _cmd="$(basename "$0")"
85 $_cmd build [containers|<name>]
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>]]
94 :containers: build, launch all containers and 'install base' packages
95 :<name>: build, launch container <name> and 'install base' packages
97 :images: copy remote images of the suite into local storage
99 :containers: delete all 'containers' or only <container-name>
100 :images: delete local images of the suite
102 :containers: start/stop all 'containers' from the suite
103 :<name>: start/stop container <name> from suite
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
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>
114 :base: prepare LXC; install basic packages
115 :suite: install LXC ${LXC_SUITE_NAME} suite into all (or <name>) containers
119 [ -n "${1+x}" ] && err_msg "$1"
123 lxc_suite_install_info
124 [ -n "${1+x}" ] && err_msg "$1"
131LXD is needed, to install run::
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
149 [[ -z $LXC_SUITE ]] && err_msg "missing LXC_SUITE" && exit 42
153 --getenv) var="$2"; echo "${!var}"; exit 0;;
154 -h|--help) usage; exit 0;;
159 ${LXC_HOST_PREFIX}-*) build_container "$2" ;;
160 ''|--|containers) build_all_containers ;;
161 *) usage "$_usage"; exit 42;;
166 ''|images) lxc_copy_images_localy;;
167 *) usage "$_usage"; exit 42;;
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"
181 *) usage "uknown or missing container <name> $2"; exit 42;;
187 ''|--|containers) lxc_cmd "$1" ;;
188 ${LXC_HOST_PREFIX}-*)
189 ! lxc_exists "$2" && usage_containers "unknown container: $2" && exit 42
191 lxc "$1" "$2" | prefix_stdout "[${_BBlue}${i}${_creset}] "
193 *) usage "uknown or missing container <name> $2"; exit 42;;
201 ${LXC_HOST_PREFIX}-*)
202 lxc exec -t "$3" -- "${LXC_REPO_ROOT}/utils/lxc.sh" __show suite \
203 | prefix_stdout "[${_BBlue}$3${_creset}] "
208 images) show_images ;;
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}] "
216 rst_title "container configurations"
218 lxc list "$LXC_HOST_PREFIX-"
226 ${LXC_HOST_PREFIX}-*)
227 ! lxc_exists "$3" && usage_containers "unknown container: $3" && exit 42
228 lxc info "$3" | prefix_stdout "[${_BBlue}${3}${_creset}] "
231 rst_title "container info"
237 *) usage "$_usage"; exit 42;;
241 # wrapped show commands, called once in each container
243 suite) lxc_suite_info ;;
250 --) shift; lxc_exec "$@" ;;
251 ${LXC_HOST_PREFIX}-*)
252 ! lxc_exists "$1" && usage_containers "unknown container: $1" && exit 42
255 lxc_exec_cmd "${name}" "$@"
257 *) usage_containers "unknown container: $1" && exit 42
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"
269 ''|--) lxc_exec "${LXC_REPO_ROOT}/utils/lxc.sh" __install "$2" ;;
270 *) usage_containers "unknown container: $3" && exit 42
273 *) usage "$_usage"; exit 42 ;;
277 # wrapped install commands, called once in each container
278 # shellcheck disable=SC2119
280 suite) lxc_suite_install ;;
281 base) FORCE_TIMEOUT=0 lxc_install_base_packages ;;
286 echo ".. generic utils/lxc.sh documentation"
288 -*) usage "unknown option $1"; exit 42;;
289 *) usage "unknown or missing command $1"; exit 42;;
294build_all_containers() {
295 rst_title "Build all LXC containers of suite"
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
304 lxc_exec "${LXC_REPO_ROOT}/utils/lxc.sh" __install base
306 lxc list "$LXC_HOST_PREFIX"
310 rst_title "Build container $1"
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}"
328 if [ -z "$container" ]; then
329 err_msg "container $1 unknown"
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"
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}] "
345 lxc list "$container"
349 rst_title "Remove all LXC containers of suite"
350 rst_para "existing containers matching ${_BGreen}$LXC_HOST_PREFIX-*${_creset}"
352 lxc list "$LXC_HOST_PREFIX-"
353 echo -en "\\n${_BRed}LXC containers to delete::${_creset}\\n\\n ${CONTAINERS[*]}\\n" | $FMT
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"
362 lxc list "$LXC_HOST_PREFIX-"
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]}"
373 # lxc image list local: && wait_key
376lxc_delete_images_localy() {
377 rst_title "Delete LXC images"
378 rst_para "local existing images"
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"
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"
395 lxc image list local:
399 rst_title "local images"
401 lxc image list local:
402 echo -en "\\n${_Green}LXC suite images::${_creset}\\n\\n ${LOCAL_IMAGES[*]}\\n"
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}] "
409 warn_msg "image ${_BBlue}$i${_creset} does not yet exists"
420 rst_title "LXC suite ($LXC_HOST_PREFIX-*)"
422 lxc list "$LXC_HOST_PREFIX-"
424 for i in "${CONTAINERS[@]}"; do
425 if ! lxc_exists "$i"; then
426 warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
428 lxc exec -t "${i}" -- "${LXC_REPO_ROOT}/utils/lxc.sh" __show suite \
429 | prefix_stdout "[${_BBlue}${i}${_creset}] "
436 for i in "${CONTAINERS[@]}"; do
437 if ! lxc_exists "$i"; then
438 warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
441 lxc "$@" "$i" | prefix_stdout "[${_BBlue}${i}${_creset}] "
450 info_msg "[${_BBlue}${name}${_creset}] ${_BGreen}${*}${_creset}"
451 lxc exec -t --cwd "${LXC_REPO_ROOT}" "${name}" -- bash -c "$*"
453 if [[ $exit_val -ne 0 ]]; then
454 warn_msg "[${_BBlue}${name}${_creset}] exit code (${_BRed}${exit_val}${_creset}) from ${_BGreen}${*}${_creset}"
456 info_msg "[${_BBlue}${name}${_creset}] exit code (${exit_val}) from ${_BGreen}${*}${_creset}"
461 for i in "${CONTAINERS[@]}"; do
462 if ! lxc_exists "$i"; then
463 warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
465 lxc_exec_cmd "${i}" "$@" | prefix_stdout "[${_BBlue}${i}${_creset}] "
470lxc_init_all_containers() {
471 rst_title "init all containers" section
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]}"
481lxc_config_all_containers() {
482 rst_title "configure all containers" section
484 for i in "${CONTAINERS[@]}"; do
485 lxc_config_container "${i}"
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
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}'"
525lxc_install_boilerplate() {
527 # usage: lxc_install_boilerplate <container-name> <string: shell commands ..>
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
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}] "
542ln -s "${LXC_REPO_ROOT}/utils/makefile.lxc" "/.lxcenv.mk"
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
550 if [[ -n "${boilerplate_script}" ]]; then
551 echo "${boilerplate_script}" \
552 | lxc exec "${container_name}" -- bash \
553 | prefix_stdout "[${_BBlue}${container_name}${_creset}] "
558# ----------------------------------------------------------------------------
560# ----------------------------------------------------------------------------
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list