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(){ |
Ivan Berezovskiy | 6b938d9 | 2018-09-11 16:12:39 +0400 | [diff] [blame^] | 85 | postgres_enabled=$(salt-call --out=text pillar.get postgresql:server:enabled | awk '{print $2}' | tr "[:upper:]" "[:lower:]") |
| 86 | if [[ "${postgres_enabled}" == "true" ]]; then |
| 87 | salt-call ${SALT_OPTS} state.sls postgresql.server |
| 88 | fi |
| 89 | |
azvyagintsev | 42a4762 | 2018-07-06 13:12:08 +0300 | [diff] [blame] | 90 | _region=$(salt-call --out=text pillar.get maas:region:enabled | awk '{print $2}' | tr "[:upper:]" "[:lower:]" ) |
| 91 | if [[ "${maas_cluster_enabled}" == 'true' ]]; then |
| 92 | salt-call ${SALT_OPTS} state.sls maas.cluster |
| 93 | else |
| 94 | echo 'WARNING: maas.cluster skipped!' |
| 95 | fi |
| 96 | if [[ "$_region" == 'true' ]]; then |
azvyagintsev | d453a71 | 2018-08-03 10:50:59 +0200 | [diff] [blame] | 97 | # FIXME MAAS still can fail in rare race condition. |
| 98 | salt-call ${SALT_OPTS} state.sls maas.region || salt-call ${SALT_OPTS} state.sls maas.region |
azvyagintsev | 42a4762 | 2018-07-06 13:12:08 +0300 | [diff] [blame] | 99 | else |
| 100 | echo 'WARNING: maas.region skipped!' |
| 101 | fi |
| 102 | # Don't move it under first cluster-only check! |
| 103 | if [[ "${maas_cluster_enabled}" == 'true' ]]; then |
| 104 | _post_maas_cfg |
| 105 | fi |
| 106 | } |
| 107 | |
| 108 | function process_jenkins(){ |
| 109 | _jjobs=$(salt-call --out=text pillar.get jenkins:client:job | awk '{print $2}') |
| 110 | if [[ "${_jjobs}" != '' ]]; then |
| 111 | salt-call ${SALT_OPTS} state.sls jenkins.client |
| 112 | fi |
| 113 | } |
| 114 | |
Petr Ruzicka | 6982da3 | 2018-08-09 15:11:06 +0200 | [diff] [blame] | 115 | failsafe_ssh_key(){ |
| 116 | if [ -f /mnt/root_auth_keys ]; then |
| 117 | echo "Installing failsafe public ssh key from /mnt/root_auth_keys to /root/.ssh/authorized_keys" |
| 118 | install -m 0700 -d /root/.ssh |
| 119 | cat /mnt/root_auth_keys >> /root/.ssh/authorized_keys |
| 120 | chmod 600 /root/.ssh/authorized_keys |
| 121 | sed -i 's/^PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config |
| 122 | sed -i 's/^PasswordAuthentication.*/PasswordAuthentication yes/g' /etc/ssh/sshd_config |
| 123 | service ssh restart |
| 124 | fi |
| 125 | } |
| 126 | |
azvyagintsev | b2a5579 | 2018-06-05 17:38:17 +0300 | [diff] [blame] | 127 | # Body ======================================================================== |
| 128 | process_network |
Richard Felkl | 4c4829d | 2017-11-11 00:12:20 +0100 | [diff] [blame] | 129 | |
| 130 | echo "Preparing metadata model" |
| 131 | mount /dev/cdrom /mnt/ |
Leontii Istomin | 68553f1 | 2018-02-21 18:10:12 +0100 | [diff] [blame] | 132 | cp -rT /mnt/model/model /srv/salt/reclass |
azvyagintsev | b2a5579 | 2018-06-05 17:38:17 +0300 | [diff] [blame] | 133 | chown -R root:root /srv/salt/reclass/* || true |
alexz | d90608b | 2018-04-12 23:49:03 +0200 | [diff] [blame] | 134 | chown -R root:root /srv/salt/reclass/.git* || true |
| 135 | chmod -R 644 /srv/salt/reclass/classes/cluster/* || true |
| 136 | chmod -R 644 /srv/salt/reclass/classes/system/* || true |
Richard Felkl | 4c4829d | 2017-11-11 00:12:20 +0100 | [diff] [blame] | 137 | |
Petr Ruzicka | 6982da3 | 2018-08-09 15:11:06 +0200 | [diff] [blame] | 138 | failsafe_ssh_key |
| 139 | |
Richard Felkl | 4c4829d | 2017-11-11 00:12:20 +0100 | [diff] [blame] | 140 | echo "Configuring salt" |
Richard Felkl | 4c4829d | 2017-11-11 00:12:20 +0100 | [diff] [blame] | 141 | envsubst < /root/minion.conf > /etc/salt/minion.d/minion.conf |
azvyagintsev | b2a5579 | 2018-06-05 17:38:17 +0300 | [diff] [blame] | 142 | enable_services |
Dennis Dmitriev | e00716b | 2018-07-05 14:50:45 +0300 | [diff] [blame] | 143 | |
| 144 | # Wait for salt-master and salt-minion to wake up after restart |
| 145 | salt-call --timeout=120 test.ping |
| 146 | |
Richard Felkl | 4c4829d | 2017-11-11 00:12:20 +0100 | [diff] [blame] | 147 | while true; do |
| 148 | salt-key | grep "$SALT_MASTER_MINION_ID" && break |
| 149 | sleep 5 |
| 150 | done |
azvyagintsev | b2a5579 | 2018-06-05 17:38:17 +0300 | [diff] [blame] | 151 | |
Richard Felkl | 4c4829d | 2017-11-11 00:12:20 +0100 | [diff] [blame] | 152 | 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' |
| 153 | |
azvyagintsev | b2a5579 | 2018-06-05 17:38:17 +0300 | [diff] [blame] | 154 | echo "updating local git repos" |
alexz | d90608b | 2018-04-12 23:49:03 +0200 | [diff] [blame] | 155 | if [[ "$PIPELINES_FROM_ISO" == "true" ]] ; then |
Jiri Broulik | cee2053 | 2018-01-08 13:30:15 +0100 | [diff] [blame] | 156 | cp -r /mnt/mk-pipelines/* /home/repo/mk/mk-pipelines/ |
| 157 | cp -r /mnt/pipeline-library/* /home/repo/mcp-ci/pipeline-library/ |
alexz | d90608b | 2018-04-12 23:49:03 +0200 | [diff] [blame] | 158 | umount /dev/cdrom || true |
Jiri Broulik | cee2053 | 2018-01-08 13:30:15 +0100 | [diff] [blame] | 159 | chown -R git:www-data /home/repo/mk/mk-pipelines/* |
| 160 | chown -R git:www-data /home/repo/mcp-ci/pipeline-library/* |
| 161 | else |
alexz | d90608b | 2018-04-12 23:49:03 +0200 | [diff] [blame] | 162 | umount /dev/cdrom || true |
| 163 | git clone --mirror "${PIPELINE_REPO_URL}/mk-pipelines.git" /home/repo/mk/mk-pipelines/ |
| 164 | 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] | 165 | chown -R git:www-data /home/repo/mk/mk-pipelines/* |
| 166 | chown -R git:www-data /home/repo/mcp-ci/pipeline-library/* |
| 167 | fi |
| 168 | |
azvyagintsev | b2a5579 | 2018-06-05 17:38:17 +0300 | [diff] [blame] | 169 | process_formulas |
Richard Felkl | 98b4534 | 2018-04-06 13:30:28 +0200 | [diff] [blame] | 170 | |
Richard Felkl | 4c4829d | 2017-11-11 00:12:20 +0100 | [diff] [blame] | 171 | salt-call saltutil.refresh_pillar |
| 172 | salt-call saltutil.sync_all |
alexz | d90608b | 2018-04-12 23:49:03 +0200 | [diff] [blame] | 173 | if ! $(reclass -n ${SALT_MASTER_MINION_ID} > /dev/null ) ; then |
| 174 | echo "ERROR: Reclass render failed!" |
| 175 | exit 1 |
Jiri Broulik | 0173a27 | 2018-04-11 10:56:35 +0200 | [diff] [blame] | 176 | fi |
| 177 | |
Dennis Dmitriev | b5c17a0 | 2018-07-06 13:07:49 +0300 | [diff] [blame] | 178 | salt-call ${SALT_OPTS} state.sls linux.network,linux,openssh |
Dennis Dmitriev | e00716b | 2018-07-05 14:50:45 +0300 | [diff] [blame] | 179 | # PROD-21179: Run salt.minion.ca to prepare CA certificate before salt.minion.cert is used |
| 180 | salt-call ${SALT_OPTS} state.sls salt.minion.ca |
Dennis Dmitriev | b5c17a0 | 2018-07-06 13:07:49 +0300 | [diff] [blame] | 181 | salt-call ${SALT_OPTS} state.sls salt |
azvyagintsev | b2a5579 | 2018-06-05 17:38:17 +0300 | [diff] [blame] | 182 | salt-call ${SALT_OPTS} pkg.install salt-master,salt-minion |
Dennis Dmitriev | e00716b | 2018-07-05 14:50:45 +0300 | [diff] [blame] | 183 | |
Jiri Broulik | 6b7ca67 | 2018-04-19 13:16:32 +0200 | [diff] [blame] | 184 | sleep 5 |
Dennis Dmitriev | e00716b | 2018-07-05 14:50:45 +0300 | [diff] [blame] | 185 | # Wait for salt-master and salt-minion to wake up after restart |
| 186 | salt-call --timeout=120 test.ping |
| 187 | |
azvyagintsev | b2a5579 | 2018-06-05 17:38:17 +0300 | [diff] [blame] | 188 | salt-call ${SALT_OPTS} state.sls salt |
azvyagintsev | b2a5579 | 2018-06-05 17:38:17 +0300 | [diff] [blame] | 189 | salt-call ${SALT_OPTS} state.sls reclass |
Jiri Broulik | 1261ca3 | 2018-01-18 15:30:25 +0100 | [diff] [blame] | 190 | |
azvyagintsev | 42a4762 | 2018-07-06 13:12:08 +0300 | [diff] [blame] | 191 | maas_cluster_enabled=$(salt-call --out=text pillar.get maas:cluster:enabled | awk '{print $2}' | tr "[:upper:]" "[:lower:]" ) |
| 192 | process_maas |
alexz | d90608b | 2018-04-12 23:49:03 +0200 | [diff] [blame] | 193 | |
| 194 | ssh-keyscan cfg01 > /var/lib/jenkins/.ssh/known_hosts || true |
| 195 | |
azvyagintsev | 42a4762 | 2018-07-06 13:12:08 +0300 | [diff] [blame] | 196 | process_jenkins |
Richard Felkl | 4c4829d | 2017-11-11 00:12:20 +0100 | [diff] [blame] | 197 | |
azvyagintsev | b2a5579 | 2018-06-05 17:38:17 +0300 | [diff] [blame] | 198 | stop_services="salt-api salt-master salt-minion jenkins maas-rackd.service maas-regiond.service postgresql.service" |
| 199 | for s in ${stop_services} ; do |
| 200 | systemctl stop ${s} || true |
| 201 | sleep 1 |
| 202 | done |
| 203 | sync |
Dmitry Stremkouski | a94b5f3 | 2017-12-02 00:41:54 +0300 | [diff] [blame] | 204 | reboot |