lib.sh: modprobe br_netfilter, don't bail on err
[fuel.git] / mcp / scripts / salt.sh
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 # Deploy Salt Master
11 #
12
13 CI_DEBUG=${CI_DEBUG:-0}; [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
14 F_GIT_ROOT=$(git rev-parse --show-toplevel)
15 F_GIT_DIR=$(cd "${F_GIT_ROOT}/mcp" && git rev-parse --git-dir)
16 F_GIT_SUBD=${F_GIT_ROOT#${F_GIT_DIR%%/.git*}}
17 OPNFV_TMP_DIR="/home/${SALT_MASTER_USER}/opnfv"
18 OPNFV_GIT_DIR="/root/opnfv"
19 OPNFV_FUEL_DIR="/root/fuel" # Should be in sync with patch.sh, scripts patches
20 OPNFV_RDIR="reclass/classes/cluster/all-mcp-arch-common"
21 OPNFV_VCP_IMG="mcp/scripts/base_image_opnfv_fuel_vcp.img"
22 OPNFV_VCP_DIR="/srv/salt/env/prd/salt/files/control/images"
23 LOCAL_GIT_DIR="${F_GIT_ROOT%${F_GIT_SUBD}}"
24 LOCAL_PDF_RECLASS=$1; shift
25 LOCAL_VIRT_NODES=$(echo ${*//cfg01/}) # unquoted to filter space
26 NODE_MASK="${LOCAL_VIRT_NODES// /|}"
27
28 # push to cfg01 current git repo first (including submodules), at ~ubuntu/opnfv
29 # later we move it to ~root/opnfv (and ln as ~root/fuel); delete the temp clone
30 remote_tmp="${SSH_SALT}:$(basename "${OPNFV_TMP_DIR}")"
31 rsync -Erl --delete -e "ssh ${SSH_OPTS}" \
32   --exclude-from="${F_GIT_ROOT}/.gitignore" \
33   "${LOCAL_GIT_DIR}/" "${remote_tmp}/"
34 if [ -n "${LOCAL_PDF_RECLASS}" ] && [ -f "${LOCAL_PDF_RECLASS}" ]; then
35   rsync -e "ssh ${SSH_OPTS}" "${LOCAL_PDF_RECLASS}" \
36     "${remote_tmp}${F_GIT_SUBD}/mcp/${OPNFV_RDIR}/opnfv/"
37 fi
38 local_vcp_img=$(dirname "${LOCAL_PDF_RECLASS}")/$(basename "${OPNFV_VCP_IMG}")
39 if [ -e "${local_vcp_img}" ]; then
40   rsync -L -e "ssh ${SSH_OPTS}" "${local_vcp_img}" \
41     "${remote_tmp}${F_GIT_SUBD}/${OPNFV_VCP_IMG}"
42 fi
43
44 # ssh to cfg01
45 # shellcheck disable=SC2086,2087
46 ssh ${SSH_OPTS} "${SSH_SALT}" bash -s -e << SALT_INSTALL_END
47   sudo -i
48   set -e
49   export TERM=${TERM}
50   export CI_DEBUG=${CI_DEBUG}; [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
51
52   echo -n 'Checking out cloud-init has finished running ...'
53   while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo -n '.'; sleep 1; done
54   echo ' done'
55
56   mkdir -p /srv/salt /usr/share/salt-formulas/reclass
57   rm -rf ${OPNFV_GIT_DIR} ${OPNFV_FUEL_DIR}
58   mv ${OPNFV_TMP_DIR} ${OPNFV_GIT_DIR} && chown -R root.root ${OPNFV_GIT_DIR}
59   find ${OPNFV_GIT_DIR} -name '.git' -type f | while read f_git; do
60     sed -i 's@${LOCAL_GIT_DIR}@${OPNFV_GIT_DIR}@g' \$f_git
61   done
62   ln -sf ${OPNFV_GIT_DIR}${F_GIT_SUBD} ${OPNFV_FUEL_DIR}
63   ln -sf ${OPNFV_FUEL_DIR}/mcp/reclass /srv/salt
64   ln -sf ${OPNFV_FUEL_DIR}/mcp/deploy/scripts /srv/salt
65   cd /srv/salt/${OPNFV_RDIR} && rm -f arch && ln -sf "\$(uname -i)" arch
66
67   cp -r ${OPNFV_FUEL_DIR}/mcp/metadata/service /usr/share/salt-formulas/reclass
68   cd /srv/salt/reclass/classes/service && \
69     ln -sf /usr/share/salt-formulas/reclass/service/opendaylight
70
71   cd /srv/salt/scripts
72   export DEBIAN_FRONTEND=noninteractive
73   echo 'Dpkg::Use-Pty "0";' > /etc/apt/apt.conf.d/90silence-dpkg
74   OLD_DOMAIN=\$(grep -sPzo "id: cfg01\.\K(\S*)" /etc/salt/minion.d/minion.conf) || true
75   BOOTSTRAP_SALTSTACK_OPTS=" -r -dX stable 2016.11 " \
76     MASTER_HOSTNAME=cfg01.${CLUSTER_DOMAIN} DISTRIB_REVISION=stable \
77       EXTRA_FORMULAS="nfs" \
78         ./salt-master-init.sh
79   salt-key -Ay
80
81   cp -r ${OPNFV_FUEL_DIR}/mcp/salt-formulas/* /usr/share/salt-formulas/env
82   cd ${OPNFV_FUEL_DIR}/mcp/patches && ./patch.sh patches.list formulas
83   cd ${OPNFV_FUEL_DIR}/mcp/patches && ./patch.sh patches.list reclass
84
85   source ${OPNFV_FUEL_DIR}/mcp/scripts/lib.sh
86   wait_for 3.0 "salt-call state.apply salt"
87
88   # In case scenario changed (and implicitly domain name), re-register minions
89   if [ -n "\${OLD_DOMAIN}" ] && [ "\${OLD_DOMAIN}" != "${CLUSTER_DOMAIN}" ]; then
90     salt "*.\${OLD_DOMAIN}" cmd.run "grep \${OLD_DOMAIN} -sRl /etc/salt | \
91       xargs --no-run-if-empty sed -i 's/\${OLD_DOMAIN}/${CLUSTER_DOMAIN}/g'; \
92         service salt-minion restart" || true
93     salt-key -yd "*.\${OLD_DOMAIN}"
94     salt-key -Ay
95   fi
96
97   # Init specific to VMs on FN (all for virtual, cfg|mas for baremetal)
98   wait_for 3.0 'salt -C "cfg01*" state.apply linux'
99   if [[ "${LOCAL_VIRT_NODES}" =~ mas ]]; then
100     wait_for 3.0 'salt -C "mas*" test.ping'
101   else
102     wait_for 3.0 'for n in ${LOCAL_VIRT_NODES}; do salt -C \${n}.* test.ping; done'
103   fi
104   wait_for 3.0 'salt -C "E@^(${NODE_MASK}|cfg01).*" saltutil.sync_all'
105   wait_for 3.0 'salt -C "E@^(${NODE_MASK}|cfg01).*" state.apply salt'
106
107   wait_for 3.0 'salt -C "E@^(${NODE_MASK}).*" state.sls linux.system,linux.storage'
108   salt -C "E@^(${NODE_MASK}).*" state.sls linux.network -b 1 || true
109   salt -C "E@^(${NODE_MASK}).*" system.reboot
110   wait_for 90.0 'salt -C "E@^(${NODE_MASK}).*" test.ping'
111   wait_for 3.0 'salt -C "E@^(${NODE_MASK}).*" pkg.upgrade refresh=False'
112
113   wait_for 3.0 'salt -C "E@^(${NODE_MASK}|cfg01).*" state.sls ntp'
114
115   if [ -f "${OPNFV_FUEL_DIR}/${OPNFV_VCP_IMG}" ]; then
116     mkdir -p "${OPNFV_VCP_DIR}"
117     mv "${OPNFV_FUEL_DIR}/${OPNFV_VCP_IMG}" "${OPNFV_VCP_DIR}/"
118   fi
119
120   # symlink manually until package with required commit is available
121   cd /usr/share/salt-formulas/env/aodh/files
122   ln -sf ocata pike
123 SALT_INSTALL_END