diff options
author | Markus Heiser <markus.heiser@darmarit.de> | 2020-03-07 20:24:08 +0100 |
---|---|---|
committer | Markus Heiser <markus.heiser@darmarit.de> | 2020-03-07 20:24:08 +0100 |
commit | b1e90cff23eae2181d2b430e77471f488947d1a9 (patch) | |
tree | 18d1c0ce98f034cdb2c38840b3138f81654ff945 /utils/lxc.sh | |
parent | a258358633e18d3e5768223ebb05201bbd2e8ca4 (diff) | |
download | searxng-b1e90cff23eae2181d2b430e77471f488947d1a9.tar.gz searxng-b1e90cff23eae2181d2b430e77471f488947d1a9.zip |
LXC: separate lxc-suite from lxc & improved command line.
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
Diffstat (limited to 'utils/lxc.sh')
-rwxr-xr-x | utils/lxc.sh | 363 |
1 files changed, 212 insertions, 151 deletions
diff --git a/utils/lxc.sh b/utils/lxc.sh index f9e6e6b7a..b5ae59a7b 100755 --- a/utils/lxc.sh +++ b/utils/lxc.sh @@ -5,7 +5,11 @@ # shellcheck source=utils/lib.sh source "$(dirname "${BASH_SOURCE[0]}")/lib.sh" source_dot_config -source "${REPO_ROOT}/utils/lxc.env" + +# load environment of the LXC suite +LXC_ENV="${LXC_ENV:-${REPO_ROOT}/utils/lxc-searx.env}" +source "$LXC_ENV" +lxc_set_suite_env # ---------------------------------------------------------------------------- # config @@ -14,33 +18,27 @@ source "${REPO_ROOT}/utils/lxc.env" # read also: # - https://lxd.readthedocs.io/en/latest/ -# name of https://images.linuxcontainers.org -LINUXCONTAINERS_ORG_NAME="${LINUXCONTAINERS_ORG_NAME:-images}" -HOST_PREFIX="${HOST_PREFIX:-searx}" +LXC_HOST_PREFIX="${LXC_HOST_PREFIX:-test}" # where all folders from HOST are mounted LXC_SHARE_FOLDER="/share" LXC_REPO_ROOT="${LXC_SHARE_FOLDER}/$(basename "${REPO_ROOT}")" -TEST_IMAGES=( - "$LINUXCONTAINERS_ORG_NAME:ubuntu/18.04" "ubu1804" - "$LINUXCONTAINERS_ORG_NAME:ubuntu/19.04" "ubu1904" - "$LINUXCONTAINERS_ORG_NAME:ubuntu/19.10" "ubu1910" - "$LINUXCONTAINERS_ORG_NAME:ubuntu/20.04" "ubu2004" - "$LINUXCONTAINERS_ORG_NAME:archlinux" "archlinux" - "$LINUXCONTAINERS_ORG_NAME:fedora/31" "fedora31" -) - -ubu1804_boilerplate=" +ubu1604_boilerplate=" export DEBIAN_FRONTEND=noninteractive apt-get update -y apt-get upgrade -y apt-get install -y git curl wget " +ubu1804_boilerplate="$ubu1604_boilerplate" ubu1904_boilerplate="$ubu1804_boilerplate" ubu1910_boilerplate="$ubu1904_boilerplate" + # shellcheck disable=SC2034 -ubu2004_boilerplate="$ubu1910_boilerplate" +ubu2004_boilerplate=" +$ubu1910_boilerplate +echo 'Set disable_coredump false' >> /etc/sudo.conf +" # shellcheck disable=SC2034 archlinux_boilerplate=" @@ -57,11 +55,13 @@ echo 'Set disable_coredump false' >> /etc/sudo.conf " REMOTE_IMAGES=() +CONTAINERS=() LOCAL_IMAGES=() -for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do - REMOTE_IMAGES=("${REMOTE_IMAGES[@]}" "${TEST_IMAGES[i]}") - LOCAL_IMAGES=("${LOCAL_IMAGES[@]}" "${HOST_PREFIX}-${TEST_IMAGES[i+1]}") +for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do + REMOTE_IMAGES=("${REMOTE_IMAGES[@]}" "${LXC_SUITE[i]}") + CONTAINERS=("${CONTAINERS[@]}" "${LXC_HOST_PREFIX}-${LXC_SUITE[i+1]}") + LOCAL_IMAGES=("${LOCAL_IMAGES[@]}" "${LXC_SUITE[i+1]}") done HOST_USER="${SUDO_USER:-$USER}" @@ -71,36 +71,48 @@ HOST_GROUP_ID=$(id -g "${HOST_USER}") # ---------------------------------------------------------------------------- usage() { # ---------------------------------------------------------------------------- - + _cmd="$(basename "$0")" cat <<EOF usage:: - $(basename "$0") build [containers] - $(basename "$0") install [searx-suite] - $(basename "$0") remove [containers|subordinate] - $(basename "$0") [start|stop] [containers|<container-name>] - $(basename "$0") show [info|config|searx-suite] - $(basename "$0") cmd ... - -build / remove - :containers: build & launch (or remove) all LXC containers + $_cmd build [containers] + $_cmd copy [images] + $_cmd remove [containers|<name>|images|subordinate] + $_cmd add [subordinate] + $_cmd [start|stop] [containers|<name>] + $_cmd show [info|config|suite|images] + $_cmd cmd [--|<name>] ... + $_cmd install [suite] + +build + :containers: build & launch all LXC containers of the suite +copy: + :images: copy remote images of the suite into local storage +remove + :containers: delete all 'containers' or only <container-name> + :images: delete local images of the suite add / remove - :subordinate: lxd permission to map ${HOST_USER}'s user/group id through + :subordinate: LXD permission to map ${HOST_USER}'s user/group id through start/stop - :containers: start/stop of all 'containers' or only <container-name> + :containers: start/stop all 'containers' from the suite + :<name>: start/stop conatiner <name> from suite show - :info: show info of all containers - :config: show config of all containers - :searx-suite: show searx-suite services of all containers -cmd ... - run commandline ... in all containers + :info: show info of all the containers from LXC suite + :config: show config of all the containers from the LXC suite + :suite: show services of all the containers from the LXC suite + :images: show information of local images +cmd + -- run command ... in all containers of the LXC suite + :<name>: run command ... in container <name> install - :searx-suite: install searx suite, includes morty & filtron + :suite: install LXC suite, includes morty & filtron -all LXC containers: - ${LOCAL_IMAGES[@]} +Images of the LXC suite: +$(echo " ${LOCAL_IMAGES[*]}" | $FMT) +Containers of the LXC suite: +$(echo " ${CONTAINERS[*]}" | $FMT) EOF [ -n "${1+x}" ] && err_msg "$1" } @@ -122,10 +134,12 @@ main() { local exit_val local _usage="unknown or missing $1 command $2" - if [[ ! $1 == __* ]] && ! required_commands lxc; then - lxd_info - exit 42 + # don't check prerequisite when in recursion + if [[ ! $1 == __* ]]; then + ! required_commands lxc && lxd_info && exit 42 + [[ -z $LXC_SUITE ]] && err_msg "missing LXC_SUITE" && exit 42 fi + case $1 in --source-only) ;; -h|--help) usage; exit 0;; @@ -133,16 +147,28 @@ main() { build) sudo_or_exit case $2 in - containers) build_instances ;; + ''|containers) build_instances ;; + *) usage "$_usage"; exit 42;; + esac + ;; + copy) + case $2 in + ''|images) lxc_copy_images_localy;; *) usage "$_usage"; exit 42;; esac ;; remove) sudo_or_exit case $2 in - containers) remove_instances ;; + ''|containers) remove_instances ;; + images) lxc_delete_images_localy ;; subordinate) echo; del_subordinate_ids ;; - *) usage "$_usage"; exit 42;; + ${LXC_HOST_PREFIX}-*) + if ask_yn "Do you really want to delete conatiner $2"; then + lxc_delete_container "$2" + fi + ;; + *) usage "unknown (or mising) container <name> $2"; exit 42;; esac ;; add) @@ -155,116 +181,86 @@ main() { start|stop) sudo_or_exit case $2 in - containers) lxc_cmd "$1" ;; - *) + ''|containers) lxc_cmd "$1" ;; + ${LXC_HOST_PREFIX}-*) info_msg "lxc $1 $2" lxc "$1" "$2" | prefix_stdout "[${_BBlue}${i}${_creset}] " ;; + *) usage "ukknown or missing container <name> $2"; exit 42;; esac ;; show) sudo_or_exit case $2 in - config) lxc_cmd config show;; - info) lxc_cmd info;; - searx-suite) - for i in "${LOCAL_IMAGES[@]}"; do - info_msg "[${_BBlue}${i}${_creset}] ${_BGreen}${LXC_REPO_ROOT}/utils/lxc.sh install $2${_creset}" - lxc exec -t "${i}" -- "${LXC_REPO_ROOT}/utils/lxc.sh" __show "$2" | prefix_stdout "[${i}] " - done + suite) show_suite ;; + images) show_images ;; + config) + rst_title "container configurations" + echo + lxc list "$LXC_HOST_PREFIX-" + echo + lxc_cmd config show + ;; + info) + rst_title "container info" + echo + lxc_cmd info ;; *) usage "$_usage"; exit 42;; esac ;; __show) case $2 in - searx-suite) searx_suite_info ;; + suite) lxc_suite_info ;; esac ;; cmd) sudo_or_exit shift - for i in "${LOCAL_IMAGES[@]}"; do - exit_val= - info_msg "[${_BBlue}${i}${_creset}] ${_BGreen}${*}${_creset}" - lxc exec "${i}" -- "$@" - exit_val=$? - if [[ $exit_val -ne 0 ]]; then - warn_msg "[${_BBlue}${i}${_creset}] exit code (${_BRed}${exit_val}${_creset}) from ${_BGreen}${*}${_creset}" - else - info_msg "[${_BBlue}${i}${_creset}] exit code (${exit_val}) from ${_BGreen}${*}${_creset}" - fi - echo - done + case $1 in + --) + shift + for name in "${CONTAINERS[@]}"; do + lxc_exec_cmd "${name}" "$@" + done + ;; + ${LXC_HOST_PREFIX}-*) + local name=$1 + shift + lxc_exec_cmd "${name}" "$@" + ;; + + *) usage "unknown <name>: $1"; exit 42 + ;; + esac ;; install) sudo_or_exit case $2 in - searx-suite) - for i in "${LOCAL_IMAGES[@]}"; do - info_msg "[${_BBlue}${i}${_creset}] ${_BGreen}${LXC_REPO_ROOT}/utils/lxc.sh install $2${_creset}" - lxc exec -t "${i}" -- "${LXC_REPO_ROOT}/utils/lxc.sh" __install "$2" | prefix_stdout "[${i}] " - done - ;; - *) usage "$_usage"; exit 42;; + suite) install_suite ;; + *) usage "$_usage"; exit 42 ;; esac ;; __install) case $2 in - searx-suite) searx_suite_install ;; + suite) lxc_suite_install ;; esac ;; doc) echo echo ".. generic utils/lxc.sh documentation" ;; - - *) - usage "unknown or missing command $1"; exit 42;; + -*) usage "unknown option $1"; exit 42;; + *) usage "unknown or missing command $1"; exit 42;; esac } -searx_suite_install() { - ( - searx_suite_set_env - export FORCE_TIMEOUT=0 - "${LXC_REPO_ROOT}/utils/searx.sh" install all - "${LXC_REPO_ROOT}/utils/morty.sh" install all - "${LXC_REPO_ROOT}/utils/filtron.sh" install all - - rst_title "searx-suite installation finished ($(hostname))" part - searx_suite_info - echo - ) -} - -searx_suite_info() { - ( - searx_suite_set_env - rst_para "Services of the container $(hostname)" - for ip in $(hostname -I); do - echo - if [[ $ip =~ .*:.* ]]; then - : - # IPv6: not yet implemented / tested - # echo " searx (filtron) --> http://[$ip]:4004/" - # echo " morty --> http://[$ip]:3000/" - else - # IPv4: - echo " searx (filtron) --> http://$ip:4004/" - echo " morty --> http://$ip:3000/" - fi - done - ) -} build_instances() { rst_title "Build LXC instances" - - rst_title "copy images" section echo + add_subordinate_ids lxc_copy_images_localy - # lxc image list local: && wait_key echo rst_title "build containers" section echo @@ -272,64 +268,141 @@ build_instances() { lxc_config_containers lxc_boilerplate_containers echo - lxc list "$HOST_PREFIX" + lxc list "$LXC_HOST_PREFIX" } remove_instances() { rst_title "Remove LXC instances" - lxc list "$HOST_PREFIX" - echo -en "\\nLXC containers(s)::\\n\\n ${LOCAL_IMAGES[*]}\\n" | $FMT - if ask_yn "Do you really want to delete all images"; then - lxc_delete_containers + rst_para "existing containers matching ${_BGreen}$LXC_HOST_PREFIX-*${_creset}" + echo + lxc list "$LXC_HOST_PREFIX-" + echo -en "\\n${_BRed}LXC containers to delete::${_creset}\\n\\n ${CONTAINERS[*]}\\n" | $FMT + if ask_yn "Do you really want to delete these conatiners"; then + for i in "${CONTAINERS[@]}"; do + lxc_delete_container "$i" + done fi echo - lxc list "$HOST_PREFIX" - # lxc image list local: && wait_key + lxc list "$LXC_HOST_PREFIX-" } # images # ------ lxc_copy_images_localy() { - for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do - if lxc image info "local:${TEST_IMAGES[i+1]}" &>/dev/null; then - info_msg "image ${TEST_IMAGES[i]} already copied --> ${TEST_IMAGES[i+1]}" + rst_title "copy images" section + echo + for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do + if lxc_image_exists "local:${LXC_SUITE[i+1]}"; then + info_msg "image ${LXC_SUITE[i]} already copied --> ${LXC_SUITE[i+1]}" else - info_msg "copy image locally ${TEST_IMAGES[i]} --> ${TEST_IMAGES[i+1]}" - lxc image copy "${TEST_IMAGES[i]}" local: \ - --alias "${TEST_IMAGES[i+1]}" | prefix_stdout + info_msg "copy image locally ${LXC_SUITE[i]} --> ${LXC_SUITE[i+1]}" + lxc image copy "${LXC_SUITE[i]}" local: \ + --alias "${LXC_SUITE[i+1]}" | prefix_stdout fi done + # lxc image list local: && wait_key } lxc_delete_images_localy() { + rst_title "Delete LXC images" + rst_para "local existing images" echo + lxc image list local: + echo -en "\\n${_BRed}LXC images to delete::${_creset}\\n\\n ${LOCAL_IMAGES[*]}\\n" + if ask_yn "Do you really want to delete these images"; then + for i in "${LOCAL_IMAGES[@]}"; do + lxc_delete_local_image "$i" + done + fi + echo + lxc image list local: +} + +show_images(){ + rst_title "local images" + echo + lxc image list local: + echo -en "\\n${_Green}LXC suite images::${_creset}\\n\\n ${LOCAL_IMAGES[*]}\\n" + wait_key for i in "${LOCAL_IMAGES[@]}"; do - info_msg "delete image 'local:$i'" - lxc image delete "local:$i" + if lxc_image_exists "$i"; then + info_msg "lxc image info ${_BBlue}${i}${_creset}" + lxc image info "$i" | prefix_stdout "[${_BBlue}${i}${_creset}] " + else + warn_msg "image ${_BBlue}$i${_creset} does not yet exists" + fi done - #lxc image list local: + } + # container # --------- +show_suite(){ + rst_title "LXC suite ($LXC_HOST_PREFIX-*)" + echo + lxc list "$LXC_HOST_PREFIX-" + echo + for i in "${CONTAINERS[@]}"; do + if ! lxc_exists "$i"; then + warn_msg "container ${_BBlue}$i${_creset} does not yet exists" + else + lxc exec -t "${i}" -- "${LXC_REPO_ROOT}/utils/lxc.sh" __show suite \ + | prefix_stdout "[${_BBlue}${i}${_creset}] " + fi + done +} + +install_suite() { + for i in "${CONTAINERS[@]}"; do + if ! lxc_exists "$i"; then + warn_msg "container ${_BBlue}$i${_creset} does not yet exists" + else + info_msg "[${_BBlue}${i}${_creset}] ${_BGreen}${LXC_REPO_ROOT}/utils/lxc.sh install suite${_creset}" + lxc exec -t "${i}" -- "${LXC_REPO_ROOT}/utils/lxc.sh" __install suite \ + | prefix_stdout "[${_BBlue}${i}${_creset}] " + fi + done +} + lxc_cmd() { - for i in "${LOCAL_IMAGES[@]}"; do - info_msg "lxc $* $i" - lxc "$@" "$i" | prefix_stdout "[${_BBlue}${i}${_creset}] " + for i in "${CONTAINERS[@]}"; do + if ! lxc_exists "$i"; then + warn_msg "container ${_BBlue}$i${_creset} does not yet exists" + else + info_msg "lxc $* $i" + lxc "$@" "$i" | prefix_stdout "[${_BBlue}${i}${_creset}] " + echo + fi done } +lxc_exec_cmd() { + local name="$1" + shift + exit_val= + info_msg "[${_BBlue}${name}${_creset}] ${_BGreen}${*}${_creset}" + lxc exec "${name}" -- "$@" + exit_val=$? + if [[ $exit_val -ne 0 ]]; then + warn_msg "[${_BBlue}${i}${_creset}] exit code (${_BRed}${exit_val}${_creset}) from ${_BGreen}${*}${_creset}" + else + info_msg "[${_BBlue}${i}${_creset}] exit code (${exit_val}) from ${_BGreen}${*}${_creset}" + fi + echo +} + lxc_init_containers() { local image_name local container_name - for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do + for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do - image_name="${TEST_IMAGES[i+1]}" - container_name="${HOST_PREFIX}-${image_name}" + image_name="${LXC_SUITE[i+1]}" + container_name="${LXC_HOST_PREFIX}-${image_name}" if lxc info "${container_name}" &>/dev/null; then info_msg "container '${container_name}' already exists" @@ -341,7 +414,7 @@ lxc_init_containers() { } lxc_config_containers() { - for i in "${LOCAL_IMAGES[@]}"; do + for i in "${CONTAINERS[@]}"; do info_msg "[${_BBlue}${i}${_creset}] configure container ..." info_msg "[${_BBlue}${i}${_creset}] map uid/gid from host to container" @@ -364,10 +437,10 @@ lxc_boilerplate_containers() { local container_name local boilerplate_script - for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do + for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do - image_name="${TEST_IMAGES[i+1]}" - container_name="${HOST_PREFIX}-${image_name}" + image_name="${LXC_SUITE[i+1]}" + container_name="${LXC_HOST_PREFIX}-${image_name}" boilerplate_script="${image_name}_boilerplate" boilerplate_script="${!boilerplate_script}" @@ -386,18 +459,6 @@ lxc_boilerplate_containers() { done } -lxc_delete_containers() { - for i in "${LOCAL_IMAGES[@]}"; do - if lxc info "$i" &>/dev/null; then - info_msg "stop & delete instance ${_BBlue}${i}${_creset}" - lxc stop "$i" &>/dev/null - lxc delete "$i" | prefix_stdout - else - warn_msg "instance '$i' does not exist / can't delete :o" - fi - done -} - # subordinates # ------------ # |