Skip to content

Commit

Permalink
Merge pull request #196 from shtripat/sds-detection-issue
Browse files Browse the repository at this point in the history
Corrected sds detection logic for OSD nodes
  • Loading branch information
r0h4n authored Feb 1, 2017
2 parents 00af2e8 + 942a458 commit 6bfb841
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from tendrl.node_agent.discovery.sds.discover_sds_plugin \
import DiscoverSDSPlugin
from tendrl.node_agent import ini2json

LOG = logging.getLogger(__name__)

Expand All @@ -14,7 +15,7 @@ def discover_storage_system(self):

# get the gluster version details
cmd = subprocess.Popen(
"ceph version -f json",
"ceph --version",
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
Expand All @@ -25,26 +26,34 @@ def discover_storage_system(self):
return ret_val

if out:
details = json.loads(out)
details = out.split()

ret_val['pkg_version'] = details['version'].split()[2]
ret_val['pkg_name'] = details['version'].split()[0]
ret_val['pkg_version'] = details[2]
ret_val['pkg_name'] = details[0]

# get the cluster_id details
cmd = subprocess.Popen(
"ceph -s -f json",
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
out, err = cmd.communicate()
if err:
LOG.error("Error getting cluster details")
return ret_val
if out:
details = json.loads(out)
ret_val['detected_cluster_id'] = details['fsid']
ret_val['cluster_attrs'] = {'fsid': details['fsid'],
'name': 'ceph'}
os_name = tendrl_ns.platform.os
cfg_file = ""
if os_name in ['CentOS Linux', 'Red Hat Enterprise Linux Server']:
cfg_file = '/etc/sysconfig/ceph'
#TODO(shtripat) handle the case of ubuntu

if cfg_file != "":
with open(cfg_file) as f:
for line in f:
if line.startswith("CLUSTER="):
cluster_name = line.split('\n')[0].split('=')[1]

if cluster_name:
raw_data = ini2json.ini_to_dict(
"/etc/ceph/%s.conf" % cluster_name
)
if "global" in raw_data:
ret_val['detected_cluster_id'] = raw_data['global']\
['fsid']
ret_val['cluster_attrs'] = {
'fsid': raw_data['global']['fsid'],
'name': 'ceph'
}

return ret_val
125 changes: 125 additions & 0 deletions tendrl/node_agent/ini2json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
from ConfigParser import ConfigParser
from ConfigParser import DEFAULTSECT
from ConfigParser import MissingSectionHeaderError
from ConfigParser import ParsingError


class StrictConfigParser(ConfigParser):

def _read(self, fp, fpname):
cursect = None # None, or a dictionary
optname = None
lineno = 0
e = None # None, or an exception
while True:
line = fp.readline()
if not line:
break
lineno = lineno + 1
# comment or blank line?
if line.strip() == '' or line[0] in '#;':
continue
if line.split(None, 1)[0].lower() == 'rem' and line[0] in "rR":
# no leading whitespace
continue
# continuation line?
if line[0].isspace() and cursect is not None and optname:
value = line.strip()
if value:
cursect[optname].append(value)
# a section header or option header?
else:
# is it a section header?
mo = self.SECTCRE.match(line)
if mo:
sectname = mo.group('header')
if sectname in self._sections:
raise ValueError('Duplicate section %r' % sectname)
elif sectname == DEFAULTSECT:
cursect = self._defaults
else:
cursect = self._dict()
cursect['__name__'] = sectname
self._sections[sectname] = cursect
# So sections can't start with a continuation line
optname = None
# no section header in the file?
elif cursect is None:
raise MissingSectionHeaderError(fpname, lineno, line)
# an option line?
else:
try:
mo = self._optcre.match(line) # 2.7
except AttributeError:
mo = self.OPTCRE.match(line) # 2.6
if mo:
optname, vi, optval = mo.group('option', 'vi', 'value')
optname = self.optionxform(optname.rstrip())
# This check is fine because the OPTCRE cannot
# match if it would set optval to None
if optval is not None:
if vi in ('=', ':') and ';' in optval:
# ';' is a comment delimiter only if it follows
# a spacing character
pos = optval.find(';')
if pos != -1 and optval[pos - 1].isspace():
optval = optval[:pos]
optval = optval.strip()
# allow empty values
if optval == '""':
optval = ''
cursect[optname] = [optval]
else:
# valueless option handling
cursect[optname] = optval
else:
# a non-fatal parsing error occurred. set up the
# exception but keep going. the exception will be
# raised at the end of the file and will contain a
# list of all bogus lines
if not e:
e = ParsingError(fpname)
e.append(lineno, repr(line))
# if any parsing errors occurred, raise an exception
if e:
raise e

# join the multi-line values collected while reading
all_sections = [self._defaults]
all_sections.extend(self._sections.values())
for options in all_sections:
for name, val in options.items():
if isinstance(val, list):
options[name] = '\n'.join(val)

def dget(self, section, option, default=None, type=str):
if not self.has_option(section, option):
return default
if type is str:
return self.get(section, option)
elif type is int:
return self.getint(section, option)
elif type is bool:
return self.getboolean(section, option)
else:
raise NotImplementedError()


def ini_to_dict(ini_file_path):
cfg = StrictConfigParser()
f = open(ini_file_path)
cfg.readfp(f)
f.close()

config = {}

for section in cfg.sections():
config[section] = {}
for name, value in cfg.items(section):
config[section][name] = [x.strip() for x in value.split() if x]
if len(config[section][name]) == 1:
config[section][name] = config[section][name][0]
elif len(config[section][name]) == 0:
config[section][name] = ''

return config
5 changes: 3 additions & 2 deletions tendrl/node_agent/manager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ def load_and_execute_platform_discovery_plugins(self):
if len(platform_details.keys()) > 0:
# update etcd
try:
tendrl_ns.node_agent.objects.Platform(
tendrl_ns.platform = tendrl_ns.node_agent.objects.Platform(
os=platform_details["Name"],
os_version=platform_details["OSVersion"],
kernel_version=platform_details["KernelVersion"],
).save()
)
tendrl_ns.platform.save()

except etcd.EtcdException as ex:
LOG.error(
Expand Down

0 comments on commit 6bfb841

Please sign in to comment.