3 # Library of shell functions
8 if [ -n "${SUDO_USER}" ] && [ "${SUDO_USER}" != 'root' ]; then
12 [ -f "${SSH_KEY}" ] || ssh-keygen -f "${SSH_KEY}" -N ''
13 install -o "${user}" -m 0600 "${SSH_KEY}" /tmp/
20 wget -P /tmp -N "${base_image}"
24 # clean up existing nodes
25 for node in $(virsh list --name | grep -P '\w{3}\d{2}'); do
26 virsh destroy "${node}"
28 for node in $(virsh list --name --all | grep -P '\w{3}\d{2}'); do
29 virsh undefine --nvram "${node}"
38 get_base_image "${base_image}"
39 envsubst < user-data.template > user-data.sh
41 for node in "${vnodes[@]}"; do
42 # create/prepare images
43 ./create-config-drive.sh -k "${SSH_KEY}.pub" -u user-data.sh \
44 -h "${node}" "images/mcp_${node}.iso"
45 cp "/tmp/${base_image/*\/}" "images/mcp_${node}.qcow2"
46 qemu-img resize "images/mcp_${node}.qcow2" 100G
51 local -n vnode_networks=$1
52 # create required networks, including constant "mcpcontrol"
53 # FIXME(alav): since we renamed "pxe" to "mcpcontrol", we need to make sure
54 # we delete the old "pxe" virtual network, or it would cause IP conflicts.
55 for net in "pxe" "mcpcontrol" "${vnode_networks[@]}"; do
56 if virsh net-info "${net}" >/dev/null 2>&1; then
57 virsh net-destroy "${net}" || true
58 virsh net-undefine "${net}"
60 # in case of custom network, host should already have the bridge in place
61 if [ -f "net_${net}.xml" ]; then
62 virsh net-define "net_${net}.xml"
63 virsh net-autostart "${net}"
64 virsh net-start "${net}"
71 local -n vnodes_ram=$2
72 local -n vnodes_vcpus=$3
73 local -n vnode_networks=$4
75 # AArch64: prepare arch specific arguments
76 local virt_extra_args=""
77 if [ "$(uname -i)" = "aarch64" ]; then
78 # No Cirrus VGA on AArch64, use vga std
79 virt_extra_args="$virt_extra_args --video=vga"
82 # create vms with specified options
83 for node in "${vnodes[@]}"; do
84 # prepare network args
85 net_args=" --network network=mcpcontrol,model=virtio"
86 if [ "${node}" = "mas01" ]; then
87 # MaaS node's 3rd interface gets connected to PXE/Admin Bridge
88 vnode_networks[2]="${vnode_networks[0]}"
90 for net in "${vnode_networks[@]:1}"; do
92 # in case of custom network, host should already have the bridge in place
93 if [ ! -f "net_${net}.xml" ]; then
96 net_args="${net_args} --network ${net_type}=${net},model=virtio"
99 # shellcheck disable=SC2086
100 virt-install --name "${node}" \
101 --ram "${vnodes_ram[$node]}" --vcpus "${vnodes_vcpus[$node]}" \
102 --cpu host-passthrough --accelerate ${net_args} \
103 --disk path="$(pwd)/images/mcp_${node}.qcow2",format=qcow2,bus=virtio,cache=none,io=native \
104 --os-type linux --os-variant none \
105 --boot hd --vnc --console pty --autostart --noreboot \
106 --disk path="$(pwd)/images/mcp_${node}.iso",device=cdrom \
112 update_mcpcontrol_network() {
113 # set static ip address for salt master node, MaaS node
114 local cmac=$(virsh domiflist cfg01 2>&1| awk '/mcpcontrol/ {print $5; exit}')
115 local amac=$(virsh domiflist mas01 2>&1| awk '/mcpcontrol/ {print $5; exit}')
116 virsh net-update "mcpcontrol" add ip-dhcp-host \
117 "<host mac='${cmac}' name='cfg01' ip='${SALT_MASTER}'/>" --live
118 [ -z "${amac}" ] || virsh net-update "mcpcontrol" add ip-dhcp-host \
119 "<host mac='${amac}' name='mas01' ip='${MAAS_IP}'/>" --live
126 for node in "${vnodes[@]}"; do
127 virsh start "${node}"
133 local total_attempts=60
138 echo '[INFO] Attempting to get into Salt master ...'
140 # wait until ssh on Salt master is available
141 while ((attempt <= total_attempts)); do
142 # shellcheck disable=SC2086
143 ssh ${SSH_OPTS} "ubuntu@${SALT_MASTER}" uptime
145 0) echo "${attempt}> Success"; break ;;
146 *) echo "${attempt}/${total_attempts}> ssh server ain't ready yet, waiting for ${sleep_time} seconds ..." ;;
161 fs="$(echo @|tr @ '\034')"
162 sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
163 -e "s|^\($s\)\($w\)$s[:-]$s\(.*\)$s\$|\1$fs\2$fs\3|p" "$1" |
165 indent = length($1)/2;
167 for (i in vname) {if (i > indent) {delete vname[i]}}
168 if (length($3) > 0) {
169 vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
170 printf("%s%s%s=(\"%s\")\n", "'"$prefix"'",vn, $2, $3);