Skip to content

Commit b96ff43

Browse files
committed
.WIP
1 parent fd34e5a commit b96ff43

File tree

3 files changed

+54
-29
lines changed

3 files changed

+54
-29
lines changed

src/cron-callback.sh

+49-24
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33

44
workdir=./data
55
mkdir -p "$workdir"
6-
cd "$workdir"
6+
cd "$workdir" || exit $?
77
mkdir -p base vm tmp
88

9+
function echo2 () {
10+
echo "$@" 1>&2
11+
}
912
function generate_metadata () {
1013
local name=$1
1114
echo "local-hostname: $name"
@@ -78,28 +81,50 @@ function start_vm_if_not_running () {
7881
# create_vm_from "$1" 2 4G 50G __hardcoded__ r 1 "$2" __hardcoded__ || exit $?
7982
# echo "DEBUG: sshpass -p 1 ssh -p 30472 r@localhost"
8083

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+
}
104100

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+
}
105130

src/init.settings

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# name;cloudimg;disk;username;password
1+
# name;cloudimg;disk;username;password (space will be trimmed)
22
instance1;focal-server-cloudimg-amd64.img;60G;r;1
33
gitlab-ci;focal-server-cloudimg-amd64.img;60G;r;1
44
httptest ;focal-server-cloudimg-amd64.img;60G;r;1

src/runtime.settings

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# name;options
2-
instance1;focal-server-cloudimg-amd64.img;60G;-m 2G -smp 2 -vnc :11 -net user,hostfwd=tcp::30472-:22
3-
gitlab-ci;focal-server-cloudimg-amd64.img;40G;-m 4G -smp 4 -vnc :12 -net user,hostfwd=tcp::30473-:22
4-
httptest ;focal-server-cloudimg-amd64.img;20G;-m 1G -smp 1 -vnc :13 -net user,hostfwd=tcp::30474-:22
1+
# name;options (space in name will be trimmed)
2+
instance1;-m 2G -smp 2 -vnc :11 -net user,hostfwd=tcp::30472-:22
3+
gitlab-ci;-m 4G -smp 4 -vnc :12 -net user,hostfwd=tcp::30473-:22
4+
httptest ;-m 1G -smp 1 -vnc :13 -net user,hostfwd=tcp::30474-:22

0 commit comments

Comments
 (0)