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