From b96ff434f1c99bab2330b764c9c4813b6e65551b Mon Sep 17 00:00:00 2001 From: Recolic <git@me.recolic.net> Date: Mon, 15 Jan 2024 18:00:05 -0800 Subject: [PATCH] .WIP --- src/cron-callback.sh | 73 +++++++++++++++++++++++++++++--------------- src/init.settings | 2 +- src/runtime.settings | 8 ++--- 3 files changed, 54 insertions(+), 29 deletions(-) diff --git a/src/cron-callback.sh b/src/cron-callback.sh index 7e930f6..27b7cad 100644 --- a/src/cron-callback.sh +++ b/src/cron-callback.sh @@ -3,9 +3,12 @@ workdir=./data mkdir -p "$workdir" -cd "$workdir" +cd "$workdir" || exit $? mkdir -p base vm tmp +function echo2 () { + echo "$@" 1>&2 +} function generate_metadata () { local name=$1 echo "local-hostname: $name" @@ -78,28 +81,50 @@ function start_vm_if_not_running () { # create_vm_from "$1" 2 4G 50G __hardcoded__ r 1 "$2" __hardcoded__ || exit $? # echo "DEBUG: sshpass -p 1 ssh -p 30472 r@localhost" -while IFS= read -r line; do - # Ignore lines starting with # - if [[ "$line" =~ ^\# ]]; then - continue - fi - - # Trim leading and trailing whitespaces - line=$(echo "$line" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') - - # Check if the line is non-empty - if [ -n "$line" ]; then - # Parse the line as "name;cloudimg;disk;username;password" - IFS=';' read -r name cloudimg disk username password <<< "$line" - - # Print the parsed values - echo "Name: $name" - echo "Cloud Image: $cloudimg" - echo "Disk: $disk" - echo "Username: $username" - echo "Password: $password" - echo "---------------------" - fi -done < ./init.settings +function do_init () { + while IFS= read -r line; do + # Check if the line is non-empty + if [ -n "$line" ]; then + # Parse the line as "name;cloudimg;disk;username;password", trim space + IFS=';' read -r name cloudimg disk username password <<< "$(echo "$line" | tr -s '[:space:]' ';')" + + # Check if all fields are non-empty + if [ -n "$name" ] && [ -n "$cloudimg" ] && [ -n "$disk" ] && [ -n "$username" ] && [ -n "$password" ]; then + create_vm_if_not_exist "$name" "$cloudimg" "$disk" "$username" "$password" || echo2 "Failed to create_vm_if_not_exist. $?" + else + echo2 "Error: Bad configuration line: $line" + fi + fi + done < ../init.settings +} +function do_start () { + while IFS= read -r line; do + # Ignore lines starting with # + if [[ "$line" =~ ^\# ]]; then + continue + fi + + # Trim leading and trailing whitespaces + line=$(echo "$line" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') + + # Check if the line is non-empty + if [ -n "$line" ]; then + # Parse the line as "name;options", only trim space in name, options can contain ; + name=$(echo "$line" | sed -e 's/[[:space:]]*;.*$//' -e 's/^[[:space:]]*//') + options=$(echo "$line" | sed 's/^[^:]*://') + + # Check if the name is empty + if [ -n "$name" ]; then + # Print the parsed values + echo "Name: $name|" + echo "Options: $options|" + echo "---------------------" + else + # Print an error message for empty name + echo "Error: Name is empty. Skipping the line." + fi + fi + done < ../runtime.settings +} diff --git a/src/init.settings b/src/init.settings index be9b8fb..8ad9c9f 100644 --- a/src/init.settings +++ b/src/init.settings @@ -1,4 +1,4 @@ -# name;cloudimg;disk;username;password +# name;cloudimg;disk;username;password (space will be trimmed) instance1;focal-server-cloudimg-amd64.img;60G;r;1 gitlab-ci;focal-server-cloudimg-amd64.img;60G;r;1 httptest ;focal-server-cloudimg-amd64.img;60G;r;1 diff --git a/src/runtime.settings b/src/runtime.settings index fca2de2..792f892 100644 --- a/src/runtime.settings +++ b/src/runtime.settings @@ -1,4 +1,4 @@ -# name;options -instance1;focal-server-cloudimg-amd64.img;60G;-m 2G -smp 2 -vnc :11 -net user,hostfwd=tcp::30472-:22 -gitlab-ci;focal-server-cloudimg-amd64.img;40G;-m 4G -smp 4 -vnc :12 -net user,hostfwd=tcp::30473-:22 -httptest ;focal-server-cloudimg-amd64.img;20G;-m 1G -smp 1 -vnc :13 -net user,hostfwd=tcp::30474-:22 +# name;options (space in name will be trimmed) +instance1;-m 2G -smp 2 -vnc :11 -net user,hostfwd=tcp::30472-:22 +gitlab-ci;-m 4G -smp 4 -vnc :12 -net user,hostfwd=tcp::30473-:22 +httptest ;-m 1G -smp 1 -vnc :13 -net user,hostfwd=tcp::30474-:22 -- GitLab