From 9a645e19f8f73da3951a58b8d35accf871c6d2b5 Mon Sep 17 00:00:00 2001 From: pvincent Date: Wed, 28 Feb 2024 15:11:24 +0400 Subject: [PATCH] wait_for_command --- lib/functions.sh | 39 +++++++++++++++++++++++++++++++++++++++ lib/install.sh | 20 +++++++++++++++++--- scripts/lxc-miaou-create | 13 ++++--------- 3 files changed, 60 insertions(+), 12 deletions(-) diff --git a/lib/functions.sh b/lib/functions.sh index 27ae604..cf876a5 100644 --- a/lib/functions.sh +++ b/lib/functions.sh @@ -209,12 +209,43 @@ function arg2_required() { return 0 } +# test arg3 required +function arg3_required() { + [[ -z "${3:-}" ]] && echoerr "ERROR: arg#3 expected!" && return 125 || return 0 +} + # test whether container exists yet? function container_exists() { arg1_required "$@" lxc list "$1" -c n -f csv | grep -q "^$1\$" } +# 3 args expected: +function wait_for_command { + arg3_required "$@" + + command=$1 + delay=$2 + max_attempt=$3 + + attempt=0 + while ! eval "$command"; do + attempt=$((attempt + 1)) + if [[ $attempt -gt $max_attempt ]]; then + echoerr "command <$command> failed after a delay of $(bc <<<"$max_attempt * $delay")s and $max_attempt attempts" + return 1 + else + sleep "$delay" + fi + done + echo SUCCESS +} + +function wait_for_container_full_initialization { + arg1_required "$@" + wait_for_command "lxc exec $1 -- test -f /root/cloud-status.json" 0.2 40 +} + # build debian image with prebuild miaou-bash and various useful settings # ARG1=release [bullseye, buster] function build_miaou_image() { @@ -304,6 +335,14 @@ EOF1 fi } +# convert array to string according to IFS arg1 +# example: join "," "${MY_ARRAY[@]}" => one,two,three +function join() { + local IFS="$1" + shift + echo "$*" +} + # execute remote scripting onto one LXC container [COMMANDS, ...] # may use one command like: `lxc_exec ct1 uname -a` # or pipe like so: ` diff --git a/lib/install.sh b/lib/install.sh index e3622c3..93c2dc4 100755 --- a/lib/install.sh +++ b/lib/install.sh @@ -13,9 +13,9 @@ readonly NEW_GROUP EXPANDED_CONF on_exit() { if [ -n "${1:-}" ]; then - echo "Aborted by $1" + echowarn "Aborted by $1" elif [ "${status:-}" -ne 0 ]; then - echo "Failure (status $status)" + echowarn "Failure (status $status)" fi } @@ -387,6 +387,14 @@ function preload_bookworm_image { fi } +function is_email_valid { + [[ "$1" =~ ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$ ]] +} + +function auto_detect_email { + : +} + ### MAIN if [[ "${1:-}" == "SESSION_RELOAD_REQUIRED" ]]; then @@ -414,8 +422,14 @@ else TARGET=${1:-} CURRENT_USER=$(id -un) - check_target + is_email_valid "pvincent" && echo OK || echo NOPE + is_email_valid "pvincent@aa" && echo OK || echo NOPE + is_email_valid "pvincent@artcode." && echo OK || echo NOPE + is_email_valid "pvincent@artcode.re" && echo OK || echo NOPE + exit 1 + sudo_required + check_target install_miaou_bash install_mandatory_commands prepare_toolbox diff --git a/scripts/lxc-miaou-create b/scripts/lxc-miaou-create index c99d5fd..5a8c7ad 100755 --- a/scripts/lxc-miaou-create +++ b/scripts/lxc-miaou-create @@ -54,6 +54,9 @@ function create() { echo -n "creating new container <$CONTAINER> based on image <$CONTAINER_RELEASE>... " bridge_gw=$(lxc network get lxdbr0 ipv4.address | cut -d'/' -f1) + packages=(git file bc bash-completion) + [[ "$OPTION_SSH" == true ]] && packages+=(openssh-server) + packages_string=$(join ', ' "${packages[@]}") user_data="$( cat < @@ -159,10 +158,6 @@ EOF fi fi - if [[ "$OPTION_SSH" == true ]]; then - lxc exec "$CONTAINER" -- /opt/miaou-bash/tools/idem_apt_install openssh-server - fi - if [[ "$OPTION_SSH" == true && "$OPTION_SAMEUSER" == true ]]; then lxc-miaou-enable-ssh "$CONTAINER" fi