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