blob: 42b11fb2cc50af5caf5c77b996eea7b95b2fcfab [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"}
16 export http_proxy=${http_proxy:-""}
17 export https_proxy=${https_proxy:-""}
18 export PIPELINES_FROM_ISO=${PIPELINES_FROM_ISO:-"true"}
19 export PIPELINE_REPO_URL=${PIPELINE_REPO_URL:-"https://github.com/Mirantis"}
20 export MCP_VERSION=${MCP_VERSION:-"stable"}
21 export MCP_SALT_REPO_KEY=${MCP_SALT_REPO_KEY:-"http://apt.mirantis.com/public.gpg"}
22 export MCP_SALT_REPO_URL=${MCP_SALT_REPO_URL:-"http://apt.mirantis.com/xenial"}
23 export MCP_SALT_REPO=${MCP_SALT_REPO:-"deb [arch=amd64] $MCP_SALT_REPO_URL $MCP_VERSION salt"}
24 export FORMULAS=${FORMULAS:-"salt-formula-*"}
25 export SALT_OPTS=${SALT_OPTS:-"-l debug -t 10 --retcode-passthrough --no-color"}
26 export CFG_BOOTSTRAP_DRIVE_URL=${CFG_BOOTSTRAP_DRIVE_URL:-""}
Ivan Berezovskiy77187172018-09-14 15:47:36 +040027master_config:
28 - &master_config |
29 function _post_maas_cfg() {
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
40 done
41 }
42
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 curl -s $MCP_SALT_REPO_KEY | apt-key add -
48 echo $MCP_SALT_REPO > /etc/apt/sources.list.d/mcp_salt.list
49 apt-get update
50 apt-get install -y salt-formula-*
51
52 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
53 for formula_service in $(ls /usr/share/salt-formulas/reclass/service/); do
54 #Since some salt formula names contain "-" and in symlinks they should contain "_" adding replacement
55 formula_service=${formula_service//-/$"_"}
56 if [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ]; then
57 ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service}
58 fi
59 done
60 }
61
62 function enable_services(){
Ivan Berezovskiyfba80942018-11-16 13:11:44 +040063 local services="postgresql.service salt-api salt-master salt-minion"
Ivan Berezovskiy77187172018-09-14 15:47:36 +040064 for s in ${services} ; do
65 systemctl enable ${s} || true
66 systemctl restart ${s} || true
67 done
68 }
69
70 function process_network(){
71 echo "Configuring network interfaces"
72 find /etc/network/interfaces.d/ -type f -delete
73 kill $(pidof /sbin/dhclient) || /bin/true
74 envsubst < /root/interfaces > /etc/network/interfaces
75 ip a flush dev ens3
76 rm -f /var/run/network/ifstate.ens3
77 if [[ $(grep -E "^\ *gateway\ " /etc/network/interfaces) ]]; then
78 (ip r s | grep ^default) && ip r d default || /bin/true
79 fi;
80 ifup ens3
81 }
82
83 function process_maas(){
84 postgres_enabled=$(salt-call --out=text pillar.get postgresql:server:enabled | awk '{print $2}' | tr "[:upper:]" "[:lower:]")
85 if [[ "${postgres_enabled}" == "true" ]]; then
86 salt-call ${SALT_OPTS} state.sls postgresql.server
87 fi
88
89 _region=$(salt-call --out=text pillar.get maas:region:enabled | awk '{print $2}' | tr "[:upper:]" "[:lower:]" )
90 if [[ "${maas_cluster_enabled}" == "true" ]]; then
91 salt-call ${SALT_OPTS} state.sls maas.cluster
92 else
93 echo "WARNING: maas.cluster skipped!"
94 fi
95 if [[ "$_region" == "true" ]]; then
96 # FIXME MAAS still can fail in rare race condition.
97 salt-call ${SALT_OPTS} state.sls maas.region || salt-call ${SALT_OPTS} state.sls maas.region
98 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
Ivan Berezovskiy11d374b2018-11-26 18:00:23 +0400107 function wait_for_jenkins() {
108 # Wait for jenkins to be functional
109 jport=$(salt-call --out=text pillar.get jenkins:master:http:port | awk '{print $2}')
110 jport=${jport:-8081}
111 wait_time=0
112 until [[ $(curl -sL -w "%{http_code}" localhost:$jport -o /dev/null) == 200 ]] || [ $wait_time -eq 15 ]; do
113 sleep $(( wait_time++ ))
114 done
115 }
116
Ivan Berezovskiyfba80942018-11-16 13:11:44 +0400117 function process_jenkins() {
118 # INFO: jenkins is in docker in 2019.x releases
119 if [ -f /root/jenkins_in_docker ]; then
120 rm -v /root/jenkins_in_docker
121 export JENKINS_HOME=/srv/volumes/jenkins
122 _swarm=$(salt-call --out=text pillar.get docker:swarm:advertise_addr | awk '{print $2}')
123 if [[ "${_swarm}" != "" ]]; then
124 salt-call ${SALT_OPTS} state.sls docker.swarm
125 fi
126 _nginx=$(salt-call --out=text pillar.get nginx:server:enabled | awk '{print $2}')
127 if [[ "${_nginx}" != "" ]]; then
128 salt-call ${SALT_OPTS} state.sls nginx
129 fi
130 _docker=$(salt-call --out=text pillar.get docker:client:enabled | awk '{print $2}')
131 if [[ "${_docker}" != "" ]]; then
132 salt-call ${SALT_OPTS} state.sls docker.client
133 fi
Ivan Berezovskiy11d374b2018-11-26 18:00:23 +0400134 _jenabled=$(salt-call --out=text pillar.get docker:client:stack:jenkins | awk '{print $2}')
135 _jclient=$(salt-call --out=text pillar.get jenkins:client | awk '{print $2}')
136 if [[ "${_jenabled}" != "" && "${_jclient}" != "" ]]; then
137 wait_for_jenkins
Ivan Berezovskiyfba80942018-11-16 13:11:44 +0400138 salt-call ${SALT_OPTS} state.sls jenkins.client
139 fi
140 else
141 export JENKINS_HOME=/var/lib/jenkins
142 systemctl enable jenkins
143 systemctl start jenkins
Ivan Berezovskiy11d374b2018-11-26 18:00:23 +0400144 wait_for_jenkins
Ivan Berezovskiyfba80942018-11-16 13:11:44 +0400145 _jjobs=$(salt-call --out=text pillar.get jenkins:client:job | awk '{print $2}')
146 if [[ "${_jjobs}" != "" ]]; then
147 salt-call ${SALT_OPTS} state.sls jenkins.client
148 fi
149 systemctl stop jenkins
150 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 +0400151 fi
Ivan Berezovskiyfba80942018-11-16 13:11:44 +0400152
Ivan Berezovskiy73cad8e2018-11-23 18:53:13 +0400153 ssh-keyscan cfg01 > ${JENKINS_HOME}/.ssh/known_hosts && chmod a+r ${JENKINS_HOME}/.ssh/known_hosts || true
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400154 }
155
156 failsafe_ssh_key(){
157 if [ -f /mnt/root_auth_keys ]; then
158 echo "Installing failsafe public ssh key from /mnt/root_auth_keys to /root/.ssh/authorized_keys"
159 install -m 0700 -d /root/.ssh
160 cat /mnt/root_auth_keys >> /root/.ssh/authorized_keys
161 chmod 600 /root/.ssh/authorized_keys
162 sed -i "s/^PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config
163 sed -i "s/^PasswordAuthentication.*/PasswordAuthentication yes/g" /etc/ssh/sshd_config
164 service ssh restart
165 fi
166 }
167
azvyagintsev9a0d7e52018-10-17 20:15:22 +0300168 function process_salt_base(){
169 # PROD-21179| PROD-21792 : To describe such trick's around salt.XX state ordering
170 salt-call ${SALT_OPTS} state.sls salt.master
171 # Wait for salt-master to wake up after restart
172 sleep 5
173 salt-call --timeout=120 test.ping
174 # Run salt.minion.ca to prepare CA certificate before salt.minion.cert is used
175 salt-call ${SALT_OPTS} state.sls salt.minion.ca
176 salt-call ${SALT_OPTS} state.sls salt.minion
177 # Wait for salt-minion to wake up after restart
178 sleep 5
179 salt-call --timeout=120 test.ping
180 salt-call ${SALT_OPTS} state.sls salt
181 salt-call ${SALT_OPTS} state.sls reclass
182 }
183 #== Body ==================================================================#
184
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400185 . /etc/cloud/master_environment
186 printenv | sort -u
187 process_network
188
189 echo "Preparing metadata model"
190 if [[ -n "${CFG_BOOTSTRAP_DRIVE_URL}" ]]; then
191 wget -O /tmp/cfg01.iso "$cfg_bootstrap_drive"
192 mount -o loop /tmp/cfg01.iso /mnt/
193 else
194 mount /dev/cdrom /mnt/
195 fi
196 cp -rT /mnt/model/model /srv/salt/reclass
197 chown -R root:root /srv/salt/reclass/* || true
198 chown -R root:root /srv/salt/reclass/.git* || true
199 chmod -R 644 /srv/salt/reclass/classes/cluster/* || true
200 chmod -R 644 /srv/salt/reclass/classes/system/* || true
201
202 failsafe_ssh_key
203
204 echo "Configuring salt"
205 envsubst < /root/minion.conf > /etc/salt/minion.d/minion.conf
206 enable_services
207
208 # Wait for salt-master and salt-minion to wake up after restart
209 salt-call --timeout=120 test.ping
210
211 while true; do
212 salt-key | grep "$SALT_MASTER_MINION_ID" && break
213 sleep 5
214 done
215
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400216 echo "updating local git repos"
217 if [[ "$PIPELINES_FROM_ISO" == "true" ]] ; then
218 cp -r /mnt/mk-pipelines/* /home/repo/mk/mk-pipelines/
219 cp -r /mnt/pipeline-library/* /home/repo/mcp-ci/pipeline-library/
220 umount /mnt || true
221 chown -R git:www-data /home/repo/mk/mk-pipelines/*
222 chown -R git:www-data /home/repo/mcp-ci/pipeline-library/*
223 else
224 umount /mnt || true
225 git clone --mirror "${PIPELINE_REPO_URL}/mk-pipelines.git" /home/repo/mk/mk-pipelines/
226 git clone --mirror "${PIPELINE_REPO_URL}/pipeline-library.git" /home/repo/mcp-ci/pipeline-library/
227 chown -R git:www-data /home/repo/mk/mk-pipelines/*
228 chown -R git:www-data /home/repo/mcp-ci/pipeline-library/*
229 fi
230
231 process_formulas
232
233 salt-call saltutil.refresh_pillar
234 salt-call saltutil.sync_all
235 if ! $(reclass -n ${SALT_MASTER_MINION_ID} > /dev/null ) ; then
236 echo "ERROR: Reclass render failed!"
237 exit 1
238 fi
239
240 salt-call ${SALT_OPTS} state.sls linux.network,linux,openssh
azvyagintsev9a0d7e52018-10-17 20:15:22 +0300241 process_salt_base
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400242 maas_cluster_enabled=$(salt-call --out=text pillar.get maas:cluster:enabled | awk '{print $2}' | tr "[:upper:]" "[:lower:]" )
243 process_maas
244
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400245 process_jenkins
246
Ivan Berezovskiyfba80942018-11-16 13:11:44 +0400247 stop_services="salt-api salt-master salt-minion maas-rackd.service maas-regiond.service postgresql.service"
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400248 for s in ${stop_services} ; do
249 systemctl stop ${s} || true
250 sleep 1
251 done
azvyagintsevc1c62042018-09-26 11:47:49 +0300252 # Set bootstrap-done flag for future
253 mkdir -p /var/log/mcp/
254 touch /var/log/mcp/.bootstrap_done
Ivan Berezovskiy77187172018-09-14 15:47:36 +0400255 sync
256 reboot
257runcmd:
azvyagintsevc1c62042018-09-26 11:47:49 +0300258 - [bash, -cex, *master_config]