blob: c008ed41d9bf8619e7667be09ee72ce368385e6a [file] [log] [blame]
Ivan Berezovskiy77187172018-09-14 15:47:36 +04001#cloud-config
Ivan Berezovskiy39fa6562018-10-09 12:58:55 +04002output:
3 all: '| tee -a /var/log/cloud-init-output.log /dev/tty0'
Ivan Berezovskiy77187172018-09-14 15:47:36 +04004write_files:
5 - owner: root:root
6 path: /etc/cloud/master_environment
7 permissions: '0644'
8 content: |
Dennis Dmitriev8fa33f92018-10-10 01:13:48 +03009 [ -f /etc/cloud/master_environment_override ] && . /etc/cloud/master_environment_override
10 export SALT_MASTER_DEPLOY_IP=${SALT_MASTER_DEPLOY_IP:-"172.16.164.15"}
11 export SALT_MASTER_MINION_ID=${SALT_MASTER_MINION_ID:-"cfg01.deploy-name.local"}
azvyagintsev829bfd82018-12-19 12:58:17 +020012
Dennis Dmitriev8fa33f92018-10-10 01:13:48 +030013 export http_proxy=${http_proxy:-""}
14 export https_proxy=${https_proxy:-""}
azvyagintsev829bfd82018-12-19 12:58:17 +020015
16 export MCP_VERSION=${MCP_VERSION:-"stable"}
17
Dennis Dmitriev8fa33f92018-10-10 01:13:48 +030018 export PIPELINES_FROM_ISO=${PIPELINES_FROM_ISO:-"true"}
Dzmitry Stremkouski9dd6a1b2019-01-24 12:03:58 +010019 export MK_PIPELINES_URL=${MK_PIPELINES_URL:-"https://gerrit.mcp.mirantis.com/mk/mk-pipelines.git"}
20 export PIPELINE_LIB_URL=${PIPELINE_LIB_URL:-"https://gerrit.mcp.mirantis.com/mcp-ci/pipeline-library.git"}
azvyagintsev829bfd82018-12-19 12:58:17 +020021
22 export MCP_SALT_REPO_URL=${MCP_SALT_REPO_URL:-"http://mirror.mirantis.com/"}
23 export MCP_SALT_REPO=${MCP_SALT_REPO:-"deb [arch=amd64] $MCP_SALT_REPO_URL/$MCP_VERSION/salt-formulas/xenial xenial main"}
24 export MCP_SALT_REPO_UPDATES=${MCP_SALT_REPO_UPDATES:-"deb [arch=amd64] $MCP_SALT_REPO_URL/update/$MCP_VERSION/salt-formulas/xenial xenial main"}
25 export MCP_SALT_REPO_KEY=${MCP_SALT_REPO_KEY:-"${MCP_SALT_REPO_URL}/${MCP_VERSION}/salt-formulas/xenial/archive-salt-formulas.key"}
26
Dennis Dmitriev8fa33f92018-10-10 01:13:48 +030027 export FORMULAS=${FORMULAS:-"salt-formula-*"}
azvyagintsev829bfd82018-12-19 12:58:17 +020028 export SALT_OPTS=${SALT_OPTS:-"-l debug -t 30 --retcode-passthrough --no-color"}
Dennis Dmitriev8fa33f92018-10-10 01:13:48 +030029 export CFG_BOOTSTRAP_DRIVE_URL=${CFG_BOOTSTRAP_DRIVE_URL:-""}
azvyagintsev829bfd82018-12-19 12:58:17 +020030
Ivan Berezovskiy77187172018-09-14 15:47:36 +040031master_config:
32 - &master_config |
33 function _post_maas_cfg() {
34 source /var/lib/maas/.maas_login.sh
35 # disable backports for maas enlist pkg repo. Those operation enforce maas
36 # to re-create sources.list and drop [source] fetch-definition from it.
37 main_arch_id=$(maas ${PROFILE} package-repositories read | jq -r ".[] | select(.name==\"main_archive\") | .id")
38 maas ${PROFILE} package-repository update ${main_arch_id} "disabled_pockets=backports" || true
39 maas ${PROFILE} package-repository update ${main_arch_id} "disabled_components=multiverse" || true
40 maas ${PROFILE} package-repository update ${main_arch_id} "arches=amd64" || true
41 # Remove stale notifications, which appear during sources configuration.
42 for i in $(maas ${PROFILE} notifications read | jq ".[]| .id"); do
Dzmitry Stremkouski9dd6a1b2019-01-24 12:03:58 +010043 maas ${PROFILE} notification delete ${i} || true
Ivan Berezovskiy77187172018-09-14 15:47:36 +040044 done
45 }
46
47 function process_formulas(){
48 local RECLASS_ROOT=${RECLASS_ROOT:-/srv/salt/reclass/}
49 local FORMULAS_PATH=${FORMULAS_PATH:-/usr/share/salt-formulas}
50
azvyagintsev829bfd82018-12-19 12:58:17 +020051 curl -s ${MCP_SALT_REPO_KEY} | apt-key add -
52 echo "${MCP_SALT_REPO}" > /etc/apt/sources.list.d/mcp_salt.list
53 echo "${MCP_SALT_REPO_UPDATES}" >> /etc/apt/sources.list.d/mcp_salt.list
Dzmitry Stremkouski9dd6a1b2019-01-24 12:03:58 +010054 apt update && apt install -y "${FORMULAS}"
Ivan Berezovskiy77187172018-09-14 15:47:36 +040055
56 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
57 for formula_service in $(ls /usr/share/salt-formulas/reclass/service/); do
58 #Since some salt formula names contain "-" and in symlinks they should contain "_" adding replacement
59 formula_service=${formula_service//-/$"_"}
60 if [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ]; then
61 ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service}
62 fi
63 done
64 }
65
66 function enable_services(){
Stanislav Riazanovc69bfc02018-12-07 16:52:14 +040067 local services="salt-api salt-master salt-minion"
68 if [ ! -f /opt/postgresql_in_docker ]; then
69 local services="${services} postgresql.service"
70 fi
71 for s in ${services} ; do
Ivan Berezovskiy77187172018-09-14 15:47:36 +040072 systemctl enable ${s} || true
73 systemctl restart ${s} || true
74 done
75 }
76
Ivan Berezovskiy77187172018-09-14 15:47:36 +040077 function process_maas(){
Stanislav Riazanov450ceaa2018-12-18 20:17:07 +040078 maas_cluster_enabled=$(salt-call --out=text pillar.get maas:cluster:enabled | awk '{print $2}' | tr "[:upper:]" "[:lower:]" )
79 _region=$(salt-call --out=text pillar.get maas:region:enabled | awk '{print $2}' | tr "[:upper:]" "[:lower:]" )
80 if ([ -f /opt/postgresql_in_docker ] && ([[ "${maas_cluster_enabled}" == "true" ]] || [[ "${_region}" == "true" ]])); then
Stanislav Riazanovc69bfc02018-12-07 16:52:14 +040081 systemctl disable postgresql.service
82 wait_for_postgresql
Dzmitry Stremkouski9dd6a1b2019-01-24 12:03:58 +010083 salt-call ${SALT_OPTS} state.apply postgresql.client
Stanislav Riazanovc69bfc02018-12-07 16:52:14 +040084 else
85 postgres_enabled=$(salt-call --out=text pillar.get postgresql:server:enabled | awk '{print $2}' | tr "[:upper:]" "[:lower:]")
86 if [[ "${postgres_enabled}" == "true" ]]; then
Dzmitry Stremkouski9dd6a1b2019-01-24 12:03:58 +010087 salt-call ${SALT_OPTS} state.apply postgresql.server
Stanislav Riazanovc69bfc02018-12-07 16:52:14 +040088 fi
Ivan Berezovskiy77187172018-09-14 15:47:36 +040089 fi
90
Ivan Berezovskiy77187172018-09-14 15:47:36 +040091 if [[ "${maas_cluster_enabled}" == "true" ]]; then
Dzmitry Stremkouski9dd6a1b2019-01-24 12:03:58 +010092 salt-call ${SALT_OPTS} state.apply maas.cluster
Ivan Berezovskiy77187172018-09-14 15:47:36 +040093 else
94 echo "WARNING: maas.cluster skipped!"
95 fi
96 if [[ "$_region" == "true" ]]; then
Dzmitry Stremkouski9dd6a1b2019-01-24 12:03:58 +010097 salt-call ${SALT_OPTS} state.apply maas.region
Ivan Berezovskiy77187172018-09-14 15:47:36 +040098 else
99 echo "WARNING: maas.region skipped!"
100 fi
101 # Do not move it under first cluster-only check!
102 if [[ "${maas_cluster_enabled}" == "true" ]]; then
103 _post_maas_cfg
104 fi
105 }
106
Stanislav Riazanovc69bfc02018-12-07 16:52:14 +0400107 function wait_for_postgresql() {
108 salt_string="salt-call --out=text pillar.get postgresql:client:server:server01:admin"
109 pg_port=$(${salt_string}:port | awk '{print $2}')
110 pg_host=$(${salt_string}:host | awk '{print $2}')
111 wait_time=0
112 until [[ $(/usr/bin/pg_isready -h ${pg_host} -p ${pg_port} | awk '{ print $3 }' ) == 'accepting' ]] || [ $wait_time -eq 20 ];
113 do
114 echo "Waiting for postgres at: ${pg_host}:${pg_port}"
115 sleep $(( wait_time++ ));
116 done
117 }
118
Ivan Berezovskiy11d374b2018-11-26 18:00:23 +0400119 function wait_for_jenkins() {
120 # Wait for jenkins to be functional
121 jport=$(salt-call --out=text pillar.get jenkins:master:http:port | awk '{print $2}')
122 jport=${jport:-8081}
123 wait_time=0
Ivan Berezovskiyc05ce492018-12-11 12:32:43 +0400124 until [[ $(curl -sL -w "%{http_code}" localhost:$jport -o /dev/null) == 200 ]] || [ $wait_time -eq 20 ]; do
Ivan Berezovskiy11d374b2018-11-26 18:00:23 +0400125 sleep $(( wait_time++ ))
126 done
127 }
128
Stanislav Riazanovc69bfc02018-12-07 16:52:14 +0400129 function process_swarm() {
130 _swarm=$(salt-call --out=text pillar.get docker:swarm:advertise_addr | awk '{print $2}')
131 if [[ "${_swarm}" != "" ]]; then
Dzmitry Stremkouski9dd6a1b2019-01-24 12:03:58 +0100132 salt-call ${SALT_OPTS} state.apply docker.swarm
Stanislav Riazanovc69bfc02018-12-07 16:52:14 +0400133 fi
134 _docker=$(salt-call --out=text pillar.get docker:client:enabled | awk '{print $2}')
135 if [[ "${_docker}" != "" ]]; then
Dzmitry Stremkouski9dd6a1b2019-01-24 12:03:58 +0100136 salt-call ${SALT_OPTS} state.apply docker.client
Stanislav Riazanovc69bfc02018-12-07 16:52:14 +0400137 fi
Dzmitry Stremkouski9dd6a1b2019-01-24 12:03:58 +0100138 salt-call ${SALT_OPTS} state.apply docker.client.images
Ivan Berezovskiy5f7a80f2018-12-17 18:08:31 +0400139 }
140
Ivan Berezovskiyfba80942018-11-16 13:11:44 +0400141 function process_jenkins() {
142 # INFO: jenkins is in docker in 2019.x releases
Ivan Berezovskiy040c3c62018-11-30 16:48:49 +0400143 if [ -f /opt/jenkins_in_docker ]; then
144 rm -v /opt/jenkins_in_docker
Ivan Berezovskiyfba80942018-11-16 13:11:44 +0400145 export JENKINS_HOME=/srv/volumes/jenkins
Ivan Berezovskiyfba80942018-11-16 13:11:44 +0400146 _nginx=$(salt-call --out=text pillar.get nginx:server:enabled | awk '{print $2}')
147 if [[ "${_nginx}" != "" ]]; then
Dzmitry Stremkouski9dd6a1b2019-01-24 12:03:58 +0100148 salt-call ${SALT_OPTS} state.apply nginx
Ivan Berezovskiyfba80942018-11-16 13:11:44 +0400149 fi
Ivan Berezovskiy11d374b2018-11-26 18:00:23 +0400150 _jenabled=$(salt-call --out=text pillar.get docker:client:stack:jenkins | awk '{print $2}')
151 _jclient=$(salt-call --out=text pillar.get jenkins:client | awk '{print $2}')
152 if [[ "${_jenabled}" != "" && "${_jclient}" != "" ]]; then
153 wait_for_jenkins
Dzmitry Stremkouski9dd6a1b2019-01-24 12:03:58 +0100154 salt-call ${SALT_OPTS} state.apply jenkins.client
Ivan Berezovskiyfba80942018-11-16 13:11:44 +0400155 fi
156 else
157 export JENKINS_HOME=/var/lib/jenkins
158 systemctl enable jenkins
159 systemctl start jenkins
Ivan Berezovskiy11d374b2018-11-26 18:00:23 +0400160 wait_for_jenkins
Ivan Berezovskiyfba80942018-11-16 13:11:44 +0400161 _jjobs=$(salt-call --out=text pillar.get jenkins:client:job | awk '{print $2}')
162 if [[ "${_jjobs}" != "" ]]; then
Dzmitry Stremkouski9dd6a1b2019-01-24 12:03:58 +0100163 salt-call ${SALT_OPTS} state.apply jenkins.client
Ivan Berezovskiyfba80942018-11-16 13:11:44 +0400164 fi
165 systemctl stop jenkins
166 find ${JENKINS_HOME}/jenkins.model.JenkinsLocationConfiguration.xml -type f -print0 | xargs -0 sed -i -e "s/10.167.4.15/$SALT_MASTER_DEPLOY_IP/g"
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400167 fi
Ivan Berezovskiyfba80942018-11-16 13:11:44 +0400168
Ivan Berezovskiy73cad8e2018-11-23 18:53:13 +0400169 ssh-keyscan cfg01 > ${JENKINS_HOME}/.ssh/known_hosts && chmod a+r ${JENKINS_HOME}/.ssh/known_hosts || true
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400170 }
171
azvyagintsev9a0d7e52018-10-17 20:15:22 +0300172 function process_salt_base(){
173 # PROD-21179| PROD-21792 : To describe such trick's around salt.XX state ordering
Dzmitry Stremkouski9dd6a1b2019-01-24 12:03:58 +0100174 salt-call ${SALT_OPTS} state.apply salt.master
azvyagintsev9a0d7e52018-10-17 20:15:22 +0300175 # Wait for salt-master to wake up after restart
176 sleep 5
177 salt-call --timeout=120 test.ping
178 # Run salt.minion.ca to prepare CA certificate before salt.minion.cert is used
Dzmitry Stremkouski9dd6a1b2019-01-24 12:03:58 +0100179 salt-call ${SALT_OPTS} state.apply salt.minion.ca
Ann Taraday5a63afc2018-12-20 23:00:11 +0400180 # Add sleep for completion of postponed restart of salt-minion. PROD-25986
181 sleep 15
Dzmitry Stremkouski9dd6a1b2019-01-24 12:03:58 +0100182 salt-call ${SALT_OPTS} state.apply salt.minion
azvyagintsev9a0d7e52018-10-17 20:15:22 +0300183 # Wait for salt-minion to wake up after restart
184 sleep 5
185 salt-call --timeout=120 test.ping
Dzmitry Stremkouski9dd6a1b2019-01-24 12:03:58 +0100186 salt-call ${SALT_OPTS} state.apply salt
187 salt-call ${SALT_OPTS} state.apply reclass
azvyagintsev9a0d7e52018-10-17 20:15:22 +0300188 }
189 #== Body ==================================================================#
190
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400191 . /etc/cloud/master_environment
192 printenv | sort -u
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400193
194 echo "Preparing metadata model"
195 if [[ -n "${CFG_BOOTSTRAP_DRIVE_URL}" ]]; then
azvyagintsev829bfd82018-12-19 12:58:17 +0200196 echo "CFG_BOOTSTRAP_DRIVE_URL detected,downloading..."
197 _tname="cfg01_${RANDOM}.iso"
198 _wget_opts="--progress=dot:mega --waitretry=15 --retry-connrefused"
199 wget ${_wget_opts} -O /${_tname} "${CFG_BOOTSTRAP_DRIVE_URL}"
Dzmitry Stremkouski9dd6a1b2019-01-24 12:03:58 +0100200 mount -o loop /${_tname} /mnt
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400201 else
Dzmitry Stremkouski9dd6a1b2019-01-24 12:03:58 +0100202 mount $(blkid -t TYPE=iso9660 -o device) /mnt
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400203 fi
Dzmitry Stremkouski9dd6a1b2019-01-24 12:03:58 +0100204 rsync -av /mnt/mcp/model/ /srv/salt/reclass/
205 chown -R root:root /srv/salt/reclass/ || true
206 find /srv/salt/reclass/classes/ -type d -exec chmod 0755 {} \;
207 find /srv/salt/reclass/classes/ -type f -exec chmod 0644 {} \;
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400208
209 echo "Configuring salt"
210 envsubst < /root/minion.conf > /etc/salt/minion.d/minion.conf
Dzmitry Stremkouski9dd6a1b2019-01-24 12:03:58 +0100211 if [ -f /mnt/mcp/gpg/salt_master_pillar.asc ]; then
Dmitry Pyzhov15c0ac42018-12-11 17:10:08 +0300212 mkdir /etc/salt/gpgkeys
Dzmitry Stremkouski9dd6a1b2019-01-24 12:03:58 +0100213 chmod 0700 /etc/salt/gpgkeys
214 GNUPGHOME=/etc/salt/gpgkeys gpg --import /mnt/mcp/gpg/salt_master_pillar.asc
Dmitry Pyzhov15c0ac42018-12-11 17:10:08 +0300215 fi
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400216 enable_services
217
218 # Wait for salt-master and salt-minion to wake up after restart
219 salt-call --timeout=120 test.ping
220
221 while true; do
Dzmitry Stremkouski9dd6a1b2019-01-24 12:03:58 +0100222 salt-key | grep -w "$SALT_MASTER_MINION_ID" && break
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400223 sleep 5
224 done
225
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400226 echo "updating local git repos"
227 if [[ "$PIPELINES_FROM_ISO" == "true" ]] ; then
Dzmitry Stremkouski9dd6a1b2019-01-24 12:03:58 +0100228 rsync -av /mnt/mcp/mk-pipelines/ /home/repo/mk/mk-pipelines/
229 rsync -av /mnt/mcp/pipeline-library/ /home/repo/mcp-ci/pipeline-library/
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400230 umount /mnt || true
Dzmitry Stremkouski9dd6a1b2019-01-24 12:03:58 +0100231 chown -R git:www-data /home/repo/mk/mk-pipelines /home/repo/mcp-ci/pipeline-library
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400232 else
233 umount /mnt || true
Dzmitry Stremkouski9dd6a1b2019-01-24 12:03:58 +0100234 git clone --mirror "${MK_PIPELINES_URL}" /home/repo/mk/mk-pipelines/
235 git clone --mirror "${PIPELINE_LIB_URL}" /home/repo/mcp-ci/pipeline-library/
236 chown -R git:www-data /home/repo/mk/mk-pipelines /home/repo/mcp-ci/pipeline-library
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400237 fi
238
239 process_formulas
240
241 salt-call saltutil.refresh_pillar
242 salt-call saltutil.sync_all
243 if ! $(reclass -n ${SALT_MASTER_MINION_ID} > /dev/null ) ; then
244 echo "ERROR: Reclass render failed!"
245 exit 1
246 fi
247
Dzmitry Stremkouski9dd6a1b2019-01-24 12:03:58 +0100248 salt-call ${SALT_OPTS} state.apply linux.system.repo,linux.network,linux.system,linux,openssh
azvyagintsev9a0d7e52018-10-17 20:15:22 +0300249 process_salt_base
Stanislav Riazanov450ceaa2018-12-18 20:17:07 +0400250
Stanislav Riazanovc69bfc02018-12-07 16:52:14 +0400251
252 if [ -f /opt/jenkins_in_docker ] || [ -f /opt/postgresql_in_docker ]; then
253 process_swarm
254 fi
255 if [ -f /opt/jenkins_in_docker ] && [ ! -f /opt/postgresql_in_docker ]; then
256 docker stack rm postgresql || true
257 fi
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400258
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400259 process_jenkins
Stanislav Riazanovc69bfc02018-12-07 16:52:14 +0400260 process_maas
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400261
Ivan Berezovskiyfba80942018-11-16 13:11:44 +0400262 stop_services="salt-api salt-master salt-minion maas-rackd.service maas-regiond.service postgresql.service"
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400263 for s in ${stop_services} ; do
264 systemctl stop ${s} || true
265 sleep 1
266 done
azvyagintsevc1c62042018-09-26 11:47:49 +0300267 # Set bootstrap-done flag for future
268 mkdir -p /var/log/mcp/
269 touch /var/log/mcp/.bootstrap_done
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400270 sync
Dzmitry Stremkouski9dd6a1b2019-01-24 12:03:58 +0100271 shutdown -r now
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400272runcmd:
azvyagintsevc1c62042018-09-26 11:47:49 +0300273 - [bash, -cex, *master_config]