|
3 | 3 |
|
4 | 4 | workdir=./data
|
5 | 5 | mkdir -p "$workdir"
|
6 |
| -cd "$workdir" |
| 6 | +cd "$workdir" || exit $? |
7 | 7 | mkdir -p base vm tmp
|
8 | 8 |
|
| 9 | +function echo2 () { |
| 10 | + echo "$@" 1>&2 |
| 11 | +} |
9 | 12 | function generate_metadata () {
|
10 | 13 | local name=$1
|
11 | 14 | echo "local-hostname: $name"
|
@@ -78,28 +81,50 @@ function start_vm_if_not_running () {
|
78 | 81 | # create_vm_from "$1" 2 4G 50G __hardcoded__ r 1 "$2" __hardcoded__ || exit $?
|
79 | 82 | # echo "DEBUG: sshpass -p 1 ssh -p 30472 r@localhost"
|
80 | 83 |
|
81 |
| -while IFS= read -r line; do |
82 |
| - # Ignore lines starting with # |
83 |
| - if [[ "$line" =~ ^\# ]]; then |
84 |
| - continue |
85 |
| - fi |
86 |
| - |
87 |
| - # Trim leading and trailing whitespaces |
88 |
| - line=$(echo "$line" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') |
89 |
| - |
90 |
| - # Check if the line is non-empty |
91 |
| - if [ -n "$line" ]; then |
92 |
| - # Parse the line as "name;cloudimg;disk;username;password" |
93 |
| - IFS=';' read -r name cloudimg disk username password <<< "$line" |
94 |
| - |
95 |
| - # Print the parsed values |
96 |
| - echo "Name: $name" |
97 |
| - echo "Cloud Image: $cloudimg" |
98 |
| - echo "Disk: $disk" |
99 |
| - echo "Username: $username" |
100 |
| - echo "Password: $password" |
101 |
| - echo "---------------------" |
102 |
| - fi |
103 |
| -done < ./init.settings |
| 84 | +function do_init () { |
| 85 | + while IFS= read -r line; do |
| 86 | + # Check if the line is non-empty |
| 87 | + if [ -n "$line" ]; then |
| 88 | + # Parse the line as "name;cloudimg;disk;username;password", trim space |
| 89 | + IFS=';' read -r name cloudimg disk username password <<< "$(echo "$line" | tr -s '[:space:]' ';')" |
| 90 | + |
| 91 | + # Check if all fields are non-empty |
| 92 | + if [ -n "$name" ] && [ -n "$cloudimg" ] && [ -n "$disk" ] && [ -n "$username" ] && [ -n "$password" ]; then |
| 93 | + create_vm_if_not_exist "$name" "$cloudimg" "$disk" "$username" "$password" || echo2 "Failed to create_vm_if_not_exist. $?" |
| 94 | + else |
| 95 | + echo2 "Error: Bad configuration line: $line" |
| 96 | + fi |
| 97 | + fi |
| 98 | + done < ../init.settings |
| 99 | +} |
104 | 100 |
|
| 101 | +function do_start () { |
| 102 | + while IFS= read -r line; do |
| 103 | + # Ignore lines starting with # |
| 104 | + if [[ "$line" =~ ^\# ]]; then |
| 105 | + continue |
| 106 | + fi |
| 107 | + |
| 108 | + # Trim leading and trailing whitespaces |
| 109 | + line=$(echo "$line" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') |
| 110 | + |
| 111 | + # Check if the line is non-empty |
| 112 | + if [ -n "$line" ]; then |
| 113 | + # Parse the line as "name;options", only trim space in name, options can contain ; |
| 114 | + name=$(echo "$line" | sed -e 's/[[:space:]]*;.*$//' -e 's/^[[:space:]]*//') |
| 115 | + options=$(echo "$line" | sed 's/^[^:]*://') |
| 116 | + |
| 117 | + # Check if the name is empty |
| 118 | + if [ -n "$name" ]; then |
| 119 | + # Print the parsed values |
| 120 | + echo "Name: $name|" |
| 121 | + echo "Options: $options|" |
| 122 | + echo "---------------------" |
| 123 | + else |
| 124 | + # Print an error message for empty name |
| 125 | + echo "Error: Name is empty. Skipping the line." |
| 126 | + fi |
| 127 | + fi |
| 128 | + done < ../runtime.settings |
| 129 | +} |
105 | 130 |
|
0 commit comments