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 < user-data.template > user-data.sh
46 for node in "${vnodes[@]}"; do
47 # create/prepare images
48 ./create-config-drive.sh -k "${SSH_KEY}.pub" -u user-data.sh \
49 -h "${node}" "images/mcp_${node}.iso"
50 cp "/tmp/${base_image/*\/}" "images/mcp_${node}.qcow2"
51 qemu-img resize "images/mcp_${node}.qcow2" 100G
56 local -n vnode_networks=$1
57 # create required networks, including constant "mcpcontrol"
58 # FIXME(alav): since we renamed "pxe" to "mcpcontrol", we need to make sure
59 # we delete the old "pxe" virtual network, or it would cause IP conflicts.
60 for net in "pxe" "mcpcontrol" "${vnode_networks[@]}"; do
61 if virsh net-info "${net}" >/dev/null 2>&1; then
62 virsh net-destroy "${net}" || true
63 virsh net-undefine "${net}"
65 # in case of custom network, host should already have the bridge in place
66 if [ -f "net_${net}.xml" ]; then
67 virsh net-define "net_${net}.xml"
68 virsh net-autostart "${net}"
69 virsh net-start "${net}"
76 local -n vnodes_ram=$2
77 local -n vnodes_vcpus=$3
78 local -n vnode_networks=$4
80 # AArch64: prepare arch specific arguments
81 local virt_extra_args=""
82 if [ "$(uname -i)" = "aarch64" ]; then
83 # No Cirrus VGA on AArch64, use vga std
84 virt_extra_args="$virt_extra_args --video=vga"
87 # create vms with specified options
88 for node in "${vnodes[@]}"; do
89 # prepare network args
90 net_args=" --network network=mcpcontrol,model=virtio"
91 if [ "${node}" = "mas01" ]; then
92 # MaaS node's 3rd interface gets connected to PXE/Admin Bridge
93 vnode_networks[2]="${vnode_networks[0]}"
95 for net in "${vnode_networks[@]:1}"; do
97 # in case of custom network, host should already have the bridge in place
98 if [ ! -f "net_${net}.xml" ]; then
101 net_args="${net_args} --network ${net_type}=${net},model=virtio"
104 # shellcheck disable=SC2086
105 virt-install --name "${node}" \
106 --ram "${vnodes_ram[$node]}" --vcpus "${vnodes_vcpus[$node]}" \
107 --cpu host-passthrough --accelerate ${net_args} \
108 --disk path="$(pwd)/images/mcp_${node}.qcow2",format=qcow2,bus=virtio,cache=none,io=native \
109 --os-type linux --os-variant none \
110 --boot hd --vnc --console pty --autostart --noreboot \
111 --disk path="$(pwd)/images/mcp_${node}.iso",device=cdrom \
117 update_mcpcontrol_network() {
118 # set static ip address for salt master node, MaaS node
119 local cmac=$(virsh domiflist cfg01 2>&1| awk '/mcpcontrol/ {print $5; exit}')
120 local amac=$(virsh domiflist mas01 2>&1| awk '/mcpcontrol/ {print $5; exit}')
121 virsh net-update "mcpcontrol" add ip-dhcp-host \
122 "<host mac='${cmac}' name='cfg01' ip='${SALT_MASTER}'/>" --live
123 [ -z "${amac}" ] || virsh net-update "mcpcontrol" add ip-dhcp-host \
124 "<host mac='${amac}' name='mas01' ip='${MAAS_IP}'/>" --live
131 for node in "${vnodes[@]}"; do
132 virsh start "${node}"
138 local total_attempts=60
143 echo '[INFO] Attempting to get into Salt master ...'
145 # wait until ssh on Salt master is available
146 while ((attempt <= total_attempts)); do
147 # shellcheck disable=SC2086
148 ssh ${SSH_OPTS} "ubuntu@${SALT_MASTER}" uptime
150 0) echo "${attempt}> Success"; break ;;
151 *) echo "${attempt}/${total_attempts}> ssh server ain't ready yet, waiting for ${sleep_time} seconds ..." ;;
166 fs="$(echo @|tr @ '\034')"
167 sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
168 -e "s|^\($s\)\($w\)$s[:-]$s\(.*\)$s\$|\1$fs\2$fs\3|p" "$1" |
170 indent = length($1)/2;
172 for (i in vname) {if (i > indent) {delete vname[i]}}
173 if (length($3) > 0) {
174 vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
175 printf("%s%s%s=(\"%s\")\n", "'"$prefix"'",vn, $2, $3);