Allow to deploy the same MCP configuration with different stack names

Change-Id: I302c3e38318994a8b446e86bc0a37fa81f06f3a0
diff --git a/jobs/pipelines/deploy-cicd-and-run-tests.groovy b/jobs/pipelines/deploy-cicd-and-run-tests.groovy
index 5106d80..a611a1a 100644
--- a/jobs/pipelines/deploy-cicd-and-run-tests.groovy
+++ b/jobs/pipelines/deploy-cicd-and-run-tests.groovy
@@ -17,7 +17,7 @@
     node_with_reports = "${NODE_NAME}"
     make_snapshot_stages = "${env.MAKE_SNAPSHOT_STAGES}" != "false" ? true : false
 } else if (env_manager == 'heat') {
-    jenkins_slave_node_name = "openstack_slave_${JOB_NAME}"
+    jenkins_slave_node_name = "openstack_slave_${env.ENV_NAME}"
     make_snapshot_stages = false
     node_with_reports = jenkins_slave_node_name
 }
@@ -222,11 +222,11 @@
             }
             def deploy = build job: "${upgrade_job}",
                 parameters: [
-                    string(name: 'PARENT_NODE_NAME', value: "openstack_slave_${env.LAB_CONFIG_NAME}"),
+                    string(name: 'PARENT_NODE_NAME', value: "openstack_slave_${env.ENV_NAME}"),
                     string(name: 'TCP_QA_REFS', value: env.TCP_QA_REFS),
                     string(name: 'PASSED_STEPS', value: steps),
                     string(name: 'TEMPEST_TEST_SUITE_NAME', value: env.TEMPEST_TEST_SUITE_NAME),
-                    string(name: 'NODE', value: "openstack_slave_${env.LAB_CONFIG_NAME}")
+                    string(name: 'NODE', value: "openstack_slave_${env.ENV_NAME}")
                 ],
                 wait: false,
                 propagate: false
diff --git a/jobs/pipelines/remove-old-jenkins-agents.groovy b/jobs/pipelines/remove-old-jenkins-agents.groovy
new file mode 100644
index 0000000..eb2ccd8
--- /dev/null
+++ b/jobs/pipelines/remove-old-jenkins-agents.groovy
@@ -0,0 +1,60 @@
+import hudson.node_monitors.*
+import hudson.slaves.*
+import java.util.concurrent.*
+
+jenkins = Jenkins.instance
+
+import javax.mail.internet.*;
+import javax.mail.*
+import javax.activation.*
+
+def getEnviron(computer) {
+   def env
+   def thread = Thread.start("Getting env from ${computer.name}", { env = computer.environment })
+   thread.join(2000)
+   if (thread.isAlive()) thread.interrupt()
+   env
+}
+
+def agentAccessible(computer) {
+    getEnviron(computer)?.get('PATH') != null
+}
+
+def numberOfflineNodes = 0
+def numberNodes = 0
+for (agent in jenkins.getNodes()) {
+   def computer = agent.computer
+   numberNodes ++
+   println ""
+   println "Checking computer ${computer.name}:"
+   def isOK = (agentAccessible(computer) && !computer.offline)
+   if (isOK) {
+     println "\t\tOK, got PATH back from agent ${computer.name}."
+
+   } else {
+     numberOfflineNodes ++
+     println "  ERROR: can't get PATH from agent ${computer.name}."
+     //if (computer.isTemporarilyOffline()) {
+     //  if (!computer.getOfflineCause().toString().contains("Disconnected by")) {
+     //    computer.setTemporarilyOffline(false, agent.getComputer().getOfflineCause())
+     //  }
+     //} else {
+     //    //computer.connect(true)
+     //}
+
+     if (computer.isOffline() && !computer.isTemporarilyOffline())
+     {
+       println "!!!! this node will be killed !!!";
+       computer.doDoDelete();
+     }
+   }
+     println('\tcomputer.isOffline: ' + computer.isOffline());
+     println('\tcomputer.isTemporarilyOffline: ' + computer.isTemporarilyOffline());
+     println('\tcomputer.getOfflineCause: ' + computer.getOfflineCause());
+     println('\tcomputer.offline: ' + computer.offline);
+     time = computer.getIdleStartMilliseconds() ;
+     println('\tcomputer.getConnectTime: ' + new Date(time).toString());
+ }
+println ("Number of Offline Nodes: " + numberOfflineNodes)
+println ("Number of Nodes: " + numberNodes)
+
diff --git a/jobs/project.yaml b/jobs/project.yaml
index 145ae0d..83b78ee 100644
--- a/jobs/project.yaml
+++ b/jobs/project.yaml
@@ -55,6 +55,7 @@
       - environment-template-ci
       - self-deploy-jobs
       - self-deploy-virtualenvs
+      - cleanup-jenkins-agents
       - tcp-qa-ci
       - test_cleanup
       # - testrail-reporter-ci - can't moved to JJB, unsupported parameters
diff --git a/jobs/templates/cleanup-jenkins-agents.yml b/jobs/templates/cleanup-jenkins-agents.yml
new file mode 100644
index 0000000..859463c
--- /dev/null
+++ b/jobs/templates/cleanup-jenkins-agents.yml
@@ -0,0 +1,18 @@
+- job-template:
+    project-type: pipeline
+    description: '{job-description}'
+    concurrent: false
+    disabled: false
+    name: cleanup-jenkins-agents
+    triggers:
+      - timed: 00 2 * * Sun
+    pipeline-scm:
+      lightweight-checkout: true
+      scm:
+      - git:
+          branches:
+          - master
+          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
diff --git a/jobs/view.yaml b/jobs/view.yaml
index a6caaf2..307c4c6 100644
--- a/jobs/view.yaml
+++ b/jobs/view.yaml
@@ -73,6 +73,7 @@
     job-name:
       - self-deploy-jobs
       - self-deploy-virtualenvs
+      - cleanup-jenkins-agents
     columns:
       - status
       - weather