#!/usr/bin/env python

def main():

    from subprocess import check_output
    from subprocess import CalledProcessError
    import shlex
    import json
    import os
    import re

    if os.path.exists('/etc/ceph'):
        grain = {}
        grain["ceph"] = {}
        conf_dir = '/etc/ceph/'
        for filename in os.listdir(conf_dir):
            if filename.endswith(".conf"):
                cluster_name = re.search('(.+?).conf', filename).group(1)
                break
        conf_file = conf_dir + cluster_name + '.conf'

        # get the fsid from config file, for salt-formulas to filter on in case of multiple ceph clusters
        with open(conf_file, 'r') as conf_fh:
            for line in conf_fh.read().splitlines():
                if 'fsid' in line:
                    attr = shlex.split(line)
                    grain['ceph']['fsid'] = attr[2]

        # osd
        if os.path.exists('/var/lib/ceph/osd'):
            devices = {}
            for osd in os.listdir('/var/lib/ceph/osd'):

                try:
                    with open('/var/lib/ceph/osd/%s/whoami' % osd) as file:
                        dev_id = file.readline().strip()
                    with open('/var/lib/ceph/osd/%s/fsid' % osd) as file:
                        fsid = file.readline().strip()
                    path = os.readlink('/var/lib/ceph/osd/%s/block' % osd)
                except IOError:
                    continue            

                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]['fsid'] = fsid
                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/'
        keyrings = {}
        if os.path.isdir(directory):
            for filename in os.listdir(directory):
                if filename.endswith(".keyring") and re.search(".client.", filename):
                    keyring_output = open(os.path.join(directory, filename), "r")
                    keyring_name = re.search('(.+?).client.(.+?).keyring', filename).group(2)
                    if keyring_output:
                        keyrings[keyring_name] = {}
                        for line in keyring_output:
                            attr = shlex.split(line)
                            if attr:
                                if attr[0] == 'key':
                                    keyrings[keyring_name]['key'] = attr[2]
                                if attr[0] == 'caps' and 'caps' in keyrings[keyring_name]:
                                    keyrings[keyring_name]['caps'][attr[1]] = attr[3]
                                elif attr[0] == 'caps' and 'caps' not in keyrings[keyring_name]:
                                    keyrings[keyring_name]['caps'] = {}
                                    keyrings[keyring_name]['caps'][attr[1]] = attr[3]
            if keyrings:
                grain["ceph"]["ceph_keyring"] = keyrings

        # mon keyring
        hostname = check_output("hostname", shell=True).rstrip()
        filepath = "/var/lib/ceph/mon/{0}-{1}/keyring".format(cluster_name, hostname)
        if os.path.isfile(filepath):
            mon_key_output = open(filepath, "r")
            if mon_key_output:
                keyrings['mon'] = {}
                for line in mon_key_output:
                    attr = shlex.split(line)
                    if attr:
                        if attr[0] == 'key':
                            keyrings['mon']['key'] = attr[2]
                        if attr[0] == 'caps' and 'caps' in keyrings['mon']:
                            keyrings['mon']['caps'][attr[1]] = attr[3]
                        elif attr[0] == 'caps' and 'caps' not in keyrings['mon']:
                            keyrings['mon']['caps'] = {}
                            keyrings['mon']['caps'][attr[1]] = attr[3]
                grain["ceph"]["ceph_keyring"] = keyrings

        return grain
    else:
        return None

