blob: 49985e052d20b609ce9ae38d7a95719db0c823a2 [file] [log] [blame]
Dennis Dmitriev492813e2017-08-09 15:08:58 +03001{# Collection of common macroses shared across different deployments #}
2
Sergii Golovatiuk50f91892017-08-04 18:11:06 +02003{% set SALT_MODELS_BRANCH = os_env('SALT_MODELS_BRANCH','master') %}
4{% set SALT_MODELS_COMMIT = os_env('SALT_MODELS_COMMIT','master') %}
5{# Reference to a patch that should be applied to the model if required, for example: export SALT_MODELS_REF_CHANGE=refs/changes/19/7219/12 #}
6{% set SALT_MODELS_REF_CHANGE = os_env('SALT_MODELS_REF_CHANGE', '') %}
7{# Pin to a specified commit in salt-models/reclass-system #}
Dmitry Tyzhnenko778515f2017-08-25 14:52:54 +03008{% set SALT_MODELS_SYSTEM_REPOSITORY = os_env('SALT_MODELS_SYSTEM_REPOSITORY','https://gerrit.mcp.mirantis.net/salt-models/reclass-system') %}
Sergii Golovatiuk50f91892017-08-04 18:11:06 +02009{% set SALT_MODELS_SYSTEM_COMMIT = os_env('SALT_MODELS_SYSTEM_COMMIT','') %}
Dmitry Tyzhnenko778515f2017-08-25 14:52:54 +030010{% set SALT_MODELS_SYSTEM_REF_CHANGE = os_env('SALT_MODELS_SYSTEM_REF_CHANGE','') %}
Sergii Golovatiuk50f91892017-08-04 18:11:06 +020011
Dennis Dmitriev492813e2017-08-09 15:08:58 +030012{% set REPOSITORY_SUITE = os_env('REPOSITORY_SUITE', 'testing') %}
13
Sergii Golovatiuk50f91892017-08-04 18:11:06 +020014{# Address pools for reclass cluster model are taken in the following order:
15 # 1. environment variables,
16 # 2. config.underlay.address_pools based on fuel-devops address pools
17 # (see generated '.ini' file after underlay is created),
18 # 3. defaults #}
19{% set address_pools = config.underlay.address_pools %}
20{% set IPV4_NET_ADMIN = os_env('IPV4_NET_ADMIN', address_pools.get('admin-pool01', '192.168.10.0/24')) %}
21{% set IPV4_NET_CONTROL = os_env('IPV4_NET_CONTROL', address_pools.get('private-pool01', '172.16.10.0/24')) %}
22{% set IPV4_NET_TENANT = os_env('IPV4_NET_TENANT', address_pools.get('tenant-pool01', '10.1.0.0/24')) %}
23{% set IPV4_NET_EXTERNAL = os_env('IPV4_NET_EXTERNAL', address_pools.get('external-pool01', '10.16.0.0/24')) %}
24{% set IPV4_NET_ADMIN_PREFIX = '.'.join(IPV4_NET_ADMIN.split('.')[0:3]) %}
25{% set IPV4_NET_CONTROL_PREFIX = '.'.join(IPV4_NET_CONTROL.split('.')[0:3]) %}
26{% set IPV4_NET_TENANT_PREFIX = '.'.join(IPV4_NET_TENANT.split('.')[0:3]) %}
27{% set IPV4_NET_EXTERNAL_PREFIX = '.'.join(IPV4_NET_EXTERNAL.split('.')[0:3]) %}
28
Victor Ryzhenkin4e077ad2017-08-10 13:42:11 +040029{# Format for formula replacement:
30 # space separated string:
31 # export SALT_FORMULAS_REFS='apache:refs/changes/xxxx kubernetes:refs/changes/xxxx' #}
32
33{% set SALT_FORMULAS_REFS = os_env('SALT_FORMULAS_REFS', '') %}
34{% set SALT_FORMULAS_REPO = os_env('SALT_FORMULAS_REPO', 'https://gerrit.mcp.mirantis.net/salt-formulas') %}
35
Dennis Dmitriev492813e2017-08-09 15:08:58 +030036
37{%- macro MACRO_INSTALL_SALT_MASTER() %}
38{######################################}
39- description: Installing salt master on cfg01
40 cmd: eatmydata apt-get install -y reclass git salt-master
41 node_name: {{ HOSTNAME_CFG01 }}
42 retry: {count: 1, delay: 1}
43 skip_fail: false
44
45- description: Configure salt-master on cfg01
46 cmd: |
47 cat << 'EOF' >> /etc/salt/master.d/master.conf
48 file_roots:
49 base:
50 - /usr/share/salt-formulas/env
51 pillar_opts: False
52 open_mode: True
53 reclass: &reclass
54 storage_type: yaml_fs
55 inventory_base_uri: /srv/salt/reclass
56 ext_pillar:
57 - reclass: *reclass
58 master_tops:
59 reclass: *reclass
60 EOF
61 node_name: {{ HOSTNAME_CFG01 }}
62 retry: {count: 1, delay: 1}
63 skip_fail: false
64
65- description: Configure GIT settings and certificates
66 cmd: touch /root/.git_trusted_certs.pem;
67 for server in github.com; do
68 openssl s_client -showcerts -connect $server:443 </dev/null
69 | openssl x509 -outform PEM
70 >> /root/.git_trusted_certs.pem;
71 done;
72 HOME=/root git config --global http.sslCAInfo /root/.git_trusted_certs.pem;
73 HOME=/root git config --global user.email "tcp-qa@example.com";
74 HOME=/root git config --global user.name "TCP QA";
75 node_name: {{ HOSTNAME_CFG01 }}
76 retry: {count: 1, delay: 1}
77 skip_fail: false
78{%- endmacro %}
79
80
81{%- macro MACRO_CLONE_RECLASS_MODELS(IS_CONTRAIL_LAB=false) %}
82{############################################################}
83- description: Clone reclass models with submodules
84 cmd: |
Sergii Golovatiuk50f91892017-08-04 18:11:06 +020085 ssh-keyscan -H github.com >> ~/.ssh/known_hosts;
Tatyana Leontovich44162732017-08-28 12:36:00 +030086 export GIT_SSL_NO_VERIFY=true; git clone -b {{ SALT_MODELS_BRANCH }} --recurse-submodules {{ SALT_MODELS_REPOSITORY }} /srv/salt/reclass;
Sergii Golovatiuk50f91892017-08-04 18:11:06 +020087 pushd /srv/salt/reclass && \
88 {%- if SALT_MODELS_REF_CHANGE != '' %}
89 {%- for item in SALT_MODELS_REF_CHANGE.split(" ") %}
90 git fetch {{ SALT_MODELS_REPOSITORY }} {{ item }} && git cherry-pick FETCH_HEAD && \
91 {%- endfor %}
92 {%- elif SALT_MODELS_COMMIT != 'master' %}
93 git checkout {{ SALT_MODELS_COMMIT }} && \
94 {%- endif %}
95 {%- if SALT_MODELS_SYSTEM_COMMIT != '' %}
96 pushd classes/system/ && \
97 git checkout {{ SALT_MODELS_SYSTEM_COMMIT }} && \
98 popd && \
Dmitry Tyzhnenko778515f2017-08-25 14:52:54 +030099 {%- elif SALT_MODELS_SYSTEM_REF_CHANGE != '' -%}
100 pushd classes/system/ && \
101 {%- for item in SALT_MODELS_SYSTEM_REF_CHANGE.split(" ") %}
102 git fetch {{ SALT_MODELS_SYSTEM_REPOSITORY }} {{ item }} && git cherry-pick FETCH_HEAD && \
103 {%- endfor %}
104 popd && \
Sergii Golovatiuk50f91892017-08-04 18:11:06 +0200105 {%- else %}
106 git submodule update --init --recursive && \
107 {%- endif %}
108 popd;
109 mkdir -p /srv/salt/reclass/classes/service;
110
111 # Replace firstly to an intermediate value to avoid intersection between
112 # already replaced and replacing networks.
113 # For example, if generated IPV4_NET_ADMIN_PREFIX=10.16.0 , then there is a risk of replacing twice:
114 # 192.168.10 -> 10.16.0 (generated network for admin)
115 # 10.16.0 -> <external network>
116 # So let's replace constant networks to the keywords, and then keywords to the desired networks.
117 find /srv/salt/reclass/ -type f -exec sed -i 's/192\.168\.10\./==IPV4_NET_ADMIN_PREFIX==/g' {} +
118 find /srv/salt/reclass/ -type f -exec sed -i 's/172\.16\.10\./==IPV4_NET_CONTROL_PREFIX==/g' {} +
119 find /srv/salt/reclass/ -type f -exec sed -i 's/10\.1\.0\./==IPV4_NET_TENANT_PREFIX==/g' {} +
120 find /srv/salt/reclass/ -type f -exec sed -i 's/10\.16\.0\./==IPV4_NET_EXTERNAL_PREFIX==/g' {} +
121
122 find /srv/salt/reclass/ -type f -exec sed -i 's/==IPV4_NET_ADMIN_PREFIX==/{{ IPV4_NET_ADMIN_PREFIX }}./g' {} +
123 find /srv/salt/reclass/ -type f -exec sed -i 's/==IPV4_NET_CONTROL_PREFIX==/{{ IPV4_NET_CONTROL_PREFIX }}./g' {} +
124 find /srv/salt/reclass/ -type f -exec sed -i 's/==IPV4_NET_TENANT_PREFIX==/{{ IPV4_NET_TENANT_PREFIX }}./g' {} +
125 find /srv/salt/reclass/ -type f -exec sed -i 's/==IPV4_NET_EXTERNAL_PREFIX==/{{ IPV4_NET_EXTERNAL_PREFIX }}./g' {} +
126
127 find /srv/salt/reclass/ -type f -exec sed -i 's/apt_mk_version:.*/apt_mk_version: {{ REPOSITORY_SUITE }}/g' {} +
128
Dennis Dmitriev492813e2017-08-09 15:08:58 +0300129 {%- if IS_CONTRAIL_LAB %}
130 # vSRX IPs for tcp-qa images have 172.16.10.90 hardcoded
131 find /srv/salt/reclass/ -type f -exec sed -i 's/opencontrail_router01_address:.*/opencontrail_router01_address: 172.16.10.90/g' {} +
132 {%- endif %}
133
Sergii Golovatiuk50f91892017-08-04 18:11:06 +0200134 # Disable checkouting the model from remote repository
135 cat << 'EOF' >> /srv/salt/reclass/nodes/{{ HOSTNAME_CFG01 }}.yml
136 # local storage
137 reclass:
138 storage:
139 data_source:
140 engine: local
141 EOF
Dennis Dmitriev492813e2017-08-09 15:08:58 +0300142
143 # Show the changes to the console
144 cd /srv/salt/reclass/; git diff
145 node_name: {{ HOSTNAME_CFG01 }}
146 retry: {count: 1, delay: 1}
147 skip_fail: false
148{%- endmacro %}
149
150
151{%- macro MACRO_CONFIGURE_RECLASS(FORMULA_SERVICES='') %}
152{#######################################################}
153- description: Configure reclass
154 cmd: |
155 FORMULA_PATH=${FORMULA_PATH:-/usr/share/salt-formulas};
156 FORMULA_REPOSITORY=${FORMULA_REPOSITORY:-deb [arch=amd64] http://apt.mirantis.com/xenial {{ REPOSITORY_SUITE }} salt};
157 FORMULA_GPG=${FORMULA_GPG:-http://apt.mirantis.com/public.gpg};
158 which wget > /dev/null || (apt-get update; apt-get install -y wget);
159 echo "${FORMULA_REPOSITORY}" > /etc/apt/sources.list.d/mcp_salt.list;
160 wget -O - "${FORMULA_GPG}" | apt-key add -;
161 apt-get clean; apt-get update;
162 [ ! -d /srv/salt/reclass/classes/service ] && mkdir -p /srv/salt/reclass/classes/service;
163 declare -a formula_services=({{ FORMULA_SERVICES }});
164 echo -e "\nInstalling all required salt formulas\n";
165 eatmydata apt-get install -y "${formula_services[@]/#/salt-formula-}";
166 for formula_service in "${formula_services[@]}"; do
167 echo -e "\nLink service metadata for formula ${formula_service} ...\n";
168 [ ! -L "/srv/salt/reclass/classes/service/${formula_service}" ] && ln -s ${FORMULA_PATH}/reclass/service/${formula_service} /srv/salt/reclass/classes/service/${formula_service};
169 done;
170 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env;
171 [ ! -L /srv/salt/env/prd ] && ln -s ${FORMULA_PATH}/env /srv/salt/env/prd;
172 [ ! -d /etc/reclass ] && mkdir /etc/reclass;
173
174 cat << 'EOF' >> /etc/reclass/reclass-config.yml
175 storage_type: yaml_fs
176 pretty_print: True
177 output: yaml
178 inventory_base_uri: /srv/salt/reclass
179 EOF
180 node_name: {{ HOSTNAME_CFG01 }}
181 retry: {count: 1, delay: 1}
182 skip_fail: false
183
184- description: "*Workaround* remove all cfg01 nodes except {{ HOSTNAME_CFG01 }} to not depend on other clusters in 'reclass --top'"
185 cmd: |
186 # Remove all other nodes except {{ HOSTNAME_CFG01 }} to not rely on them for 'reclass --top'
187 find /srv/salt/reclass/nodes/ -type f -not -name {{ HOSTNAME_CFG01 }}.yml -delete
188 node_name: {{ HOSTNAME_CFG01 }}
189 retry: {count: 1, delay: 5}
190 skip_fail: false
191
192- description: Configure salt adoptors on cfg01
193 cmd: |
194 ln -s /usr/lib/python2.7/dist-packages/reclass/adapters/salt.py /usr/local/sbin/reclass-salt;
195 chmod +x /usr/lib/python2.7/dist-packages/reclass/adapters/salt.py
196 node_name: {{ HOSTNAME_CFG01 }}
197 retry: {count: 1, delay: 1}
198 skip_fail: false
199
200- description: Show reclass-salt --top for cfg01 node
201 cmd: reclass-salt --top
202 node_name: {{ HOSTNAME_CFG01 }}
203 retry: {count: 1, delay: 5}
204 skip_fail: false
205
206- description: Restart salt-master service
207 cmd: systemctl restart salt-master;
208 node_name: {{ HOSTNAME_CFG01 }}
209 retry: {count: 1, delay: 5}
210 skip_fail: false
211{%- endmacro %}
212
213
214{%- macro MACRO_INSTALL_SALT_MINIONS() %}
215{#######################################}
216{% for ssh in config.underlay.ssh %}
217- description: Configure salt-minion on {{ ssh['node_name'] }}
218 cmd: |
219 [ ! -d /etc/salt/minion.d ] && mkdir -p /etc/salt/minion.d;
220 cat << "EOF" >> /etc/salt/minion.d/minion.conf
221 id: {{ ssh['node_name'] }}
222 master: {{ config.salt.salt_master_host }}
223 EOF
224 eatmydata apt-get install -y salt-minion;
225 echo "Check for system info and metadata availability ...";
226 salt-call --no-color grains.items;
227 salt-call --no-color pillar.items;
228 node_name: {{ ssh['node_name'] }}
229 retry: {count: 1, delay: 1}
230 skip_fail: false
231{% endfor %}
232
233
234- description: Accept salt keys from all the nodes
235 cmd: salt-key -A -y
236 node_name: {{ HOSTNAME_CFG01 }}
237 retry: {count: 1, delay: 5}
238 skip_fail: true
239{%- endmacro %}
240
241
242{%- macro MACRO_RUN_SALT_MASTER_UNDERLAY_STATES() %}
243{##################################################}
244
245{# Prepare salt services and nodes settings #}
246
247- description: Run 'linux' formula on cfg01
248 cmd: salt --hard-crash --state-output=mixed --state-verbose=False -C 'I@salt:master' state.sls linux;
249 node_name: {{ HOSTNAME_CFG01 }}
250 retry: {count: 3, delay: 5}
251 skip_fail: false
252
253- description: Run 'openssh' formula on cfg01
254 cmd: salt --hard-crash --state-output=mixed --state-verbose=False
255 -C 'I@salt:master' state.sls openssh;
256 salt --hard-crash --state-output=mixed --state-verbose=False
257 -C 'I@salt:master' cmd.run "sed -i 's/PasswordAuthentication no/PasswordAuthentication
258 yes/' /etc/ssh/sshd_config && service ssh reload";
259 node_name: {{ HOSTNAME_CFG01 }}
260 retry: {count: 3, delay: 5}
261 skip_fail: false
262
263- description: '*Workaround* of the bug https://mirantis.jira.com/browse/PROD-7962'
264 cmd: salt --hard-crash --state-output=mixed --state-verbose=False
265 '*' cmd.run "echo ' StrictHostKeyChecking no' >> /root/.ssh/config"
266 node_name: {{ HOSTNAME_CFG01 }}
267 retry: {count: 1, delay: 1}
268 skip_fail: false
269
270- description: Run 'salt.master' formula on cfg01
271 cmd: timeout 120 salt --timeout=120 --hard-crash --state-output=mixed --state-verbose=False -C 'I@salt:master' state.sls salt.master;
272 node_name: {{ HOSTNAME_CFG01 }}
273 retry: {count: 2, delay: 5}
274 skip_fail: false
275
276# TODO(ddmitriev): apply custom patches for formulas here
Victor Ryzhenkin4e077ad2017-08-10 13:42:11 +0400277{%- if SALT_FORMULAS_REFS != '' %}
278- description: Replace needed formulas to desired version
279 cmd: |
280 set -e;
281 {%- for formula in SALT_FORMULAS_REFS.split(' ') %}
282 {% set formula_name = formula.0 %}
283 {% set formula_ref = formula.1 %}
Dennis Dmitrievbc57b802017-08-16 18:10:57 +0300284 {% set formula_dir = '/tmp/salt-formula-' + formula_name %}
Victor Ryzhenkin4e077ad2017-08-10 13:42:11 +0400285 git clone {{ SALT_FORMULAS_REPO }}/{{ formula_name }} {{ formula_dir }} &&
286 pushd {{ formula_dir }} &&
287 git fetch {{ SALT_FORMULAS_REPO }}/{{ formula_name }} {{ formula_ref }} &&
288 git checkout FETCH_HEAD &&
289 popd &&
290 if [ -d "{{ formula_dir }}" ]; then
291 echo "Going to replace packaged formula {{ formula_name }}" &&
292 rm -rfv /usr/share/salt-formulas/{env,reclass/service}/{{ formula_name }} &&
293 ln -v -s \"{{ formula_dir }}/{{ formula_name }}\" \"/usr/share/salt-formulas/env/{{ formula_name }}\" &&
294 ln -v -s \"{{ formula_dir }}/metadata/service/\" \"/usr/share/salt-formulas/reclass/service/{{ formula_name }}\";
295 else
296 echo "Stopped, directory /root/salt-formula-{{ formula_name }} does not exist!";
297 fi
298 {%- endfor %}
299 node_name: {{ HOSTNAME_CFG01 }}
300 retry: {count: 1, delay: 10}
301 skip_fail: false
302{%- endif %}
Dennis Dmitriev492813e2017-08-09 15:08:58 +0300303
304- description: Refresh pillars on salt-master minion
305 cmd: salt --hard-crash --state-output=mixed --state-verbose=False -C 'I@salt:master' saltutil.refresh_pillar
306 node_name: {{ HOSTNAME_CFG01 }}
307 retry: {count: 1, delay: 5}
308 skip_fail: false
309
310- description: Show reclass-salt --top for all generated nodes
311 cmd: reclass-salt --top
312 node_name: {{ HOSTNAME_CFG01 }}
313 retry: {count: 1, delay: 5}
314 skip_fail: false
315
316- description: Sync all salt resources on salt-master minion
Dennis Dmitriev9a6cacc2017-08-14 00:08:41 +0300317 cmd: salt --hard-crash --state-output=mixed --state-verbose=False -C 'I@salt:master' saltutil.sync_all && sleep 5
Dennis Dmitriev492813e2017-08-09 15:08:58 +0300318 node_name: {{ HOSTNAME_CFG01 }}
319 retry: {count: 1, delay: 5}
320 skip_fail: false
321
322- description: Configure linux on master
323 cmd: salt --hard-crash --state-output=mixed --state-verbose=False -C 'I@salt:master' state.sls 'linux.system'
324 node_name: {{ HOSTNAME_CFG01 }}
325 retry: {count: 1, delay: 5}
326 skip_fail: false
327
328- description: Configure salt.minion on master
329 cmd: salt --timeout=120 --hard-crash --state-output=mixed --state-verbose=False
330 -C 'I@salt:master' state.sls salt.minion && sleep 10
331 node_name: {{ HOSTNAME_CFG01 }}
332 retry: {count: 3, delay: 10}
333 skip_fail: false
334
335- description: Run state 'salt' on master (for salt.api, etc)
336 cmd: salt --hard-crash --state-output=mixed --state-verbose=False
337 -C 'I@salt:master' state.sls salt
338 node_name: {{ HOSTNAME_CFG01 }}
339 retry: {count: 3, delay: 10}
340 skip_fail: false
341{%- endmacro %}
342
343{%- macro MACRO_GENERATE_INVENTORY() %}
344{#####################################}
345- description: Generate inventory for all the nodes to the /srv/salt/reclass/nodes/_generated
346 cmd: salt --hard-crash --state-output=mixed --state-verbose=False
347 -C 'I@salt:master' state.sls reclass
348 node_name: {{ HOSTNAME_CFG01 }}
349 retry: {count: 1, delay: 5}
350 skip_fail: false
351
352- description: Refresh pillars on all minions
353 cmd: salt --hard-crash --state-output=mixed --state-verbose=False '*' saltutil.refresh_pillar
354 node_name: {{ HOSTNAME_CFG01 }}
355 retry: {count: 1, delay: 5}
356 skip_fail: false
357
358- description: Show reclass-salt --top for all generated nodes
359 cmd: reclass-salt --top
360 node_name: {{ HOSTNAME_CFG01 }}
361 retry: {count: 1, delay: 5}
362 skip_fail: false
363
364- description: Sync all salt resources
Dennis Dmitriev9a6cacc2017-08-14 00:08:41 +0300365 cmd: salt --hard-crash --state-output=mixed --state-verbose=False '*' saltutil.sync_all && sleep 5
Dennis Dmitriev492813e2017-08-09 15:08:58 +0300366 node_name: {{ HOSTNAME_CFG01 }}
367 retry: {count: 1, delay: 5}
368 skip_fail: false
369{%- endmacro %}
370
371
372{%- macro MACRO_BOOTSTRAP_ALL_MINIONS() %}
373{########################################}
374# Bootstrap all nodes
375- description: Configure linux on other nodes
376 cmd: salt --hard-crash --state-output=mixed --state-verbose=False -C 'I@linux:system' state.sls linux
377 node_name: {{ HOSTNAME_CFG01 }}
378 retry: {count: 5, delay: 10}
379 skip_fail: false
380
381- description: Configure openssh on all nodes
382 cmd: salt --hard-crash --state-output=mixed --state-verbose=False -C 'I@linux:system and not cfg01*' state.sls openssh;
383 salt --hard-crash --state-output=mixed --state-verbose=False
384 -C 'I@linux:system and not cfg01*' cmd.run "sed -i 's/PasswordAuthentication no/PasswordAuthentication
385 yes/' /etc/ssh/sshd_config && service ssh reload"
386 node_name: {{ HOSTNAME_CFG01 }}
387 retry: {count: 1, delay: 5}
388 skip_fail: false
389
390- description: Configure salt.minion on other nodes
391 cmd: salt --timeout=120 --hard-crash --state-output=mixed --state-verbose=False -C 'I@linux:system and not cfg01*' state.sls salt.minion &&
392 sleep 10
393 node_name: {{ HOSTNAME_CFG01 }}
394 retry: {count: 3, delay: 15}
395 skip_fail: false
396
397- description: Check salt minion versions on slaves
398 cmd: salt '*' test.version
399 node_name: {{ HOSTNAME_CFG01 }}
400 retry: {count: 3, delay: 15}
401 skip_fail: false
402
403- description: Check salt top states on nodes
404 cmd: salt '*' state.show_top
405 node_name: {{ HOSTNAME_CFG01 }}
406 retry: {count: 1, delay: 5}
407 skip_fail: false
408
409- description: Configure ntp and rsyslog on nodes
410 cmd: salt --hard-crash --state-output=mixed --state-verbose=False -C 'I@linux:system' state.sls ntp,rsyslog
411 node_name: {{ HOSTNAME_CFG01 }}
412 retry: {count: 1, delay: 10}
413 skip_fail: false
Sergii Golovatiuk50f91892017-08-04 18:11:06 +0200414{%- endmacro %}