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
107 # in case of custom network, host should already have the bridge in place
108 if [ -f "net_${net}.xml" ] && \
109 [ ! -d "/sys/class/net/${net}/bridge" ]; then
112 net_args="${net_args} --network ${net_type}=${net},model=virtio"
115 # shellcheck disable=SC2086
116 virt-install --name "${node}" \
117 --ram "${vnodes_ram[$node]}" --vcpus "${vnodes_vcpus[$node]}" \
118 --cpu host-passthrough --accelerate ${net_args} \
119 --disk path="${image_dir}/mcp_${node}.qcow2",format=qcow2,bus=virtio,cache=none,io=native \
120 --os-type linux --os-variant none \
121 --boot hd --vnc --console pty --autostart --noreboot \
122 --disk path="${image_dir}/mcp_${node}.iso",device=cdrom \
128 update_mcpcontrol_network() {
129 # set static ip address for salt master node, MaaS node
130 # shellcheck disable=SC2155
131 local cmac=$(virsh domiflist cfg01 2>&1| awk '/mcpcontrol/ {print $5; exit}')
132 # shellcheck disable=SC2155
133 local amac=$(virsh domiflist mas01 2>&1| awk '/mcpcontrol/ {print $5; exit}')
134 virsh net-update "mcpcontrol" add ip-dhcp-host \
135 "<host mac='${cmac}' name='cfg01' ip='${SALT_MASTER}'/>" --live
136 [ -z "${amac}" ] || virsh net-update "mcpcontrol" add ip-dhcp-host \
137 "<host mac='${amac}' name='mas01' ip='${MAAS_IP}'/>" --live
144 for node in "${vnodes[@]}"; do
145 virsh start "${node}"
146 sleep $((RANDOM%5+1))
151 local total_attempts=60
156 echo '[INFO] Attempting to get into Salt master ...'
158 # wait until ssh on Salt master is available
159 while ((attempt <= total_attempts)); do
160 # shellcheck disable=SC2086
161 ssh ${SSH_OPTS} "ubuntu@${SALT_MASTER}" uptime
163 0) echo "${attempt}> Success"; break ;;
164 *) echo "${attempt}/${total_attempts}> ssh server ain't ready yet, waiting for ${sleep_time} seconds ..." ;;
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);