ceph grain updated to run only on ceph nodes
Change-Id: If7384ca866b7c0909a38d2c286a7903bcbf3f516
diff --git a/_grains/ceph.py b/_grains/ceph.py
index 003fcd0..5df302c 100644
--- a/_grains/ceph.py
+++ b/_grains/ceph.py
@@ -8,84 +8,89 @@
import os
import re
- # 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)
- grain = {}
- grain["ceph"] = {}
- 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 = d[0].replace('1','')
- encrypted = True
- break
- else:
- dev = device[0].replace('1','')
- device[0] = device[2]
- devices[device[0]] = {}
- devices[device[0]]['dev'] = dev
- if encrypted:
- devices[device[0]]['dmcrypt'] = 'true'
- tline = check_output("ceph 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
- grain["ceph"]["ceph_disk"] = devices
+ if os.path.exists('/etc/ceph'):
+ grain = {}
+ grain["ceph"] = {}
- # keyrings
- directory = '/etc/ceph/'
- keyrings = {}
- if os.path.isdir(directory):
- for filename in os.listdir(directory):
- if filename.endswith(".keyring") and filename.startswith("ceph.client"):
- keyring_output = open(os.path.join(directory, filename), "r")
- keyring_name = re.search('ceph.client.(.+?).keyring', filename).group(1)
- 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
+ # 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 = d[0].replace('1','')
+ encrypted = True
+ break
+ else:
+ dev = device[0].replace('1','')
+ device[0] = device[2]
+ devices[device[0]] = {}
+ devices[device[0]]['dev'] = dev
+ if encrypted:
+ devices[device[0]]['dmcrypt'] = 'true'
+ tline = check_output("ceph 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
+ grain["ceph"]["ceph_disk"] = devices
- # mon keyring
- hostname = check_output("hostname", shell=True).rstrip()
- filepath = "/var/lib/ceph/mon/ceph-{0}/keyring".format(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
+ # keyrings
+ directory = '/etc/ceph/'
+ keyrings = {}
+ if os.path.isdir(directory):
+ for filename in os.listdir(directory):
+ if filename.endswith(".keyring") and filename.startswith("ceph.client"):
+ keyring_output = open(os.path.join(directory, filename), "r")
+ keyring_name = re.search('ceph.client.(.+?).keyring', filename).group(1)
+ 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
- return grain
+ # mon keyring
+ hostname = check_output("hostname", shell=True).rstrip()
+ filepath = "/var/lib/ceph/mon/ceph-{0}/keyring".format(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