3 BASE_IMAGE=https://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-amd64-disk1.img
4 declare -A NODES=( [cfg01]=4096 [ctl01]=6144 [ctl02]=6144 [ctl03]=6144 [gtw01]=2048 [cmp01]=2048 )
6 # get required packages
7 apt-get install -y mkisofs curl virtinst cpu-checker qemu-kvm
10 [ -f $SSH_KEY ] || ssh-keygen -f $SSH_KEY -N ''
14 wget -P /tmp -nc $BASE_IMAGE
16 for node in "${!NODES[@]}"; do
17 # clean up existing nodes
18 if [ "$(virsh domstate $node 2>/dev/null)" == 'running' ]; then
23 # create/prepare images
24 [ -f images/mcp_${node}.iso ] || ./create-config-drive.sh -k ${SSH_KEY}.pub -u user-data.sh -h ${node} images/mcp_${node}.iso
25 cp /tmp/${BASE_IMAGE/*\/} images/mcp_${node}.qcow2
26 qemu-img resize images/mcp_${node}.qcow2 100G
29 # create required networks
30 for net in pxe mgmt internal public; do
31 if virsh net-info $net >/dev/null 2>&1; then
32 virsh net-destroy ${net}
33 virsh net-undefine ${net}
35 virsh net-define net_${net}.xml
36 virsh net-autostart ${net}
37 virsh net-start ${net}
40 # create vms with specified options
41 for node in "${!NODES[@]}"; do
42 virt-install --name ${node} --ram ${NODES[$node]} --vcpus=2 --cpu host --accelerate \
43 --network network:pxe,model=virtio \
44 --network network:mgmt,model=virtio \
45 --network network:internal,model=virtio \
46 --network network:public,model=virtio \
47 --disk path=$(pwd)/images/mcp_${node}.qcow2,format=qcow2,bus=virtio,cache=none,io=native \
48 --os-type linux --os-variant none \
49 --boot hd --vnc --console pty --autostart --noreboot \
50 --disk path=$(pwd)/images/mcp_${node}.iso,device=cdrom
53 # set static ip address for salt master node
54 virsh net-update pxe add ip-dhcp-host \
55 "<host mac='$(virsh domiflist cfg01 | awk '/pxe/ {print $5}')' name='cfg01' ip='$SALT_MASTER'/>" --live
58 for node in "${!NODES[@]}"; do
63 CONNECTION_ATTEMPTS=20
66 # wait until ssh on Salt master is available
67 echo "Attempting to ssh to Salt master ..."
70 while (($ATTEMPT <= $CONNECTION_ATTEMPTS)); do
71 ssh $SSH_OPTS ubuntu@$SALT_MASTER uptime
73 (0) echo "${ATTEMPT}> Success"; break ;;
74 (*) echo "${ATTEMPT}/${CONNECTION_ATTEMPTS}> ssh server ain't ready yet, waiting for ${SLEEP} seconds ..." ;;