Refactor reclass cloning

SALT_MODELS_REF_CHANGE SALT_MODELS_COMMIT are both exclusive. If
SALT_MODELS_REF_CHANGE is specified then it means we don't need to do a
checkout to special commit. SALT_MODELS_COMMIT is specified but
SALT_MODELS_REF_CHANGE is not, then we need to checkout that commit.

* This change refactor the logic across all templates.
* This change introduce for loop for SALT_MODELS_REF_CHANGE. Several
  commits can be specified using space as separator. Here is an example
  export SALT_MODELS_REF_CHANGE='refs/changes/12/8412/2 refs/changes/58/8458/1'
  All commits should not have any conflicts. In case of conflicts create
  dependant commits and use top one.
* The 'cmd:' for the step "Clone reclass models with submodules" was moved
  to shared-salt.yaml and included to other templates as a jinja2 'macro'.
* SALT_MODELS_SYSTEM_COMMIT now works as expected. If not specified then
  will be used the 'system' commit specified in the cluster model
  submodule.

Doc-Impact

Change-Id: I0d57b1eea79a7c011231dcf7f46fb3599a62c33f
Signed-off-by: Sergii Golovatiuk <sgolovatiuk@mirantis.com>
Reviewed-on: https://review.gerrithub.io/372906
Reviewed-by: Sergii Golovatiuk <holser@gmail.com>
Reviewed-by: Dennis Dmitriev <dis.xcom@gmail.com>
Tested-by: Dennis Dmitriev <dis.xcom@gmail.com>
diff --git a/tcp_tests/templates/shared-salt.yaml b/tcp_tests/templates/shared-salt.yaml
new file mode 100644
index 0000000..5e9179c
--- /dev/null
+++ b/tcp_tests/templates/shared-salt.yaml
@@ -0,0 +1,71 @@
+{% set SALT_MODELS_BRANCH = os_env('SALT_MODELS_BRANCH','master') %}
+{% set SALT_MODELS_COMMIT = os_env('SALT_MODELS_COMMIT','master') %}
+{# 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 #}
+{% set SALT_MODELS_REF_CHANGE = os_env('SALT_MODELS_REF_CHANGE', '') %}
+{# Pin to a specified commit in salt-models/reclass-system #}
+{% set SALT_MODELS_SYSTEM_COMMIT = os_env('SALT_MODELS_SYSTEM_COMMIT','') %}
+
+{# Address pools for reclass cluster model are taken in the following order:
+ # 1. environment variables,
+ # 2. config.underlay.address_pools based on fuel-devops address pools
+ #    (see generated '.ini' file after underlay is created),
+ # 3. defaults #}
+{% set address_pools = config.underlay.address_pools %}
+{% set IPV4_NET_ADMIN = os_env('IPV4_NET_ADMIN', address_pools.get('admin-pool01', '192.168.10.0/24')) %}
+{% set IPV4_NET_CONTROL = os_env('IPV4_NET_CONTROL', address_pools.get('private-pool01', '172.16.10.0/24')) %}
+{% set IPV4_NET_TENANT = os_env('IPV4_NET_TENANT', address_pools.get('tenant-pool01', '10.1.0.0/24')) %}
+{% set IPV4_NET_EXTERNAL = os_env('IPV4_NET_EXTERNAL', address_pools.get('external-pool01', '10.16.0.0/24')) %}
+{% set IPV4_NET_ADMIN_PREFIX = '.'.join(IPV4_NET_ADMIN.split('.')[0:3]) %}
+{% set IPV4_NET_CONTROL_PREFIX = '.'.join(IPV4_NET_CONTROL.split('.')[0:3]) %}
+{% set IPV4_NET_TENANT_PREFIX = '.'.join(IPV4_NET_TENANT.split('.')[0:3]) %}
+{% set IPV4_NET_EXTERNAL_PREFIX = '.'.join(IPV4_NET_EXTERNAL.split('.')[0:3]) %}
+
+{# - description: Clone reclass models with submodules #}
+{%- macro MACRO_CLONE_RECLASS_MODELS() %}
+    ssh-keyscan -H github.com >> ~/.ssh/known_hosts;
+    git clone -b {{ SALT_MODELS_BRANCH }} --recurse-submodules {{ SALT_MODELS_REPOSITORY }} /srv/salt/reclass;
+    pushd /srv/salt/reclass && \
+    {%- if SALT_MODELS_REF_CHANGE != '' %}
+    {%- for item in SALT_MODELS_REF_CHANGE.split(" ") %}
+    git fetch {{ SALT_MODELS_REPOSITORY }} {{ item }} && git cherry-pick FETCH_HEAD && \
+    {%- endfor %}
+    {%- elif SALT_MODELS_COMMIT != 'master' %}
+    git checkout {{ SALT_MODELS_COMMIT }} && \
+    {%- endif %}
+    {%- if SALT_MODELS_SYSTEM_COMMIT != '' %}
+    pushd classes/system/ && \
+    git checkout {{ SALT_MODELS_SYSTEM_COMMIT }} && \
+    popd && \
+    {%- else %}
+    git submodule update --init --recursive && \
+    {%- endif %}
+    popd;
+    mkdir -p /srv/salt/reclass/classes/service;
+
+    # Replace firstly to an intermediate value to avoid intersection between
+    # already replaced and replacing networks.
+    # For example, if generated IPV4_NET_ADMIN_PREFIX=10.16.0 , then there is a risk of replacing twice:
+    # 192.168.10 -> 10.16.0 (generated network for admin)
+    # 10.16.0 -> <external network>
+    # So let's replace constant networks to the keywords, and then keywords to the desired networks.
+    find /srv/salt/reclass/ -type f -exec sed -i 's/192\.168\.10\./==IPV4_NET_ADMIN_PREFIX==/g' {} +
+    find /srv/salt/reclass/ -type f -exec sed -i 's/172\.16\.10\./==IPV4_NET_CONTROL_PREFIX==/g' {} +
+    find /srv/salt/reclass/ -type f -exec sed -i 's/10\.1\.0\./==IPV4_NET_TENANT_PREFIX==/g' {} +
+    find /srv/salt/reclass/ -type f -exec sed -i 's/10\.16\.0\./==IPV4_NET_EXTERNAL_PREFIX==/g' {} +
+
+    find /srv/salt/reclass/ -type f -exec sed -i 's/==IPV4_NET_ADMIN_PREFIX==/{{ IPV4_NET_ADMIN_PREFIX }}./g' {} +
+    find /srv/salt/reclass/ -type f -exec sed -i 's/==IPV4_NET_CONTROL_PREFIX==/{{ IPV4_NET_CONTROL_PREFIX }}./g' {} +
+    find /srv/salt/reclass/ -type f -exec sed -i 's/==IPV4_NET_TENANT_PREFIX==/{{ IPV4_NET_TENANT_PREFIX }}./g' {} +
+    find /srv/salt/reclass/ -type f -exec sed -i 's/==IPV4_NET_EXTERNAL_PREFIX==/{{ IPV4_NET_EXTERNAL_PREFIX }}./g' {} +
+
+    find /srv/salt/reclass/ -type f -exec sed -i 's/apt_mk_version:.*/apt_mk_version: {{ REPOSITORY_SUITE }}/g' {} +
+
+    # Disable checkouting the model from remote repository
+    cat << 'EOF' >> /srv/salt/reclass/nodes/{{ HOSTNAME_CFG01 }}.yml
+    # local storage
+      reclass:
+        storage:
+          data_source:
+            engine: local
+    EOF
+{%- endmacro %}