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
50 for net in "${vnode_networks[@]}"; do
51 if virsh net-info "${net}" >/dev/null 2>&1; then
52 virsh net-destroy "${net}"
53 virsh net-undefine "${net}"
55 # in case of custom network, host should already have the bridge in place
56 if [ -f "net_${net}.xml" ]; then
57 virsh net-define "net_${net}.xml"
58 virsh net-autostart "${net}"
59 virsh net-start "${net}"
66 local -n vnodes_ram=$2
67 local -n vnodes_vcpus=$3
68 local -n vnode_networks=$4
70 # prepare network args
72 for net in "${vnode_networks[@]}"; do
74 # in case of custom network, host should already have the bridge in place
75 if [ ! -f "net_${net}.xml" ]; then
78 net_args="${net_args} --network ${net_type}=${net},model=virtio"
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 # shellcheck disable=SC2086
91 virt-install --name "${node}" \
92 --ram "${vnodes_ram[$node]}" --vcpus "${vnodes_vcpus[$node]}" \
93 --cpu host-passthrough --accelerate ${net_args} \
94 --disk path="$(pwd)/images/mcp_${node}.qcow2",format=qcow2,bus=virtio,cache=none,io=native \
95 --os-type linux --os-variant none \
96 --boot hd --vnc --console pty --autostart --noreboot \
97 --disk path="$(pwd)/images/mcp_${node}.iso",device=cdrom \
103 update_pxe_network() {
104 local -n vnode_networks=$1
105 if virsh net-info "${vnode_networks[0]}" >/dev/null 2>&1; then
106 # set static ip address for salt master node, only if managed via virsh
107 # NOTE: below expr assume PXE network is always the first in domiflist
108 virsh net-update "${vnode_networks[0]}" add ip-dhcp-host \
109 "<host mac='$(virsh domiflist cfg01 | awk '/network/ {print $5; exit}')' name='cfg01' ip='${SALT_MASTER}'/>" --live
117 for node in "${vnodes[@]}"; do
118 virsh start "${node}"
124 local total_attempts=60
129 echo '[INFO] Attempting to get into Salt master ...'
131 # wait until ssh on Salt master is available
132 while ((attempt <= total_attempts)); do
133 # shellcheck disable=SC2086
134 ssh ${SSH_OPTS} "ubuntu@${SALT_MASTER}" uptime
136 0) echo "${attempt}> Success"; break ;;
137 *) echo "${attempt}/${total_attempts}> ssh server ain't ready yet, waiting for ${sleep_time} seconds ..." ;;
152 fs="$(echo @|tr @ '\034')"
153 sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
154 -e "s|^\($s\)\($w\)$s[:-]$s\(.*\)$s\$|\1$fs\2$fs\3|p" "$1" |
156 indent = length($1)/2;
158 for (i in vname) {if (i > indent) {delete vname[i]}}
159 if (length($3) > 0) {
160 vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
161 printf("%s%s%s=(\"%s\")\n", "'"$prefix"'",vn, $2, $3);