Merge "Add swarm-cookied-model-generator to JJB PROD-36999"
diff --git a/jobs/pipelines/cookied-model-generator.sh b/jobs/pipelines/cookied-model-generator.sh
new file mode 100644
index 0000000..3dd9b3c
--- /dev/null
+++ b/jobs/pipelines/cookied-model-generator.sh
@@ -0,0 +1,106 @@
+# export REMOTE_JENKINS='172.19.112.216'
+export REMOTE_JENKINS='sre-ci.scc.mirantis.net'
+
+if [ ! -n "$LAB_CONTEXT_NAME" ]; then
+  echo 'LAB_CONTEXT_NAME is not set!
+    This name will be used:
+    - to find the template file tcp_tests/templates/cookied-model-generator/salt_${LAB_CONTEXT_NAME}.yaml
+    - to set the salt model cluster name
+    - to make DOMAIN_NAME=${LAB_CONTEXT_NAME}.local
+  ';
+  exit 1
+fi
+
+
+if [ -n "$TCP_QA_REVIEW" ]; then
+	git fetch https://gerrit.mcp.mirantis.com/mcp/tcp-qa $TCP_QA_REVIEW && git checkout FETCH_HEAD
+fi
+
+. ${VENV_PATH}/bin/activate
+
+if [ "$ERASE_EXISTING_ENVIRONMENT" = "true" ]; then
+    dos.py erase $ENV_NAME || true
+fi
+
+cd tcp_tests
+
+sed -i 's/%LOGIN%/rHm22xBV9K/' templates/${LAB_CONTEXT_NAME}/salt-context-cookiecutter*
+sed -i 's/%PASS%/az9Gq7Wq4jRic29YWP/' templates/${LAB_CONTEXT_NAME}/salt-context-cookiecutter*
+
+rm -f ./*.tar.gz
+
+#MODEL_FILENAME=model_${CLUSTER_NAME}_${BUILD_NUMBER}.tar.gz
+MODEL_FILENAME=model_${ENV_NAME}.tar.gz
+if [[ -f $MODEL_FILENAME ]]; then
+   rm $MODEL_FILENAME
+fi
+
+####################
+# Generate the model
+git config  user.name "tester"
+git config  user.email "tester@example.com"
+
+py.test --junit-xml=deploy_generate_model.xml -k ${TEST_GROUP}
+
+
+########################################################
+# Upload the generated model to the repository on cz8133
+
+# Create a directory where the created model will be unpacked
+rm -rf ./model
+mkdir -p ./model
+
+eval $(ssh-agent)
+ssh-add ${COOKIECUTTER_TEMPLATES_REPOSITORY_KEY_PATH}
+
+# Create remote repository
+ssh jenkins@${REMOTE_JENKINS} "
+  if [[ ! -d /home/jenkins/salt-models/${LAB_CONTEXT_NAME}.git ]]; then
+    mkdir -p /home/jenkins/salt-models/${LAB_CONTEXT_NAME}.git;
+    cd /home/jenkins/salt-models/${LAB_CONTEXT_NAME}.git;
+    git init --bare;
+    cp hooks/post-update.sample hooks/post-update;
+  fi"
+
+cd ./model
+
+# Prepare the repository in the ./model
+git init
+git remote add integration ssh://jenkins@${REMOTE_JENKINS}/home/jenkins/salt-models/${LAB_CONTEXT_NAME}.git
+git pull integration ${REPOSITORY_SUITE} || true
+git checkout -b ${REPOSITORY_SUITE}
+git rm -rf . || true   # remove previous content from the working dir
+
+# unpack new model into ./model dir
+tar -xzf ../${MODEL_FILENAME} -C ./
+# Add reclass-system submodule
+git submodule add https://gerrit.mcp.mirantis.com/salt-models/reclass-system classes/system/
+cd classes/system
+git checkout ${SALT_MODELS_SYSTEM_COMMIT}
+cd ../..
+
+# The directory "classes/service" is created at bootstrap time
+# and cannot be commited to the repository automatically.
+# It is not needed in the repo because contains only symlinks.
+cat << EOF > ./.gitignore
+classes/service
+EOF
+
+# create a commit with new model (if any change exists)
+git add -A
+git commit -m "${LAB_CONTEXT_NAME}@${REPOSITORY_SUITE} generated at $(date +%Y-%m-%d/%H-%M)
+
+REPOSITORY_SUITE=${REPOSITORY_SUITE}
+SALT_MODELS_SYSTEM_COMMIT=${SALT_MODELS_SYSTEM_COMMIT}
+COOKIECUTTER_TEMPLATE_COMMIT=${COOKIECUTTER_TEMPLATE_COMMIT}
+"
+
+# Push new commit to the ${REMOTE_JENKINS} repository
+git push integration ${REPOSITORY_SUITE}
+
+echo "######################################################################################################################"
+echo "#"
+echo "# Clone the model into the tests:  git clone http://${REMOTE_JENKINS}:8098/${LAB_CONTEXT_NAME}.git -b ${REPOSITORY_SUITE}"
+echo "#"
+echo "######################################################################################################################"
+
diff --git a/jobs/project.yaml b/jobs/project.yaml
index edd3db6..d98278f 100644
--- a/jobs/project.yaml
+++ b/jobs/project.yaml
@@ -47,6 +47,7 @@
       - download-config-drive
       - swarm-deploy-cicd
       - swarm-deploy-platform
+      - swarm-cookied-model-generator
       - swarm-run-pytest
       # - generate-report - can't moved to JJB, a lot of parameters is not supported like Inject environment variables
       # - mark-reports - can't moved to JJB, a lot of parameters is not supported like Inject environment variables
diff --git a/jobs/templates/cleanup-jenkins-agents.yml b/jobs/templates/cleanup-jenkins-agents.yml
index 859463c..d20614d 100644
--- a/jobs/templates/cleanup-jenkins-agents.yml
+++ b/jobs/templates/cleanup-jenkins-agents.yml
@@ -15,4 +15,4 @@
           url: https://gerrit.mcp.mirantis.com/mcp/tcp-qa
       script-path: jobs/pipelines/remove-old-jenkins-agents.groovy
     logrotate:
-      daysToKeep: 365
\ No newline at end of file
+      daysToKeep: 30
\ No newline at end of file
diff --git a/jobs/templates/swarm-cookied-model-generator.yml b/jobs/templates/swarm-cookied-model-generator.yml
new file mode 100644
index 0000000..79695b4
--- /dev/null
+++ b/jobs/templates/swarm-cookied-model-generator.yml
@@ -0,0 +1,171 @@
+- job-template:
+    project-type: freestyle
+    description: -|
+        {job-description}
+      WARNING! models are pushed to the same LAB_CONTEXT_NAME repositories under the specified tags! 
+      Fuel-devops environment is erased after the build by default
+    concurrent: true
+    disabled: false
+    name: swarm-cookied-model-generator
+    parameters:
+      - string:
+          default: ''
+          description: 'This name will be used:
+            - to find the template file tcp_tests/templates/cookied-model-generator/salt_$LAB_CONTEXT_NAME.yaml
+            - to set the salt model cluster name'
+          name: LAB_CONTEXT_NAME
+          trim: 'false'
+      - string:
+          default: ''
+          description: 'Cluster name wich will be used for model creation
+Default: LAB_CONFIG_NAME'
+          name: CLUSTER_NAME
+          trim: 'false'
+      - string:
+          default: ''
+          description: 'Domain name wich will be used for model creation
+Default: LAB_CONFIG_NAME.local'
+          name: DOMAIN_NAME
+          trim: 'false'
+      - string:
+          default: 'proposed'
+          description: ''
+          name: REPOSITORY_SUITE
+          trim: 'false'
+      - string:
+          default: 'proposed'
+          description: ''
+          name: SALT_MODELS_SYSTEM_COMMIT
+          trim: 'false'
+      - string:
+          default: 'proposed'
+          description: ''
+          name: COOKIECUTTER_TEMPLATE_COMMIT
+          trim: 'false'
+      - string:
+          default: ''
+          description: ''
+          name: JENKINS_PIPELINE_BRANCH
+          trim: 'false'
+      - string:
+          default: ''
+          description: ''
+          name: SALT_MODELS_SYSTEM_REF_CHANGE
+          trim: 'false'
+      - string:
+          default: 'sre-team-infra'
+          description: ''
+          name: NODE_NAME
+          trim: 'false'
+      - string:
+          default: ''
+          description: ''
+          name: TCP_QA_REFS
+      - string:
+          default: ''
+          description: 'environment-templates repository ref change'
+          name: ENVIRONMENT_TEMPLATE_REF_CHANGE
+          trim: 'false'
+      - string:
+          default: false
+          description: cookiecutter-templates repository ref change
+          name: COOKIECUTTER_REF_CHANGE
+      - bool:
+          default: true
+          description: ''
+          name: ERASE_EXISTING_ENVIRONMENT
+      - bool:
+          default: true
+          description: ''
+          name: SHUTDOWN_ENV_ON_TEARDOWN
+      - bool:
+          default: true
+          description: ''
+          name: ERASE_ENVIRONMENT_AFTER_BUILD
+      - string:
+          default: '192.168.10.0/24'
+          description: 'Management (admin/PXE) network. For now /24 only.'
+          name: IPV4_NET_ADMIN
+          trim: 'false'
+      - string:
+          default: '172.16.10.0/24'
+          description: 'Control (internal cluster communications) network. For now /24 only.'
+          name: IPV4_NET_CONTROL
+          trim: 'false'
+      - string:
+          default: '10.1.0.0/24'
+          description: 'OpenStack tenant (transport for user-defined internal networks for instances) network. For now /24 only.'
+          name: IPV4_NET_TENANT
+          trim: 'false'
+      - string:
+          default: '10.16.0.0/24'
+          description: 'OpenStack external network (for incoming connections with floating IPs). For now /24 only.'
+          name: IPV4_NET_EXTERNAL
+          trim: 'false'
+      - string:
+          default: '/home/jenkins/images/cfg01-day01.qcow2'
+          description: 'Image used for the model generation and validation'
+          name: IMAGE_PATH_CFG01_DAY01
+          trim: 'false'
+      - string:
+          default: ''
+          description: ''
+          name: UPDATE_REPO_CUSTOM_TAG
+          trim: 'false'
+      - string:
+          default: ''
+          description: ''
+          name: UPDATE_VERSION
+          trim: 'false'
+      - string:
+          default: ''
+          description: ''
+          name: DEPLOY_NETWORK_NETMASK
+          trim: 'false'
+    logrotate:
+      daysToKeep: 365
+    scm:
+      - git:
+          branches:
+            - FETCH_HEAD
+          refspec: '${{TCP_QA_REFS}}'
+          url: https://gerrit.mcp.mirantis.com/mcp/tcp-qa
+    wrappers:
+      - inject:
+          properties-content: |-
+              VENV_PATH=/home/jenkins/fuel-devops30
+              ENV_NAME=swarm_model_$BUILD_NUMBER
+              LAB_CONFIG_NAME=cookied-model-generator
+
+              SALT_STEPS_PATH=templates/$LAB_CONFIG_NAME/salt_$LAB_CONTEXT_NAME.yaml
+
+              TEST_GROUP=test_generate_model
+              #STORAGE_POOL_NAME=second_pool
+              PYTHONIOENCODING=UTF-8
+              MAKE_SNAPSHOT_STAGES=false
+              DEVOPS_LOCK_FILE=/run/lock/devops_lock
+      - credentials-binding:
+          - ssh-user-private-key:
+              credential-id: f4fb9dd6-ba63-4085-82f7-3fa601334d95
+              key-file-variable: COOKIECUTTER_TEMPLATES_REPOSITORY_KEY_PATH
+    builders:
+      - shell: "chmod +x jobs/pipelines/cookied-model-generator.sh"
+      - shell: "./jobs/pipelines/cookied-model-generator.sh"
+    publishers:
+      - archive:
+          artifacts: 'tcp_tests/*.log,tcp_tests/*.tar.gz,tcp_tests/*.xml'
+          allow-empty: false
+      - post-tasks:
+        - run-if-job-successful: false
+          matches:
+            # Small hack to start post-tasks every time after build
+            - log-text: test session starts
+            - operator: OR
+          script: " . $VENV_PATH/bin/activate;
+          if [ $SHUTDOWN_ENV_ON_TEARDOWN = 'true' ]; then
+          dos.py destroy $ENV_NAME || true;
+          fi;
+          
+          if [ $ERASE_ENVIRONMENT_AFTER_BUILD = 'true' ]; then
+              dos.py erase $ENV_NAME || true;
+          fi;"
diff --git a/src/com/mirantis/system_qa/SharedPipeline.groovy b/src/com/mirantis/system_qa/SharedPipeline.groovy
index 50f883f..832cf9b 100644
--- a/src/com/mirantis/system_qa/SharedPipeline.groovy
+++ b/src/com/mirantis/system_qa/SharedPipeline.groovy
@@ -451,7 +451,7 @@
                 string(name: 'COOKIECUTTER_TEMPLATE_COMMIT', value: "${cookiecuttertemplate_commit}"),
                 string(name: 'COOKIECUTTER_REF_CHANGE', value: "${cookiecutter_ref_change}"),
                 string(name: 'ENVIRONMENT_TEMPLATE_REF_CHANGE', value: "${environment_template_ref_change}"),
-                string(name: 'TCP_QA_REVIEW', value: "${tcp_qa_refs}"),
+                string(name: 'TCP_QA_REFS', value: "${tcp_qa_refs}"),
                 string(name: 'IPV4_NET_ADMIN', value: IPV4_NET_ADMIN),
                 string(name: 'IPV4_NET_CONTROL', value: IPV4_NET_CONTROL),
                 string(name: 'IPV4_NET_TENANT', value: IPV4_NET_TENANT),