upgrade pipeline fixes

Change-Id: I3eccbe1e18a950084047bbdb618cfdfdf3c868b0
diff --git a/openstack-compute-upgrade.groovy b/openstack-compute-upgrade.groovy
new file mode 100644
index 0000000..cde839c
--- /dev/null
+++ b/openstack-compute-upgrade.groovy
@@ -0,0 +1,112 @@
+/**
+ * Update packages on given nodes
+ *
+ * Expected parameters:
+ *   SALT_MASTER_CREDENTIALS    Credentials to the Salt API.
+ *   SALT_MASTER_URL            Full Salt API address [https://10.10.10.1:8000].
+ *   TARGET_SERVERS             Salt compound target to match nodes to be updated [*, G@osfamily:debian].
+ *   TARGET_PACKAGES            Space delimited list of packages to be updates [package1=version package2=version], empty string means all updating all packages to the latest version.
+ *   TARGET_SUBSET_TEST         Number of nodes to list package updates, empty string means all targetted nodes.
+ *   TARGET_SUBSET_LIVE         Number of selected nodes to live apply selected package update.
+ *   TARGET_BATCH_LIVE          Batch size for the complete live package update on all nodes, empty string means apply to all targetted nodes.
+ *
+**/
+
+def common = new com.mirantis.mk.Common()
+def salt = new com.mirantis.mk.Salt()
+
+def saltMaster
+def targetAll = ['expression': TARGET_SERVERS, 'type': 'compound']
+def targetTestSubset
+def targetLiveSubset
+def targetLiveAll
+def minions
+def result
+def packages
+def command
+def commandKwargs
+
+node() {
+    try {
+
+        stage('Connect to Salt master') {
+            saltMaster = salt.connection(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
+        }
+
+        stage('List target servers') {
+            minions = salt.getMinions(saltMaster, targetAll)
+
+            if (minions.isEmpty()) {
+                throw new Exception("No minion was targeted")
+            }
+
+            if (TARGET_SUBSET_TEST != "") {
+                targetTestSubset = minions.subList(0, Integer.valueOf(TARGET_SUBSET_TEST)).join(' or ')
+            } else {
+                targetTestSubset = minions.join(' or ')
+            }
+            targetLiveSubset = minions.subList(0, Integer.valueOf(TARGET_SUBSET_LIVE)).join(' or ')
+
+            targetLiveAll = minions.join(' or ')
+            common.infoMsg("Found nodes: ${targetLiveAll}")
+            common.infoMsg("Selected test nodes: ${targetTestSubset}")
+            common.infoMsg("Selected sample nodes: ${targetLiveSubset}")
+        }
+
+        stage("Add new repos on sample") {
+            salt.enforceState(saltMaster, targetTestSubset, 'linux.system')
+        }
+
+        stage("List package upgrades") {
+            salt.runSaltProcessStep(saltMaster, targetTestSubset, 'pkg.list_upgrades', [], null, true)
+        }
+
+        stage('Confirm live package upgrades on sample') {
+            if(TARGET_PACKAGES==""){
+                timeout(time: 2, unit: 'HOURS') {
+                    def userInput = input(
+                     id: 'userInput', message: 'Insert package names for update', parameters: [
+                     [$class: 'TextParameterDefinition', defaultValue: '', description: 'Package names (or *)', name: 'packages']
+                    ])
+                    if(userInput!= "" && userInput!= "*"){
+                        TARGET_PACKAGES = userInput
+                    }
+                }
+            }else{
+                timeout(time: 2, unit: 'HOURS') {
+                   input message: "Approve live package upgrades on ${targetLiveSubset} nodes?"
+                }
+            }
+        }
+
+        if (TARGET_PACKAGES != "") {
+            command = "pkg.install"
+            packages = TARGET_PACKAGES.tokenize(' ')
+            commandKwargs = ['only_upgrade': 'true']
+        }else {
+            command = "pkg.upgrade"
+            packages = null
+        }
+
+        stage('Apply package upgrades on sample') {
+            out = salt.runSaltCommand(saltMaster, 'local', ['expression': targetLiveSubset, 'type': 'compound'], command, null, packages, commandKwargs)
+            salt.printSaltCommandResult(out)
+        }
+
+        stage('Confirm package upgrades on all nodes') {
+            timeout(time: 2, unit: 'HOURS') {
+               input message: "Approve live package upgrades on ${targetLiveAll} nodes?"
+            }
+        }
+
+        stage('Apply package upgrades on all nodes') {
+            out = salt.runSaltCommand(saltMaster, 'local', ['expression': targetLiveAll, 'type': 'compound'], command, null, packages, commandKwargs)
+            salt.printSaltCommandResult(out)
+        }
+
+    } catch (Throwable e) {
+        // If there was an error or exception thrown, the build failed
+        currentBuild.result = "FAILURE"
+        throw e
+    }
+}
diff --git a/openstack-control-upgrade.groovy b/openstack-control-upgrade.groovy
index 895179c..66cdebb 100644
--- a/openstack-control-upgrade.groovy
+++ b/openstack-control-upgrade.groovy
@@ -81,7 +81,7 @@
                 // salt 'kvm02*' state.sls salt.control
                 salt.enforceState(saltMaster, "${upgNodeProvider}", 'salt.control')
 
-                sleep(60)
+                sleep(70)
 
                 // salt '*' saltutil.refresh_pillar
                 salt.runSaltProcessStep(saltMaster, 'upg*', 'saltutil.refresh_pillar', [], null, true)
@@ -283,7 +283,7 @@
                 // salt 'kvm*' state.sls salt.control
                 salt.enforceState(saltMaster, 'I@salt:control', 'salt.control')
 
-                sleep(60)
+                sleep(70)
 
                 // salt '*' saltutil.refresh_pillar
                 salt.runSaltProcessStep(saltMaster, '*', 'saltutil.refresh_pillar', [], null, true)
@@ -531,7 +531,7 @@
                 // salt 'cmp*' cmd.run 'service nova-compute restart'
                 salt.runSaltProcessStep(saltMaster, 'cmp*', 'service.restart', ['nova-compute'], null, true)
 
-                sleep(60)
+                sleep(70)
 
                 salt.cmdRun(saltMaster, 'ctl01*', '. /root/keystonerc; nova service-list; glance image-list; nova flavor-list; nova hypervisor-list; nova list; neutron net-list; cinder list; heat service-list')
             }
diff --git a/update-package.groovy b/update-package.groovy
index 1dd6e02..c6d008d 100644
--- a/update-package.groovy
+++ b/update-package.groovy
@@ -6,9 +6,9 @@
  *   SALT_MASTER_URL            Full Salt API address [https://10.10.10.1:8000].
  *   TARGET_SERVERS             Salt compound target to match nodes to be updated [*, G@osfamily:debian].
  *   TARGET_PACKAGES            Space delimited list of packages to be updates [package1=version package2=version], empty string means all updating all packages to the latest version.
- *   TARGET_SIZE_TEST           Number of nodes to list package updates, empty string means all targetted nodes.
- *   TARGET_SIZE_SAMPLE         Number of selected noded to live apply selected package update.
- *   TARGET_SIZE_BATCH          Batch size for the complete live package update on all nodes, empty string means apply to all targetted nodes.
+ *   TARGET_SUBSET_TEST         Number of nodes to list package updates, empty string means all targetted nodes.
+ *   TARGET_SUBSET_LIVE         Number of selected nodes to live apply selected package update.
+ *   TARGET_BATCH_LIVE          Batch size for the complete live package update on all nodes, empty string means apply to all targetted nodes.
  *
 **/