Merge "[salt-formulas] opnfv: Drop obsolete route_wrapper"
[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 # shellcheck disable=SC2116,SC2086
26 LOCAL_VIRT_NODES=$(echo ${*//cfg01/}) # unquoted to filter space
27 NODE_MASK="${LOCAL_VIRT_NODES// /|}"
28
29 # push to cfg01 current git repo first (including submodules), at ~ubuntu/opnfv
30 # later we move it to ~root/opnfv (and ln as ~root/fuel); delete the temp clone
31 remote_tmp="${SSH_SALT}:$(basename "${OPNFV_TMP_DIR}")"
32 STORAGE_DIR=$(dirname "${LOCAL_PDF_RECLASS}")
33 REL_STORAGE_DIR_PATH=${STORAGE_DIR#${LOCAL_GIT_DIR}}
34 if [[ "${REL_STORAGE_DIR_PATH}" == "${STORAGE_DIR}" ]]
35 then
36   REL_STORAGE_DIR_PATH=""
37 fi
38 rsync -Erl --delete -e "ssh ${SSH_OPTS}" \
39   --exclude={.gitignore,"$REL_STORAGE_DIR_PATH"} \
40   "${LOCAL_GIT_DIR}/" "${remote_tmp}/"
41 if [ -n "${LOCAL_PDF_RECLASS}" ] && [ -f "${LOCAL_PDF_RECLASS}" ]; then
42   rsync -e "ssh ${SSH_OPTS}" "${LOCAL_PDF_RECLASS}" \
43     "${remote_tmp}${F_GIT_SUBD}/mcp/${OPNFV_RDIR}/opnfv/"
44 fi
45 local_vcp_img=$(dirname "${LOCAL_PDF_RECLASS}")/$(basename "${OPNFV_VCP_IMG}")
46 if [ -e "${local_vcp_img}" ]; then
47   rsync -L -e "ssh ${SSH_OPTS}" "${local_vcp_img}" \
48     "${remote_tmp}${F_GIT_SUBD}/${OPNFV_VCP_IMG}"
49 fi
50
51 # ssh to cfg01
52 # shellcheck disable=SC2086,2087
53 ssh ${SSH_OPTS} "${SSH_SALT}" bash -s -e << SALT_INSTALL_END
54   sudo -i
55   set -e
56   export TERM=${TERM}
57   export CI_DEBUG=${CI_DEBUG}; [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
58
59   echo -n 'Checking out cloud-init has finished running ...'
60   while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo -n '.'; sleep 1; done
61   echo ' done'
62
63   mkdir -p /srv/salt /usr/share/salt-formulas/reclass
64   rm -rf ${OPNFV_GIT_DIR} ${OPNFV_FUEL_DIR}
65   mv ${OPNFV_TMP_DIR} ${OPNFV_GIT_DIR} && chown -R root.root ${OPNFV_GIT_DIR}
66   find ${OPNFV_GIT_DIR} -name '.git' -type f | while read f_git; do
67     sed -i 's@${LOCAL_GIT_DIR}@${OPNFV_GIT_DIR}@g' \$f_git
68   done
69   ln -sf ${OPNFV_GIT_DIR}${F_GIT_SUBD} ${OPNFV_FUEL_DIR}
70   ln -sf ${OPNFV_FUEL_DIR}/mcp/reclass /srv/salt
71   ln -sf ${OPNFV_FUEL_DIR}/mcp/deploy/scripts /srv/salt
72   ln -sf ${OPNFV_FUEL_DIR}/mcp/scripts/mcp.rsa $(dirname "${OPNFV_FUEL_DIR}")
73
74   cp -r ${OPNFV_FUEL_DIR}/mcp/metadata/service /usr/share/salt-formulas/reclass
75   cd /srv/salt/reclass/classes/service && \
76     ln -sf /usr/share/salt-formulas/reclass/service/opendaylight
77
78   # Armband APT-MK nightly/extra repo for forked & extended reclass
79   wget -qO - https://linux.enea.com/apt-mk/public.gpg | apt-key add -
80   echo 'deb http://linux.enea.com/apt-mk/xenial nightly extra' > \
81     '/etc/apt/sources.list.d/armband_mcp_extra.list'
82   apt-get update
83
84   cd /srv/salt/scripts
85   export DEBIAN_FRONTEND=noninteractive
86   echo 'Dpkg::Use-Pty "0";' > /etc/apt/apt.conf.d/90silence-dpkg
87   OLD_DOMAIN=\$(grep -sPzo "id: cfg01\.\K(\S*)" /etc/salt/minion.d/minion.conf) || true
88   BOOTSTRAP_SALTSTACK_OPTS=" -r -dX stable 2016.11 " \
89     MASTER_HOSTNAME=cfg01.${CLUSTER_DOMAIN} DISTRIB_REVISION=nightly \
90       EXTRA_FORMULAS="nfs panko gnocchi oslo-templates" \
91         ./salt-master-init.sh
92   salt-key -Ay
93
94   cp -r ${OPNFV_FUEL_DIR}/mcp/salt-formulas/* /usr/share/salt-formulas/env
95   cd ${OPNFV_FUEL_DIR}/mcp/patches && ./patch.sh patches.list formulas
96   cd ${OPNFV_FUEL_DIR}/mcp/patches && ./patch.sh patches.list reclass
97
98   source ${OPNFV_FUEL_DIR}/mcp/scripts/lib.sh
99   wait_for 3.0 "salt-call state.apply salt"
100
101   # In case scenario changed (and implicitly domain name), re-register minions
102   if [ -n "\${OLD_DOMAIN}" ] && [ "\${OLD_DOMAIN}" != "${CLUSTER_DOMAIN}" ]; then
103     salt "*.\${OLD_DOMAIN}" cmd.run "grep \${OLD_DOMAIN} -sRl /etc/salt | \
104       xargs --no-run-if-empty sed -i 's/\${OLD_DOMAIN}/${CLUSTER_DOMAIN}/g'; \
105         service salt-minion restart" || true
106     salt-key -yd "*.\${OLD_DOMAIN}"
107     salt-key -Ay
108   fi
109
110   # Init specific to VMs on FN (all for virtual, cfg|mas for baremetal)
111   wait_for 3.0 'salt -C "cfg01*" state.apply linux'
112   if [[ "${LOCAL_VIRT_NODES}" =~ mas ]]; then
113     wait_for 3.0 'salt -C "mas*" test.ping'
114   else
115     wait_for 3.0 '(for n in ${LOCAL_VIRT_NODES}; do salt -C \${n}.* test.ping || exit; done)'
116   fi
117   wait_for 3.0 'salt -C "E@^(${NODE_MASK}|cfg01).*" saltutil.sync_all'
118   wait_for 3.0 'salt -C "E@^(${NODE_MASK}|cfg01).*" state.apply salt'
119
120   wait_for 3.0 'salt -C "E@^(${NODE_MASK}).*" state.sls linux.system,linux.storage'
121   wait_for 2.0 'salt -C "E@^(${NODE_MASK}).*" state.sls linux.network'
122   salt -C "E@^(${NODE_MASK}).*" system.reboot
123   wait_for 90.0 'salt -C "E@^(${NODE_MASK}).*" test.ping'
124   wait_for 3.0 'salt -C "E@^(${NODE_MASK}).*" pkg.upgrade refresh=False dist_upgrade=True'
125
126   wait_for 3.0 'salt -C "E@^(${NODE_MASK}|cfg01).*" state.sls ntp'
127
128   if [ -f "${OPNFV_FUEL_DIR}/${OPNFV_VCP_IMG}" ]; then
129     mkdir -p "${OPNFV_VCP_DIR}"
130     mv "${OPNFV_FUEL_DIR}/${OPNFV_VCP_IMG}" "${OPNFV_VCP_DIR}/"
131   fi
132 SALT_INSTALL_END