forked from desrod/maas-autobuilder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.sh
251 lines (199 loc) · 7.47 KB
/
functions.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/bin/bash
# how long you want to wait for commissioning
# default is 1200, i.e. 20 mins
state_timeout=1200
install_deps()
{
# Install some of the dependent packages
deps="jq"
if [[ "$0" =~ "manage-vm-nodes" ]] ; then
deps+=" virtinst"
fi
sudo apt -y update && sudo apt -y install ${deps}
# We install the snap, as maas-cli is not in distributions, this ensures
# that the package we invoke would be consistent
sudo snap install maas --channel=${maas_version}/stable
}
pkg_cleanup()
{
sudo snap remove maas maas-cli lxd
}
# Ensures that any dependent packages are installed for any MAAS CLI commands
# This also logs in to MAAS, and sets up the admin profile
maas_login()
{
# Login to MAAS using the API key and the endpoint
login=$(echo ${maas_api_key} | maas login ${maas_profile} ${maas_endpoint} -)
}
# Grabs the unique system_id for the host human readable hostname
maas_system_id()
{
node_name=$1
maas ${maas_profile} machines read hostname=${node_name} | jq -r ".[].system_id"
}
# Based on the nodename, finds the pod id, if it exists
maas_pod_id()
{
node_name=$1
maas ${maas_profile} pods read | jq -c ".[] | {pod_id:.id, hyp_name:.name}" | \
grep ${node_name} | jq -r ".pod_id"
}
machine_add_tag()
{
system_id=$1
tag=$2
[[ -n "$enable_tagging" ]] && [[ $enable_tagging == "false" ]] && return
# If the tag doesn't exist, then create it
if [[ $(maas ${maas_profile} tag read ${tag}) == "Not Found" ]] ; then
case $tag in
"pod-console-logging")
kernel_opts="console=tty1 console=ttyS0"
;;
*)
kernel_opts=""
;;
esac
tag_create=$(maas ${maas_profile} tags create name=${tag} kernel_opts=${kernel_opts})
fi
# Assign the tag to the machine
tag_update=$(maas ${maas_profile} tag update-nodes ${tag} add=${system_id})
}
machine_set_zone()
{
system_id=$1
zone=$2
if [[ $(maas ${maas_profile} zone read ${zone}) == "Not Found" ]] ; then
zone_create=$(maas ${maas_profile} zones create name=${zone})
fi
zone_set=$(maas ${maas_profile} machine update ${system_id} zone=${zone})
}
# This takes the system_id, and ensures that the machine is in $state state
# You may want to tweak the commission_timeout above in somehow it's failing
# and needs to be done quicker
ensure_machine_in_state()
{
system_id=$1
state=$2
# TODO: add a $3 to be able to customise the timeout
# timout= if [[ $3 == "" ]] ; then state_timeout else $3 ; fi
timeout=${state_timeout}
# The epoch time when this part started
time_start=$(date +%s)
# variable that will be used to check against for the timeout
time_end=${time_start}
# The initial state of the system
status_name=$(maas ${maas_profile} machine read ${system_id} | jq -r ".status_name")
# We will continue to check the state of the machine to see if it is in
# $state or the timeout has occured, which defaults to 20 mins
while [[ ${status_name} != "${state}" ]] && [[ $(( ${time_end} - ${time_start} )) -le ${timeout} ]]
do
# Check every 20 seconds of the state
sleep 20
# Grab the current state
status_name=$(maas ${maas_profile} machine read ${system_id} | jq -r ".status_name")
# Grab the current time to compare against
time_end=$(date +%s)
done
}
# Adds the VM into MAAS
maas_add_node()
{
node_name=$1
mac_addr=$2
node_type=$3
machine_type="vm"
[[ $node_type == "physical" ]] && machine_type="$node_type"
if [[ $machine_type == "vm" ]] ; then
power_type="virsh"
power_params="power_parameters_power_id=${node_name}"
power_params+=" power_parameters_power_address=qemu+ssh://${virsh_user}@${hypervisor_ip}/system"
power_params+=" power_parameters_power_pass=${qemu_password}"
else
power_type="manual"
power_params=""
fi
# Check if the system already exists
system_id=$(maas_system_id ${node_name})
# This command creates the machine in MAAS, if it doesn't already exist.
# This will then automatically turn the machines on, and start
# commissioning.
if [[ -z "$system_id" ]] ; then
machine_create=$(maas ${maas_profile} machines create \
hostname=${node_name} \
mac_addresses=${mac_addr} \
architecture=amd64/generic \
power_type=${power_type} ${power_params})
system_id=$(echo $machine_create | jq -r .system_id)
ensure_machine_in_state ${system_id} "Ready"
maas_assign_networks ${system_id}
else
boot_int=$(maas ${maas_profile} machine read ${system_id} | jq ".boot_interface | {mac:.mac_address, int_id:.id}")
if [[ $mac_addr != "$(echo $boot_int | jq .mac | sed s/\"//g)" ]] ; then
# A quick hack so that we can change the mac address of the interface.
# The machine needs to be broken, ready or allocated.
hack_commission=$(maas $maas_profile machine commission ${system_id})
hack_break=$(maas $maas_profile machine mark-broken ${system_id})
int_update=$(maas $maas_profile interface update ${system_id} $(echo $boot_int | jq -r .int_id) mac_address=${mac_addr})
fi
machine_power_update=$(maas ${maas_profile} machine update ${system_id} \
power_type=${power_type} ${power_params})
commission_node ${system_id}
fi
machine_add_tag ${system_id} ${node_type}
machine_set_zone ${system_id} ${hypervisor_name}
[[ $machine_type == "vm" ]] && machine_add_tag ${system_id} "pod-console-logging"
maas_create_partitions ${system_id}
}
add_dns_record()
{
record=$1
domain=$2
ip_addr=$3
domain_entry=$(add_domain $domain)
domain_id=$(echo $domain_entry | jq .id)
maas admin dnsresources read | jq -rc --arg record "landscape-internal" '.[] | select(.fqdn | contains($record)) |{fqdn:.fqdn,ip:.ip_addresses[].ip}'
}
add_domain()
{
domain=$1
domain_entry=$(maas ${maas_profile} domains read | jq -rc --arg domainname "${domain}" '.[] | select(.name == $domainname)')
[[ -z $domain_exists ]] && domain_entry=$(maas ${maas_profile} domains create name="${domain}" authoritative=true)
echo $domain_entry
}
commission_node()
{
system_id=$1
commission_machine=$(maas ${maas_profile} machine commission ${system_id})
# Ensure that the machine is in ready state before the next step
ensure_machine_in_state ${system_id} "Ready"
maas_assign_networks ${system_id}
}
read_configs()
{
configs=""
configs+=" configs/default.config"
configs+=" configs/maas.config"
if [[ "$0" =~ "manage-vm-nodes" ]] ; then
configs+=" configs/hypervisor.config"
fi
for config in $configs ; do
read_config $config
done
# Dynamically generate the node count
# The amount of memory add on 10% then divide by node_ram then add 1
# For a 32GB machine we'll get 10 VMs altogether
# 1 x 4GB juju, 1 x 8GB controler, 8 x 4GB compute
# The juju VM is not included in the count
node_count=$(( (( `cat /proc/meminfo | grep -i memtotal | awk '{print $2}'` - ( ${control_count} * ${control_ram} * 1024 )) * 11 / 10) / 1024 / ${node_ram} + (7*7/10) ))
}
read_config()
{
config=$1
if [ ! -f $config ]; then
printf "Error: missing config file. Please create the file '$config'.\n"
exit 1
else
shopt -s extglob
source "$config"
fi
}