Merge "ci/deploy.sh: Add new `-E` arg for env erase"
[fuel.git] / mcp / config / states / virtual_control_plane
1 #!/bin/bash -e
2 ##############################################################################
3 # Copyright (c) 2017 Mirantis Inc., Enea AB and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 CI_DEBUG=${CI_DEBUG:-0}; [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
11 ERASE_ENV=${ERASE_ENV:-0}
12
13 # shellcheck disable=SC1090
14 source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/lib.sh"
15
16 # Optionally destroy VCP VMs from a previous run
17 if [ "${ERASE_ENV}" -eq 1 ]; then
18     kvm_vms=$(salt --out yaml 'kvm*' virt.list_domains | \
19               sed -e 's/- //g' -e 's/:.*$//g')
20     for line in ${kvm_vms}; do
21         if [[ "${line}" =~ ^kvm ]]; then
22             kvm_node=${line}
23         elif [ -n "${kvm_node}" ]; then
24             salt "${kvm_node}" virt.purge dirs=True "${line}" || true
25         fi
26     done
27 fi
28
29 # KVM, compute node prereqs (libvirt first), VCP deployment
30 # patch the networking module for Debian based distros
31 debian_ip_source=/usr/lib/python2.7/dist-packages/salt/modules/debian_ip.py
32 salt -C 'kvm* or cmp*' file.line $debian_ip_source \
33   content='iface = iface.lower()' mode='delete'
34
35 salt -C 'kvm* or cmp*' file.replace $debian_ip_source \
36   pattern="^\s{8}__salt__\['pkg.install'\]\('vlan'\)" \
37   repl="\n        if not __salt__['pkg.version']('vlan'):\n            __salt__['pkg.install']('vlan')"
38
39 salt -C 'kvm*' pkg.install bridge-utils
40 salt -C 'kvm*' state.apply linux.network
41 salt -C 'cmp*' state.apply linux.system
42 salt -C 'cmp*' state.apply linux.network || true
43 salt -C 'kvm* or cmp*' system.reboot
44 wait_for 90 "! salt -C 'kvm* or cmp*' test.ping | " \
45   "tee /dev/stderr | grep -Fq 'Not connected'"
46
47 salt -C '* and not cfg01* and not mas01*' state.apply linux,ntp
48
49 wait_for 5 "salt -C 'kvm*' state.sls libvirt"
50
51 salt -C '* and not cfg01* and not mas01*' state.apply salt
52 salt -C 'kvm*' saltutil.sync_all
53 wait_for 10 "! salt -C 'kvm*' state.sls salt.control | " \
54   "tee /dev/stderr | grep -Fq 'Not connected'"
55
56 vcp_nodes=$(salt --out yaml 'kvm01*' pillar.get salt:control:cluster:internal:node | \
57             awk '/\s+\w+:$/ {gsub(/:$/, "*"); print $1}')
58
59 # Check all vcp nodes are available
60 rc=1
61 attempt=0
62 total_attempts=50
63 while [ $rc -ne 0 ] && [ ${attempt} -lt ${total_attempts} ]; do
64   rc=0
65   for node in $vcp_nodes; do
66     salt "$node" test.ping 2>/dev/null || { rc=$?; break; };
67   done
68   sleep 5
69   ((attempt+=1))
70 done
71
72 wait_for 10 "salt -C '* and not cfg01* and not mas01*' saltutil.sync_all"
73 wait_for 10 "salt -C 'E@^(?!cfg01|mas01|kvm|cmp00).*' state.apply salt"
74 wait_for 10 "! salt -C 'E@^(?!cfg01|mas01|kvm|cmp00).*' state.apply linux,ntp | " \
75   "tee /dev/stderr | grep -Fq 'Not connected'"
76
77 wait_for 10 "salt -C 'E@^(?!cfg01|mas01|kvm|cmp00).*' ssh.set_auth_key ${SUDO_USER} \
78   $(awk 'NR==1{print $2}' "$(eval echo "~${SUDO_USER}/.ssh/authorized_keys")")"
79
80 # Disable proxy dhcp routes after installation
81 salt -C 'prx*' file.write /etc/dhcp/dhclient-enter-hooks.d/no-default-route \
82   args='unset new_routers'