-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.sh
executable file
·75 lines (67 loc) · 1.68 KB
/
helper.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash
function create {
POOLID=$(pwgen 5 1); \
echo "Generated pool ID: $POOLID"; \
echo "Enter project name: "; \
read PROJNAME; \
sed 's/PROJNAME/'$PROJNAME'/' tmpl/variables.tf.tmpl > variables.tf && \
sed 's/POOLID/'$POOLID'/' tmpl/main.tf.tmpl > main.tf && \
sed 's/POOLID/'$POOLID'/' tmpl/vm.tf.tmpl.orig > tmpl/vm.tf.tmpl
echo "Terraform files updated correctly" && \
terraform init -upgrade
# creates vm.tf and tmp vm.tf.tmpl
}
function addvm {
# Prompt the user for variable values
read -p "Enter VM name (it will also be its hostname): " vmname ;\
read -p "Enter VM size in GB: " vmsize ;\
read -p "Enter VM CPU count: " vmcpu ;\
read -p "Enter VM RAM in GB: " vmram ;\
sed 's/VMNAMECHANGEME/'$vmname'/g' tmpl/vm.tf.tmpl > "${vmname}".tf &&\
sed -i 's/vmsize/'$vmsize'/g' "${vmname}".tf &&\
sed -i 's/vmcpu/'$vmcpu'/g' "${vmname}".tf &&\
sed -i 's/vmram/'$vmram'/g' "${vmname}".tf #&&\
# creates vmname.tf
terraform init -upgrade
}
function deploy {
terraform apply
}
function destroy {
terraform destroy
}
function clean {
terraform destroy
rm -rf tmpl/vm.tf.tmpl *.tf terraform.* .terraform.lock.hcl .terraform
}
if [ $# -ne 1 ]; then
echo "Usage: $0 [create|addvm|deploy|destroy|clean]"
exit 1
fi
case "$1" in
"create")
echo "Calling create function..."
create
;;
"addvm")
echo "Calling addvm function..."
addvm
;;
"deploy")
echo "Calling deploy function..."
deploy
;;
"destroy")
echo "Calling destroy function..."
destroy
;;
"clean")
echo "Calling clean function..."
clean
;;
*)
echo "Invalid argument: $1"
echo "Usage: $0 [create|addvm|deploy|destroy|clean]"
exit 1
;;
esac