-
Notifications
You must be signed in to change notification settings - Fork 0
/
bunch-up
executable file
·175 lines (157 loc) · 4.78 KB
/
bunch-up
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env bash
set -Eeuo pipefail
project_path=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
generated_path=generated
project_generator_path="${project_path}/platform/vm-generator"
generated_project_path="${project_generator_path}/${generated_path}"
__usage=$(
cat <<-HELPMSG
$(tput setaf 2)Usage:$(tput sgr0) $(tput bold)$0 [OPTIONS]$(tput sgr0)
$(tput setaf 3)$(tput bold)Manage machines provisioning.$(tput sgr0)
Options:
-a, --all $(tput setaf 8)Run all tasks to setup virtual machines (in order: generate create setup provision)$(tput sgr0)
-g, --generate $(tput setaf 8)Generate setup scripts$(tput sgr0)
-c, --create $(tput setaf 8)Create virtual machines$(tput sgr0)
-s, --setup $(tput setaf 8)Setup virtual machines$(tput sgr0)
-p, --provision $(tput setaf 8)Provision virtual machines$(tput sgr0)
-d, --delete $(tput setaf 8)Delete virtual machines$(tput sgr0)
-h, --help $(tput setaf 8)This help$(tput sgr0)
HELPMSG
)
usage() {
echo -e "$__usage"
}
if [ $# -lt 1 ]; then
usage
exit 1
fi
check_dependency() {
if ! [ -x "$(command -v "$1")" ]; then
echo -e "$(tput setaf 2)Error: $(tput bold)$1$(tput sgr0)$(tput setaf 2) is not installed.$(tput sgr0)" >&2
exit 1
fi
}
generate_ansible_ssh_keys() {
if [ -f "${generated_project_path}/.ssh/id_ed25519.pub" ]; then
echo "Using existing SSH keys for ansible ✅"
else
check_dependency 'ssh-keygen'
echo "Generating SSH keys for ansible ..."
mkdir -p "${generated_project_path}/.ssh"
ssh-keygen -t ed25519 -C "automator@iam-demo.test" -f "${generated_project_path}/.ssh/id_ed25519" -q -N ""
fi
}
generate_bootstrap_scripts() {
check_dependency 'jsonnet'
if [ -f "${project_generator_path}/config.libsonnet" ]; then
echo "Using existing $(tput bold)config.libsonnet$(tput sgr0) ✅"
else
echo "Missing config.libsonnet"
echo "Copying config.libsonnet.example to config.libsonnet"
cp "${project_generator_path}/config.libsonnet.example" "${project_generator_path}/config.libsonnet"
fi
cd "${project_generator_path}"
jsonnet --create-output-dirs \
--multi "${generated_project_path}" \
--tla-str orchestrator_name="multipass" \
--string virtual-machines.jsonnet
chmod u+x "${generated_project_path}"/*.sh
}
machines_creations() {
cd "${generated_project_path}"
./vms-create.sh
}
machines_setup() {
cd "${generated_project_path}"
./vms-setup.sh
}
machines_provisioning() {
cd "${generated_project_path}"
./vms-provisioning.sh
}
machines_delete() {
cd "${generated_project_path}"
./vms-destroy.sh
}
create=false
generate=false
provision=false
setup=false
delete=false
while [ $# -gt 0 ] && [ "$1" != "" ]; do
case $1 in
-a | --all)
shift
generate=true
create=true
setup=true
provision=true
;;
-g | --generate)
shift
generate=true
;;
-c | --create)
shift
create=true
;;
-s | --setup)
shift
setup=true
;;
-p | --provision)
shift
provision=true
;;
-d | --delete)
shift
delete=true
;;
-h | --help)
shift
usage
exit 0
;;
*)
printf "$(tput setaf 1)Unexpected argument: $(tput bold)%s$(tput sgr0)\n" "$1" >&2
usage
exit 1
;;
esac
done
if $delete; then
machines_delete
exit 0
fi
if $generate; then
echo "$(tput bold)$(tput setaf 2) - Creating virtual machines setup scripts ... - $(tput sgr0)"
generate_ansible_ssh_keys
generate_bootstrap_scripts
echo "$(tput bold)$(tput setaf 2) - VMs setup scripts generated ✅ - $(tput sgr0)"
fi
if $create; then
echo "$(tput bold)$(tput setaf 2) - Starting virtual machines creation ... - $(tput sgr0)"
machines_creations
echo "$(tput bold)$(tput setaf 2) - VMs creation done ✅ - $(tput sgr0)"
fi
if $setup; then
echo "$(tput bold)$(tput setaf 2) - Running virtual machines setup ... - $(tput sgr0)"
machines_setup
echo "$(tput bold)$(tput setaf 2) - VMs setup done ✅ - $(tput sgr0)"
fi
if $provision; then
echo "$(tput bold)$(tput setaf 2) - Starting virtual machine provisioning ... - $(tput sgr0)"
machines_provisioning
echo "$(tput bold)$(tput setaf 2) - VMs provisioning done ✅ - $(tput sgr0)"
fi
echo "$(tput bold)$(tput setaf 2) - All done ✅ - $(tput sgr0)"
echo
echo "$(tput setaf 3)Project path: $(tput bold)${project_path}$(tput sgr0)"
if $create || $setup || $provision; then
echo
echo "$(tput setaf 3)To ssh into a virtual machine use $(tput bold)vm-shell.sh <vm-name>$(tput sgr0) from the project path."
echo " $(tput bold)$(tput setaf 5)Example:$(tput sgr0)"
echo " $(tput bold)cd ${project_path}$(tput sgr0)"
echo " $(tput bold)./${generated_path}/vm-shell.sh ansible-controller$(tput sgr0)"
fi
exit 0