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/virtual-mcp11-k8s-contrail/salt.yaml b/tcp_tests/templates/virtual-mcp11-k8s-contrail/salt.yaml
index b9324e9..d186c37 100644
--- a/tcp_tests/templates/virtual-mcp11-k8s-contrail/salt.yaml
+++ b/tcp_tests/templates/virtual-mcp11-k8s-contrail/salt.yaml
@@ -2,25 +2,12 @@
 {% from 'virtual-mcp11-k8s-contrail/underlay.yaml' import REPOSITORY_SUITE with context %}
 
 {% set SALT_MODELS_REPOSITORY = os_env('SALT_MODELS_REPOSITORY','https://gerrit.mcp.mirantis.net/salt-models/mcp-virtual-lab') %}
-{% set SALT_MODELS_BRANCH = os_env('SALT_MODELS_BRANCH','master') %}
-{% set SALT_MODELS_COMMIT = os_env('SALT_MODELS_COMMIT','master') %}
+# Other salt model repository parameters see in shared-salt.yaml
 
-# 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', '') %}
-
-# 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('public-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_ADMIN_PREFIX = '.'.join(IPV4_NET_ADMIN.split('.')[0:3]) %}
-{% set IPV4_NET_CONTROL_PREFIX = '.'.join(IPV4_NET_CONTROL.split('.')[0:3]) %}
+{% import 'shared-salt.yaml' as SHARED with context %}
 
 # Install salt to the config node
+
 - description: Installing salt master on cfg01
   cmd:  apt-get install -y reclass git; apt-get install -y salt-master
   node_name: {{ HOSTNAME_CFG01 }}
@@ -63,40 +50,9 @@
 
 - description: Clone reclass models with submodules
   cmd: |
-    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 && git checkout {{ SALT_MODELS_COMMIT }} && popd;
-    {%- if SALT_MODELS_REF_CHANGE != '' %}
-    pushd /srv/salt/reclass && git fetch {{ SALT_MODELS_REPOSITORY }} {{ SALT_MODELS_REF_CHANGE }} && git checkout FETCH_HEAD && popd;
-    {%- endif %}
-    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/==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/apt_mk_version:.*/apt_mk_version: {{ REPOSITORY_SUITE }}/g' {} +
-
+{{  SHARED.MACRO_CLONE_RECLASS_MODELS() }}
     # vSRX IPs for tcp-qa images have 172.16.10.90 hardcoded
     find /srv/salt/reclass/ -type f -exec sed -i 's/opencontrail_router01_address:.*/opencontrail_router01_address: 172.16.10.90/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
-
     # Show the changes to the console
     cd /srv/salt/reclass/; git diff
   node_name: {{ HOSTNAME_CFG01 }}