sexy, sexy, sexy...
This commit is contained in:
parent
89b637e851
commit
2dbddaaf58
2 changed files with 41 additions and 27 deletions
|
|
@ -1,19 +1,25 @@
|
||||||
class LayouException(Exception):
|
class LayoutException(Exception):
|
||||||
def __init__(self, msg):
|
def __init__(self, msg):
|
||||||
self.msg = msg
|
self.msg = msg
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return repr(self.msg)
|
return repr(self.msg)
|
||||||
|
|
||||||
|
|
||||||
class Layout:
|
class Layout:
|
||||||
def __init__(self, name, enabled = False , hostgroups=None):
|
def __init__(self, name, enabled=False, hostgroups=None, members=None):
|
||||||
self._name = name
|
self._name = name
|
||||||
self._enabled = enabled
|
self._enabled = enabled
|
||||||
|
|
||||||
if hostgroups is None or len(hostgroups) <= 0:
|
if hostgroups is None or len(hostgroups) <= 0:
|
||||||
raise Exception("Layout: " + name + " without hostgroups is useless")
|
raise LayoutException("Layout: " + self._name + " without hostgroups is useless")
|
||||||
else:
|
else:
|
||||||
self._hostgroups = hostgroups
|
self._hostgroups = hostgroups
|
||||||
|
|
||||||
|
if members is None or len(members) <= 0:
|
||||||
|
raise LayoutException("Layout: " + self._name + " without members is useless")
|
||||||
|
else:
|
||||||
|
self._members = members
|
||||||
|
|
||||||
def get_name(self):
|
def get_name(self):
|
||||||
return self._name
|
return self._name
|
||||||
|
|
@ -24,6 +30,9 @@ class Layout:
|
||||||
def get_hostgroups(self):
|
def get_hostgroups(self):
|
||||||
return self._hostgroups
|
return self._hostgroups
|
||||||
|
|
||||||
|
def get_members(self):
|
||||||
|
return self._members
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
ret = "Layout: 'Name:" + str(self.name) + "', 'Enabled: " + str(self.enabled) + " "
|
ret = "Layout: 'Name:" + str(self.name) + "', 'Enabled: " + str(self.enabled) + " "
|
||||||
for group in self.hostgroups:
|
for group in self.hostgroups:
|
||||||
|
|
|
||||||
|
|
@ -4,11 +4,12 @@ Created on Jun 9, 2013
|
||||||
@author: Rafael Timmerberg(raffn1+linspector@gmail.com)
|
@author: Rafael Timmerberg(raffn1+linspector@gmail.com)
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import os, os.path as path
|
from os.path import isfile
|
||||||
import json
|
import json
|
||||||
from layouts import Layout
|
from layouts import Layout
|
||||||
from hostgroups import HostGroup
|
from hostgroups import HostGroup
|
||||||
|
|
||||||
|
|
||||||
class ConfigurationException(Exception):
|
class ConfigurationException(Exception):
|
||||||
def __init__(self, msg, log):
|
def __init__(self, msg, log):
|
||||||
log.e(msg)
|
log.e(msg)
|
||||||
|
|
@ -25,7 +26,6 @@ KEY_PERIODS = "periods"
|
||||||
KEY_CORE = "core"
|
KEY_CORE = "core"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class ConfigParser:
|
class ConfigParser:
|
||||||
def __init__(self, log):
|
def __init__(self, log):
|
||||||
'''
|
'''
|
||||||
|
|
@ -38,8 +38,7 @@ class ConfigParser:
|
||||||
self.hostgroups = {}
|
self.hostgroups = {}
|
||||||
self.members = {}
|
self.members = {}
|
||||||
self.periods = {}
|
self.periods = {}
|
||||||
|
|
||||||
|
|
||||||
def _read_json_config(self, configFilename):
|
def _read_json_config(self, configFilename):
|
||||||
'''
|
'''
|
||||||
reads the config File and returns a dictionary, while lowering the first keys
|
reads the config File and returns a dictionary, while lowering the first keys
|
||||||
|
|
@ -47,7 +46,7 @@ class ConfigParser:
|
||||||
params:
|
params:
|
||||||
configFilename: the path under which the configuration file should be found
|
configFilename: the path under which the configuration file should be found
|
||||||
'''
|
'''
|
||||||
if not path.isfile(configFilename):
|
if not isfile(configFilename):
|
||||||
msg = "config file not found at " + str(configFilename)
|
msg = "config file not found at " + str(configFilename)
|
||||||
raise ConfigurationException(msg, self.log)
|
raise ConfigurationException(msg, self.log)
|
||||||
|
|
||||||
|
|
@ -59,8 +58,6 @@ class ConfigParser:
|
||||||
self.log.i("reading Config: " + configFilename)
|
self.log.i("reading Config: " + configFilename)
|
||||||
return json.loads(config)
|
return json.loads(config)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def _get_as_list(self, configValue):
|
def _get_as_list(self, configValue):
|
||||||
'''
|
'''
|
||||||
In some cases the config permits to define a list or a single value.
|
In some cases the config permits to define a list or a single value.
|
||||||
|
|
@ -68,7 +65,6 @@ class ConfigParser:
|
||||||
returns the value as list
|
returns the value as list
|
||||||
'''
|
'''
|
||||||
return configValue if isinstance(configValue, list) else [configValue]
|
return configValue if isinstance(configValue, list) else [configValue]
|
||||||
|
|
||||||
|
|
||||||
def create_layouts_from_json(self, jsonLayouts):
|
def create_layouts_from_json(self, jsonLayouts):
|
||||||
layouts = []
|
layouts = []
|
||||||
|
|
@ -76,11 +72,10 @@ class ConfigParser:
|
||||||
try:
|
try:
|
||||||
layout = Layout(lName, **lValues)
|
layout = Layout(lName, **lValues)
|
||||||
layouts.append(layout)
|
layouts.append(layout)
|
||||||
except Exception:
|
except ConfigurationException:
|
||||||
self.log.w("ignoring Layout " + lName + "! reason:")
|
self.log.w("ignoring Layout " + lName + "! reason:")
|
||||||
self.log.w(str(Exception))
|
self.log.w(str(Exception))
|
||||||
return layouts
|
return layouts
|
||||||
|
|
||||||
|
|
||||||
def create_hostgroups_from_json(self, jsonHostGroups):
|
def create_hostgroups_from_json(self, jsonHostGroups):
|
||||||
'''
|
'''
|
||||||
|
|
@ -91,14 +86,25 @@ class ConfigParser:
|
||||||
try:
|
try:
|
||||||
hostgroup = HostGroup(hgName, **hgValues)
|
hostgroup = HostGroup(hgName, **hgValues)
|
||||||
hostgroups.append(hostgroup)
|
hostgroups.append(hostgroup)
|
||||||
except Exception:
|
except ConfigurationException:
|
||||||
self.log.w("ignoring hostgroup: " + hgName + "!")
|
self.log.w("ignoring hostgroup: " + hgName + "!")
|
||||||
self.log.w("reason: " + str(Exception))
|
self.log.w("reason: " + str(Exception))
|
||||||
return hostgroups
|
return hostgroups
|
||||||
|
|
||||||
|
def create_members_from_json(self, jsonMembers):
|
||||||
|
'''
|
||||||
|
creates Members from the jsonConfig
|
||||||
|
'''
|
||||||
|
members = []
|
||||||
|
for memberName, memberValues in jsonMembers.items():
|
||||||
|
try:
|
||||||
|
member = memberName(memberName, **memberValues)
|
||||||
|
members.append(member)
|
||||||
|
except ConfigurationException:
|
||||||
|
self.log.w("ignoring member: " + memberName + "!")
|
||||||
|
self.log.w("reason: " + str(Exception))
|
||||||
|
return members
|
||||||
|
|
||||||
def parse_config(self, configFilename):
|
def parse_config(self, configFilename):
|
||||||
'''
|
'''
|
||||||
parses the json configuration and returns a list of layouts,
|
parses the json configuration and returns a list of layouts,
|
||||||
|
|
@ -135,19 +141,18 @@ class ConfigParser:
|
||||||
self.hostgroups = self.create_hostgroups_from_json(jsonHostgroups)
|
self.hostgroups = self.create_hostgroups_from_json(jsonHostgroups)
|
||||||
|
|
||||||
memberNames = set()
|
memberNames = set()
|
||||||
for hostgroup in self.hostgroups:
|
for layout in layouts:
|
||||||
for memberName in layout.get_mebers():
|
for memberName in layout.get_members():
|
||||||
memberNames.add(memberName)
|
memberNames.add(memberName)
|
||||||
|
|
||||||
|
jsonMembers = {}
|
||||||
jsonMembernames = {}
|
for memberName in memberNames:
|
||||||
for memberName in hostgroupNames:
|
|
||||||
if not memberName in self.jsonDict[KEY_MEMBERS]:
|
if not memberName in self.jsonDict[KEY_MEMBERS]:
|
||||||
self.log.w("Member " + memberName + " not found!")
|
self.log.w("Member " + memberName + " not found!")
|
||||||
for hostgroup in self.hostgroups:
|
for hostgroup in self.hostgroups:
|
||||||
if memberName in hostgroup.members:
|
if memberName in hostgroup.members:
|
||||||
del hostgroup.members[hostgroup.members.index(hgName)]
|
del hostgroup.members[hostgroup.members.index(memberName)]
|
||||||
jsonHostgroups[hgName] = self.jsonDict[KEY_HOSTGROUPS][hgName]
|
jsonMembers[memberName] = self.jsonDict[KEY_HOSTGROUPS][memberName]
|
||||||
|
|
||||||
self.hostgroups = self.create_hostgroups_from_json(jsonHostgroups)
|
self.members = self.create_members_from_json(jsonMembers)
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue