Refactoring for using minion names instead of pillars and some standart
functions instead of cmds
Related-Prod: #PROD-30065 (PROD:30065)
Related-Prod: #PROD-29949 (PROD:29949)
Cherry-picked from commit: f99b1165574a761743226148d843b91f3e4e2cc9

Change-Id: Ia639a462a91095b04a944fb986fe5d05f6291a5e
diff --git a/update-ceph.groovy b/update-ceph.groovy
index 72ac2d5..c26c229 100644
--- a/update-ceph.groovy
+++ b/update-ceph.groovy
@@ -1,10 +1,9 @@
 /**
- * Update packages on given nodes
+ * Update packages
  *
  * 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].
  */
 
 pepperEnv = "pepperEnv"
@@ -19,12 +18,13 @@
 def command
 def commandKwargs
 def selMinions = []
+def check_mon
 
 def runCephCommand(master, target, cmd) {
     return salt.cmdRun(master, target, cmd)
 }
 
-def waitForHealthy(master, tgt, count=0, attempts=100) {
+def waitForHealthy(master, tgt, count = 0, attempts=100) {
     // wait for healthy cluster
     common = new com.mirantis.mk.Common()
     while (count<attempts) {
@@ -42,87 +42,62 @@
     node() {
         try {
 
+            def targets = ["common": "ceph-common", "osd": "ceph-osd", "mon": "ceph-mon",
+                          "mgr":"ceph-mgr", "radosgw": "radosgw"]
+
             stage('Setup virtualenv for Pepper') {
                 python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
             }
 
-            stage('List target servers') {
-                minions = salt.getMinions(pepperEnv, TARGET_SERVERS)
-
-                if (minions.isEmpty()) {
-                    throw new Exception("No minion was targeted")
-                }
-
-                for (m  in minions) {
-                    if (m.startsWith("osd") || m.startsWith("cmn") || m.startsWith("rgw")) {
-                        selMinions.add(m)
-                    }
-                }
-            }
-
-
-
             stage('Apply package upgrades on all nodes') {
 
-                for (tgt in selMinions) {
-                    try {
-                        if (tgt.startsWith("osd")) {
-                            out = runCephCommand(pepperEnv, tgt, "apt install --only-upgrade ceph-osd -y")
-                            salt.printSaltCommandResult(out)
-                        } else if (tgt.startsWith("cmn")) {
-                            out = runCephCommand(pepperEnv, tgt, "apt install --only-upgrade ceph-mon -y")
-                            salt.printSaltCommandResult(out)
-                        } else if (tgt.startsWith("rgw")) {
-                            out = runCephCommand(pepperEnv, tgt, "apt install --only-upgrade radosgw -y")
-                            salt.printSaltCommandResult(out)
-                        }
-                    } catch (Throwable e) {
-                        if (e.message.contains("Unmet dependencies")) {
-                            out = runCephCommand(pepperEnv, tgt, "apt -f install -y")
-                            salt.printSaltCommandResult(out)
-                        } else {
-                            throw (e)
-                        }
-                    }
+                targets.each { key, value ->
+                   // try {
+                        command = "pkg.install"
+                        packages = value
+                        commandKwargs = ['only_upgrade': 'true','force_yes': 'true']
+                        target = "I@ceph:${key}"
+                        out = salt.runSaltCommand(pepperEnv, 'local', ['expression': target, 'type': 'compound'], command, true, packages, commandKwargs)
+                        salt.printSaltCommandResult(out)
                 }
             }
 
             stage("Restart MONs and RGWs") {
+                selMinions = salt.getMinions(pepperEnv, "I@ceph:mon")
                 for (tgt in selMinions) {
-                    if (tgt.contains("cmn")) {
-                        runCephCommand(pepperEnv, tgt, "systemctl restart ceph-mon.target")
-                        waitForHealthy(pepperEnv, tgt)
-                    } else if (tgt.contains("rgw")) {
-                        runCephCommand(pepperEnv, tgt, "systemctl restart ceph-radosgw.target")
-                        waitForHealthy(pepperEnv, tgt)
-                    }
+                    // runSaltProcessStep 'service.restart' don't work for this services
+                    runCephCommand(pepperEnv, tgt, "systemctl restart ceph-mon.target")
+                    waitForHealthy(pepperEnv, tgt)
+                }
+                selMinions = salt.getMinions(pepperEnv, "I@ceph:radosgw")
+                for (tgt in selMinions) {
+                    runCephCommand(pepperEnv, tgt, "systemctl restart ceph-radosgw.target")
+                    waitForHealthy(pepperEnv, tgt)
                 }
             }
 
             stage('Restart OSDs') {
 
+                selMinions = salt.getMinions(pepperEnv, "I@ceph:osd")
                 for (tgt in selMinions) {
-                    if (tgt.contains("osd")) {
-                        salt.runSaltProcessStep(pepperEnv, tgt, 'saltutil.sync_grains', [], null, true, 5)
-                        def ceph_disks = salt.getGrain(pepperEnv, tgt, 'ceph')['return'][0].values()[0].values()[0]['ceph_disk']
+                    salt.runSaltProcessStep(pepperEnv, tgt, 'saltutil.sync_grains', [], null, true, 5)
+                    def ceph_disks = salt.getGrain(pepperEnv, tgt, 'ceph')['return'][0].values()[0].values()[0]['ceph_disk']
 
-                        def osd_ids = []
-                        for (i in ceph_disks) {
-                            def osd_id = i.getKey().toString()
-                            osd_ids.add('osd.' + osd_id)
-                        }
-
-                        runCephCommand(pepperEnv, tgt, 'ceph osd set noout')
-
-                        for (i in osd_ids) {
-
-                            salt.runSaltProcessStep(pepperEnv, tgt, 'service.restart', ['ceph-osd@' + i.replaceAll('osd.', '')],  null, true)
-                            // wait for healthy cluster
-                            waitForHealthy(pepperEnv, tgt)
-                        }
-
-                        runCephCommand(pepperEnv, tgt, 'ceph osd unset noout')
+                    def osd_ids = []
+                    for (i in ceph_disks) {
+                        def osd_id = i.getKey().toString()
+                        osd_ids.add('osd.' + osd_id)
                     }
+
+                    runCephCommand(pepperEnv, tgt, 'ceph osd set noout')
+
+                    for (i in osd_ids) {
+                        salt.runSaltProcessStep(pepperEnv, tgt, 'service.restart', ['ceph-osd@' + i.replaceAll('osd.', '')],  null, true)
+                        // wait for healthy cluster
+                        waitForHealthy(pepperEnv, tgt)
+                    }
+
+                    runCephCommand(pepperEnv, tgt, 'ceph osd unset noout')
                 }
             }
 
@@ -134,4 +109,4 @@
             throw e
         }
     }
-}
\ No newline at end of file
+}