From e8cf22504650f742da247bc923abebccec869676 Mon Sep 17 00:00:00 2001 From: Markus Heiser Date: Sun, 16 Feb 2020 18:18:15 +0100 Subject: [PATCH] LXC: tooling box - add boilerplate to containers Signed-off-by: Markus Heiser --- utils/lxc.sh | 109 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 80 insertions(+), 29 deletions(-) diff --git a/utils/lxc.sh b/utils/lxc.sh index 1762abd7c..d0ad49bd1 100755 --- a/utils/lxc.sh +++ b/utils/lxc.sh @@ -27,12 +27,18 @@ TEST_IMAGES=( #"$LINUXCONTAINERS_ORG_NAME:fedora/31" "fedora31" ) +ubu1804_boilerplate=" +export DEBIAN_FRONTEND=noninteractive +apt-get install -y git curl wget +" +ubu1904_boilerplate="$ubu1804_boilerplate" + REMOTE_IMAGES=() LOCAL_IMAGES=() for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do REMOTE_IMAGES=("${REMOTE_IMAGES[@]}" "${TEST_IMAGES[i]}") - LOCAL_IMAGES=("${LOCAL_IMAGES[@]}" "${TEST_IMAGES[i+1]}") + LOCAL_IMAGES=("${LOCAL_IMAGES[@]}" "${HOST_PREFIX}-${TEST_IMAGES[i+1]}") done HOST_USER="${SUDO_USER:-$USER}" @@ -72,9 +78,26 @@ EOF [ ! -z "${1+x}" ] && err_msg "$1" } +lxd_info() { + + cat </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: \ - --alias "${TEST_IMAGES[i+1]}" prefix_stdout + --alias "${TEST_IMAGES[i+1]}" | prefix_stdout fi done } @@ -175,53 +211,68 @@ lxc_delete_images_localy() { # --------- lxc_cmd() { - echo for i in "${LOCAL_IMAGES[@]}"; do - info_msg "lxc $* $HOST_PREFIX-$i" - lxc "$@" "$HOST_PREFIX-$i" + info_msg "lxc $* $i" + lxc "$@" "$i" done } lxc_init_containers() { - echo for i in "${LOCAL_IMAGES[@]}"; do - if lxc info "$HOST_PREFIX-$i" &>/dev/null; then - info_msg "conatiner '$HOST_PREFIX-$i' already exists" + if lxc info "$i" &>/dev/null; then + info_msg "conatiner '$i' already exists" else - info_msg "create conatiner instance: $HOST_PREFIX-$i" - lxc init "local:$i" "$HOST_PREFIX-$i" + info_msg "create conatiner instance: $i" + lxc init "local:$i" "$i" fi done } lxc_config_containers() { - echo for i in "${LOCAL_IMAGES[@]}"; do + info_msg "configure container: ${_BBlue}${i}${_creset}" - info_msg "map uid/gid from host to conatiner: $HOST_PREFIX-$i" + info_msg "map uid/gid from host to container" # https://lxd.readthedocs.io/en/latest/userns-idmap/#custom-idmaps echo -e -n "uid $HOST_USER_ID 1000\\ngid $HOST_GROUP_ID 1000"\ - | lxc config set "$HOST_PREFIX-$i" raw.idmap - + | lxc config set "$i" raw.idmap - - info_msg "share ${REPO_ROOT} (repo_share) from HOST into container: $HOST_PREFIX-$i" + info_msg "share ${REPO_ROOT} (repo_share) from HOST into container" # https://lxd.readthedocs.io/en/latest/instances/#type-disk - lxc config device add "$HOST_PREFIX-$i" repo_share disk \ + lxc config device add "$i" repo_share disk \ source="${REPO_ROOT}" \ - path="/share/$(basename "${REPO_ROOT}")" + path="/share/$(basename "${REPO_ROOT}")" &>/dev/null + # lxc config show "$i" && wait_key + done +} - # lxc config show "$HOST_PREFIX-$i" && wait_key +lxc_boilerplate_containers() { + local shortname + local boilerplate_script + for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do + shortname="${TEST_IMAGES[i+1]}" + info_msg "install boilerplate: ${_BBlue}${HOST_PREFIX}-${shortname}${_creset}" + lxc start -q "${HOST_PREFIX}-${shortname}" &>/dev/null + boilerplate_script="${shortname}_boilerplate" + boilerplate_script="${!boilerplate_script}" + if [[ ! -z "${boilerplate_script}" ]]; then + echo "$boilerplate_script" \ + | lxc exec "${HOST_PREFIX}-${shortname}" -- bash \ + | prefix_stdout " ${HOST_PREFIX}-${shortname} | " + else + warn_msg "no boilerplate for instance '$i'" + fi done } lxc_delete_containers() { - echo for i in "${LOCAL_IMAGES[@]}"; do - if lxc info "$HOST_PREFIX-$i" &>/dev/null; then - info_msg "stop & delete instance '$HOST_PREFIX-$i'" - lxc stop "$HOST_PREFIX-$i" &>/dev/null - lxc delete "$HOST_PREFIX-$i" | prefix_stdout + if lxc info "$i" &>/dev/null; then + info_msg "stop & delete instance '$i'" + lxc stop "$i" &>/dev/null + lxc delete "$i" | prefix_stdout else - warn_msg "instance '$HOST_PREFIX-$i' does not exist / can't delete :o" + warn_msg "instance '$i' does not exist / can't delete :o" fi done }