3 # Library of shell functions
7 # shellcheck disable=SC2155
8 local mcp_ssh_key=$(basename "${SSH_KEY}")
10 if [ -n "${SUDO_USER}" ] && [ "${SUDO_USER}" != 'root' ]; then
14 if [ -f "${SSH_KEY}" ]; then
16 ssh-keygen -f "${mcp_ssh_key}" -y > "${mcp_ssh_key}.pub"
19 [ -f "${mcp_ssh_key}" ] || ssh-keygen -f "${mcp_ssh_key}" -N ''
20 sudo install -D -o "${user}" -m 0600 "${mcp_ssh_key}" "${SSH_KEY}"
27 mkdir -p "${image_dir}"
28 wget -P "${image_dir}" -N "${base_image}"
32 # clean up existing nodes
33 for node in $(virsh list --name | grep -P '\w{3}\d{2}'); do
34 virsh destroy "${node}"
36 for node in $(virsh list --name --all | grep -P '\w{3}\d{2}'); do
37 virsh domblklist "${node}" | awk '/^.da/ {print $2}' | \
38 xargs --no-run-if-empty -I{} sudo rm -f {}
39 virsh undefine "${node}" --remove-all-storage --nvram
49 get_base_image "${base_image}" "${image_dir}"
50 # shellcheck disable=SC2016
51 envsubst '${SALT_MASTER},${CLUSTER_DOMAIN}' < \
52 user-data.template > user-data.sh
54 for node in "${vnodes[@]}"; do
55 # create/prepare images
56 ./create-config-drive.sh -k "$(basename "${SSH_KEY}").pub" -u user-data.sh \
57 -h "${node}" "${image_dir}/mcp_${node}.iso"
58 cp "${image_dir}/${base_image/*\/}" "${image_dir}/mcp_${node}.qcow2"
59 qemu-img resize "${image_dir}/mcp_${node}.qcow2" 100G
64 local -n vnode_networks=$1
65 # create required networks, including constant "mcpcontrol"
66 # FIXME(alav): since we renamed "pxe" to "mcpcontrol", we need to make sure
67 # we delete the old "pxe" virtual network, or it would cause IP conflicts.
68 for net in "pxe" "mcpcontrol" "${vnode_networks[@]}"; do
69 if virsh net-info "${net}" >/dev/null 2>&1; then
70 virsh net-destroy "${net}" || true
71 virsh net-undefine "${net}"
73 # in case of custom network, host should already have the bridge in place
74 if [ -f "net_${net}.xml" ]; then
75 virsh net-define "net_${net}.xml"
76 virsh net-autostart "${net}"
77 virsh net-start "${net}"
84 local -n vnodes_ram=$2
85 local -n vnodes_vcpus=$3
86 local -n vnode_networks=$4
89 # AArch64: prepare arch specific arguments
90 local virt_extra_args=""
91 if [ "$(uname -i)" = "aarch64" ]; then
92 # No Cirrus VGA on AArch64, use vga std
93 virt_extra_args="$virt_extra_args --video=vga"
96 # create vms with specified options
97 for node in "${vnodes[@]}"; do
98 # prepare network args
99 net_args=" --network network=mcpcontrol,model=virtio"
100 if [ "${node}" = "mas01" ]; then
101 # MaaS node's 3rd interface gets connected to PXE/Admin Bridge
102 vnode_networks[2]="${vnode_networks[0]}"
104 for net in "${vnode_networks[@]:1}"; do
106 # in case of custom network, host should already have the bridge in place
107 if [ ! -f "net_${net}.xml" ]; then
110 net_args="${net_args} --network ${net_type}=${net},model=virtio"
113 # shellcheck disable=SC2086
114 virt-install --name "${node}" \
115 --ram "${vnodes_ram[$node]}" --vcpus "${vnodes_vcpus[$node]}" \
116 --cpu host-passthrough --accelerate ${net_args} \
117 --disk path="${image_dir}/mcp_${node}.qcow2",format=qcow2,bus=virtio,cache=none,io=native \
118 --os-type linux --os-variant none \
119 --boot hd --vnc --console pty --autostart --noreboot \
120 --disk path="${image_dir}/mcp_${node}.iso",device=cdrom \
126 update_mcpcontrol_network() {
127 # set static ip address for salt master node, MaaS node
128 # shellcheck disable=SC2155
129 local cmac=$(virsh domiflist cfg01 2>&1| awk '/mcpcontrol/ {print $5; exit}')
130 # shellcheck disable=SC2155
131 local amac=$(virsh domiflist mas01 2>&1| awk '/mcpcontrol/ {print $5; exit}')
132 virsh net-update "mcpcontrol" add ip-dhcp-host \
133 "<host mac='${cmac}' name='cfg01' ip='${SALT_MASTER}'/>" --live
134 [ -z "${amac}" ] || virsh net-update "mcpcontrol" add ip-dhcp-host \
135 "<host mac='${amac}' name='mas01' ip='${MAAS_IP}'/>" --live
142 for node in "${vnodes[@]}"; do
143 virsh start "${node}"
144 sleep $((RANDOM%5+1))
149 local total_attempts=60
154 echo '[INFO] Attempting to get into Salt master ...'
156 # wait until ssh on Salt master is available
157 while ((attempt <= total_attempts)); do
158 # shellcheck disable=SC2086
159 ssh ${SSH_OPTS} "ubuntu@${SALT_MASTER}" uptime
161 0) echo "${attempt}> Success"; break ;;
162 *) echo "${attempt}/${total_attempts}> ssh server ain't ready yet, waiting for ${sleep_time} seconds ..." ;;
177 fs="$(echo @|tr @ '\034')"
178 sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
179 -e "s|^\($s\)\($w\)$s[:-]$s\(.*\)$s\$|\1$fs\2$fs\3|p" "$1" |
181 indent = length($1)/2;
183 for (i in vname) {if (i > indent) {delete vname[i]}}
184 if (length($3) > 0) {
185 vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
186 printf("%s%s%s=(\"%s\")\n", "'"$prefix"'",vn, $2, $3);