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 {}; \
45 rm -rf /boot/efi/*\"" || true
48 function cleanup_vms {
49 # clean up existing nodes
50 for node in $(virsh list --name | grep -P '\w{3}\d{2}'); do
51 virsh destroy "${node}"
53 for node in $(virsh list --name --all | grep -P '\w{3}\d{2}'); do
54 virsh domblklist "${node}" | awk '/^.da/ {print $2}' | \
55 xargs --no-run-if-empty -I{} sudo rm -f {}
56 virsh undefine "${node}" --remove-all-storage --nvram
60 function prepare_vms {
61 local base_image=$1; shift
62 local image_dir=$1; shift
67 get_base_image "${base_image}" "${image_dir}"
68 # shellcheck disable=SC2016
69 envsubst '${SALT_MASTER},${CLUSTER_DOMAIN}' < \
70 user-data.template > user-data.sh
72 for node in "${vnodes[@]}"; do
73 # create/prepare images
74 ./create-config-drive.sh -k "$(basename "${SSH_KEY}").pub" -u user-data.sh \
75 -h "${node}" "${image_dir}/mcp_${node}.iso"
76 cp "${image_dir}/${base_image/*\/}" "${image_dir}/mcp_${node}.qcow2"
77 qemu-img resize "${image_dir}/mcp_${node}.qcow2" 100G
81 function create_networks {
82 local vnode_networks=("$@")
83 # create required networks, including constant "mcpcontrol"
84 # FIXME(alav): since we renamed "pxe" to "mcpcontrol", we need to make sure
85 # we delete the old "pxe" virtual network, or it would cause IP conflicts.
86 # FIXME(alav): The same applies for "fuel1" virsh network.
87 for net in "fuel1" "pxe" "mcpcontrol" "${vnode_networks[@]}"; do
88 if virsh net-info "${net}" >/dev/null 2>&1; then
89 virsh net-destroy "${net}" || true
90 virsh net-undefine "${net}"
92 # in case of custom network, host should already have the bridge in place
93 if [ -f "net_${net}.xml" ] && [ ! -d "/sys/class/net/${net}/bridge" ]; then
94 virsh net-define "net_${net}.xml"
95 virsh net-autostart "${net}"
96 virsh net-start "${net}"
101 function create_vms {
102 local image_dir=$1; shift
103 IFS='|' read -r -a vnodes <<< "$1"; shift
104 local vnode_networks=("$@")
106 # AArch64: prepare arch specific arguments
107 local virt_extra_args=""
108 if [ "$(uname -i)" = "aarch64" ]; then
109 # No Cirrus VGA on AArch64, use virtio instead
110 virt_extra_args="$virt_extra_args --video=virtio"
113 # create vms with specified options
114 for serialized_vnode_data in "${vnodes[@]}"; do
115 IFS=',' read -r -a vnode_data <<< "${serialized_vnode_data}"
117 # prepare network args
118 net_args=" --network network=mcpcontrol,model=virtio"
119 if [ "${vnode_data[0]}" = "mas01" ]; then
120 # MaaS node's 3rd interface gets connected to PXE/Admin Bridge
121 vnode_networks[2]="${vnode_networks[0]}"
123 for net in "${vnode_networks[@]:1}"; do
124 net_args="${net_args} --network bridge=${net},model=virtio"
127 # shellcheck disable=SC2086
128 virt-install --name "${vnode_data[0]}" \
129 --ram "${vnode_data[1]}" --vcpus "${vnode_data[2]}" \
130 --cpu host-passthrough --accelerate ${net_args} \
131 --disk path="${image_dir}/mcp_${vnode_data[0]}.qcow2",format=qcow2,bus=virtio,cache=none,io=native \
132 --os-type linux --os-variant none \
133 --boot hd --vnc --console pty --autostart --noreboot \
134 --disk path="${image_dir}/mcp_${vnode_data[0]}.iso",device=cdrom \
140 function update_mcpcontrol_network {
141 # set static ip address for salt master node, MaaS node
142 # shellcheck disable=SC2155
143 local cmac=$(virsh domiflist cfg01 2>&1| awk '/mcpcontrol/ {print $5; exit}')
144 # shellcheck disable=SC2155
145 local amac=$(virsh domiflist mas01 2>&1| awk '/mcpcontrol/ {print $5; exit}')
146 virsh net-update "mcpcontrol" add ip-dhcp-host \
147 "<host mac='${cmac}' name='cfg01' ip='${SALT_MASTER}'/>" --live --config
148 [ -z "${amac}" ] || virsh net-update "mcpcontrol" add ip-dhcp-host \
149 "<host mac='${amac}' name='mas01' ip='${MAAS_IP}'/>" --live --config
156 for node in "${vnodes[@]}"; do
157 virsh start "${node}"
158 sleep $((RANDOM%5+1))
162 function check_connection {
163 local total_attempts=60
167 echo '[INFO] Attempting to get into Salt master ...'
169 # wait until ssh on Salt master is available
170 # shellcheck disable=SC2034
171 for attempt in $(seq "${total_attempts}"); do
172 # shellcheck disable=SC2086
173 ssh ${SSH_OPTS} "ubuntu@${SALT_MASTER}" uptime
175 0) echo "${attempt}> Success"; break ;;
176 *) echo "${attempt}/${total_attempts}> ssh server ain't ready yet, waiting for ${sleep_time} seconds ..." ;;
183 function parse_yaml {
190 fs="$(echo @|tr @ '\034')"
191 sed -e 's|---||g' -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
192 -e "s|^\($s\)\($w\)$s[:-]$s\(.*\)$s\$|\1$fs\2$fs\3|p" "$1" |
194 indent = length($1)/2;
196 for (i in vname) {if (i > indent) {delete vname[i]}}
197 if (length($3) > 0) {
198 vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
199 printf("%s%s%s=(\"%s\")\n", "'"$prefix"'",vn, $2, $3);
205 # Execute in a subshell to prevent local variable override during recursion
207 local total_attempts=$1; shift
210 echo "[NOTE] Waiting for cmd to return success: ${cmdstr}"
211 # shellcheck disable=SC2034
212 for attempt in $(seq "${total_attempts}"); do
213 # shellcheck disable=SC2015
214 eval "${cmdstr}" && return 0 || true
215 echo -n '.'; sleep "${sleep_time}"