2 ##############################################################################
3 # Copyright (c) 2017 Mirantis Inc., Enea AB and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
10 # Library of shell functions
13 function generate_ssh_key {
14 # shellcheck disable=SC2155
15 local mcp_ssh_key=$(basename "${SSH_KEY}")
17 if [ -n "${SUDO_USER}" ] && [ "${SUDO_USER}" != 'root' ]; then
21 if [ -f "${SSH_KEY}" ]; then
23 ssh-keygen -f "${mcp_ssh_key}" -y > "${mcp_ssh_key}.pub"
26 [ -f "${mcp_ssh_key}" ] || ssh-keygen -f "${mcp_ssh_key}" -N ''
27 sudo install -D -o "${user}" -m 0600 "${mcp_ssh_key}" "${SSH_KEY}"
30 function get_base_image {
34 mkdir -p "${image_dir}"
35 wget -P "${image_dir}" -N "${base_image}"
38 function cleanup_uefi {
39 # Clean up Ubuntu boot entry if cfg01, kvm nodes online from previous deploy
40 # shellcheck disable=SC2086
41 ssh ${SSH_OPTS} "${SSH_SALT}" "sudo salt -C 'kvm* or cmp*' cmd.run \
42 \"which efibootmgr > /dev/null 2>&1 && \
43 efibootmgr | grep -oP '(?<=Boot)[0-9]+(?=.*ubuntu)' | \
44 xargs -I{} efibootmgr --delete-bootnum --bootnum {}\"" || true
47 function cleanup_vms {
48 # clean up existing nodes
49 for node in $(virsh list --name | grep -P '\w{3}\d{2}'); do
50 virsh destroy "${node}"
52 for node in $(virsh list --name --all | grep -P '\w{3}\d{2}'); do
53 virsh domblklist "${node}" | awk '/^.da/ {print $2}' | \
54 xargs --no-run-if-empty -I{} sudo rm -f {}
55 virsh undefine "${node}" --remove-all-storage --nvram
59 function prepare_vms {
60 local base_image=$1; shift
61 local image_dir=$1; shift
66 get_base_image "${base_image}" "${image_dir}"
67 # shellcheck disable=SC2016
68 envsubst '${SALT_MASTER},${CLUSTER_DOMAIN}' < \
69 user-data.template > user-data.sh
71 for node in "${vnodes[@]}"; do
72 # create/prepare images
73 ./create-config-drive.sh -k "$(basename "${SSH_KEY}").pub" -u user-data.sh \
74 -h "${node}" "${image_dir}/mcp_${node}.iso"
75 cp "${image_dir}/${base_image/*\/}" "${image_dir}/mcp_${node}.qcow2"
76 qemu-img resize "${image_dir}/mcp_${node}.qcow2" 100G
80 function create_networks {
81 local vnode_networks=("$@")
82 # create required networks, including constant "mcpcontrol"
83 # FIXME(alav): since we renamed "pxe" to "mcpcontrol", we need to make sure
84 # we delete the old "pxe" virtual network, or it would cause IP conflicts.
85 # FIXME(alav): The same applies for "fuel1" virsh network.
86 for net in "fuel1" "pxe" "mcpcontrol" "${vnode_networks[@]}"; do
87 if virsh net-info "${net}" >/dev/null 2>&1; then
88 virsh net-destroy "${net}" || true
89 virsh net-undefine "${net}"
91 # in case of custom network, host should already have the bridge in place
92 if [ -f "net_${net}.xml" ] && [ ! -d "/sys/class/net/${net}/bridge" ]; then
93 virsh net-define "net_${net}.xml"
94 virsh net-autostart "${net}"
95 virsh net-start "${net}"
100 function create_vms {
101 local image_dir=$1; shift
102 IFS='|' read -r -a vnodes <<< "$1"; shift
103 local vnode_networks=("$@")
105 # AArch64: prepare arch specific arguments
106 local virt_extra_args=""
107 if [ "$(uname -i)" = "aarch64" ]; then
108 # No Cirrus VGA on AArch64, use virtio instead
109 virt_extra_args="$virt_extra_args --video=virtio"
112 # create vms with specified options
113 for serialized_vnode_data in "${vnodes[@]}"; do
114 IFS=',' read -r -a vnode_data <<< "${serialized_vnode_data}"
116 # prepare network args
117 net_args=" --network network=mcpcontrol,model=virtio"
118 if [ "${vnode_data[0]}" = "mas01" ]; then
119 # MaaS node's 3rd interface gets connected to PXE/Admin Bridge
120 vnode_networks[2]="${vnode_networks[0]}"
122 for net in "${vnode_networks[@]:1}"; do
123 net_args="${net_args} --network bridge=${net},model=virtio"
126 # shellcheck disable=SC2086
127 virt-install --name "${vnode_data[0]}" \
128 --ram "${vnode_data[1]}" --vcpus "${vnode_data[2]}" \
129 --cpu host-passthrough --accelerate ${net_args} \
130 --disk path="${image_dir}/mcp_${vnode_data[0]}.qcow2",format=qcow2,bus=virtio,cache=none,io=native \
131 --os-type linux --os-variant none \
132 --boot hd --vnc --console pty --autostart --noreboot \
133 --disk path="${image_dir}/mcp_${vnode_data[0]}.iso",device=cdrom \
139 function update_mcpcontrol_network {
140 # set static ip address for salt master node, MaaS node
141 # shellcheck disable=SC2155
142 local cmac=$(virsh domiflist cfg01 2>&1| awk '/mcpcontrol/ {print $5; exit}')
143 # shellcheck disable=SC2155
144 local amac=$(virsh domiflist mas01 2>&1| awk '/mcpcontrol/ {print $5; exit}')
145 virsh net-update "mcpcontrol" add ip-dhcp-host \
146 "<host mac='${cmac}' name='cfg01' ip='${SALT_MASTER}'/>" --live
147 [ -z "${amac}" ] || virsh net-update "mcpcontrol" add ip-dhcp-host \
148 "<host mac='${amac}' name='mas01' ip='${MAAS_IP}'/>" --live
155 for node in "${vnodes[@]}"; do
156 virsh start "${node}"
157 sleep $((RANDOM%5+1))
161 function check_connection {
162 local total_attempts=60
166 echo '[INFO] Attempting to get into Salt master ...'
168 # wait until ssh on Salt master is available
169 # shellcheck disable=SC2034
170 for attempt in $(seq "${total_attempts}"); do
171 # shellcheck disable=SC2086
172 ssh ${SSH_OPTS} "ubuntu@${SALT_MASTER}" uptime
174 0) echo "${attempt}> Success"; break ;;
175 *) echo "${attempt}/${total_attempts}> ssh server ain't ready yet, waiting for ${sleep_time} seconds ..." ;;
182 function parse_yaml {
189 fs="$(echo @|tr @ '\034')"
190 sed -e 's|---||g' -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
191 -e "s|^\($s\)\($w\)$s[:-]$s\(.*\)$s\$|\1$fs\2$fs\3|p" "$1" |
193 indent = length($1)/2;
195 for (i in vname) {if (i > indent) {delete vname[i]}}
196 if (length($3) > 0) {
197 vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
198 printf("%s%s%s=(\"%s\")\n", "'"$prefix"'",vn, $2, $3);
204 # Execute in a subshell to prevent local variable override during recursion
206 local total_attempts=$1; shift
209 echo "[NOTE] Waiting for cmd to return success: ${cmdstr}"
210 # shellcheck disable=SC2034
211 for attempt in $(seq "${total_attempts}"); do
212 # shellcheck disable=SC2015
213 eval "${cmdstr}" && return 0 || true
214 echo -n '.'; sleep "${sleep_time}"