b83f2150873101be0784869fdf51481f31ae1634
[fuel.git] / mcp / reclass / scripts / infra.sh
1 #!/bin/bash
2
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 )
5
6 # get required packages
7 apt-get install -y mkisofs curl virtinst cpu-checker qemu-kvm
8
9 # generate ssh key
10 [ -f $SSH_KEY ] || ssh-keygen -f $SSH_KEY -N ''
11
12 # get base image
13 mkdir -p images
14 wget -P /tmp -nc $BASE_IMAGE
15
16 for node in "${!NODES[@]}"; do
17   # clean up existing nodes
18   if [ "$(virsh domstate $node 2>/dev/null)" == 'running' ]; then
19     virsh destroy $node
20     virsh undefine $node
21   fi
22
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
27 done
28
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}
34   fi
35   virsh net-define net_${net}.xml
36   virsh net-autostart ${net}
37   virsh net-start ${net}
38 done
39
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
51 done
52
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
56
57 # start vms
58 for node in "${!NODES[@]}"; do
59   virsh start ${node}
60   sleep $[RANDOM%5+1]
61 done
62
63 CONNECTION_ATTEMPTS=20
64 SLEEP=15
65
66 # wait until ssh on Salt master is available
67 echo "Attempting to ssh to Salt master ..."
68 ATTEMPT=1
69
70 while (($ATTEMPT <= $CONNECTION_ATTEMPTS)); do
71   ssh $SSH_OPTS ubuntu@$SALT_MASTER uptime
72   case $? in
73     (0) echo "${ATTEMPT}> Success"; break ;;
74     (*) echo "${ATTEMPT}/${CONNECTION_ATTEMPTS}> ssh server ain't ready yet, waiting for ${SLEEP} seconds ..." ;;
75   esac
76   sleep $SLEEP
77   ((ATTEMPT+=1))
78 done