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 # create vms with specified options
82 for node in "${vnodes[@]}"; do
83 # shellcheck disable=SC2086
84 virt-install --name "${node}" \
85 --ram "${vnodes_ram[$node]}" --vcpus "${vnodes_vcpus[$node]}" \
86 --cpu host-passthrough --accelerate ${net_args} \
87 --disk path="$(pwd)/images/mcp_${node}.qcow2",format=qcow2,bus=virtio,cache=none,io=native \
88 --os-type linux --os-variant none \
89 --boot hd --vnc --console pty --autostart --noreboot \
90 --disk path="$(pwd)/images/mcp_${node}.iso",device=cdrom \
95 update_pxe_network() {
96 local -n vnode_networks=$1
97 if virsh net-info "${vnode_networks[0]}" >/dev/null 2>&1; then
98 # set static ip address for salt master node, only if managed via virsh
99 # NOTE: below expr assume PXE network is always the first in domiflist
100 virsh net-update "${vnode_networks[0]}" add ip-dhcp-host \
101 "<host mac='$(virsh domiflist cfg01 | awk '/network/ {print $5; exit}')' name='cfg01' ip='${SALT_MASTER}'/>" --live
109 for node in "${vnodes[@]}"; do
110 virsh start "${node}"
116 local total_attempts=60
121 echo '[INFO] Attempting to get into Salt master ...'
123 # wait until ssh on Salt master is available
124 while ((attempt <= total_attempts)); do
125 # shellcheck disable=SC2086
126 ssh ${SSH_OPTS} "ubuntu@${SALT_MASTER}" uptime
128 0) echo "${attempt}> Success"; break ;;
129 *) echo "${attempt}/${total_attempts}> ssh server ain't ready yet, waiting for ${sleep_time} seconds ..." ;;
144 fs="$(echo @|tr @ '\034')"
145 sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
146 -e "s|^\($s\)\($w\)$s[:-]$s\(.*\)$s\$|\1$fs\2$fs\3|p" "$1" |
148 indent = length($1)/2;
150 for (i in vname) {if (i > indent) {delete vname[i]}}
151 if (length($3) > 0) {
152 vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
153 printf("%s%s%s=(\"%s\")\n", "'"$prefix"'",vn, $2, $3);