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