#!/usr/bin/env python


def main():

    from subprocess import check_output
    from subprocess import CalledProcessError
    import shlex
    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'):
            mount_path = check_output("df -h | awk '{print $6}' | grep ceph | grep -v lockbox | sed 's/-[0-9]*//g' | awk 'NR==1{print $1}'", shell=True).rstrip()
            sed = 'sed \'s#{0}-##g\''.format(mount_path)
            cmd = "lsblk -rp | awk '{print $1,$6,$7}' | grep -v lockbox | grep ceph | " + sed
            osd_output = check_output(cmd, shell=True)
            if osd_output:
                devices = {}
                for line in osd_output.splitlines():
                    device = line.split()
                    encrypted = False
                    if "crypt" in device[1]:
                        output = check_output("lsblk -rp | grep -B1 " + device[0], shell=True)
                        for l in output.splitlines():
                            d = l.split()
                            dev = re.sub("\d+", "", device[0])
                            encrypted = True
                            break
                    else:
                        dev = device[0].replace('1','')
                        dev = re.sub("\d+", "", device[0])
                    device[0] = device[2]
                    try:
                        devices[device[0]] = {}
                        devices[device[0]]['dev'] = dev
                        if encrypted:
                            devices[device[0]]['dmcrypt'] = 'true'
                        tline = check_output("ceph -c " + conf_file + " osd tree | awk '{print $1,$2,$3,$4}' | grep -w 'osd." + device[0] + "'", shell=True)
                        osd = tline.split()
                        if "osd" not in osd[2]:
                            crush_class = osd[1]
                            crush_weight = osd[2]
                            devices[device[0]]['class'] = crush_class
                            devices[device[0]]['weight'] = crush_weight
                        else:
                            crush_weight = osd[1]
                            devices[device[0]]['weight'] = crush_weight
                    except CalledProcessError:
                        continue
                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
