refactoring - return the same grains regardless of ceph version

Change-Id: Iac7cb1436faf15dfec4ee2454fdd6961ca946f5a
Related-Prod: PROD-35018
diff --git a/_grains/ceph.py b/_grains/ceph.py
index 54bc97e..981ec18 100644
--- a/_grains/ceph.py
+++ b/_grains/ceph.py
@@ -1,6 +1,5 @@
 #!/usr/bin/env python
 
-
 def main():
 
     from subprocess import check_output
@@ -29,60 +28,38 @@
 
         # osd
         if os.path.exists('/var/lib/ceph/osd'):
-            cmd = "ceph-volume lvm list --format json"
-            osd_output = check_output(cmd, shell=True)
-            osd_output = json.loads(osd_output)
-            dev_id = ''
             devices = {}
-            if osd_output:
-                for osd, params in osd_output.iteritems():
-                    dev_id = osd
-                    devices[dev_id] = {}
-                    devices[dev_id]['dev'] = params[0]['devices'][0]
-                    devices[dev_id]['path'] = params[0]['path']
-                    devices[dev_id]['uuid'] = params[0]['tags']['ceph.osd_fsid']
+            for osd in os.listdir('/var/lib/ceph/osd'):
 
-                cmd = "ceph osd tree --format json"
-                osd_tree_output = check_output(cmd, shell=True)
-                osd_tree_output = json.loads(osd_tree_output)
-                for osd in osd_tree_output['nodes']:
-                    if 'type_id' in osd.keys():
-                        if str(osd['type_id']) == '0':
-                            for dev_id in devices.keys():
-                                if str(osd['id']) == str(dev_id):
-                                    devices[dev_id]['weight'] = osd['crush_weight']
-                                    devices[dev_id]['class'] = osd['device_class']
-                grain["ceph"]["ceph_volume"] = devices
-            else:
-                cmd = "ceph-disk list --format json"
-                osd_output = check_output(cmd, shell=True).decode("utf-8")
-                osd_output = json.loads(osd_output)
-                dev_id = ''
-                devices = {}
-                if osd_output:
-                    for line in osd_output:
-                        if "is_partition" not in line.keys():
-                            dev = line["path"]
-                            parts = line["partitions"]
-                            for p in parts:
-                                if "mount" in p.keys() and "ceph" in p["mount"]:
-                                    dev_id = p["whoami"]
-                                    devices[dev_id] = {}
-                                    devices[dev_id]['dev'] = dev
-                                    if len(p["dmcrypt"]) > 0:
-                                        devices[dev_id]['dmcrypt'] = 'true'
+                try:
+                    with open('/var/lib/ceph/osd/%s/whoami' % osd) as file:
+                        dev_id = file.readline().strip()
+                    path = os.readlink('/var/lib/ceph/osd/%s/block' % osd)
+                except IOError:
+                    continue            
 
-                    cmd = "ceph osd tree --format json"
-                    osd_tree_output = check_output(cmd, shell=True).decode("utf-8")
-                    osd_tree_output = json.loads(osd_tree_output)
-                    for osd in osd_tree_output['nodes']:
-                        if 'type_id' in osd.keys():
-                            if str(osd['type_id']) == '0':
-                                for dev_id in devices.keys():
-                                    if str(osd['id']) == str(dev_id):
-                                        devices[dev_id]['weight'] = osd['crush_weight']
-                                        devices[dev_id]['class'] = osd['device_class']
-                    grain["ceph"]["ceph_disk"] = devices
+                cmd = "lsblk -no pkname %s" % path
+                dev = check_output(cmd, shell=True).strip()
+                if dev:
+                    dev = '/dev/%s' % dev
+                else:
+                    cmd = "pvs  | grep %s | awk '{print $1}'" % path.split('/')[-2]
+                    dev = check_output(cmd, shell=True).strip()
+                devices[dev_id] = {}
+                devices[dev_id]['path'] = path
+                devices[dev_id]['dev'] = dev
+                
+            cmd = "ceph osd tree --format json"
+            osd_tree_output = check_output(cmd, shell=True).decode("utf-8")
+            osd_tree_output = json.loads(osd_tree_output)
+            for osd in osd_tree_output['nodes']:
+                dev_id = str(osd['id'])
+                if dev_id >= 0 and devices.has_key(dev_id):
+                    devices[dev_id]['weight'] = osd['crush_weight']
+                    devices[dev_id]['class'] = osd['device_class']
+
+            if devices:
+                grain['ceph']['ceph_disk'] = devices
 
         # keyrings
         directory = '/etc/ceph/'
@@ -129,3 +106,4 @@
         return grain
     else:
         return None
+