2020-02-08 18:12:28 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
# -*- coding: utf-8; mode: sh indent-tabs-mode: nil -*-
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
|
|
|
# shellcheck source=utils/lib.sh
|
|
|
|
source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
|
|
|
|
source_dot_config
|
2020-03-01 17:28:10 +00:00
|
|
|
source "${REPO_ROOT}/utils/lxc.env"
|
2020-02-08 18:12:28 +00:00
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
# config
|
|
|
|
# ----------------------------------------------------------------------------
|
2020-02-13 17:25:03 +00:00
|
|
|
#
|
|
|
|
# read also:
|
|
|
|
# - https://lxd.readthedocs.io/en/latest/
|
2020-02-08 18:12:28 +00:00
|
|
|
|
|
|
|
# name of https://images.linuxcontainers.org
|
|
|
|
LINUXCONTAINERS_ORG_NAME="${LINUXCONTAINERS_ORG_NAME:-images}"
|
|
|
|
HOST_PREFIX="${HOST_PREFIX:-searx}"
|
|
|
|
|
2020-02-18 17:20:03 +00:00
|
|
|
# where all folders from HOST are mounted
|
|
|
|
LXC_SHARE_FOLDER="/share"
|
2020-02-26 18:07:55 +00:00
|
|
|
LXC_REPO_ROOT="${LXC_SHARE_FOLDER}/$(basename "${REPO_ROOT}")"
|
2020-02-18 17:20:03 +00:00
|
|
|
|
2020-02-08 18:12:28 +00:00
|
|
|
TEST_IMAGES=(
|
|
|
|
"$LINUXCONTAINERS_ORG_NAME:ubuntu/18.04" "ubu1804"
|
|
|
|
"$LINUXCONTAINERS_ORG_NAME:ubuntu/19.04" "ubu1904"
|
2020-03-02 18:01:08 +00:00
|
|
|
"$LINUXCONTAINERS_ORG_NAME:ubuntu/19.10" "ubu1910"
|
|
|
|
"$LINUXCONTAINERS_ORG_NAME:ubuntu/20.04" "ubu2004"
|
2020-02-18 17:40:34 +00:00
|
|
|
"$LINUXCONTAINERS_ORG_NAME:archlinux" "archlinux"
|
|
|
|
"$LINUXCONTAINERS_ORG_NAME:fedora/31" "fedora31"
|
2020-02-08 18:12:28 +00:00
|
|
|
)
|
|
|
|
|
2020-02-16 17:18:15 +00:00
|
|
|
ubu1804_boilerplate="
|
|
|
|
export DEBIAN_FRONTEND=noninteractive
|
2020-02-18 17:40:34 +00:00
|
|
|
apt-get update -y
|
|
|
|
apt-get upgrade -y
|
2020-02-16 17:18:15 +00:00
|
|
|
apt-get install -y git curl wget
|
|
|
|
"
|
|
|
|
ubu1904_boilerplate="$ubu1804_boilerplate"
|
2020-03-02 18:01:08 +00:00
|
|
|
ubu1910_boilerplate="$ubu1904_boilerplate"
|
|
|
|
# shellcheck disable=SC2034
|
|
|
|
ubu2004_boilerplate="$ubu1910_boilerplate"
|
2020-02-16 17:18:15 +00:00
|
|
|
|
2020-02-18 17:40:34 +00:00
|
|
|
# shellcheck disable=SC2034
|
|
|
|
archlinux_boilerplate="
|
|
|
|
pacman -Syu --noconfirm
|
2020-02-23 11:10:45 +00:00
|
|
|
pacman -S --noconfirm git curl wget sudo
|
|
|
|
echo 'Set disable_coredump false' >> /etc/sudo.conf
|
2020-02-18 17:40:34 +00:00
|
|
|
"
|
|
|
|
|
2020-02-23 11:10:45 +00:00
|
|
|
# shellcheck disable=SC2034
|
2020-02-18 17:40:34 +00:00
|
|
|
fedora31_boilerplate="
|
|
|
|
dnf update -y
|
2020-02-23 11:10:45 +00:00
|
|
|
dnf install -y git curl wget hostname
|
|
|
|
echo 'Set disable_coredump false' >> /etc/sudo.conf
|
2020-02-18 17:40:34 +00:00
|
|
|
"
|
|
|
|
|
2020-02-08 18:12:28 +00:00
|
|
|
REMOTE_IMAGES=()
|
|
|
|
LOCAL_IMAGES=()
|
|
|
|
|
|
|
|
for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do
|
|
|
|
REMOTE_IMAGES=("${REMOTE_IMAGES[@]}" "${TEST_IMAGES[i]}")
|
2020-02-16 17:18:15 +00:00
|
|
|
LOCAL_IMAGES=("${LOCAL_IMAGES[@]}" "${HOST_PREFIX}-${TEST_IMAGES[i+1]}")
|
2020-02-08 18:12:28 +00:00
|
|
|
done
|
|
|
|
|
2020-02-13 17:25:03 +00:00
|
|
|
HOST_USER="${SUDO_USER:-$USER}"
|
|
|
|
HOST_USER_ID=$(id -u "${HOST_USER}")
|
|
|
|
HOST_GROUP_ID=$(id -g "${HOST_USER}")
|
|
|
|
|
2020-02-08 18:12:28 +00:00
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
usage() {
|
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
cat <<EOF
|
|
|
|
|
|
|
|
usage::
|
|
|
|
|
2020-02-13 17:25:03 +00:00
|
|
|
$(basename "$0") build [containers]
|
2020-02-26 18:07:55 +00:00
|
|
|
$(basename "$0") install [searx-suite]
|
2020-02-16 21:26:03 +00:00
|
|
|
$(basename "$0") remove [containers|subordinate]
|
2020-02-27 18:13:03 +00:00
|
|
|
$(basename "$0") [start|stop] [containers|<container-name>]
|
|
|
|
$(basename "$0") show [info|config|searx-suite]
|
2020-02-13 17:25:03 +00:00
|
|
|
$(basename "$0") cmd ...
|
2020-02-08 18:12:28 +00:00
|
|
|
|
2020-02-16 21:26:03 +00:00
|
|
|
build / remove
|
2020-02-18 17:20:03 +00:00
|
|
|
:containers: build & launch (or remove) all LXC containers
|
2020-02-16 21:26:03 +00:00
|
|
|
add / remove
|
2020-02-13 17:25:03 +00:00
|
|
|
:subordinate: lxd permission to map ${HOST_USER}'s user/group id through
|
|
|
|
start/stop
|
2020-02-27 18:13:03 +00:00
|
|
|
:containers: start/stop of all 'containers' or only <container-name>
|
|
|
|
show
|
|
|
|
:info: show info of all containers
|
|
|
|
:config: show config of all containers
|
|
|
|
:searx-suite: show searx-suite services of all containers
|
2020-02-13 17:25:03 +00:00
|
|
|
cmd ...
|
|
|
|
run commandline ... in all containers
|
2020-02-26 18:07:55 +00:00
|
|
|
install
|
|
|
|
:searx-suite: install searx suite, includes morty & filtron
|
2020-02-08 18:12:28 +00:00
|
|
|
|
2020-02-13 17:25:03 +00:00
|
|
|
all LXC containers:
|
2020-02-08 18:12:28 +00:00
|
|
|
${LOCAL_IMAGES[@]}
|
|
|
|
|
|
|
|
EOF
|
2020-02-16 19:07:37 +00:00
|
|
|
[ -n "${1+x}" ] && err_msg "$1"
|
2020-02-08 18:12:28 +00:00
|
|
|
}
|
|
|
|
|
2020-02-16 17:18:15 +00:00
|
|
|
lxd_info() {
|
|
|
|
|
|
|
|
cat <<EOF
|
|
|
|
|
|
|
|
LXD is needed, to install run::
|
|
|
|
|
|
|
|
snap install lxd
|
2020-02-16 21:26:03 +00:00
|
|
|
lxd init --auto
|
2020-02-16 17:18:15 +00:00
|
|
|
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2020-02-08 18:12:28 +00:00
|
|
|
main() {
|
|
|
|
|
2020-02-16 17:18:15 +00:00
|
|
|
local exit_val
|
2020-02-08 18:12:28 +00:00
|
|
|
local _usage="unknown or missing $1 command $2"
|
|
|
|
|
2020-02-27 18:13:03 +00:00
|
|
|
if [[ ! $1 == __* ]] && ! required_commands lxc; then
|
|
|
|
lxd_info
|
|
|
|
exit 42
|
|
|
|
fi
|
2020-02-08 18:12:28 +00:00
|
|
|
case $1 in
|
|
|
|
--source-only) ;;
|
|
|
|
-h|--help) usage; exit 0;;
|
|
|
|
|
|
|
|
build)
|
|
|
|
sudo_or_exit
|
|
|
|
case $2 in
|
2020-02-13 17:25:03 +00:00
|
|
|
containers) build_instances ;;
|
2020-02-08 18:12:28 +00:00
|
|
|
*) usage "$_usage"; exit 42;;
|
2020-02-27 18:13:03 +00:00
|
|
|
esac
|
|
|
|
;;
|
2020-02-16 21:26:03 +00:00
|
|
|
remove)
|
2020-02-08 18:12:28 +00:00
|
|
|
sudo_or_exit
|
|
|
|
case $2 in
|
2020-02-16 21:26:03 +00:00
|
|
|
containers) remove_instances ;;
|
2020-02-13 17:25:03 +00:00
|
|
|
subordinate) echo; del_subordinate_ids ;;
|
|
|
|
*) usage "$_usage"; exit 42;;
|
2020-02-27 18:13:03 +00:00
|
|
|
esac
|
|
|
|
;;
|
2020-02-13 17:25:03 +00:00
|
|
|
add)
|
|
|
|
sudo_or_exit
|
|
|
|
case $2 in
|
|
|
|
subordinate) echo; add_subordinate_ids ;;
|
|
|
|
*) usage "$_usage"; exit 42;;
|
2020-02-27 18:13:03 +00:00
|
|
|
esac
|
|
|
|
;;
|
2020-02-13 17:25:03 +00:00
|
|
|
start|stop)
|
|
|
|
sudo_or_exit
|
|
|
|
case $2 in
|
|
|
|
containers) lxc_cmd "$1" ;;
|
2020-02-18 17:20:03 +00:00
|
|
|
*)
|
|
|
|
info_msg "lxc $1 $2"
|
|
|
|
lxc "$1" "$2" | prefix_stdout "[${_BBlue}${i}${_creset}] "
|
|
|
|
;;
|
2020-02-27 18:13:03 +00:00
|
|
|
esac
|
|
|
|
;;
|
|
|
|
show)
|
2020-02-13 17:25:03 +00:00
|
|
|
sudo_or_exit
|
|
|
|
case $2 in
|
|
|
|
config) lxc_cmd config show;;
|
|
|
|
info) lxc_cmd info;;
|
2020-02-27 18:13:03 +00:00
|
|
|
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
|
|
|
|
;;
|
2020-02-13 17:25:03 +00:00
|
|
|
*) usage "$_usage"; exit 42;;
|
2020-02-27 18:13:03 +00:00
|
|
|
esac
|
|
|
|
;;
|
|
|
|
__show)
|
|
|
|
case $2 in
|
|
|
|
searx-suite) searx_suite_info ;;
|
|
|
|
esac
|
|
|
|
;;
|
2020-02-13 17:25:03 +00:00
|
|
|
cmd)
|
|
|
|
sudo_or_exit
|
|
|
|
shift
|
|
|
|
for i in "${LOCAL_IMAGES[@]}"; do
|
2020-02-26 18:07:55 +00:00
|
|
|
exit_val=
|
2020-02-18 17:20:03 +00:00
|
|
|
info_msg "[${_BBlue}${i}${_creset}] ${_BGreen}${*}${_creset}"
|
2020-02-16 17:18:15 +00:00
|
|
|
lxc exec "${i}" -- "$@"
|
|
|
|
exit_val=$?
|
2020-02-18 17:20:03 +00:00
|
|
|
if [[ $exit_val -ne 0 ]]; then
|
2020-02-23 11:10:45 +00:00
|
|
|
warn_msg "[${_BBlue}${i}${_creset}] exit code (${_BRed}${exit_val}${_creset}) from ${_BGreen}${*}${_creset}"
|
|
|
|
else
|
2020-03-04 18:56:40 +00:00
|
|
|
info_msg "[${_BBlue}${i}${_creset}] exit code (${exit_val}) from ${_BGreen}${*}${_creset}"
|
2020-02-16 17:18:15 +00:00
|
|
|
fi
|
2020-03-04 18:56:40 +00:00
|
|
|
echo
|
2020-02-13 17:25:03 +00:00
|
|
|
done
|
|
|
|
;;
|
2020-02-26 18:07:55 +00:00
|
|
|
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}"
|
2020-02-27 18:13:03 +00:00
|
|
|
lxc exec -t "${i}" -- "${LXC_REPO_ROOT}/utils/lxc.sh" __install "$2" | prefix_stdout "[${i}] "
|
2020-02-26 18:07:55 +00:00
|
|
|
done
|
|
|
|
;;
|
|
|
|
*) usage "$_usage"; exit 42;;
|
2020-02-27 18:13:03 +00:00
|
|
|
esac
|
|
|
|
;;
|
|
|
|
__install)
|
|
|
|
case $2 in
|
|
|
|
searx-suite) searx_suite_install ;;
|
|
|
|
esac
|
|
|
|
;;
|
2020-03-02 18:00:19 +00:00
|
|
|
doc)
|
|
|
|
echo
|
|
|
|
echo ".. generic utils/lxc.sh documentation"
|
|
|
|
;;
|
|
|
|
|
2020-02-08 18:12:28 +00:00
|
|
|
*)
|
|
|
|
usage "unknown or missing command $1"; exit 42;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2020-02-27 18:13:03 +00:00
|
|
|
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
|
|
|
|
)
|
2020-02-26 18:07:55 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 18:13:03 +00:00
|
|
|
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
|
|
|
|
)
|
|
|
|
}
|
2020-02-26 18:07:55 +00:00
|
|
|
|
2020-02-08 18:12:28 +00:00
|
|
|
build_instances() {
|
|
|
|
rst_title "Build LXC instances"
|
2020-02-16 17:18:15 +00:00
|
|
|
|
|
|
|
rst_title "copy images" section
|
|
|
|
echo
|
2020-02-08 18:12:28 +00:00
|
|
|
lxc_copy_images_localy
|
2020-02-18 17:20:03 +00:00
|
|
|
# lxc image list local: && wait_key
|
2020-02-16 17:18:15 +00:00
|
|
|
echo
|
|
|
|
rst_title "build containers" section
|
|
|
|
echo
|
2020-02-08 18:12:28 +00:00
|
|
|
lxc_init_containers
|
2020-02-13 17:25:03 +00:00
|
|
|
lxc_config_containers
|
2020-02-16 17:18:15 +00:00
|
|
|
lxc_boilerplate_containers
|
|
|
|
echo
|
2020-02-13 17:25:03 +00:00
|
|
|
lxc list "$HOST_PREFIX"
|
2020-02-08 18:12:28 +00:00
|
|
|
}
|
|
|
|
|
2020-02-16 21:26:03 +00:00
|
|
|
remove_instances() {
|
|
|
|
rst_title "Remove LXC instances"
|
2020-02-18 17:20:03 +00:00
|
|
|
lxc list "$HOST_PREFIX"
|
2020-02-13 17:25:03 +00:00
|
|
|
echo -en "\\nLXC containers(s)::\\n\\n ${LOCAL_IMAGES[*]}\\n" | $FMT
|
2020-02-08 18:12:28 +00:00
|
|
|
if ask_yn "Do you really want to delete all images"; then
|
|
|
|
lxc_delete_containers
|
|
|
|
fi
|
2020-02-16 17:18:15 +00:00
|
|
|
echo
|
|
|
|
lxc list "$HOST_PREFIX"
|
2020-02-13 17:25:03 +00:00
|
|
|
# lxc image list local: && wait_key
|
2020-02-08 18:12:28 +00:00
|
|
|
}
|
|
|
|
|
2020-02-13 17:25:03 +00:00
|
|
|
# images
|
|
|
|
# ------
|
|
|
|
|
2020-02-08 18:12:28 +00:00
|
|
|
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]}"
|
|
|
|
else
|
|
|
|
info_msg "copy image locally ${TEST_IMAGES[i]} --> ${TEST_IMAGES[i+1]}"
|
|
|
|
lxc image copy "${TEST_IMAGES[i]}" local: \
|
2020-02-16 17:18:15 +00:00
|
|
|
--alias "${TEST_IMAGES[i+1]}" | prefix_stdout
|
2020-02-08 18:12:28 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
lxc_delete_images_localy() {
|
|
|
|
echo
|
|
|
|
for i in "${LOCAL_IMAGES[@]}"; do
|
|
|
|
info_msg "delete image 'local:$i'"
|
|
|
|
lxc image delete "local:$i"
|
|
|
|
done
|
|
|
|
#lxc image list local:
|
|
|
|
}
|
|
|
|
|
2020-02-13 17:25:03 +00:00
|
|
|
# container
|
|
|
|
# ---------
|
|
|
|
|
|
|
|
lxc_cmd() {
|
|
|
|
for i in "${LOCAL_IMAGES[@]}"; do
|
2020-02-16 17:18:15 +00:00
|
|
|
info_msg "lxc $* $i"
|
2020-02-18 17:20:03 +00:00
|
|
|
lxc "$@" "$i" | prefix_stdout "[${_BBlue}${i}${_creset}] "
|
2020-02-13 17:25:03 +00:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2020-02-08 18:12:28 +00:00
|
|
|
lxc_init_containers() {
|
2020-02-18 17:20:03 +00:00
|
|
|
|
|
|
|
local image_name
|
|
|
|
local container_name
|
|
|
|
|
2020-02-16 21:26:03 +00:00
|
|
|
for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do
|
2020-02-18 17:20:03 +00:00
|
|
|
|
|
|
|
image_name="${TEST_IMAGES[i+1]}"
|
|
|
|
container_name="${HOST_PREFIX}-${image_name}"
|
|
|
|
|
|
|
|
if lxc info "${container_name}" &>/dev/null; then
|
|
|
|
info_msg "container '${container_name}' already exists"
|
2020-02-08 18:12:28 +00:00
|
|
|
else
|
2020-02-18 17:20:03 +00:00
|
|
|
info_msg "create conatiner instance: ${container_name}"
|
|
|
|
lxc init "local:${image_name}" "${container_name}"
|
2020-02-08 18:12:28 +00:00
|
|
|
fi
|
|
|
|
done
|
2020-02-13 17:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lxc_config_containers() {
|
|
|
|
for i in "${LOCAL_IMAGES[@]}"; do
|
2020-02-18 17:20:03 +00:00
|
|
|
info_msg "[${_BBlue}${i}${_creset}] configure container ..."
|
2020-02-13 17:25:03 +00:00
|
|
|
|
2020-02-18 17:20:03 +00:00
|
|
|
info_msg "[${_BBlue}${i}${_creset}] map uid/gid from host to container"
|
2020-02-13 17:25:03 +00:00
|
|
|
# https://lxd.readthedocs.io/en/latest/userns-idmap/#custom-idmaps
|
|
|
|
echo -e -n "uid $HOST_USER_ID 1000\\ngid $HOST_GROUP_ID 1000"\
|
2020-02-16 17:18:15 +00:00
|
|
|
| lxc config set "$i" raw.idmap -
|
2020-02-13 17:25:03 +00:00
|
|
|
|
2020-02-18 17:20:03 +00:00
|
|
|
info_msg "[${_BBlue}${i}${_creset}] share ${REPO_ROOT} (repo_share) from HOST into container"
|
2020-02-13 17:25:03 +00:00
|
|
|
# https://lxd.readthedocs.io/en/latest/instances/#type-disk
|
2020-02-16 17:18:15 +00:00
|
|
|
lxc config device add "$i" repo_share disk \
|
2020-02-13 17:25:03 +00:00
|
|
|
source="${REPO_ROOT}" \
|
2020-02-26 18:07:55 +00:00
|
|
|
path="${LXC_REPO_ROOT}" &>/dev/null
|
2020-02-16 17:18:15 +00:00
|
|
|
# lxc config show "$i" && wait_key
|
|
|
|
done
|
|
|
|
}
|
2020-02-13 17:25:03 +00:00
|
|
|
|
2020-02-16 17:18:15 +00:00
|
|
|
lxc_boilerplate_containers() {
|
2020-02-18 17:20:03 +00:00
|
|
|
|
|
|
|
local image_name
|
|
|
|
local container_name
|
2020-02-16 17:18:15 +00:00
|
|
|
local boilerplate_script
|
2020-02-18 17:20:03 +00:00
|
|
|
|
2020-02-16 17:18:15 +00:00
|
|
|
for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do
|
2020-02-18 17:20:03 +00:00
|
|
|
|
|
|
|
image_name="${TEST_IMAGES[i+1]}"
|
|
|
|
container_name="${HOST_PREFIX}-${image_name}"
|
|
|
|
boilerplate_script="${image_name}_boilerplate"
|
2020-02-16 17:18:15 +00:00
|
|
|
boilerplate_script="${!boilerplate_script}"
|
2020-02-18 17:20:03 +00:00
|
|
|
|
|
|
|
info_msg "[${_BBlue}${container_name}${_creset}] install boilerplate"
|
|
|
|
if lxc start -q "${container_name}" &>/dev/null; then
|
|
|
|
sleep 5 # guest needs some time to come up and get an IP
|
|
|
|
fi
|
2020-02-16 19:07:37 +00:00
|
|
|
if [[ -n "${boilerplate_script}" ]]; then
|
2020-02-18 17:20:03 +00:00
|
|
|
echo "${boilerplate_script}" \
|
|
|
|
| lxc exec "${container_name}" -- bash \
|
|
|
|
| prefix_stdout "[${_BBlue}${container_name}${_creset}] "
|
2020-02-16 17:18:15 +00:00
|
|
|
else
|
2020-02-18 17:20:03 +00:00
|
|
|
err_msg "[${_BBlue}${container_name}${_creset}] no boilerplate for image '${image_name}'"
|
2020-02-16 17:18:15 +00:00
|
|
|
fi
|
2020-02-18 17:20:03 +00:00
|
|
|
|
2020-02-13 17:25:03 +00:00
|
|
|
done
|
2020-02-08 18:12:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lxc_delete_containers() {
|
|
|
|
for i in "${LOCAL_IMAGES[@]}"; do
|
2020-02-16 17:18:15 +00:00
|
|
|
if lxc info "$i" &>/dev/null; then
|
2020-02-18 17:20:03 +00:00
|
|
|
info_msg "stop & delete instance ${_BBlue}${i}${_creset}"
|
2020-02-16 17:18:15 +00:00
|
|
|
lxc stop "$i" &>/dev/null
|
|
|
|
lxc delete "$i" | prefix_stdout
|
2020-02-08 18:12:28 +00:00
|
|
|
else
|
2020-02-16 17:18:15 +00:00
|
|
|
warn_msg "instance '$i' does not exist / can't delete :o"
|
2020-02-08 18:12:28 +00:00
|
|
|
fi
|
|
|
|
done
|
2020-02-13 17:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# subordinates
|
|
|
|
# ------------
|
|
|
|
#
|
|
|
|
# see man: subgid(5), subuid(5), https://lxd.readthedocs.io/en/latest/userns-idmap
|
|
|
|
#
|
|
|
|
# E.g. in the HOST you have uid=1001(user) and/or gid=1001(user) ::
|
|
|
|
#
|
|
|
|
# root:1001:1
|
|
|
|
#
|
|
|
|
# in the CONTAINER::
|
|
|
|
#
|
|
|
|
# config:
|
|
|
|
# raw.idmap: |
|
|
|
|
# uid 1001 1000
|
|
|
|
# gid 1001 1000
|
|
|
|
|
|
|
|
add_subordinate_ids() {
|
|
|
|
if grep "root:${HOST_USER_ID}:1" /etc/subuid -qs; then
|
|
|
|
info_msg "lxd already has permission to map ${HOST_USER_ID}'s user/group id through"
|
|
|
|
else
|
|
|
|
info_msg "add lxd permission to map ${HOST_USER_ID}'s user/group id through"
|
|
|
|
usermod --add-subuids "${HOST_USER_ID}-${HOST_USER_ID}" \
|
|
|
|
--add-subgids "${HOST_GROUP_ID}-${HOST_GROUP_ID}" root
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
del_subordinate_ids() {
|
|
|
|
local out
|
2020-02-25 19:20:17 +00:00
|
|
|
local exit_val
|
2020-02-13 17:25:03 +00:00
|
|
|
if grep "root:${HOST_USER_ID}:1" /etc/subuid -qs; then
|
|
|
|
# TODO: root user is always in use by process 1, how can we remove subordinates?
|
|
|
|
info_msg "remove lxd permission to map ${HOST_USER_ID}'s user/group id through"
|
|
|
|
out=$(usermod --del-subuids "${HOST_USER_ID}-${HOST_USER_ID}" --del-subgids "${HOST_GROUP_ID}-${HOST_GROUP_ID}" root 2>&1)
|
2020-02-16 19:07:37 +00:00
|
|
|
exit_val=$?
|
|
|
|
if [ $exit_val -ne 0 ]; then
|
2020-02-13 17:25:03 +00:00
|
|
|
err_msg "$out"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
info_msg "lxd does not have permission to map ${HOST_USER_ID}'s user/group id through"
|
|
|
|
fi
|
2020-02-08 18:12:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
main "$@"
|
|
|
|
# ----------------------------------------------------------------------------
|