Browse Source

wait_for_command

main
pvincent 7 months ago
parent
commit
9a645e19f8
  1. 39
      lib/functions.sh
  2. 20
      lib/install.sh
  3. 13
      scripts/lxc-miaou-create

39
lib/functions.sh

@ -209,12 +209,43 @@ function arg2_required() {
return 0 return 0
} }
# test arg3 required
function arg3_required() {
[[ -z "${3:-}" ]] && echoerr "ERROR: arg#3 expected!" && return 125 || return 0
}
# test whether container <ct> exists yet? # test whether container <ct> exists yet?
function container_exists() { function container_exists() {
arg1_required "$@" arg1_required "$@"
lxc list "$1" -c n -f csv | grep -q "^$1\$" lxc list "$1" -c n -f csv | grep -q "^$1\$"
} }
# 3 args expected: <commmand> <delay in s, example: 0.2> <max_attempts>
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 # build debian image with prebuild miaou-bash and various useful settings
# ARG1=release [bullseye, buster] # ARG1=release [bullseye, buster]
function build_miaou_image() { function build_miaou_image() {
@ -304,6 +335,14 @@ EOF1
fi 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 <CONTAINER> [COMMANDS, ...] # execute remote scripting onto one LXC container <CONTAINER> [COMMANDS, ...]
# may use one command like: `lxc_exec ct1 uname -a` # may use one command like: `lxc_exec ct1 uname -a`
# or pipe like so: ` # or pipe like so: `

20
lib/install.sh

@ -13,9 +13,9 @@ readonly NEW_GROUP EXPANDED_CONF
on_exit() { on_exit() {
if [ -n "${1:-}" ]; then if [ -n "${1:-}" ]; then
echo "Aborted by $1"
echowarn "Aborted by $1"
elif [ "${status:-}" -ne 0 ]; then elif [ "${status:-}" -ne 0 ]; then
echo "Failure (status $status)"
echowarn "Failure (status $status)"
fi fi
} }
@ -387,6 +387,14 @@ function preload_bookworm_image {
fi fi
} }
function is_email_valid {
[[ "$1" =~ ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$ ]]
}
function auto_detect_email {
:
}
### MAIN ### MAIN
if [[ "${1:-}" == "SESSION_RELOAD_REQUIRED" ]]; then if [[ "${1:-}" == "SESSION_RELOAD_REQUIRED" ]]; then
@ -414,8 +422,14 @@ else
TARGET=${1:-} TARGET=${1:-}
CURRENT_USER=$(id -un) 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 sudo_required
check_target
install_miaou_bash install_miaou_bash
install_mandatory_commands install_mandatory_commands
prepare_toolbox prepare_toolbox

13
scripts/lxc-miaou-create

@ -54,6 +54,9 @@ function create() {
echo -n "creating new container <$CONTAINER> based on image <$CONTAINER_RELEASE>... " echo -n "creating new container <$CONTAINER> based on image <$CONTAINER_RELEASE>... "
bridge_gw=$(lxc network get lxdbr0 ipv4.address | cut -d'/' -f1) 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="$( user_data="$(
cat <<EOF cat <<EOF
#cloud-config #cloud-config
@ -77,11 +80,7 @@ apt:
package_update: true package_update: true
package_upgrade: true package_upgrade: true
package_reboot_if_required: true package_reboot_if_required: true
packages:
- git
- file
- bc
- bash-completion
packages: $packages_string
write_files: write_files:
- path: /etc/sudoers.d/10-add_TOOLBOX_to_secure_path - path: /etc/sudoers.d/10-add_TOOLBOX_to_secure_path
content: > content: >
@ -159,10 +158,6 @@ EOF
fi fi
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 if [[ "$OPTION_SSH" == true && "$OPTION_SAMEUSER" == true ]]; then
lxc-miaou-enable-ssh "$CONTAINER" lxc-miaou-enable-ssh "$CONTAINER"
fi fi

Loading…
Cancel
Save