diff --git a/src/cron-callback.sh b/src/cron-callback.sh index 7e930f65e2bbc910f8b034841b91e42878cbc0dd..27b7cadef485e5feafe32b98f9b5851fa5d921b5 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 be9b8fbce1388560d8848543ce5bc9050208f9e9..8ad9c9f2eace627550251e057ecda0b7e03fe5e8 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 fca2de23e7dda152534edf2e5a663e496afb532a..792f8927f18140e463889b66ba5b08684158faf2 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