2 # Library of shell functions
6 local user=${SUDO_USER:-$USER}
8 [ -f "$SSH_KEY" ] || ssh-keygen -f ${SSH_KEY} -N ''
9 install -o $user -m 0600 ${SSH_KEY} /tmp/
16 wget -P /tmp -nc $base_image
20 # clean up existing nodes
21 for node in $(virsh list --name | grep -P '\w{3}\d{2}'); do
24 for node in $(virsh list --name --all | grep -P '\w{3}\d{2}'); do
25 virsh undefine --nvram $node
34 get_base_image $base_image
35 envsubst < user-data.template > user-data.sh
37 for node in "${vnodes[@]}"; do
38 # create/prepare images
39 ./create-config-drive.sh -k ${SSH_KEY}.pub -u user-data.sh -h ${node} images/mcp_${node}.iso
40 cp /tmp/${base_image/*\/} images/mcp_${node}.qcow2
41 qemu-img resize images/mcp_${node}.qcow2 100G
46 local -n vnode_networks=$1
47 # create required networks
48 for net in "${vnode_networks[@]}"; do
49 if virsh net-info "${net}" >/dev/null 2>&1; then
50 virsh net-destroy "${net}"
51 virsh net-undefine "${net}"
53 # in case of custom network, host should already have the bridge in place
54 if [ -f "net_${net}.xml" ]; then
55 virsh net-define "net_${net}.xml"
56 virsh net-autostart "${net}"
57 virsh net-start "${net}"
64 local -n vnodes_ram=$2
65 local -n vnodes_vcpus=$3
66 local -n vnode_networks=$4
68 # prepare network args
70 for net in "${vnode_networks[@]}"; do
72 # in case of custom network, host should already have the bridge in place
73 if [ ! -f "net_${net}.xml" ]; then
76 net_args="${net_args} --network ${net_type}=${net},model=virtio"
79 # create vms with specified options
80 for node in "${vnodes[@]}"; do
81 virt-install --name "${node}" \
82 --ram "${vnodes_ram[$node]}" --vcpus "${vnodes_vcpus[$node]}" \
83 --cpu host-passthrough --accelerate ${net_args} \
84 --disk path="$(pwd)/images/mcp_${node}.qcow2",format=qcow2,bus=virtio,cache=none,io=native \
85 --os-type linux --os-variant none \
86 --boot hd --vnc --console pty --autostart --noreboot \
87 --disk path="$(pwd)/images/mcp_${node}.iso",device=cdrom \
92 update_pxe_network() {
93 local -n vnode_networks=$1
94 if virsh net-info "${vnode_networks[0]}" >/dev/null 2>&1; then
95 # set static ip address for salt master node, only if managed via virsh
96 # NOTE: below expr assume PXE network is always the first in domiflist
97 virsh net-update "${vnode_networks[0]}" add ip-dhcp-host \
98 "<host mac='$(virsh domiflist cfg01 | awk '/network/ {print $5; exit}')' name='cfg01' ip='${SALT_MASTER}'/>" --live
106 for node in "${vnodes[@]}"; do
113 local total_attempts=60
118 echo '[INFO] Attempting to get into Salt master ...'
120 # wait until ssh on Salt master is available
121 while (($attempt <= $total_attempts)); do
122 ssh ${SSH_OPTS} ubuntu@${SALT_MASTER} uptime
124 0) echo "${attempt}> Success"; break ;;
125 *) echo "${attempt}/${total_attempts}> ssh server ain't ready yet, waiting for ${sleep_time} seconds ..." ;;
140 fs="$(echo @|tr @ '\034')"
141 sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
142 -e "s|^\($s\)\($w\)$s[:-]$s\(.*\)$s\$|\1$fs\2$fs\3|p" "$1" |
144 indent = length($1)/2;
146 for (i in vname) {if (i > indent) {delete vname[i]}}
147 if (length($3) > 0) {
148 vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
149 printf("%s%s%s=(\"%s\")\n", "'"$prefix"'",vn, $2, $3);