2020-02-08 19:12:28 +01: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-07 20:24:08 +01:00
|
|
|
|
|
|
|
# load environment of the LXC suite
|
|
|
|
LXC_ENV="${LXC_ENV:-${REPO_ROOT}/utils/lxc-searx.env}"
|
|
|
|
source "$LXC_ENV"
|
|
|
|
lxc_set_suite_env
|
2020-02-08 19:12:28 +01:00
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
# config
|
|
|
|
# ----------------------------------------------------------------------------
|
2020-02-13 18:25:03 +01:00
|
|
|
#
|
|
|
|
# read also:
|
|
|
|
# - https://lxd.readthedocs.io/en/latest/
|
2020-02-08 19:12:28 +01:00
|
|
|
|
2020-03-07 20:24:08 +01:00
|
|
|
LXC_HOST_PREFIX="${LXC_HOST_PREFIX:-test}"
|
2020-02-08 19:12:28 +01:00
|
|
|
|
2020-02-18 18:20:03 +01:00
|
|
|
# where all folders from HOST are mounted
|
|
|
|
LXC_SHARE_FOLDER="/share"
|
2020-02-26 19:07:55 +01:00
|
|
|
LXC_REPO_ROOT="${LXC_SHARE_FOLDER}/$(basename "${REPO_ROOT}")"
|
2020-02-18 18:20:03 +01:00
|
|
|
|
2020-03-07 20:24:08 +01:00
|
|
|
ubu1604_boilerplate="
|
2020-02-16 18:18:15 +01:00
|
|
|
export DEBIAN_FRONTEND=noninteractive
|
2020-02-18 18:40:34 +01:00
|
|
|
apt-get update -y
|
|
|
|
apt-get upgrade -y
|
2020-02-16 18:18:15 +01:00
|
|
|
apt-get install -y git curl wget
|
|
|
|
"
|
2020-03-07 20:24:08 +01:00
|
|
|
ubu1804_boilerplate="$ubu1604_boilerplate"
|
2020-02-16 18:18:15 +01:00
|
|
|
ubu1904_boilerplate="$ubu1804_boilerplate"
|
2020-03-02 19:01:08 +01:00
|
|
|
ubu1910_boilerplate="$ubu1904_boilerplate"
|
2020-03-07 20:24:08 +01:00
|
|
|
|
2020-03-02 19:01:08 +01:00
|
|
|
# shellcheck disable=SC2034
|
2020-03-07 20:24:08 +01:00
|
|
|
ubu2004_boilerplate="
|
|
|
|
$ubu1910_boilerplate
|
|
|
|
echo 'Set disable_coredump false' >> /etc/sudo.conf
|
|
|
|
"
|
2020-02-16 18:18:15 +01:00
|
|
|
|
2020-02-18 18:40:34 +01:00
|
|
|
# shellcheck disable=SC2034
|
|
|
|
archlinux_boilerplate="
|
|
|
|
pacman -Syu --noconfirm
|
2020-02-23 12:10:45 +01:00
|
|
|
pacman -S --noconfirm git curl wget sudo
|
|
|
|
echo 'Set disable_coredump false' >> /etc/sudo.conf
|
2020-02-18 18:40:34 +01:00
|
|
|
"
|
|
|
|
|
2020-02-23 12:10:45 +01:00
|
|
|
# shellcheck disable=SC2034
|
2020-02-18 18:40:34 +01:00
|
|
|
fedora31_boilerplate="
|
|
|
|
dnf update -y
|
2020-02-23 12:10:45 +01:00
|
|
|
dnf install -y git curl wget hostname
|
|
|
|
echo 'Set disable_coredump false' >> /etc/sudo.conf
|
2020-02-18 18:40:34 +01:00
|
|
|
"
|
|
|
|
|
2020-02-08 19:12:28 +01:00
|
|
|
REMOTE_IMAGES=()
|
2020-03-07 20:24:08 +01:00
|
|
|
CONTAINERS=()
|
2020-02-08 19:12:28 +01:00
|
|
|
LOCAL_IMAGES=()
|
|
|
|
|
2020-03-07 20:24:08 +01:00
|
|
|
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]}")
|
2020-02-08 19:12:28 +01:00
|
|
|
done
|
|
|
|
|
2020-02-13 18:25:03 +01:00
|
|
|
HOST_USER="${SUDO_USER:-$USER}"
|
|
|
|
HOST_USER_ID=$(id -u "${HOST_USER}")
|
|
|
|
HOST_GROUP_ID=$(id -g "${HOST_USER}")
|
|
|
|
|
2020-02-08 19:12:28 +01:00
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
usage() {
|
|
|
|
# ----------------------------------------------------------------------------
|
2020-03-07 20:24:08 +01:00
|
|
|
_cmd="$(basename "$0")"
|
2020-02-08 19:12:28 +01:00
|
|
|
cat <<EOF
|
|
|
|
|
|
|
|
usage::
|
|
|
|
|
2020-03-07 20:24:08 +01:00
|
|
|
$_cmd build [containers]
|
|
|
|
$_cmd copy [images]
|
2020-03-08 18:30:25 +01:00
|
|
|
$_cmd remove [containers|<name>|images]
|
2020-03-07 20:24:08 +01:00
|
|
|
$_cmd [start|stop] [containers|<name>]
|
|
|
|
$_cmd show [info|config|suite|images]
|
2020-03-08 18:30:25 +01:00
|
|
|
$_cmd cmd [--|<name>] '...'
|
2020-03-18 15:34:46 +01:00
|
|
|
$_cmd install [suite|buildhost]
|
2020-03-07 20:24:08 +01:00
|
|
|
|
|
|
|
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
|
2020-02-13 18:25:03 +01:00
|
|
|
start/stop
|
2020-03-07 20:24:08 +01:00
|
|
|
:containers: start/stop all 'containers' from the suite
|
|
|
|
:<name>: start/stop conatiner <name> from suite
|
2020-02-27 19:13:03 +01:00
|
|
|
show
|
2020-03-07 20:24:08 +01:00
|
|
|
: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
|
2020-03-08 18:30:25 +01:00
|
|
|
use single qoutes to evaluate in container's bash, e.g. 'echo $(hostname)'
|
|
|
|
-- run command '...' in all containers of the LXC suite
|
|
|
|
:<name>: run command '...' in container <name>
|
2020-02-26 19:07:55 +01:00
|
|
|
install
|
2020-03-15 17:01:36 +01:00
|
|
|
:suite: install LXC suite; ${lxc_suite_install_info}
|
|
|
|
:buildhost: prepare LXC; buildhost
|
2020-02-08 19:12:28 +01:00
|
|
|
|
2020-03-08 02:41:45 +01:00
|
|
|
EOF
|
|
|
|
usage_images
|
|
|
|
echo
|
|
|
|
usage_containers
|
|
|
|
echo
|
|
|
|
[ -n "${1+x}" ] && err_msg "$1"
|
|
|
|
}
|
2020-02-08 19:12:28 +01:00
|
|
|
|
2020-03-08 02:41:45 +01:00
|
|
|
usage_containers() {
|
|
|
|
cat <<EOF
|
|
|
|
LXC suite containers:
|
2020-03-07 20:24:08 +01:00
|
|
|
$(echo " ${CONTAINERS[*]}" | $FMT)
|
2020-02-08 19:12:28 +01:00
|
|
|
EOF
|
2020-02-16 20:07:37 +01:00
|
|
|
[ -n "${1+x}" ] && err_msg "$1"
|
2020-02-08 19:12:28 +01:00
|
|
|
}
|
|
|
|
|
2020-03-08 02:41:45 +01:00
|
|
|
usage_images() {
|
|
|
|
cat <<EOF
|
|
|
|
LXC suite images:
|
|
|
|
$(echo " ${LOCAL_IMAGES[*]}" | $FMT)
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2020-02-16 18:18:15 +01:00
|
|
|
lxd_info() {
|
|
|
|
|
|
|
|
cat <<EOF
|
|
|
|
|
|
|
|
LXD is needed, to install run::
|
|
|
|
|
|
|
|
snap install lxd
|
2020-02-16 22:26:03 +01:00
|
|
|
lxd init --auto
|
2020-02-16 18:18:15 +01:00
|
|
|
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2020-02-08 19:12:28 +01:00
|
|
|
main() {
|
|
|
|
|
2020-02-16 18:18:15 +01:00
|
|
|
local exit_val
|
2020-02-08 19:12:28 +01:00
|
|
|
local _usage="unknown or missing $1 command $2"
|
|
|
|
|
2020-03-07 20:24:08 +01:00
|
|
|
# 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
|
2020-02-27 19:13:03 +01:00
|
|
|
fi
|
2020-03-07 20:24:08 +01:00
|
|
|
|
2020-02-08 19:12:28 +01:00
|
|
|
case $1 in
|
|
|
|
--source-only) ;;
|
|
|
|
-h|--help) usage; exit 0;;
|
|
|
|
|
|
|
|
build)
|
|
|
|
sudo_or_exit
|
|
|
|
case $2 in
|
2020-03-07 20:24:08 +01:00
|
|
|
''|containers) build_instances ;;
|
|
|
|
*) usage "$_usage"; exit 42;;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
copy)
|
|
|
|
case $2 in
|
|
|
|
''|images) lxc_copy_images_localy;;
|
2020-02-08 19:12:28 +01:00
|
|
|
*) usage "$_usage"; exit 42;;
|
2020-02-27 19:13:03 +01:00
|
|
|
esac
|
|
|
|
;;
|
2020-02-16 22:26:03 +01:00
|
|
|
remove)
|
2020-02-08 19:12:28 +01:00
|
|
|
sudo_or_exit
|
|
|
|
case $2 in
|
2020-03-07 20:24:08 +01:00
|
|
|
''|containers) remove_instances ;;
|
|
|
|
images) lxc_delete_images_localy ;;
|
|
|
|
${LXC_HOST_PREFIX}-*)
|
2020-03-08 02:41:45 +01:00
|
|
|
! lxc_exists "$2" && usage_containers "unknown container: $2" && exit 42
|
2020-03-07 20:24:08 +01:00
|
|
|
if ask_yn "Do you really want to delete conatiner $2"; then
|
|
|
|
lxc_delete_container "$2"
|
|
|
|
fi
|
|
|
|
;;
|
2020-03-08 02:41:45 +01:00
|
|
|
*) usage "uknown or missing container <name> $2"; exit 42;;
|
2020-02-27 19:13:03 +01:00
|
|
|
esac
|
|
|
|
;;
|
2020-02-13 18:25:03 +01:00
|
|
|
start|stop)
|
|
|
|
sudo_or_exit
|
|
|
|
case $2 in
|
2020-03-07 20:24:08 +01:00
|
|
|
''|containers) lxc_cmd "$1" ;;
|
|
|
|
${LXC_HOST_PREFIX}-*)
|
2020-03-08 02:41:45 +01:00
|
|
|
! lxc_exists "$2" && usage_containers "unknown container: $2" && exit 42
|
2020-02-18 18:20:03 +01:00
|
|
|
info_msg "lxc $1 $2"
|
|
|
|
lxc "$1" "$2" | prefix_stdout "[${_BBlue}${i}${_creset}] "
|
|
|
|
;;
|
2020-03-08 02:41:45 +01:00
|
|
|
*) usage "uknown or missing container <name> $2"; exit 42;;
|
2020-02-27 19:13:03 +01:00
|
|
|
esac
|
|
|
|
;;
|
|
|
|
show)
|
2020-02-13 18:25:03 +01:00
|
|
|
sudo_or_exit
|
|
|
|
case $2 in
|
2020-03-07 20:24:08 +01:00
|
|
|
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
|
2020-02-27 19:13:03 +01:00
|
|
|
;;
|
2020-02-13 18:25:03 +01:00
|
|
|
*) usage "$_usage"; exit 42;;
|
2020-02-27 19:13:03 +01:00
|
|
|
esac
|
|
|
|
;;
|
|
|
|
__show)
|
|
|
|
case $2 in
|
2020-03-07 20:24:08 +01:00
|
|
|
suite) lxc_suite_info ;;
|
2020-02-27 19:13:03 +01:00
|
|
|
esac
|
|
|
|
;;
|
2020-02-13 18:25:03 +01:00
|
|
|
cmd)
|
|
|
|
sudo_or_exit
|
|
|
|
shift
|
2020-03-07 20:24:08 +01:00
|
|
|
case $1 in
|
2020-03-15 17:01:36 +01:00
|
|
|
--) shift; lxc_exec "$@" ;;
|
2020-03-07 20:24:08 +01:00
|
|
|
${LXC_HOST_PREFIX}-*)
|
2020-03-08 02:41:45 +01:00
|
|
|
! lxc_exists "$1" && usage_containers "unknown container: $1" && exit 42
|
2020-03-07 20:24:08 +01:00
|
|
|
local name=$1
|
|
|
|
shift
|
|
|
|
lxc_exec_cmd "${name}" "$@"
|
|
|
|
;;
|
2020-03-09 01:37:26 +01:00
|
|
|
*) usage "uknown or missing container <name> $1"; exit 42;;
|
2020-03-07 20:24:08 +01:00
|
|
|
esac
|
2020-02-13 18:25:03 +01:00
|
|
|
;;
|
2020-02-26 19:07:55 +01:00
|
|
|
install)
|
|
|
|
sudo_or_exit
|
|
|
|
case $2 in
|
2020-03-15 17:01:36 +01:00
|
|
|
suite) lxc_exec "${LXC_REPO_ROOT}/utils/lxc.sh" __install suite;;
|
|
|
|
buildhost) lxc_exec "${LXC_REPO_ROOT}/utils/lxc.sh" __install buildhost;;
|
2020-03-07 20:24:08 +01:00
|
|
|
*) usage "$_usage"; exit 42 ;;
|
2020-02-27 19:13:03 +01:00
|
|
|
esac
|
|
|
|
;;
|
|
|
|
__install)
|
|
|
|
case $2 in
|
2020-03-07 20:24:08 +01:00
|
|
|
suite) lxc_suite_install ;;
|
2020-03-15 17:01:36 +01:00
|
|
|
buildhost) lxc_suite_prepare_buildhost ;;
|
2020-02-27 19:13:03 +01:00
|
|
|
esac
|
|
|
|
;;
|
2020-03-02 19:00:19 +01:00
|
|
|
doc)
|
|
|
|
echo
|
|
|
|
echo ".. generic utils/lxc.sh documentation"
|
|
|
|
;;
|
2020-03-07 20:24:08 +01:00
|
|
|
-*) usage "unknown option $1"; exit 42;;
|
|
|
|
*) usage "unknown or missing command $1"; exit 42;;
|
2020-02-08 19:12:28 +01:00
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2020-02-26 19:07:55 +01:00
|
|
|
|
2020-02-08 19:12:28 +01:00
|
|
|
build_instances() {
|
|
|
|
rst_title "Build LXC instances"
|
|
|
|
lxc_copy_images_localy
|
2020-02-16 18:18:15 +01:00
|
|
|
echo
|
|
|
|
rst_title "build containers" section
|
|
|
|
echo
|
2020-02-08 19:12:28 +01:00
|
|
|
lxc_init_containers
|
2020-02-13 18:25:03 +01:00
|
|
|
lxc_config_containers
|
2020-02-16 18:18:15 +01:00
|
|
|
lxc_boilerplate_containers
|
|
|
|
echo
|
2020-03-07 20:24:08 +01:00
|
|
|
lxc list "$LXC_HOST_PREFIX"
|
2020-02-08 19:12:28 +01:00
|
|
|
}
|
|
|
|
|
2020-02-16 22:26:03 +01:00
|
|
|
remove_instances() {
|
|
|
|
rst_title "Remove LXC instances"
|
2020-03-07 20:24:08 +01:00
|
|
|
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
|
2020-02-08 19:12:28 +01:00
|
|
|
fi
|
2020-02-16 18:18:15 +01:00
|
|
|
echo
|
2020-03-07 20:24:08 +01:00
|
|
|
lxc list "$LXC_HOST_PREFIX-"
|
2020-02-08 19:12:28 +01:00
|
|
|
}
|
|
|
|
|
2020-02-13 18:25:03 +01:00
|
|
|
# images
|
|
|
|
# ------
|
|
|
|
|
2020-02-08 19:12:28 +01:00
|
|
|
lxc_copy_images_localy() {
|
2020-03-07 20:24:08 +01:00
|
|
|
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]}"
|
2020-02-08 19:12:28 +01:00
|
|
|
else
|
2020-03-07 20:24:08 +01:00
|
|
|
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
|
2020-02-08 19:12:28 +01:00
|
|
|
fi
|
|
|
|
done
|
2020-03-07 20:24:08 +01:00
|
|
|
# lxc image list local: && wait_key
|
2020-02-08 19:12:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lxc_delete_images_localy() {
|
2020-03-07 20:24:08 +01:00
|
|
|
rst_title "Delete LXC images"
|
|
|
|
rst_para "local existing images"
|
2020-02-08 19:12:28 +01:00
|
|
|
echo
|
2020-03-07 20:24:08 +01:00
|
|
|
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
|
2020-02-08 19:12:28 +01:00
|
|
|
for i in "${LOCAL_IMAGES[@]}"; do
|
2020-03-07 20:24:08 +01:00
|
|
|
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
|
2020-02-08 19:12:28 +01:00
|
|
|
done
|
2020-03-07 20:24:08 +01:00
|
|
|
|
2020-02-08 19:12:28 +01:00
|
|
|
}
|
|
|
|
|
2020-03-07 20:24:08 +01:00
|
|
|
|
2020-02-13 18:25:03 +01:00
|
|
|
# container
|
|
|
|
# ---------
|
|
|
|
|
2020-03-07 20:24:08 +01:00
|
|
|
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}] "
|
2020-03-08 02:41:45 +01:00
|
|
|
echo
|
2020-03-07 20:24:08 +01:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2020-02-13 18:25:03 +01:00
|
|
|
lxc_cmd() {
|
2020-03-07 20:24:08 +01:00
|
|
|
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
|
2020-02-13 18:25:03 +01:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2020-03-07 20:24:08 +01:00
|
|
|
lxc_exec_cmd() {
|
|
|
|
local name="$1"
|
|
|
|
shift
|
|
|
|
exit_val=
|
|
|
|
info_msg "[${_BBlue}${name}${_creset}] ${_BGreen}${*}${_creset}"
|
2020-03-15 17:01:36 +01:00
|
|
|
lxc exec -t --cwd "${LXC_REPO_ROOT}" "${name}" -- bash -c "$*"
|
2020-03-07 20:24:08 +01:00
|
|
|
exit_val=$?
|
|
|
|
if [[ $exit_val -ne 0 ]]; then
|
2020-03-08 02:41:45 +01:00
|
|
|
warn_msg "[${_BBlue}${name}${_creset}] exit code (${_BRed}${exit_val}${_creset}) from ${_BGreen}${*}${_creset}"
|
2020-03-07 20:24:08 +01:00
|
|
|
else
|
2020-03-08 02:41:45 +01:00
|
|
|
info_msg "[${_BBlue}${name}${_creset}] exit code (${exit_val}) from ${_BGreen}${*}${_creset}"
|
2020-03-07 20:24:08 +01:00
|
|
|
fi
|
2020-03-15 17:01:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lxc_exec() {
|
|
|
|
for i in "${CONTAINERS[@]}"; do
|
|
|
|
if ! lxc_exists "$i"; then
|
|
|
|
warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
|
|
|
|
else
|
|
|
|
lxc_exec_cmd "${i}" "$@" | prefix_stdout "[${_BBlue}${i}${_creset}] "
|
|
|
|
fi
|
|
|
|
done
|
2020-03-07 20:24:08 +01:00
|
|
|
}
|
|
|
|
|
2020-02-08 19:12:28 +01:00
|
|
|
lxc_init_containers() {
|
2020-02-18 18:20:03 +01:00
|
|
|
|
|
|
|
local image_name
|
|
|
|
local container_name
|
|
|
|
|
2020-03-07 20:24:08 +01:00
|
|
|
for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
|
2020-02-18 18:20:03 +01:00
|
|
|
|
2020-03-07 20:24:08 +01:00
|
|
|
image_name="${LXC_SUITE[i+1]}"
|
|
|
|
container_name="${LXC_HOST_PREFIX}-${image_name}"
|
2020-02-18 18:20:03 +01:00
|
|
|
|
|
|
|
if lxc info "${container_name}" &>/dev/null; then
|
|
|
|
info_msg "container '${container_name}' already exists"
|
2020-02-08 19:12:28 +01:00
|
|
|
else
|
2020-02-18 18:20:03 +01:00
|
|
|
info_msg "create conatiner instance: ${container_name}"
|
|
|
|
lxc init "local:${image_name}" "${container_name}"
|
2020-02-08 19:12:28 +01:00
|
|
|
fi
|
|
|
|
done
|
2020-02-13 18:25:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lxc_config_containers() {
|
2020-03-07 20:24:08 +01:00
|
|
|
for i in "${CONTAINERS[@]}"; do
|
2020-02-18 18:20:03 +01:00
|
|
|
info_msg "[${_BBlue}${i}${_creset}] configure container ..."
|
2020-02-13 18:25:03 +01:00
|
|
|
|
2020-02-18 18:20:03 +01:00
|
|
|
info_msg "[${_BBlue}${i}${_creset}] map uid/gid from host to container"
|
2020-02-13 18:25:03 +01:00
|
|
|
# https://lxd.readthedocs.io/en/latest/userns-idmap/#custom-idmaps
|
2020-03-08 18:30:25 +01:00
|
|
|
echo -e -n "uid $HOST_USER_ID 0\\ngid $HOST_GROUP_ID 0"\
|
2020-02-16 18:18:15 +01:00
|
|
|
| lxc config set "$i" raw.idmap -
|
2020-02-13 18:25:03 +01:00
|
|
|
|
2020-02-18 18:20:03 +01:00
|
|
|
info_msg "[${_BBlue}${i}${_creset}] share ${REPO_ROOT} (repo_share) from HOST into container"
|
2020-02-13 18:25:03 +01:00
|
|
|
# https://lxd.readthedocs.io/en/latest/instances/#type-disk
|
2020-02-16 18:18:15 +01:00
|
|
|
lxc config device add "$i" repo_share disk \
|
2020-02-13 18:25:03 +01:00
|
|
|
source="${REPO_ROOT}" \
|
2020-02-26 19:07:55 +01:00
|
|
|
path="${LXC_REPO_ROOT}" &>/dev/null
|
2020-02-16 18:18:15 +01:00
|
|
|
# lxc config show "$i" && wait_key
|
|
|
|
done
|
|
|
|
}
|
2020-02-13 18:25:03 +01:00
|
|
|
|
2020-02-16 18:18:15 +01:00
|
|
|
lxc_boilerplate_containers() {
|
2020-02-18 18:20:03 +01:00
|
|
|
|
|
|
|
local image_name
|
|
|
|
local container_name
|
2020-02-16 18:18:15 +01:00
|
|
|
local boilerplate_script
|
2020-02-18 18:20:03 +01:00
|
|
|
|
2020-03-07 20:24:08 +01:00
|
|
|
for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
|
2020-02-18 18:20:03 +01:00
|
|
|
|
2020-03-07 20:24:08 +01:00
|
|
|
image_name="${LXC_SUITE[i+1]}"
|
|
|
|
container_name="${LXC_HOST_PREFIX}-${image_name}"
|
2020-02-18 18:20:03 +01:00
|
|
|
boilerplate_script="${image_name}_boilerplate"
|
2020-02-16 18:18:15 +01:00
|
|
|
boilerplate_script="${!boilerplate_script}"
|
2020-02-18 18:20:03 +01:00
|
|
|
|
2020-03-18 15:34:46 +01:00
|
|
|
info_msg "[${_BBlue}${container_name}${_creset}] init .."
|
2020-03-15 17:01:36 +01:00
|
|
|
if lxc start -q "${container_name}" &>/dev/null; then
|
|
|
|
sleep 5 # guest needs some time to come up and get an IP
|
|
|
|
fi
|
2020-03-18 15:34:46 +01:00
|
|
|
lxc_init_container "${container_name}"
|
|
|
|
info_msg "[${_BBlue}${container_name}${_creset}] install /.lxcenv.mk .."
|
2020-03-15 17:01:36 +01:00
|
|
|
cat <<EOF | lxc exec "${container_name}" -- bash | prefix_stdout "[${_BBlue}${container_name}${_creset}] "
|
|
|
|
rm -f "/.lxcenv.mk"
|
|
|
|
ln -s "${LXC_REPO_ROOT}/utils/makefile.lxc" "/.lxcenv.mk"
|
|
|
|
ls -l "/.lxcenv.mk"
|
|
|
|
EOF
|
|
|
|
|
|
|
|
info_msg "[${_BBlue}${container_name}${_creset}] install boilerplate .."
|
2020-02-18 18:20:03 +01:00
|
|
|
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 20:07:37 +01:00
|
|
|
if [[ -n "${boilerplate_script}" ]]; then
|
2020-02-18 18:20:03 +01:00
|
|
|
echo "${boilerplate_script}" \
|
|
|
|
| lxc exec "${container_name}" -- bash \
|
|
|
|
| prefix_stdout "[${_BBlue}${container_name}${_creset}] "
|
2020-02-16 18:18:15 +01:00
|
|
|
else
|
2020-02-18 18:20:03 +01:00
|
|
|
err_msg "[${_BBlue}${container_name}${_creset}] no boilerplate for image '${image_name}'"
|
2020-02-16 18:18:15 +01:00
|
|
|
fi
|
2020-02-18 18:20:03 +01:00
|
|
|
|
2020-02-13 18:25:03 +01:00
|
|
|
done
|
2020-02-08 19:12:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
main "$@"
|
|
|
|
# ----------------------------------------------------------------------------
|