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 # FIXME(alav): The same applies for "fuel1" virsh network.
69 for net in "fuel1" "pxe" "mcpcontrol" "${vnode_networks[@]}"; do
70 if virsh net-info "${net}" >/dev/null 2>&1; then
71 virsh net-destroy "${net}" || true
72 virsh net-undefine "${net}"
74 # in case of custom network, host should already have the bridge in place
75 if [ -f "net_${net}.xml" ] && [ ! -d "/sys/class/net/${net}/bridge" ]; then
76 virsh net-define "net_${net}.xml"
77 virsh net-autostart "${net}"
78 virsh net-start "${net}"
85 local -n vnodes_ram=$2
86 local -n vnodes_vcpus=$3
87 local -n vnode_networks=$4
90 # AArch64: prepare arch specific arguments
91 local virt_extra_args=""
92 if [ "$(uname -i)" = "aarch64" ]; then
93 # No Cirrus VGA on AArch64, use vga std
94 virt_extra_args="$virt_extra_args --video=vga"
97 # create vms with specified options
98 for node in "${vnodes[@]}"; do
99 # prepare network args
100 net_args=" --network network=mcpcontrol,model=virtio"
101 if [ "${node}" = "mas01" ]; then
102 # MaaS node's 3rd interface gets connected to PXE/Admin Bridge
103 vnode_networks[2]="${vnode_networks[0]}"
105 for net in "${vnode_networks[@]:1}"; do
106 net_args="${net_args} --network bridge=${net},model=virtio"
109 # shellcheck disable=SC2086
110 virt-install --name "${node}" \
111 --ram "${vnodes_ram[$node]}" --vcpus "${vnodes_vcpus[$node]}" \
112 --cpu host-passthrough --accelerate ${net_args} \
113 --disk path="${image_dir}/mcp_${node}.qcow2",format=qcow2,bus=virtio,cache=none,io=native \
114 --os-type linux --os-variant none \
115 --boot hd --vnc --console pty --autostart --noreboot \
116 --disk path="${image_dir}/mcp_${node}.iso",device=cdrom \
122 update_mcpcontrol_network() {
123 # set static ip address for salt master node, MaaS node
124 # shellcheck disable=SC2155
125 local cmac=$(virsh domiflist cfg01 2>&1| awk '/mcpcontrol/ {print $5; exit}')
126 # shellcheck disable=SC2155
127 local amac=$(virsh domiflist mas01 2>&1| awk '/mcpcontrol/ {print $5; exit}')
128 virsh net-update "mcpcontrol" add ip-dhcp-host \
129 "<host mac='${cmac}' name='cfg01' ip='${SALT_MASTER}'/>" --live
130 [ -z "${amac}" ] || virsh net-update "mcpcontrol" add ip-dhcp-host \
131 "<host mac='${amac}' name='mas01' ip='${MAAS_IP}'/>" --live
138 for node in "${vnodes[@]}"; do
139 virsh start "${node}"
140 sleep $((RANDOM%5+1))
145 local total_attempts=60
150 echo '[INFO] Attempting to get into Salt master ...'
152 # wait until ssh on Salt master is available
153 while ((attempt <= total_attempts)); do
154 # shellcheck disable=SC2086
155 ssh ${SSH_OPTS} "ubuntu@${SALT_MASTER}" uptime
157 0) echo "${attempt}> Success"; break ;;
158 *) echo "${attempt}/${total_attempts}> ssh server ain't ready yet, waiting for ${sleep_time} seconds ..." ;;
173 fs="$(echo @|tr @ '\034')"
174 sed -e 's|---||g' -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
175 -e "s|^\($s\)\($w\)$s[:-]$s\(.*\)$s\$|\1$fs\2$fs\3|p" "$1" |
177 indent = length($1)/2;
179 for (i in vname) {if (i > indent) {delete vname[i]}}
180 if (length($3) > 0) {
181 vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
182 printf("%s%s%s=(\"%s\")\n", "'"$prefix"'",vn, $2, $3);