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_vms {
39 # clean up existing nodes
40 for node in $(virsh list --name | grep -P '\w{3}\d{2}'); do
41 virsh destroy "${node}"
43 for node in $(virsh list --name --all | grep -P '\w{3}\d{2}'); do
44 virsh domblklist "${node}" | awk '/^.da/ {print $2}' | \
45 xargs --no-run-if-empty -I{} sudo rm -f {}
46 virsh undefine "${node}" --remove-all-storage --nvram
50 function prepare_vms {
51 local base_image=$1; shift
52 local image_dir=$1; shift
56 get_base_image "${base_image}" "${image_dir}"
57 # shellcheck disable=SC2016
58 envsubst '${SALT_MASTER},${CLUSTER_DOMAIN}' < \
59 user-data.template > user-data.sh
61 for node in "${vnodes[@]}"; do
62 # create/prepare images
63 ./create-config-drive.sh -k "$(basename "${SSH_KEY}").pub" -u user-data.sh \
64 -h "${node}" "${image_dir}/mcp_${node}.iso"
65 cp "${image_dir}/${base_image/*\/}" "${image_dir}/mcp_${node}.qcow2"
66 qemu-img resize "${image_dir}/mcp_${node}.qcow2" 100G
70 function create_networks {
71 local vnode_networks=("$@")
72 # create required networks, including constant "mcpcontrol"
73 # FIXME(alav): since we renamed "pxe" to "mcpcontrol", we need to make sure
74 # we delete the old "pxe" virtual network, or it would cause IP conflicts.
75 # FIXME(alav): The same applies for "fuel1" virsh network.
76 for net in "fuel1" "pxe" "mcpcontrol" "${vnode_networks[@]}"; do
77 if virsh net-info "${net}" >/dev/null 2>&1; then
78 virsh net-destroy "${net}" || true
79 virsh net-undefine "${net}"
81 # in case of custom network, host should already have the bridge in place
82 if [ -f "net_${net}.xml" ] && [ ! -d "/sys/class/net/${net}/bridge" ]; then
83 virsh net-define "net_${net}.xml"
84 virsh net-autostart "${net}"
85 virsh net-start "${net}"
91 local image_dir=$1; shift
92 IFS='|' read -r -a vnodes <<< "$1"; shift
93 local vnode_networks=("$@")
95 # AArch64: prepare arch specific arguments
96 local virt_extra_args=""
97 if [ "$(uname -i)" = "aarch64" ]; then
98 # No Cirrus VGA on AArch64, use virtio instead
99 virt_extra_args="$virt_extra_args --video=virtio"
102 # create vms with specified options
103 for serialized_vnode_data in "${vnodes[@]}"; do
104 IFS=',' read -r -a vnode_data <<< "${serialized_vnode_data}"
106 # prepare network args
107 net_args=" --network network=mcpcontrol,model=virtio"
108 if [ "${vnode_data[0]}" = "mas01" ]; then
109 # MaaS node's 3rd interface gets connected to PXE/Admin Bridge
110 vnode_networks[2]="${vnode_networks[0]}"
112 for net in "${vnode_networks[@]:1}"; do
113 net_args="${net_args} --network bridge=${net},model=virtio"
116 # shellcheck disable=SC2086
117 virt-install --name "${vnode_data[0]}" \
118 --ram "${vnode_data[1]}" --vcpus "${vnode_data[2]}" \
119 --cpu host-passthrough --accelerate ${net_args} \
120 --disk path="${image_dir}/mcp_${vnode_data[0]}.qcow2",format=qcow2,bus=virtio,cache=none,io=native \
121 --os-type linux --os-variant none \
122 --boot hd --vnc --console pty --autostart --noreboot \
123 --disk path="${image_dir}/mcp_${vnode_data[0]}.iso",device=cdrom \
129 function update_mcpcontrol_network {
130 # set static ip address for salt master node, MaaS node
131 # shellcheck disable=SC2155
132 local cmac=$(virsh domiflist cfg01 2>&1| awk '/mcpcontrol/ {print $5; exit}')
133 # shellcheck disable=SC2155
134 local amac=$(virsh domiflist mas01 2>&1| awk '/mcpcontrol/ {print $5; exit}')
135 virsh net-update "mcpcontrol" add ip-dhcp-host \
136 "<host mac='${cmac}' name='cfg01' ip='${SALT_MASTER}'/>" --live
137 [ -z "${amac}" ] || virsh net-update "mcpcontrol" add ip-dhcp-host \
138 "<host mac='${amac}' name='mas01' ip='${MAAS_IP}'/>" --live
145 for node in "${vnodes[@]}"; do
146 virsh start "${node}"
147 sleep $((RANDOM%5+1))
151 function check_connection {
152 local total_attempts=60
156 echo '[INFO] Attempting to get into Salt master ...'
158 # wait until ssh on Salt master is available
159 # shellcheck disable=SC2034
160 for attempt in $(seq "${total_attempts}"); do
161 # shellcheck disable=SC2086
162 ssh ${SSH_OPTS} "ubuntu@${SALT_MASTER}" uptime
164 0) echo "${attempt}> Success"; break ;;
165 *) echo "${attempt}/${total_attempts}> ssh server ain't ready yet, waiting for ${sleep_time} seconds ..." ;;
172 function parse_yaml {
179 fs="$(echo @|tr @ '\034')"
180 sed -e 's|---||g' -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
181 -e "s|^\($s\)\($w\)$s[:-]$s\(.*\)$s\$|\1$fs\2$fs\3|p" "$1" |
183 indent = length($1)/2;
185 for (i in vname) {if (i > indent) {delete vname[i]}}
186 if (length($3) > 0) {
187 vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
188 printf("%s%s%s=(\"%s\")\n", "'"$prefix"'",vn, $2, $3);
194 # Execute in a subshell to prevent local variable override during recursion
196 local total_attempts=$1; shift
199 echo "[NOTE] Waiting for cmd to return success: ${cmdstr}"
200 # shellcheck disable=SC2034
201 for attempt in $(seq "${total_attempts}"); do
202 # shellcheck disable=SC2015
203 eval "${cmdstr}" && return 0 || true
204 echo -n '.'; sleep "${sleep_time}"