3 # Library of shell functions
8 if [ -n "${SUDO_USER}" ] && [ "${SUDO_USER}" != 'root' ]; then
12 if [ -f "/tmp/${SSH_KEY}" ]; then
13 cp "/tmp/${SSH_KEY}" .
14 ssh-keygen -f "${SSH_KEY}" -y > "${SSH_KEY}.pub"
17 [ -f "${SSH_KEY}" ] || ssh-keygen -f "${SSH_KEY}" -N ''
18 install -o "${user}" -m 0600 "${SSH_KEY}" /tmp/
25 wget -P /tmp -N "${base_image}"
29 # clean up existing nodes
30 for node in $(virsh list --name | grep -P '\w{3}\d{2}'); do
31 virsh destroy "${node}"
33 for node in $(virsh list --name --all | grep -P '\w{3}\d{2}'); do
34 virsh undefine --nvram "${node}"
43 get_base_image "${base_image}"
44 envsubst '${SALT_MASTER},${CLUSTER_DOMAIN}' < \
45 user-data.template > user-data.sh
47 for node in "${vnodes[@]}"; do
48 # create/prepare images
49 ./create-config-drive.sh -k "${SSH_KEY}.pub" -u user-data.sh \
50 -h "${node}" "images/mcp_${node}.iso"
51 cp "/tmp/${base_image/*\/}" "images/mcp_${node}.qcow2"
52 qemu-img resize "images/mcp_${node}.qcow2" 100G
57 local -n vnode_networks=$1
58 # create required networks, including constant "mcpcontrol"
59 # FIXME(alav): since we renamed "pxe" to "mcpcontrol", we need to make sure
60 # we delete the old "pxe" virtual network, or it would cause IP conflicts.
61 for net in "pxe" "mcpcontrol" "${vnode_networks[@]}"; do
62 if virsh net-info "${net}" >/dev/null 2>&1; then
63 virsh net-destroy "${net}" || true
64 virsh net-undefine "${net}"
66 # in case of custom network, host should already have the bridge in place
67 if [ -f "net_${net}.xml" ]; then
68 virsh net-define "net_${net}.xml"
69 virsh net-autostart "${net}"
70 virsh net-start "${net}"
77 local -n vnodes_ram=$2
78 local -n vnodes_vcpus=$3
79 local -n vnode_networks=$4
81 # AArch64: prepare arch specific arguments
82 local virt_extra_args=""
83 if [ "$(uname -i)" = "aarch64" ]; then
84 # No Cirrus VGA on AArch64, use vga std
85 virt_extra_args="$virt_extra_args --video=vga"
88 # create vms with specified options
89 for node in "${vnodes[@]}"; do
90 # prepare network args
91 net_args=" --network network=mcpcontrol,model=virtio"
92 if [ "${node}" = "mas01" ]; then
93 # MaaS node's 3rd interface gets connected to PXE/Admin Bridge
94 vnode_networks[2]="${vnode_networks[0]}"
96 for net in "${vnode_networks[@]:1}"; do
98 # in case of custom network, host should already have the bridge in place
99 if [ ! -f "net_${net}.xml" ]; then
102 net_args="${net_args} --network ${net_type}=${net},model=virtio"
105 # shellcheck disable=SC2086
106 virt-install --name "${node}" \
107 --ram "${vnodes_ram[$node]}" --vcpus "${vnodes_vcpus[$node]}" \
108 --cpu host-passthrough --accelerate ${net_args} \
109 --disk path="$(pwd)/images/mcp_${node}.qcow2",format=qcow2,bus=virtio,cache=none,io=native \
110 --os-type linux --os-variant none \
111 --boot hd --vnc --console pty --autostart --noreboot \
112 --disk path="$(pwd)/images/mcp_${node}.iso",device=cdrom \
118 update_mcpcontrol_network() {
119 # set static ip address for salt master node, MaaS node
120 local cmac=$(virsh domiflist cfg01 2>&1| awk '/mcpcontrol/ {print $5; exit}')
121 local amac=$(virsh domiflist mas01 2>&1| awk '/mcpcontrol/ {print $5; exit}')
122 virsh net-update "mcpcontrol" add ip-dhcp-host \
123 "<host mac='${cmac}' name='cfg01' ip='${SALT_MASTER}'/>" --live
124 [ -z "${amac}" ] || virsh net-update "mcpcontrol" add ip-dhcp-host \
125 "<host mac='${amac}' name='mas01' ip='${MAAS_IP}'/>" --live
132 for node in "${vnodes[@]}"; do
133 virsh start "${node}"
139 local total_attempts=60
144 echo '[INFO] Attempting to get into Salt master ...'
146 # wait until ssh on Salt master is available
147 while ((attempt <= total_attempts)); do
148 # shellcheck disable=SC2086
149 ssh ${SSH_OPTS} "ubuntu@${SALT_MASTER}" uptime
151 0) echo "${attempt}> Success"; break ;;
152 *) echo "${attempt}/${total_attempts}> ssh server ain't ready yet, waiting for ${sleep_time} seconds ..." ;;
167 fs="$(echo @|tr @ '\034')"
168 sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
169 -e "s|^\($s\)\($w\)$s[:-]$s\(.*\)$s\$|\1$fs\2$fs\3|p" "$1" |
171 indent = length($1)/2;
173 for (i in vname) {if (i > indent) {delete vname[i]}}
174 if (length($3) > 0) {
175 vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
176 printf("%s%s%s=(\"%s\")\n", "'"$prefix"'",vn, $2, $3);