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