Richard Felkl | 4c4829d | 2017-11-11 00:12:20 +0100 | [diff] [blame] | 1 | #!/bin/bash -xe |
alexz | d90608b | 2018-04-12 23:49:03 +0200 | [diff] [blame] | 2 | |
azvyagintsev | b2a5579 | 2018-06-05 17:38:17 +0300 | [diff] [blame] | 3 | #============================================================================== |
| 4 | # Required packages: |
| 5 | # apt-get install -y jq |
| 6 | #============================================================================== |
Petr Ruzicka | 6f9aea0 | 2018-06-19 16:46:01 +0200 | [diff] [blame] | 7 | export SALT_MASTER_DEPLOY_IP=${SALT_MASTER_DEPLOY_IP:-"172.16.164.15"} |
| 8 | export SALT_MASTER_MINION_ID=${SALT_MASTER_MINION_ID:-"cfg01.deploy-name.local"} |
| 9 | export DEPLOY_NETWORK_GW=${DEPLOY_NETWORK_GW:-"172.16.164.1"} |
| 10 | export DEPLOY_NETWORK_NETMASK=${DEPLOY_NETWORK_NETMASK:-"255.255.255.192"} |
Petr Ruzicka | a540745 | 2018-07-03 12:30:16 +0200 | [diff] [blame] | 11 | export DEPLOY_NETWORK_MTU=${DEPLOY_NETWORK_MTU:-"1500"} |
Petr Ruzicka | 6f9aea0 | 2018-06-19 16:46:01 +0200 | [diff] [blame] | 12 | export DNS_SERVERS=${DNS_SERVERS:-"8.8.8.8"} |
| 13 | export http_proxy=${http_proxy:-""} |
| 14 | export https_proxy=${https_proxy:-""} |
| 15 | export PIPELINES_FROM_ISO=${PIPELINES_FROM_ISO:-"true"} |
| 16 | export PIPELINE_REPO_URL=${PIPELINE_REPO_URL:-"https://github.com/Mirantis"} |
| 17 | export MCP_VERSION=${MCP_VERSION:-"stable"} |
| 18 | export MCP_SALT_REPO_KEY=${MCP_SALT_REPO_KEY:-"http://apt.mirantis.com/public.gpg"} |
| 19 | export MCP_SALT_REPO_URL=${MCP_SALT_REPO_URL:-"http://apt.mirantis.com/xenial"} |
Richard Felkl | 98b4534 | 2018-04-06 13:30:28 +0200 | [diff] [blame] | 20 | export MCP_SALT_REPO="deb [arch=amd64] $MCP_SALT_REPO_URL $MCP_VERSION salt" |
| 21 | export FORMULAS="salt-formula-*" |
azvyagintsev | b2a5579 | 2018-06-05 17:38:17 +0300 | [diff] [blame] | 22 | # for cloning from aptly image use port 8088 |
Jiri Broulik | cee2053 | 2018-01-08 13:30:15 +0100 | [diff] [blame] | 23 | #export PIPELINE_REPO_URL=http://172.16.47.182:8088 |
azvyagintsev | b2a5579 | 2018-06-05 17:38:17 +0300 | [diff] [blame] | 24 | # |
| 25 | SALT_OPTS="-l debug -t 10 --retcode-passthrough --no-color" |
Richard Felkl | 4c4829d | 2017-11-11 00:12:20 +0100 | [diff] [blame] | 26 | |
azvyagintsev | b2a5579 | 2018-06-05 17:38:17 +0300 | [diff] [blame] | 27 | # Funcs ======================================================================= |
alexz | d90608b | 2018-04-12 23:49:03 +0200 | [diff] [blame] | 28 | function _post_maas_cfg(){ |
azvyagintsev | b2a5579 | 2018-06-05 17:38:17 +0300 | [diff] [blame] | 29 | chmod 0755 /var/lib/maas/.maas_login.sh |
| 30 | source /var/lib/maas/.maas_login.sh |
| 31 | # disable backports for maas enlist pkg repo. Those operation enforce maas |
| 32 | # to re-create sources.list and drop [source] fetch-definition from it. |
| 33 | main_arch_id=$(maas ${PROFILE} package-repositories read | jq -r '.[] | select(.name=="main_archive") | .id') |
| 34 | maas ${PROFILE} package-repository update ${main_arch_id} "disabled_pockets=backports" || true |
| 35 | maas ${PROFILE} package-repository update ${main_arch_id} "disabled_components=multiverse" || true |
| 36 | maas ${PROFILE} package-repository update ${main_arch_id} "arches=amd64" || true |
| 37 | # Remove stale notifications, which appear during sources configuration. |
| 38 | for i in $(maas ${PROFILE} notifications read | jq '.[]| .id'); do |
| 39 | maas ${PROFILE} notification delete ${i} || true |
Pavel Cizinsky | 5f8fb09 | 2018-05-30 17:27:19 +0200 | [diff] [blame] | 40 | done |
alexz | d90608b | 2018-04-12 23:49:03 +0200 | [diff] [blame] | 41 | } |
| 42 | |
azvyagintsev | b2a5579 | 2018-06-05 17:38:17 +0300 | [diff] [blame] | 43 | function process_formulas(){ |
| 44 | local RECLASS_ROOT=${RECLASS_ROOT:-/srv/salt/reclass/} |
| 45 | local FORMULAS_PATH=${FORMULAS_PATH:-/usr/share/salt-formulas} |
| 46 | |
| 47 | echo "Configuring formulas ..." |
| 48 | curl -s $MCP_SALT_REPO_KEY | apt-key add - |
| 49 | echo $MCP_SALT_REPO > /etc/apt/sources.list.d/mcp_salt.list |
| 50 | apt-get update |
| 51 | apt-get install -y $FORMULAS |
| 52 | |
| 53 | [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service |
| 54 | for formula_service in $(ls /usr/share/salt-formulas/reclass/service/); do |
| 55 | #Since some salt formula names contain "-" and in symlinks they should contain "_" adding replacement |
| 56 | formula_service=${formula_service//-/$'_'} |
Dennis Dmitriev | e00716b | 2018-07-05 14:50:45 +0300 | [diff] [blame] | 57 | if [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ]; then |
azvyagintsev | b2a5579 | 2018-06-05 17:38:17 +0300 | [diff] [blame] | 58 | ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service} |
Dennis Dmitriev | e00716b | 2018-07-05 14:50:45 +0300 | [diff] [blame] | 59 | fi |
azvyagintsev | b2a5579 | 2018-06-05 17:38:17 +0300 | [diff] [blame] | 60 | done |
| 61 | } |
| 62 | |
| 63 | function enable_services(){ |
| 64 | local services="postgresql.service salt-api salt-master salt-minion jenkins" |
| 65 | for s in ${services} ; do |
| 66 | systemctl enable ${s} || true |
| 67 | systemctl restart ${s} || true |
| 68 | done |
| 69 | } |
| 70 | |
| 71 | function process_network(){ |
| 72 | echo "Configuring network interfaces" |
| 73 | find /etc/network/interfaces.d/ -type f -delete |
| 74 | kill $(pidof /sbin/dhclient) || /bin/true |
| 75 | envsubst < /root/interfaces > /etc/network/interfaces |
| 76 | ip a flush dev ens3 |
| 77 | rm -f /var/run/network/ifstate.ens3 |
| 78 | if [[ $(grep -E '^\ *gateway\ ' /etc/network/interfaces) ]]; then |
| 79 | (ip r s | grep ^default) && ip r d default || /bin/true |
| 80 | fi; |
| 81 | ifup ens3 |
| 82 | } |
| 83 | |
azvyagintsev | 42a4762 | 2018-07-06 13:12:08 +0300 | [diff] [blame] | 84 | function process_maas(){ |
| 85 | _region=$(salt-call --out=text pillar.get maas:region:enabled | awk '{print $2}' | tr "[:upper:]" "[:lower:]" ) |
| 86 | if [[ "${maas_cluster_enabled}" == 'true' ]]; then |
| 87 | salt-call ${SALT_OPTS} state.sls maas.cluster |
| 88 | else |
| 89 | echo 'WARNING: maas.cluster skipped!' |
| 90 | fi |
| 91 | if [[ "$_region" == 'true' ]]; then |
| 92 | salt-call ${SALT_OPTS} state.sls maas.region |
| 93 | else |
| 94 | echo 'WARNING: maas.region skipped!' |
| 95 | fi |
| 96 | # Don't move it under first cluster-only check! |
| 97 | if [[ "${maas_cluster_enabled}" == 'true' ]]; then |
| 98 | _post_maas_cfg |
| 99 | fi |
| 100 | } |
| 101 | |
| 102 | function process_jenkins(){ |
| 103 | _jjobs=$(salt-call --out=text pillar.get jenkins:client:job | awk '{print $2}') |
| 104 | if [[ "${_jjobs}" != '' ]]; then |
| 105 | salt-call ${SALT_OPTS} state.sls jenkins.client |
| 106 | fi |
| 107 | } |
| 108 | |
azvyagintsev | b2a5579 | 2018-06-05 17:38:17 +0300 | [diff] [blame] | 109 | # Body ======================================================================== |
| 110 | process_network |
Richard Felkl | 4c4829d | 2017-11-11 00:12:20 +0100 | [diff] [blame] | 111 | |
| 112 | echo "Preparing metadata model" |
| 113 | mount /dev/cdrom /mnt/ |
Leontii Istomin | 68553f1 | 2018-02-21 18:10:12 +0100 | [diff] [blame] | 114 | cp -rT /mnt/model/model /srv/salt/reclass |
azvyagintsev | b2a5579 | 2018-06-05 17:38:17 +0300 | [diff] [blame] | 115 | chown -R root:root /srv/salt/reclass/* || true |
alexz | d90608b | 2018-04-12 23:49:03 +0200 | [diff] [blame] | 116 | chown -R root:root /srv/salt/reclass/.git* || true |
| 117 | chmod -R 644 /srv/salt/reclass/classes/cluster/* || true |
| 118 | chmod -R 644 /srv/salt/reclass/classes/system/* || true |
Richard Felkl | 4c4829d | 2017-11-11 00:12:20 +0100 | [diff] [blame] | 119 | |
Richard Felkl | 4c4829d | 2017-11-11 00:12:20 +0100 | [diff] [blame] | 120 | echo "Configuring salt" |
Richard Felkl | 4c4829d | 2017-11-11 00:12:20 +0100 | [diff] [blame] | 121 | envsubst < /root/minion.conf > /etc/salt/minion.d/minion.conf |
azvyagintsev | b2a5579 | 2018-06-05 17:38:17 +0300 | [diff] [blame] | 122 | enable_services |
Dennis Dmitriev | e00716b | 2018-07-05 14:50:45 +0300 | [diff] [blame] | 123 | |
| 124 | # Wait for salt-master and salt-minion to wake up after restart |
| 125 | salt-call --timeout=120 test.ping |
| 126 | |
Richard Felkl | 4c4829d | 2017-11-11 00:12:20 +0100 | [diff] [blame] | 127 | while true; do |
| 128 | salt-key | grep "$SALT_MASTER_MINION_ID" && break |
| 129 | sleep 5 |
| 130 | done |
azvyagintsev | b2a5579 | 2018-06-05 17:38:17 +0300 | [diff] [blame] | 131 | |
Vasyl Saienko | b873ca4 | 2018-07-19 19:23:10 +0000 | [diff] [blame] | 132 | sleep 5 |
| 133 | for i in $(salt-key -l accepted | grep -v Accepted | grep -v "$SALT_MASTER_MINION_ID"); do |
| 134 | salt-key -d $i -y |
| 135 | done |
| 136 | |
Richard Felkl | 4c4829d | 2017-11-11 00:12:20 +0100 | [diff] [blame] | 137 | find /var/lib/jenkins/jenkins.model.JenkinsLocationConfiguration.xml -type f -print0 | xargs -0 sed -i -e 's/10.167.4.15/'$SALT_MASTER_DEPLOY_IP'/g' |
| 138 | |
azvyagintsev | b2a5579 | 2018-06-05 17:38:17 +0300 | [diff] [blame] | 139 | echo "updating local git repos" |
alexz | d90608b | 2018-04-12 23:49:03 +0200 | [diff] [blame] | 140 | if [[ "$PIPELINES_FROM_ISO" == "true" ]] ; then |
Jiri Broulik | cee2053 | 2018-01-08 13:30:15 +0100 | [diff] [blame] | 141 | cp -r /mnt/mk-pipelines/* /home/repo/mk/mk-pipelines/ |
| 142 | cp -r /mnt/pipeline-library/* /home/repo/mcp-ci/pipeline-library/ |
alexz | d90608b | 2018-04-12 23:49:03 +0200 | [diff] [blame] | 143 | umount /dev/cdrom || true |
Jiri Broulik | cee2053 | 2018-01-08 13:30:15 +0100 | [diff] [blame] | 144 | chown -R git:www-data /home/repo/mk/mk-pipelines/* |
| 145 | chown -R git:www-data /home/repo/mcp-ci/pipeline-library/* |
| 146 | else |
alexz | d90608b | 2018-04-12 23:49:03 +0200 | [diff] [blame] | 147 | umount /dev/cdrom || true |
| 148 | git clone --mirror "${PIPELINE_REPO_URL}/mk-pipelines.git" /home/repo/mk/mk-pipelines/ |
| 149 | git clone --mirror "${PIPELINE_REPO_URL}/pipeline-library.git" /home/repo/mcp-ci/pipeline-library/ |
Jiri Broulik | cee2053 | 2018-01-08 13:30:15 +0100 | [diff] [blame] | 150 | chown -R git:www-data /home/repo/mk/mk-pipelines/* |
| 151 | chown -R git:www-data /home/repo/mcp-ci/pipeline-library/* |
| 152 | fi |
| 153 | |
azvyagintsev | b2a5579 | 2018-06-05 17:38:17 +0300 | [diff] [blame] | 154 | process_formulas |
Richard Felkl | 98b4534 | 2018-04-06 13:30:28 +0200 | [diff] [blame] | 155 | |
Richard Felkl | 4c4829d | 2017-11-11 00:12:20 +0100 | [diff] [blame] | 156 | salt-call saltutil.refresh_pillar |
| 157 | salt-call saltutil.sync_all |
alexz | d90608b | 2018-04-12 23:49:03 +0200 | [diff] [blame] | 158 | if ! $(reclass -n ${SALT_MASTER_MINION_ID} > /dev/null ) ; then |
| 159 | echo "ERROR: Reclass render failed!" |
| 160 | exit 1 |
Jiri Broulik | 0173a27 | 2018-04-11 10:56:35 +0200 | [diff] [blame] | 161 | fi |
| 162 | |
Dennis Dmitriev | b5c17a0 | 2018-07-06 13:07:49 +0300 | [diff] [blame] | 163 | salt-call ${SALT_OPTS} state.sls linux.network,linux,openssh |
Dennis Dmitriev | e00716b | 2018-07-05 14:50:45 +0300 | [diff] [blame] | 164 | # PROD-21179: Run salt.minion.ca to prepare CA certificate before salt.minion.cert is used |
| 165 | salt-call ${SALT_OPTS} state.sls salt.minion.ca |
Dennis Dmitriev | b5c17a0 | 2018-07-06 13:07:49 +0300 | [diff] [blame] | 166 | salt-call ${SALT_OPTS} state.sls salt |
azvyagintsev | b2a5579 | 2018-06-05 17:38:17 +0300 | [diff] [blame] | 167 | salt-call ${SALT_OPTS} pkg.install salt-master,salt-minion |
Dennis Dmitriev | e00716b | 2018-07-05 14:50:45 +0300 | [diff] [blame] | 168 | |
Jiri Broulik | 6b7ca67 | 2018-04-19 13:16:32 +0200 | [diff] [blame] | 169 | sleep 5 |
Dennis Dmitriev | e00716b | 2018-07-05 14:50:45 +0300 | [diff] [blame] | 170 | # Wait for salt-master and salt-minion to wake up after restart |
| 171 | salt-call --timeout=120 test.ping |
| 172 | |
azvyagintsev | b2a5579 | 2018-06-05 17:38:17 +0300 | [diff] [blame] | 173 | salt-call ${SALT_OPTS} state.sls salt |
azvyagintsev | b2a5579 | 2018-06-05 17:38:17 +0300 | [diff] [blame] | 174 | salt-call ${SALT_OPTS} state.sls reclass |
Jiri Broulik | 1261ca3 | 2018-01-18 15:30:25 +0100 | [diff] [blame] | 175 | |
azvyagintsev | 42a4762 | 2018-07-06 13:12:08 +0300 | [diff] [blame] | 176 | maas_cluster_enabled=$(salt-call --out=text pillar.get maas:cluster:enabled | awk '{print $2}' | tr "[:upper:]" "[:lower:]" ) |
| 177 | process_maas |
alexz | d90608b | 2018-04-12 23:49:03 +0200 | [diff] [blame] | 178 | |
| 179 | ssh-keyscan cfg01 > /var/lib/jenkins/.ssh/known_hosts || true |
| 180 | |
azvyagintsev | 42a4762 | 2018-07-06 13:12:08 +0300 | [diff] [blame] | 181 | process_jenkins |
Richard Felkl | 4c4829d | 2017-11-11 00:12:20 +0100 | [diff] [blame] | 182 | |
azvyagintsev | b2a5579 | 2018-06-05 17:38:17 +0300 | [diff] [blame] | 183 | stop_services="salt-api salt-master salt-minion jenkins maas-rackd.service maas-regiond.service postgresql.service" |
| 184 | for s in ${stop_services} ; do |
| 185 | systemctl stop ${s} || true |
| 186 | sleep 1 |
| 187 | done |
| 188 | sync |
Dmitry Stremkouski | a94b5f3 | 2017-12-02 00:41:54 +0300 | [diff] [blame] | 189 | reboot |