Artem Panchenko | edf70ef | 2017-06-13 09:14:34 +0300 | [diff] [blame] | 1 | {% from 'virtual-mcp11-k8s-calico-minimal/underlay.yaml' import HOSTNAME_CFG01 with context %} |
| 2 | {% from 'virtual-mcp11-k8s-calico-minimal/underlay.yaml' import REPOSITORY_SUITE with context %} |
| 3 | |
| 4 | {% set SALT_MODELS_REPOSITORY = os_env('SALT_MODELS_REPOSITORY','https://gerrit.mcp.mirantis.net/salt-models/mcp-virtual-lab') %} |
| 5 | {% set SALT_MODELS_BRANCH = os_env('SALT_MODELS_BRANCH','master') %} |
| 6 | {% set SALT_MODELS_COMMIT = os_env('SALT_MODELS_COMMIT','master') %} |
| 7 | |
| 8 | # Address pools for reclass cluster model are taken in the following order: |
| 9 | # 1. environment variables, |
| 10 | # 2. config.underlay.address_pools based on fuel-devops address pools |
| 11 | # (see generated '.ini' file after underlay is created), |
| 12 | # 3. defaults |
| 13 | {% set address_pools = config.underlay.address_pools %} |
| 14 | {% set IPV4_NET_ADMIN = os_env('IPV4_NET_ADMIN', address_pools.get('admin-pool01', '172.16.10.0/24')) %} |
| 15 | {% set IPV4_NET_CONTROL = os_env('IPV4_NET_CONTROL', address_pools.get('private-pool01', '192.168.10.0/24')) %} |
| 16 | |
| 17 | {% set IPV4_NET_ADMIN_PREFIX = '.'.join(IPV4_NET_ADMIN.split('.')[0:3]) %} |
| 18 | {% set IPV4_NET_CONTROL_PREFIX = '.'.join(IPV4_NET_CONTROL.split('.')[0:3]) %} |
| 19 | |
| 20 | # Install salt to the config node |
| 21 | - description: Installing salt master on cfg01 |
| 22 | cmd: apt-get install -y reclass git; apt-get install -y salt-master |
| 23 | node_name: {{ HOSTNAME_CFG01 }} |
| 24 | retry: {count: 1, delay: 1} |
| 25 | skip_fail: false |
| 26 | |
| 27 | - description: Configure salt-master on cfg01 |
| 28 | cmd: | |
| 29 | cat << 'EOF' >> /etc/salt/master.d/master.conf |
| 30 | file_roots: |
| 31 | base: |
| 32 | - /usr/share/salt-formulas/env |
| 33 | pillar_opts: False |
| 34 | open_mode: True |
| 35 | reclass: &reclass |
| 36 | storage_type: yaml_fs |
| 37 | inventory_base_uri: /srv/salt/reclass |
| 38 | ext_pillar: |
| 39 | - reclass: *reclass |
| 40 | master_tops: |
| 41 | reclass: *reclass |
| 42 | EOF |
| 43 | node_name: {{ HOSTNAME_CFG01 }} |
| 44 | retry: {count: 1, delay: 1} |
| 45 | skip_fail: false |
| 46 | |
| 47 | - description: Configure GIT settings and certificates |
| 48 | cmd: touch /root/.git_trusted_certs.pem; |
| 49 | for server in github.com; do |
| 50 | openssl s_client -showcerts -connect $server:443 </dev/null |
| 51 | | openssl x509 -outform PEM |
| 52 | >> /root/.git_trusted_certs.pem; |
| 53 | done; |
| 54 | HOME=/root git config --global http.sslCAInfo /root/.git_trusted_certs.pem; |
| 55 | HOME=/root git config --global user.email "tcp-qa@example.com"; |
| 56 | HOME=/root git config --global user.name "TCP QA"; |
| 57 | node_name: {{ HOSTNAME_CFG01 }} |
| 58 | retry: {count: 1, delay: 1} |
| 59 | skip_fail: false |
| 60 | |
| 61 | - description: Clone reclass models with submodules |
| 62 | cmd: | |
| 63 | ssh-keyscan -H github.com >> ~/.ssh/known_hosts; |
| 64 | git clone -b {{ SALT_MODELS_BRANCH }} --recurse-submodules {{ SALT_MODELS_REPOSITORY }} /srv/salt/reclass; |
| 65 | pushd /srv/salt/reclass && git checkout {{ SALT_MODELS_COMMIT }} && popd |
| 66 | mkdir -p /srv/salt/reclass/classes/service; |
| 67 | |
| 68 | # Replace firstly to an intermediate value to avoid intersection between |
| 69 | # already replaced and replacing networks. |
| 70 | # For example, if generated IPV4_NET_ADMIN_PREFIX=10.16.0 , then there is a risk of replacing twice: |
| 71 | # 192.168.10 -> 10.16.0 (generated network for admin) |
| 72 | # 10.16.0 -> <external network> |
| 73 | # So let's replace constant networks to the keywords, and then keywords to the desired networks. |
| 74 | find /srv/salt/reclass/ -type f -exec sed -i 's/192\.168\.10\./==IPV4_NET_ADMIN_PREFIX==/g' {} + |
| 75 | find /srv/salt/reclass/ -type f -exec sed -i 's/172\.16\.10\./==IPV4_NET_CONTROL_PREFIX==/g' {} + |
| 76 | |
| 77 | find /srv/salt/reclass/ -type f -exec sed -i 's/==IPV4_NET_ADMIN_PREFIX==/{{ IPV4_NET_ADMIN_PREFIX }}./g' {} + |
| 78 | find /srv/salt/reclass/ -type f -exec sed -i 's/==IPV4_NET_CONTROL_PREFIX==/{{ IPV4_NET_CONTROL_PREFIX }}./g' {} + |
| 79 | |
| 80 | find /srv/salt/reclass/ -type f -exec sed -i 's/apt_mk_version:.*/apt_mk_version: {{ REPOSITORY_SUITE }}/g' {} + |
| 81 | |
| 82 | # Disable checkouting the model from remote repository |
| 83 | cat << 'EOF' >> /srv/salt/reclass/nodes/{{ HOSTNAME_CFG01 }}.yml |
| 84 | # local storage |
| 85 | reclass: |
| 86 | storage: |
| 87 | data_source: |
| 88 | engine: local |
| 89 | EOF |
| 90 | |
| 91 | # Show the changes to the console |
| 92 | cd /srv/salt/reclass/; git diff |
| 93 | node_name: {{ HOSTNAME_CFG01 }} |
| 94 | retry: {count: 1, delay: 1} |
| 95 | skip_fail: false |
| 96 | |
| 97 | |
| 98 | - description: Configure reclass |
| 99 | cmd: | |
| 100 | FORMULA_PATH=${FORMULA_PATH:-/usr/share/salt-formulas}; |
| 101 | FORMULA_REPOSITORY=${FORMULA_REPOSITORY:-deb [arch=amd64] http://apt-mk.mirantis.com/xenial {{ REPOSITORY_SUITE }} salt}; |
| 102 | FORMULA_GPG=${FORMULA_GPG:-http://apt-mk.mirantis.com/public.gpg}; |
| 103 | which wget > /dev/null || (apt-get update; apt-get install -y wget); |
| 104 | echo "${FORMULA_REPOSITORY}" > /etc/apt/sources.list.d/mcp_salt.list; |
| 105 | wget -O - "${FORMULA_GPG}" | apt-key add -; |
| 106 | apt-get clean; apt-get update; |
| 107 | [ ! -d /srv/salt/reclass/classes/service ] && mkdir -p /srv/salt/reclass/classes/service; |
| 108 | declare -a formula_services=("linux" "reclass" "salt" "openssh" "ntp" "git" "nginx" "collectd" "sensu" "heka" "sphinx" "keystone" "mysql" "grafana" "haproxy" "rsyslog" "horizon" "prometheus" "telegraf"); |
| 109 | echo -e "\nInstalling all required salt formulas\n"; |
| 110 | eatmydata apt-get install -y "${formula_services[@]/#/salt-formula-}"; |
| 111 | for formula_service in "${formula_services[@]}"; do |
| 112 | echo -e "\nLink service metadata for formula ${formula_service} ...\n"; |
| 113 | [ ! -L "/srv/salt/reclass/classes/service/${formula_service}" ] && ln -s ${FORMULA_PATH}/reclass/service/${formula_service} /srv/salt/reclass/classes/service/${formula_service}; |
| 114 | done; |
| 115 | [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env; |
| 116 | [ ! -L /srv/salt/env/prd ] && ln -s ${FORMULA_PATH}/env /srv/salt/env/prd; |
| 117 | [ ! -d /etc/reclass ] && mkdir /etc/reclass; |
| 118 | |
| 119 | cat << 'EOF' >> /etc/reclass/reclass-config.yml |
| 120 | storage_type: yaml_fs |
| 121 | pretty_print: True |
| 122 | output: yaml |
| 123 | inventory_base_uri: /srv/salt/reclass |
| 124 | EOF |
| 125 | node_name: {{ HOSTNAME_CFG01 }} |
| 126 | retry: {count: 1, delay: 1} |
| 127 | skip_fail: false |
| 128 | |
| 129 | - description: Restart salt-master service |
| 130 | cmd: | |
| 131 | systemctl restart salt-master; |
| 132 | node_name: {{ HOSTNAME_CFG01 }} |
| 133 | retry: {count: 1, delay: 5} |
| 134 | skip_fail: false |
| 135 | |
| 136 | {% for ssh in config.underlay.ssh %} |
| 137 | - description: Configure salt-minion on {{ ssh['node_name'] }} |
| 138 | cmd: | |
| 139 | [ ! -d /etc/salt/minion.d ] && mkdir -p /etc/salt/minion.d; |
| 140 | cat << "EOF" >> /etc/salt/minion.d/minion.conf |
| 141 | id: {{ ssh['node_name'] }} |
| 142 | master: {{ config.salt.salt_master_host }} |
| 143 | EOF |
| 144 | eatmydata apt-get install -y salt-minion; |
| 145 | echo "Check for system info and metadata availability ..."; |
| 146 | salt-call --no-color grains.items; |
| 147 | salt-call --no-color pillar.items; |
| 148 | node_name: {{ ssh['node_name'] }} |
| 149 | retry: {count: 1, delay: 1} |
| 150 | skip_fail: false |
| 151 | {% endfor %} |
| 152 | |
| 153 | - description: Accept salt keys from all the nodes |
| 154 | cmd: salt-key -A -y |
| 155 | node_name: {{ HOSTNAME_CFG01 }} |
| 156 | retry: {count: 1, delay: 5} |
| 157 | skip_fail: true |
| 158 | |
| 159 | - description: Configure salt adoptors on cfg01 |
| 160 | cmd: | |
| 161 | ln -s /usr/lib/python2.7/dist-packages/reclass/adapters/salt.py /usr/local/sbin/reclass-salt; |
| 162 | chmod +x /usr/lib/python2.7/dist-packages/reclass/adapters/salt.py |
| 163 | node_name: {{ HOSTNAME_CFG01 }} |
| 164 | retry: {count: 1, delay: 1} |
| 165 | skip_fail: false |
| 166 | |
| 167 | # Prepare salt services and nodes settings |
| 168 | - description: Run 'linux' formula on cfg01 |
| 169 | cmd: salt --hard-crash --state-output=mixed --state-verbose=False |
| 170 | -C 'I@salt:master' state.sls linux; |
| 171 | node_name: {{ HOSTNAME_CFG01 }} |
| 172 | retry: {count: 1, delay: 5} |
| 173 | skip_fail: false |
| 174 | |
| 175 | - description: Run 'openssh' formula on cfg01 |
| 176 | cmd: salt --hard-crash --state-output=mixed --state-verbose=False |
| 177 | -C 'I@salt:master' state.sls openssh; |
| 178 | salt --hard-crash --state-output=mixed --state-verbose=False |
| 179 | -C 'I@salt:master' cmd.run "sed -i 's/PasswordAuthentication no/PasswordAuthentication |
| 180 | yes/' /etc/ssh/sshd_config && service ssh restart"; |
| 181 | node_name: {{ HOSTNAME_CFG01 }} |
| 182 | retry: {count: 3, delay: 5} |
| 183 | skip_fail: false |
| 184 | |
| 185 | - description: '*Workaround* of the bug https://mirantis.jira.com/browse/PROD-7962' |
| 186 | cmd: salt --hard-crash --state-output=mixed --state-verbose=False |
| 187 | '*' cmd.run "echo ' StrictHostKeyChecking no' >> /root/.ssh/config" |
| 188 | node_name: {{ HOSTNAME_CFG01 }} |
| 189 | retry: {count: 1, delay: 1} |
| 190 | skip_fail: false |
| 191 | |
| 192 | - description: Run 'salt.master' formula on cfg01 |
| 193 | cmd: timeout 120 salt --hard-crash --state-output=mixed --state-verbose=False |
| 194 | -C 'I@salt:master' state.sls salt.master.service; |
| 195 | node_name: {{ HOSTNAME_CFG01 }} |
| 196 | retry: {count: 2, delay: 5} |
| 197 | skip_fail: false |
| 198 | |
| 199 | - description: Run 'salt' formula on cfg01 with workaround proposed in PROD-10894 |
| 200 | cmd: salt --hard-crash --state-output=mixed --state-verbose=False |
| 201 | -C 'I@salt:master' state.sls salt; |
| 202 | salt --hard-crash --state-output=mixed --state-verbose=False |
| 203 | -C 'I@salt:master' saltutil.sync_all |
| 204 | node_name: {{ HOSTNAME_CFG01 }} |
| 205 | retry: {count: 5, delay: 5} |
| 206 | skip_fail: false |
| 207 | |
| 208 | - description: Set k8s deploy parameters |
| 209 | cmd: | |
| 210 | {% for k8s_opt, value in config.k8s_deploy.items() %} |
| 211 | {% if value|string() %} |
| 212 | salt-call reclass.cluster_meta_set {{ k8s_opt }} {{ value }}; |
| 213 | {% endif %} |
| 214 | {% endfor %} |
| 215 | node_name: {{ HOSTNAME_CFG01 }} |
| 216 | retry: {count: 1, delay: 1} |
| 217 | skip_fail: false |
| 218 | |
| 219 | - description: Generate inventory for all the nodes to the /srv/salt/reclass/nodes/_generated |
| 220 | cmd: salt --hard-crash --state-output=mixed --state-verbose=False |
| 221 | -C 'I@salt:master' state.sls reclass |
| 222 | node_name: {{ HOSTNAME_CFG01 }} |
| 223 | retry: {count: 1, delay: 5} |
| 224 | skip_fail: false |
| 225 | |
| 226 | - description: Refresh pillars on all minions |
| 227 | cmd: salt --hard-crash --state-output=mixed --state-verbose=False '*' saltutil.refresh_pillar |
| 228 | node_name: {{ HOSTNAME_CFG01 }} |
| 229 | retry: {count: 1, delay: 5} |
| 230 | skip_fail: false |
| 231 | |
| 232 | - description: Sync all salt resources |
| 233 | cmd: salt --hard-crash --state-output=mixed --state-verbose=False '*' saltutil.sync_all |
| 234 | node_name: {{ HOSTNAME_CFG01 }} |
| 235 | retry: {count: 1, delay: 5} |
| 236 | skip_fail: false |
| 237 | |
| 238 | - description: Show reclass-salt --top |
| 239 | cmd: reclass-salt --top |
| 240 | node_name: {{ HOSTNAME_CFG01 }} |
| 241 | retry: {count: 1, delay: 5} |
| 242 | skip_fail: false |
| 243 | |
| 244 | - description: Execute salt.minion.cert |
| 245 | cmd: salt-call --no-color state.sls salt.minion.cert -l info; |
| 246 | node_name: {{ HOSTNAME_CFG01 }} |
| 247 | retry: {count: 1, delay: 5} |
| 248 | skip_fail: false |
| 249 | |
| 250 | # Bootstrap all nodes |
| 251 | |
| 252 | - description: Configure linux on other nodes |
| 253 | cmd: salt --hard-crash --state-output=mixed --state-verbose=False -C '* and not |
| 254 | cfg01*' state.sls linux |
| 255 | node_name: {{ HOSTNAME_CFG01 }} |
| 256 | retry: {count: 1, delay: 5} |
| 257 | skip_fail: false |
| 258 | |
| 259 | - description: Configure openssh on all nodes |
| 260 | cmd: salt --hard-crash --state-output=mixed --state-verbose=False -C '* and not |
| 261 | cfg01*' state.sls openssh;salt --hard-crash --state-output=mixed --state-verbose=False |
| 262 | -C '* and not cfg*' cmd.run "sed -i 's/PasswordAuthentication no/PasswordAuthentication |
| 263 | yes/' /etc/ssh/sshd_config && service ssh restart" |
| 264 | node_name: {{ HOSTNAME_CFG01 }} |
| 265 | retry: {count: 1, delay: 5} |
| 266 | skip_fail: false |
| 267 | |
| 268 | - description: Configure salt.minion on other nodes |
| 269 | cmd: salt --hard-crash --state-output=mixed --state-verbose=False -C '* and not |
| 270 | cfg01*' state.sls salt.minion |
| 271 | node_name: {{ HOSTNAME_CFG01 }} |
| 272 | retry: {count: 3, delay: 5} |
| 273 | skip_fail: false |
| 274 | |
| 275 | - description: Check salt minion versions on slaves |
| 276 | cmd: salt '*' test.version |
| 277 | node_name: {{ HOSTNAME_CFG01 }} |
| 278 | retry: {count: 1, delay: 5} |
| 279 | skip_fail: false |
| 280 | |
| 281 | - description: Check salt top states on nodes |
| 282 | cmd: salt '*' state.show_top |
| 283 | node_name: {{ HOSTNAME_CFG01 }} |
| 284 | retry: {count: 1, delay: 5} |
| 285 | skip_fail: false |
| 286 | |
| 287 | - description: Configure ntp and rsyslog on nodes |
| 288 | cmd: salt --hard-crash --state-output=mixed --state-verbose=False '*' state.sls ntp,rsyslog |
| 289 | node_name: {{ HOSTNAME_CFG01 }} |
| 290 | retry: {count: 1, delay: 10} |
| 291 | skip_fail: false |