@@ -5,7 +5,11 @@
5# shellcheck source=utils/lib.sh
5# shellcheck source=utils/lib.sh
6source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
6source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
7source_dot_config
7source_dot_config
9+# load environment of the LXC suite
8−source "${REPO_ROOT}/utils/lxc.env"
10+LXC_ENV="${LXC_ENV:-${REPO_ROOT}/utils/lxc-searx.env}"
10# ----------------------------------------------------------------------------
14# ----------------------------------------------------------------------------
@@ -14,33 +18,27 @@ source "${REPO_ROOT}/utils/lxc.env"
14# read also:
18# read also:
15# - https://lxd.readthedocs.io/en/latest/
19# - https://lxd.readthedocs.io/en/latest/
17−# name of https://images.linuxcontainers.org
21+LXC_HOST_PREFIX="${LXC_HOST_PREFIX:-test}"
18−LINUXCONTAINERS_ORG_NAME="${LINUXCONTAINERS_ORG_NAME:-images}"
19−HOST_PREFIX="${HOST_PREFIX:-searx}"
21# where all folders from HOST are mounted
23# where all folders from HOST are mounted
22LXC_SHARE_FOLDER="/share"
24LXC_SHARE_FOLDER="/share"
23LXC_REPO_ROOT="${LXC_SHARE_FOLDER}/$(basename "${REPO_ROOT}")"
25LXC_REPO_ROOT="${LXC_SHARE_FOLDER}/$(basename "${REPO_ROOT}")"
25−TEST_IMAGES=(
27+ubu1604_boilerplate="
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"
35export DEBIAN_FRONTEND=noninteractive
28export DEBIAN_FRONTEND=noninteractive
36apt-get update -y
29apt-get update -y
37apt-get upgrade -y
30apt-get upgrade -y
38apt-get install -y git curl wget
31apt-get install -y git curl wget
33+ubu1804_boilerplate="$ubu1604_boilerplate"
40ubu1904_boilerplate="$ubu1804_boilerplate"
34ubu1904_boilerplate="$ubu1804_boilerplate"
41ubu1910_boilerplate="$ubu1904_boilerplate"
35ubu1910_boilerplate="$ubu1904_boilerplate"
42# shellcheck disable=SC2034
37# shellcheck disable=SC2034
43−ubu2004_boilerplate="$ubu1910_boilerplate"
38+ubu2004_boilerplate="
40+echo 'Set disable_coredump false' >> /etc/sudo.conf
45# shellcheck disable=SC2034
43# shellcheck disable=SC2034
46archlinux_boilerplate="
44archlinux_boilerplate="
@@ -57,11 +55,13 @@ echo 'Set disable_coredump false' >> /etc/sudo.conf
59REMOTE_IMAGES=()
57REMOTE_IMAGES=()
60LOCAL_IMAGES=()
59LOCAL_IMAGES=()
62−for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do
61+for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
63− REMOTE_IMAGES=("${REMOTE_IMAGES[@]}" "${TEST_IMAGES[i]}")
62+ REMOTE_IMAGES=("${REMOTE_IMAGES[@]}" "${LXC_SUITE[i]}")
64− LOCAL_IMAGES=("${LOCAL_IMAGES[@]}" "${HOST_PREFIX}-${TEST_IMAGES[i+1]}")
63+ CONTAINERS=("${CONTAINERS[@]}" "${LXC_HOST_PREFIX}-${LXC_SUITE[i+1]}")
64+ LOCAL_IMAGES=("${LOCAL_IMAGES[@]}" "${LXC_SUITE[i+1]}")
67HOST_USER="${SUDO_USER:-$USER}"
67HOST_USER="${SUDO_USER:-$USER}"
@@ -71,36 +71,48 @@ HOST_GROUP_ID=$(id -g "${HOST_USER}")
71# ----------------------------------------------------------------------------
71# ----------------------------------------------------------------------------
73# ----------------------------------------------------------------------------
73# ----------------------------------------------------------------------------
74−
74+ _cmd="$(basename "$0")"
79− $(basename "$0") build [containers]
79+ $_cmd build [containers]
80− $(basename "$0") install [searx-suite]
80+ $_cmd copy [images]
81− $(basename "$0") remove [containers|subordinate]
81+ $_cmd remove [containers|<name>|images|subordinate]
82+ $_cmd add [subordinate]
82− $(basename "$0") [start|stop] [containers|<container-name>]
83+ $_cmd [start|stop] [containers|<name>]
83− $(basename "$0") show [info|config|searx-suite]
84+ $_cmd show [info|config|suite|images]
84− $(basename "$0") cmd ...
85+ $_cmd cmd [--|<name>] ...
85−
86+ $_cmd install [suite]
86−build / remove
88+build
87− :containers: build & launch (or remove) all LXC containers
89+ :containers: build & launch all LXC containers of the suite
91+ :images: copy remote images of the suite into local storage
93+ :containers: delete all 'containers' or only <container-name>
94+ :images: delete local images of the suite
88add / remove
95add / 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
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
93− :info: show info of all containers
101+ :info: show info of all the containers from LXC suite
94− :config: show config of all containers
102+ :config: show config of all the containers from the LXC suite
95− :searx-suite: show searx-suite services of all containers
103+ :suite: show services of all the containers from the LXC suite
104+ :images: show information of local images
97− run commandline ... in all containers
106+ -- run command ... in all containers of the LXC suite
107+ :<name>: run command ... in container <name>
99− :searx-suite: install searx suite, includes morty & filtron
109+ :suite: install LXC suite, includes morty & filtron
101−all LXC containers:
111+Images of the LXC suite:
102− ${LOCAL_IMAGES[@]}
112+$(echo " ${LOCAL_IMAGES[*]}" | $FMT)
114+Containers of the LXC suite:
115+$(echo " ${CONTAINERS[*]}" | $FMT)
105 [ -n "${1+x}" ] && err_msg "$1"
117 [ -n "${1+x}" ] && err_msg "$1"
@@ -122,10 +134,12 @@ main() {
122 local exit_val
134 local exit_val
123 local _usage="unknown or missing $1 command $2"
135 local _usage="unknown or missing $1 command $2"
137+ # don't check prerequisite when in recursion
138+ if [[ ! $1 == __* ]]; then
125− if [[ ! $1 == __* ]] && ! required_commands lxc; then
139+ ! required_commands lxc && lxd_info && exit 42
126− lxd_info
140+ [[ -z $LXC_SUITE ]] && err_msg "missing LXC_SUITE" && exit 42
129 case $1 in
143 case $1 in
130 --source-only) ;;
144 --source-only) ;;
131 -h|--help) usage; exit 0;;
145 -h|--help) usage; exit 0;;
@@ -133,16 +147,28 @@ main() {
134 sudo_or_exit
148 sudo_or_exit
135 case $2 in
149 case $2 in
136− containers) build_instances ;;
150+ ''|containers) build_instances ;;
151+ *) usage "$_usage"; exit 42;;
156+ ''|images) lxc_copy_images_localy;;
137 *) usage "$_usage"; exit 42;;
157 *) usage "$_usage"; exit 42;;
141 sudo_or_exit
161 sudo_or_exit
142 case $2 in
162 case $2 in
143− containers) remove_instances ;;
163+ ''|containers) remove_instances ;;
164+ images) lxc_delete_images_localy ;;
144 subordinate) echo; del_subordinate_ids ;;
165 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"
171+ *) usage "unknown (or mising) container <name> $2"; exit 42;;
@@ -155,116 +181,86 @@ main() {
155 start|stop)
181 start|stop)
156 sudo_or_exit
182 sudo_or_exit
157 case $2 in
183 case $2 in
158− containers) lxc_cmd "$1" ;;
184+ ''|containers) lxc_cmd "$1" ;;
159− *)
185+ ${LXC_HOST_PREFIX}-*)
160 info_msg "lxc $1 $2"
186 info_msg "lxc $1 $2"
161 lxc "$1" "$2" | prefix_stdout "[${_BBlue}${i}${_creset}] "
187 lxc "$1" "$2" | prefix_stdout "[${_BBlue}${i}${_creset}] "
189+ *) usage "ukknown or missing container <name> $2"; exit 42;;
166 sudo_or_exit
193 sudo_or_exit
167 case $2 in
194 case $2 in
168− config) lxc_cmd config show;;
169− info) lxc_cmd info;;
170− searx-suite)
195+ suite) show_suite ;;
171− for i in "${LOCAL_IMAGES[@]}"; do
196+ images) show_images ;;
172− info_msg "[${_BBlue}${i}${_creset}] ${_BGreen}${LXC_REPO_ROOT}/utils/lxc.sh install $2${_creset}"
197+ config)
173− lxc exec -t "${i}" -- "${LXC_REPO_ROOT}/utils/lxc.sh" __show "$2" | prefix_stdout "[${i}] "
198+ rst_title "container configurations"
200+ lxc list "$LXC_HOST_PREFIX-"
205+ rst_title "container info"
176 *) usage "$_usage"; exit 42;;
209 *) usage "$_usage"; exit 42;;
180 case $2 in
213 case $2 in
181− searx-suite) searx_suite_info ;;
214+ suite) lxc_suite_info ;;
185 sudo_or_exit
218 sudo_or_exit
187− for i in "${LOCAL_IMAGES[@]}"; do
220+ case $1 in
189− info_msg "[${_BBlue}${i}${_creset}] ${_BGreen}${*}${_creset}"
222+ shift
190− lxc exec "${i}" -- "$@"
223+ for name in "${CONTAINERS[@]}"; do
191− exit_val=$?
224+ lxc_exec_cmd "${name}" "$@"
192− if [[ $exit_val -ne 0 ]]; then
193− warn_msg "[${_BBlue}${i}${_creset}] exit code (${_BRed}${exit_val}${_creset}) from ${_BGreen}${*}${_creset}"
195− info_msg "[${_BBlue}${i}${_creset}] exit code (${exit_val}) from ${_BGreen}${*}${_creset}"
227+ ${LXC_HOST_PREFIX}-*)
230+ lxc_exec_cmd "${name}" "$@"
233+ *) usage "unknown <name>: $1"; exit 42
201 sudo_or_exit
238 sudo_or_exit
202 case $2 in
239 case $2 in
203− searx-suite)
240+ suite) install_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}] "
209− *) usage "$_usage"; exit 42;;
241+ *) usage "$_usage"; exit 42 ;;
212 __install)
244 __install)
213 case $2 in
245 case $2 in
214− searx-suite) searx_suite_install ;;
246+ suite) lxc_suite_install ;;
219 echo ".. generic utils/lxc.sh documentation"
251 echo ".. generic utils/lxc.sh documentation"
221−
253+ -*) usage "unknown option $1"; exit 42;;
223− usage "unknown or missing command $1"; exit 42;;
254+ *) usage "unknown or missing command $1"; exit 42;;
227−searx_suite_install() {
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
235− rst_title "searx-suite installation finished ($(hostname))" part
244− rst_para "Services of the container $(hostname)"
245− for ip in $(hostname -I); do
247− if [[ $ip =~ .*:.* ]]; then
249− # IPv6: not yet implemented / tested
250− # echo " searx (filtron) --> http://[$ip]:4004/"
251− # echo " morty --> http://[$ip]:3000/"
254− echo " searx (filtron) --> http://$ip:4004/"
255− echo " morty --> http://$ip:3000/"
261build_instances() {
259build_instances() {
262 rst_title "Build LXC instances"
260 rst_title "Build LXC instances"
264− rst_title "copy images" section
266 lxc_copy_images_localy
263 lxc_copy_images_localy
267− # lxc image list local: && wait_key
269 rst_title "build containers" section
265 rst_title "build containers" section
@@ -272,64 +268,141 @@ build_instances() {
272 lxc_config_containers
268 lxc_config_containers
273 lxc_boilerplate_containers
269 lxc_boilerplate_containers
275− lxc list "$HOST_PREFIX"
271+ lxc list "$LXC_HOST_PREFIX"
278remove_instances() {
274remove_instances() {
279 rst_title "Remove LXC instances"
275 rst_title "Remove LXC instances"
280− lxc list "$HOST_PREFIX"
276+ rst_para "existing containers matching ${_BGreen}$LXC_HOST_PREFIX-*${_creset}"
278+ lxc list "$LXC_HOST_PREFIX-"
281− echo -en "\\nLXC containers(s)::\\n\\n ${LOCAL_IMAGES[*]}\\n" | $FMT
279+ echo -en "\\n${_BRed}LXC containers to delete::${_creset}\\n\\n ${CONTAINERS[*]}\\n" | $FMT
282− if ask_yn "Do you really want to delete all images"; then
280+ if ask_yn "Do you really want to delete these conatiners"; then
283− lxc_delete_containers
281+ for i in "${CONTAINERS[@]}"; do
282+ lxc_delete_container "$i"
286− lxc list "$HOST_PREFIX"
286+ lxc list "$LXC_HOST_PREFIX-"
287− # lxc image list local: && wait_key
293lxc_copy_images_localy() {
292lxc_copy_images_localy() {
293+ rst_title "copy images" section
294− for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do
295+ for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
295− if lxc image info "local:${TEST_IMAGES[i+1]}" &>/dev/null; then
296+ if lxc_image_exists "local:${LXC_SUITE[i+1]}"; then
296− info_msg "image ${TEST_IMAGES[i]} already copied --> ${TEST_IMAGES[i+1]}"
297+ info_msg "image ${LXC_SUITE[i]} already copied --> ${LXC_SUITE[i+1]}"
298− info_msg "copy image locally ${TEST_IMAGES[i]} --> ${TEST_IMAGES[i+1]}"
299+ info_msg "copy image locally ${LXC_SUITE[i]} --> ${LXC_SUITE[i+1]}"
299− lxc image copy "${TEST_IMAGES[i]}" local: \
300+ lxc image copy "${LXC_SUITE[i]}" local: \
300− --alias "${TEST_IMAGES[i+1]}" | prefix_stdout
301+ --alias "${LXC_SUITE[i+1]}" | prefix_stdout
304+ # lxc image list local: && wait_key
305lxc_delete_images_localy() {
307lxc_delete_images_localy() {
308+ rst_title "Delete LXC images"
309+ rst_para "local existing images"
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"
319+ lxc image list local:
323+ rst_title "local images"
325+ lxc image list local:
326+ echo -en "\\n${_Green}LXC suite images::${_creset}\\n\\n ${LOCAL_IMAGES[*]}\\n"
307 for i in "${LOCAL_IMAGES[@]}"; do
328 for i in "${LOCAL_IMAGES[@]}"; do
329+ if lxc_image_exists "$i"; then
308− info_msg "delete image 'local:$i'"
330+ info_msg "lxc image info ${_BBlue}${i}${_creset}"
309− lxc image delete "local:$i"
331+ lxc image info "$i" | prefix_stdout "[${_BBlue}${i}${_creset}] "
333+ warn_msg "image ${_BBlue}$i${_creset} does not yet exists"
311− #lxc image list local:
336+
314# container
340# container
315# ---------
341# ---------
344+ rst_title "LXC suite ($LXC_HOST_PREFIX-*)"
346+ lxc list "$LXC_HOST_PREFIX-"
348+ for i in "${CONTAINERS[@]}"; do
349+ if ! lxc_exists "$i"; then
350+ warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
352+ lxc exec -t "${i}" -- "${LXC_REPO_ROOT}/utils/lxc.sh" __show suite \
353+ | prefix_stdout "[${_BBlue}${i}${_creset}] "
359+ for i in "${CONTAINERS[@]}"; do
360+ if ! lxc_exists "$i"; then
361+ warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
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}] "
317lxc_cmd() {
370lxc_cmd() {
318− for i in "${LOCAL_IMAGES[@]}"; do
371+ for i in "${CONTAINERS[@]}"; do
372+ if ! lxc_exists "$i"; then
373+ warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
319− info_msg "lxc $* $i"
375+ info_msg "lxc $* $i"
320− lxc "$@" "$i" | prefix_stdout "[${_BBlue}${i}${_creset}] "
376+ lxc "$@" "$i" | prefix_stdout "[${_BBlue}${i}${_creset}] "
386+ info_msg "[${_BBlue}${name}${_creset}] ${_BGreen}${*}${_creset}"
387+ lxc exec "${name}" -- "$@"
389+ if [[ $exit_val -ne 0 ]]; then
390+ warn_msg "[${_BBlue}${i}${_creset}] exit code (${_BRed}${exit_val}${_creset}) from ${_BGreen}${*}${_creset}"
392+ info_msg "[${_BBlue}${i}${_creset}] exit code (${exit_val}) from ${_BGreen}${*}${_creset}"
324lxc_init_containers() {
397lxc_init_containers() {
326 local image_name
399 local image_name
327 local container_name
400 local container_name
329− for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do
402+ for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
331− image_name="${TEST_IMAGES[i+1]}"
404+ image_name="${LXC_SUITE[i+1]}"
332− container_name="${HOST_PREFIX}-${image_name}"
405+ container_name="${LXC_HOST_PREFIX}-${image_name}"
334 if lxc info "${container_name}" &>/dev/null; then
407 if lxc info "${container_name}" &>/dev/null; then
335 info_msg "container '${container_name}' already exists"
408 info_msg "container '${container_name}' already exists"
@@ -341,7 +414,7 @@ lxc_init_containers() {
343lxc_config_containers() {
416lxc_config_containers() {
344− for i in "${LOCAL_IMAGES[@]}"; do
417+ for i in "${CONTAINERS[@]}"; do
345 info_msg "[${_BBlue}${i}${_creset}] configure container ..."
418 info_msg "[${_BBlue}${i}${_creset}] configure container ..."
347 info_msg "[${_BBlue}${i}${_creset}] map uid/gid from host to container"
420 info_msg "[${_BBlue}${i}${_creset}] map uid/gid from host to container"
@@ -364,10 +437,10 @@ lxc_boilerplate_containers() {
364 local container_name
437 local container_name
365 local boilerplate_script
438 local boilerplate_script
367− for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do
440+ for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
369− image_name="${TEST_IMAGES[i+1]}"
442+ image_name="${LXC_SUITE[i+1]}"
370− container_name="${HOST_PREFIX}-${image_name}"
443+ container_name="${LXC_HOST_PREFIX}-${image_name}"
371 boilerplate_script="${image_name}_boilerplate"
444 boilerplate_script="${image_name}_boilerplate"
372 boilerplate_script="${!boilerplate_script}"
445 boilerplate_script="${!boilerplate_script}"
@@ -386,18 +459,6 @@ lxc_boilerplate_containers() {
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
396− warn_msg "instance '$i' does not exist / can't delete :o"
401# subordinates
462# subordinates
402# ------------
463# ------------