blob: 51649bb24010c7847b9177ffac1d4ae140b46311 [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"}
12 export DEPLOY_NETWORK_GW=${DEPLOY_NETWORK_GW:-"172.16.164.1"}
13 export DEPLOY_NETWORK_NETMASK=${DEPLOY_NETWORK_NETMASK:-"255.255.255.192"}
14 export DEPLOY_NETWORK_MTU=${DEPLOY_NETWORK_MTU:-"1500"}
15 export DNS_SERVERS=${DNS_SERVERS:-"8.8.8.8"}
azvyagintsev829bfd82018-12-19 12:58:17 +020016
Dennis Dmitriev8fa33f92018-10-10 01:13:48 +030017 export http_proxy=${http_proxy:-""}
18 export https_proxy=${https_proxy:-""}
azvyagintsev829bfd82018-12-19 12:58:17 +020019
20 export MCP_VERSION=${MCP_VERSION:-"stable"}
21
Dennis Dmitriev8fa33f92018-10-10 01:13:48 +030022 export PIPELINES_FROM_ISO=${PIPELINES_FROM_ISO:-"true"}
23 export PIPELINE_REPO_URL=${PIPELINE_REPO_URL:-"https://github.com/Mirantis"}
azvyagintsev829bfd82018-12-19 12:58:17 +020024
25 export MCP_SALT_REPO_URL=${MCP_SALT_REPO_URL:-"http://mirror.mirantis.com/"}
26 export MCP_SALT_REPO=${MCP_SALT_REPO:-"deb [arch=amd64] $MCP_SALT_REPO_URL/$MCP_VERSION/salt-formulas/xenial xenial main"}
27 export MCP_SALT_REPO_UPDATES=${MCP_SALT_REPO_UPDATES:-"deb [arch=amd64] $MCP_SALT_REPO_URL/update/$MCP_VERSION/salt-formulas/xenial xenial main"}
28 export MCP_SALT_REPO_KEY=${MCP_SALT_REPO_KEY:-"${MCP_SALT_REPO_URL}/${MCP_VERSION}/salt-formulas/xenial/archive-salt-formulas.key"}
29
Dennis Dmitriev8fa33f92018-10-10 01:13:48 +030030 export FORMULAS=${FORMULAS:-"salt-formula-*"}
azvyagintsev829bfd82018-12-19 12:58:17 +020031 export SALT_OPTS=${SALT_OPTS:-"-l debug -t 30 --retcode-passthrough --no-color"}
Dennis Dmitriev8fa33f92018-10-10 01:13:48 +030032 export CFG_BOOTSTRAP_DRIVE_URL=${CFG_BOOTSTRAP_DRIVE_URL:-""}
azvyagintsev829bfd82018-12-19 12:58:17 +020033
Ivan Berezovskiy77187172018-09-14 15:47:36 +040034master_config:
35 - &master_config |
36 function _post_maas_cfg() {
37 source /var/lib/maas/.maas_login.sh
38 # disable backports for maas enlist pkg repo. Those operation enforce maas
39 # to re-create sources.list and drop [source] fetch-definition from it.
40 main_arch_id=$(maas ${PROFILE} package-repositories read | jq -r ".[] | select(.name==\"main_archive\") | .id")
41 maas ${PROFILE} package-repository update ${main_arch_id} "disabled_pockets=backports" || true
42 maas ${PROFILE} package-repository update ${main_arch_id} "disabled_components=multiverse" || true
43 maas ${PROFILE} package-repository update ${main_arch_id} "arches=amd64" || true
44 # Remove stale notifications, which appear during sources configuration.
45 for i in $(maas ${PROFILE} notifications read | jq ".[]| .id"); do
46 maas ${PROFILE} notification delete ${i} || true
47 done
48 }
49
50 function process_formulas(){
51 local RECLASS_ROOT=${RECLASS_ROOT:-/srv/salt/reclass/}
52 local FORMULAS_PATH=${FORMULAS_PATH:-/usr/share/salt-formulas}
53
azvyagintsev829bfd82018-12-19 12:58:17 +020054 curl -s ${MCP_SALT_REPO_KEY} | apt-key add -
55 echo "${MCP_SALT_REPO}" > /etc/apt/sources.list.d/mcp_salt.list
56 echo "${MCP_SALT_REPO_UPDATES}" >> /etc/apt/sources.list.d/mcp_salt.list
Ivan Berezovskiy77187172018-09-14 15:47:36 +040057 apt-get update
58 apt-get install -y salt-formula-*
59
60 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
61 for formula_service in $(ls /usr/share/salt-formulas/reclass/service/); do
62 #Since some salt formula names contain "-" and in symlinks they should contain "_" adding replacement
63 formula_service=${formula_service//-/$"_"}
64 if [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ]; then
65 ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service}
66 fi
67 done
68 }
69
70 function enable_services(){
Stanislav Riazanovc69bfc02018-12-07 16:52:14 +040071 local services="salt-api salt-master salt-minion"
72 if [ ! -f /opt/postgresql_in_docker ]; then
73 local services="${services} postgresql.service"
74 fi
75 for s in ${services} ; do
Ivan Berezovskiy77187172018-09-14 15:47:36 +040076 systemctl enable ${s} || true
77 systemctl restart ${s} || true
78 done
79 }
80
81 function process_network(){
82 echo "Configuring network interfaces"
83 find /etc/network/interfaces.d/ -type f -delete
84 kill $(pidof /sbin/dhclient) || /bin/true
85 envsubst < /root/interfaces > /etc/network/interfaces
86 ip a flush dev ens3
87 rm -f /var/run/network/ifstate.ens3
88 if [[ $(grep -E "^\ *gateway\ " /etc/network/interfaces) ]]; then
89 (ip r s | grep ^default) && ip r d default || /bin/true
90 fi;
91 ifup ens3
92 }
93
94 function process_maas(){
Stanislav Riazanov450ceaa2018-12-18 20:17:07 +040095 maas_cluster_enabled=$(salt-call --out=text pillar.get maas:cluster:enabled | awk '{print $2}' | tr "[:upper:]" "[:lower:]" )
96 _region=$(salt-call --out=text pillar.get maas:region:enabled | awk '{print $2}' | tr "[:upper:]" "[:lower:]" )
97 if ([ -f /opt/postgresql_in_docker ] && ([[ "${maas_cluster_enabled}" == "true" ]] || [[ "${_region}" == "true" ]])); then
Stanislav Riazanovc69bfc02018-12-07 16:52:14 +040098 systemctl disable postgresql.service
99 wait_for_postgresql
100 salt-call ${SALT_OPTS} state.sls postgresql.client
101 else
102 postgres_enabled=$(salt-call --out=text pillar.get postgresql:server:enabled | awk '{print $2}' | tr "[:upper:]" "[:lower:]")
103 if [[ "${postgres_enabled}" == "true" ]]; then
104 salt-call ${SALT_OPTS} state.sls postgresql.server
105 fi
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400106 fi
107
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400108 if [[ "${maas_cluster_enabled}" == "true" ]]; then
109 salt-call ${SALT_OPTS} state.sls maas.cluster
110 else
111 echo "WARNING: maas.cluster skipped!"
112 fi
113 if [[ "$_region" == "true" ]]; then
Stanislav Riazanov450ceaa2018-12-18 20:17:07 +0400114 salt-call ${SALT_OPTS} state.sls maas.region
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400115 else
116 echo "WARNING: maas.region skipped!"
117 fi
118 # Do not move it under first cluster-only check!
119 if [[ "${maas_cluster_enabled}" == "true" ]]; then
120 _post_maas_cfg
121 fi
122 }
123
Stanislav Riazanovc69bfc02018-12-07 16:52:14 +0400124 function wait_for_postgresql() {
125 salt_string="salt-call --out=text pillar.get postgresql:client:server:server01:admin"
126 pg_port=$(${salt_string}:port | awk '{print $2}')
127 pg_host=$(${salt_string}:host | awk '{print $2}')
128 wait_time=0
129 until [[ $(/usr/bin/pg_isready -h ${pg_host} -p ${pg_port} | awk '{ print $3 }' ) == 'accepting' ]] || [ $wait_time -eq 20 ];
130 do
131 echo "Waiting for postgres at: ${pg_host}:${pg_port}"
132 sleep $(( wait_time++ ));
133 done
134 }
135
Ivan Berezovskiy11d374b2018-11-26 18:00:23 +0400136 function wait_for_jenkins() {
137 # Wait for jenkins to be functional
138 jport=$(salt-call --out=text pillar.get jenkins:master:http:port | awk '{print $2}')
139 jport=${jport:-8081}
140 wait_time=0
Ivan Berezovskiyc05ce492018-12-11 12:32:43 +0400141 until [[ $(curl -sL -w "%{http_code}" localhost:$jport -o /dev/null) == 200 ]] || [ $wait_time -eq 20 ]; do
Ivan Berezovskiy11d374b2018-11-26 18:00:23 +0400142 sleep $(( wait_time++ ))
143 done
144 }
145
Stanislav Riazanovc69bfc02018-12-07 16:52:14 +0400146 function process_swarm() {
147 _swarm=$(salt-call --out=text pillar.get docker:swarm:advertise_addr | awk '{print $2}')
148 if [[ "${_swarm}" != "" ]]; then
149 salt-call ${SALT_OPTS} state.sls docker.swarm
150 fi
151 _docker=$(salt-call --out=text pillar.get docker:client:enabled | awk '{print $2}')
152 if [[ "${_docker}" != "" ]]; then
153 salt-call ${SALT_OPTS} state.sls docker.client
154 fi
155 }
156
Ivan Berezovskiy5f7a80f2018-12-17 18:08:31 +0400157 # $1 is stack name
158 function wait_for_docker_images() {
159 local IMAGES=$(docker stack ps ${1} --format "{{.Image}}" | awk -F ':' '{print $1}' | uniq);
Ivan Berezovskiy101a2d82018-12-17 19:20:57 +0400160 local NOT_PULLED="NOT_PULLED"
Ivan Berezovskiy5f7a80f2018-12-17 18:08:31 +0400161 local counter=0
Ivan Berezovskiy101a2d82018-12-17 19:20:57 +0400162 until [[ -z "${NOT_PULLED}" ]] || [ $counter -eq 90 ]; do
163 NOT_PULLED=""
Ivan Berezovskiy5f7a80f2018-12-17 18:08:31 +0400164 for image in ${IMAGES}; do
Ivan Berezovskiy101a2d82018-12-17 19:20:57 +0400165 local exist=$(docker images --format "{{if eq .Repository \"$image\"}}{{.Repository}}{{end}}" | tr -d '\n');
Ivan Berezovskiy5f7a80f2018-12-17 18:08:31 +0400166 if [ -z "${exist}" ]; then
167 NOT_PULLED="${image} ${NOT_PULLED}"
168 fi;
169 done
Ivan Berezovskiy101a2d82018-12-17 19:20:57 +0400170 if [ -n "${NOT_PULLED}" ]; then
171 echo "Images ${NOT_PULLED} are not pulled yet"
172 sleep 10;
Ivan Berezovskiy7d32f7e2018-12-17 21:30:50 +0400173 counter=$((counter+1))
Ivan Berezovskiy101a2d82018-12-17 19:20:57 +0400174 fi
Ivan Berezovskiy5f7a80f2018-12-17 18:08:31 +0400175 done
176 }
177
Ivan Berezovskiyfba80942018-11-16 13:11:44 +0400178 function process_jenkins() {
179 # INFO: jenkins is in docker in 2019.x releases
Ivan Berezovskiy040c3c62018-11-30 16:48:49 +0400180 if [ -f /opt/jenkins_in_docker ]; then
181 rm -v /opt/jenkins_in_docker
Ivan Berezovskiyfba80942018-11-16 13:11:44 +0400182 export JENKINS_HOME=/srv/volumes/jenkins
Ivan Berezovskiyfba80942018-11-16 13:11:44 +0400183 _nginx=$(salt-call --out=text pillar.get nginx:server:enabled | awk '{print $2}')
184 if [[ "${_nginx}" != "" ]]; then
185 salt-call ${SALT_OPTS} state.sls nginx
186 fi
Ivan Berezovskiy11d374b2018-11-26 18:00:23 +0400187 _jenabled=$(salt-call --out=text pillar.get docker:client:stack:jenkins | awk '{print $2}')
188 _jclient=$(salt-call --out=text pillar.get jenkins:client | awk '{print $2}')
189 if [[ "${_jenabled}" != "" && "${_jclient}" != "" ]]; then
190 wait_for_jenkins
Ivan Berezovskiyfba80942018-11-16 13:11:44 +0400191 salt-call ${SALT_OPTS} state.sls jenkins.client
192 fi
193 else
194 export JENKINS_HOME=/var/lib/jenkins
195 systemctl enable jenkins
196 systemctl start jenkins
Ivan Berezovskiy11d374b2018-11-26 18:00:23 +0400197 wait_for_jenkins
Ivan Berezovskiyfba80942018-11-16 13:11:44 +0400198 _jjobs=$(salt-call --out=text pillar.get jenkins:client:job | awk '{print $2}')
199 if [[ "${_jjobs}" != "" ]]; then
200 salt-call ${SALT_OPTS} state.sls jenkins.client
201 fi
202 systemctl stop jenkins
203 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 +0400204 fi
Ivan Berezovskiyfba80942018-11-16 13:11:44 +0400205
Ivan Berezovskiy73cad8e2018-11-23 18:53:13 +0400206 ssh-keyscan cfg01 > ${JENKINS_HOME}/.ssh/known_hosts && chmod a+r ${JENKINS_HOME}/.ssh/known_hosts || true
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400207 }
208
209 failsafe_ssh_key(){
210 if [ -f /mnt/root_auth_keys ]; then
211 echo "Installing failsafe public ssh key from /mnt/root_auth_keys to /root/.ssh/authorized_keys"
212 install -m 0700 -d /root/.ssh
213 cat /mnt/root_auth_keys >> /root/.ssh/authorized_keys
214 chmod 600 /root/.ssh/authorized_keys
215 sed -i "s/^PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config
216 sed -i "s/^PasswordAuthentication.*/PasswordAuthentication yes/g" /etc/ssh/sshd_config
217 service ssh restart
218 fi
219 }
220
azvyagintsev9a0d7e52018-10-17 20:15:22 +0300221 function process_salt_base(){
222 # PROD-21179| PROD-21792 : To describe such trick's around salt.XX state ordering
223 salt-call ${SALT_OPTS} state.sls salt.master
224 # Wait for salt-master to wake up after restart
225 sleep 5
226 salt-call --timeout=120 test.ping
227 # Run salt.minion.ca to prepare CA certificate before salt.minion.cert is used
228 salt-call ${SALT_OPTS} state.sls salt.minion.ca
229 salt-call ${SALT_OPTS} state.sls salt.minion
230 # Wait for salt-minion to wake up after restart
231 sleep 5
232 salt-call --timeout=120 test.ping
233 salt-call ${SALT_OPTS} state.sls salt
234 salt-call ${SALT_OPTS} state.sls reclass
235 }
236 #== Body ==================================================================#
237
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400238 . /etc/cloud/master_environment
239 printenv | sort -u
240 process_network
241
242 echo "Preparing metadata model"
243 if [[ -n "${CFG_BOOTSTRAP_DRIVE_URL}" ]]; then
azvyagintsev829bfd82018-12-19 12:58:17 +0200244 echo "CFG_BOOTSTRAP_DRIVE_URL detected,downloading..."
245 _tname="cfg01_${RANDOM}.iso"
246 _wget_opts="--progress=dot:mega --waitretry=15 --retry-connrefused"
247 wget ${_wget_opts} -O /${_tname} "${CFG_BOOTSTRAP_DRIVE_URL}"
248 mount -o loop /${_tname} /mnt/
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400249 else
250 mount /dev/cdrom /mnt/
251 fi
252 cp -rT /mnt/model/model /srv/salt/reclass
253 chown -R root:root /srv/salt/reclass/* || true
254 chown -R root:root /srv/salt/reclass/.git* || true
255 chmod -R 644 /srv/salt/reclass/classes/cluster/* || true
256 chmod -R 644 /srv/salt/reclass/classes/system/* || true
257
258 failsafe_ssh_key
259
260 echo "Configuring salt"
261 envsubst < /root/minion.conf > /etc/salt/minion.d/minion.conf
Dmitry Pyzhov15c0ac42018-12-11 17:10:08 +0300262 if [ -f /mnt/gpg/salt_master_pillar.asc ]; then
263 mkdir /etc/salt/gpgkeys
264 chmod 700 /etc/salt/gpgkeys
265 GNUPGHOME=/etc/salt/gpgkeys gpg --import /mnt/gpg/salt_master_pillar.asc
266 fi
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400267 enable_services
268
269 # Wait for salt-master and salt-minion to wake up after restart
270 salt-call --timeout=120 test.ping
271
272 while true; do
273 salt-key | grep "$SALT_MASTER_MINION_ID" && break
274 sleep 5
275 done
276
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400277 echo "updating local git repos"
278 if [[ "$PIPELINES_FROM_ISO" == "true" ]] ; then
279 cp -r /mnt/mk-pipelines/* /home/repo/mk/mk-pipelines/
280 cp -r /mnt/pipeline-library/* /home/repo/mcp-ci/pipeline-library/
281 umount /mnt || true
282 chown -R git:www-data /home/repo/mk/mk-pipelines/*
283 chown -R git:www-data /home/repo/mcp-ci/pipeline-library/*
284 else
285 umount /mnt || true
286 git clone --mirror "${PIPELINE_REPO_URL}/mk-pipelines.git" /home/repo/mk/mk-pipelines/
287 git clone --mirror "${PIPELINE_REPO_URL}/pipeline-library.git" /home/repo/mcp-ci/pipeline-library/
288 chown -R git:www-data /home/repo/mk/mk-pipelines/*
289 chown -R git:www-data /home/repo/mcp-ci/pipeline-library/*
290 fi
291
292 process_formulas
293
294 salt-call saltutil.refresh_pillar
295 salt-call saltutil.sync_all
296 if ! $(reclass -n ${SALT_MASTER_MINION_ID} > /dev/null ) ; then
297 echo "ERROR: Reclass render failed!"
298 exit 1
299 fi
300
301 salt-call ${SALT_OPTS} state.sls linux.network,linux,openssh
azvyagintsev9a0d7e52018-10-17 20:15:22 +0300302 process_salt_base
Stanislav Riazanov450ceaa2018-12-18 20:17:07 +0400303
Stanislav Riazanovc69bfc02018-12-07 16:52:14 +0400304
305 if [ -f /opt/jenkins_in_docker ] || [ -f /opt/postgresql_in_docker ]; then
306 process_swarm
Ivan Berezovskiy5f7a80f2018-12-17 18:08:31 +0400307 wait_for_docker_images postgresql
308 wait_for_docker_images jenkins
Stanislav Riazanovc69bfc02018-12-07 16:52:14 +0400309 fi
310 if [ -f /opt/jenkins_in_docker ] && [ ! -f /opt/postgresql_in_docker ]; then
311 docker stack rm postgresql || true
312 fi
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400313
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400314 process_jenkins
Stanislav Riazanovc69bfc02018-12-07 16:52:14 +0400315 process_maas
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400316
Ivan Berezovskiyfba80942018-11-16 13:11:44 +0400317 stop_services="salt-api salt-master salt-minion maas-rackd.service maas-regiond.service postgresql.service"
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400318 for s in ${stop_services} ; do
319 systemctl stop ${s} || true
320 sleep 1
321 done
azvyagintsevc1c62042018-09-26 11:47:49 +0300322 # Set bootstrap-done flag for future
323 mkdir -p /var/log/mcp/
324 touch /var/log/mcp/.bootstrap_done
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400325 sync
326 reboot
327runcmd:
azvyagintsevc1c62042018-09-26 11:47:49 +0300328 - [bash, -cex, *master_config]