Commit fa09225f authored by Cédric Le Ninivin's avatar Cédric Le Ninivin

enhanced slaptes

	 - Check if all parameters are in section 'slapos' and 'slapformat'
	 - Detect duplicated lines in cron.d
	 - Added comments
parent f9e93aa6
...@@ -108,14 +108,11 @@ def get_slapos_conf_example(): ...@@ -108,14 +108,11 @@ def get_slapos_conf_example():
return path return path
def check_networkcache(config,logger,configuration_parser): def check_networkcache(config,logger,configuration_parser
,configuration_example_parser):
""" """
Check network cache download Check network cache download
""" """
slapos_cfg_example = get_slapos_conf_example()
configuration_example_parser = ConfigParser.RawConfigParser()
configuration_example_parser.read(slapos_cfg_example)
os.remove(slapos_cfg_example)
section = "networkcache" section = "networkcache"
configuration_example_dict = dict(configuration_example_parser.items(section)) configuration_example_dict = dict(configuration_example_parser.items(section))
configuration_dict = dict(configuration_parser.items(section)) configuration_dict = dict(configuration_parser.items(section))
...@@ -124,7 +121,7 @@ def check_networkcache(config,logger,configuration_parser): ...@@ -124,7 +121,7 @@ def check_networkcache(config,logger,configuration_parser):
if not configuration_dict[key] == configuration_example_dict[key] : if not configuration_dict[key] == configuration_example_dict[key] :
logger.warn("%s parameter in %s section is out of date" % (key, section)) logger.warn("%s parameter in %s section is out of date" % (key, section))
except KeyError: except KeyError:
logger.warn("No %s parameter in your file" % key) logger.warn("No %s parameter in %s section" % (key,section))
pass pass
if config.test_agent: if config.test_agent:
...@@ -176,10 +173,14 @@ def check_networkcache_upload(config,logger,configuration_dict): ...@@ -176,10 +173,14 @@ def check_networkcache_upload(config,logger,configuration_dict):
%(key,configuration_dict[key] %(key,configuration_dict[key]
,upload_parameters.data[key])) ,upload_parameters.data[key]))
except KeyError: except KeyError:
logger.critical ("No %s parameter in your file" % key) logger.critical("No %s parameter in networkcache section "
% (key))
pass pass
def get_computer_name(certificate): def get_computer_name(certificate):
"""
Extract computer_id for certificate
"""
certificate = open(certificate,"r") certificate = open(certificate,"r")
for line in certificate: for line in certificate:
i=0 i=0
...@@ -191,6 +192,9 @@ def get_computer_name(certificate): ...@@ -191,6 +192,9 @@ def get_computer_name(certificate):
return -1 return -1
def check_computer_id(logger,computer_id,cert_file): def check_computer_id(logger,computer_id,cert_file):
"""
Get computer id from cert_file and compare with computer_id
"""
comp_cert = get_computer_name(cert_file) comp_cert = get_computer_name(cert_file)
if comp_cert == "": if comp_cert == "":
logger.error("Certificate file indicated is corrupted (no computer id)") logger.error("Certificate file indicated is corrupted (no computer id)")
...@@ -214,22 +218,34 @@ def slapos_conf_check (config): ...@@ -214,22 +218,34 @@ def slapos_conf_check (config):
,'slapos.cfg') ,'slapos.cfg')
configuration_parser = ConfigParser.SafeConfigParser() configuration_parser = ConfigParser.SafeConfigParser()
configuration_parser.read(configuration_file_path) configuration_parser.read(configuration_file_path)
# Check if files for slapos and slapformat exists # Get example configuration file
slapos_cfg_example = get_slapos_conf_example()
configuration_example_parser = ConfigParser.RawConfigParser()
configuration_example_parser.read(slapos_cfg_example)
os.remove(slapos_cfg_example)
# Check if parameters for slapos and slapformat exists
for section in ("slapos","slapformat"): for section in ("slapos","slapformat"):
configuration_dict = dict(configuration_parser.items(section)) configuration_dict = dict(configuration_parser.items(section))
for key in configuration_dict: configuration_example_dict = dict(configuration_example_parser.items(section))
if key in ("key_file","cert_file","certificate_repository_path"): for key in configuration_example_dict:
if not key in configuration_dict:
logger.critical("No %s parameter in %s section "
% (key,section))
# check if necessary files exist
elif key in ("key_file","cert_file","certificate_repository_path"):
files = configuration_dict[key] files = configuration_dict[key]
if not os.path.exists(files) : if not os.path.exists(files) :
logger.critical ("%s file for %s parameters does not exist " logger.critical ("%s file for %s parameters does not exist "
% (files,key)) % (files,key))
else : else :
logger.info ("%s parameter:%s does exists" % (key,files)) logger.info ("%s parameter:%s does exists" % (key,files))
# check if computer id is the same in slapos.cfg and certificate
if key == "cert_file": if key == "cert_file":
check_computer_id(logger,configuration_dict["computer_id"], check_computer_id(logger,configuration_dict["computer_id"],
configuration_dict["cert_file"]) configuration_dict["cert_file"])
# Check networkcache # Check networkcache
check_networkcache(config,logger,configuration_parser) check_networkcache(config,logger,configuration_parser,
configuration_example_parser)
class CronFile: class CronFile:
...@@ -252,12 +268,22 @@ class CronFile: ...@@ -252,12 +268,22 @@ class CronFile:
""" Parse cron line and give value to attributes """ """ Parse cron line and give value to attributes """
line = cron_line.split() line = cron_line.split()
command = line[6] command = line[6]
if "slapformat" in command : self.slapformat = self.compare(self.slapformat_base.split() , line) if "slapformat" in command :
if "slapgrid-ur" in command : self.slapgrid_ur = self.compare(self.slapgrid_ur_base.split() , line) self.slapformat = self.compare(self.slapformat,
if "slapgrid-cp" in command : self.slapgrid_cp = self.compare(self.slapgrid_cp_base.split() , line) self.slapformat_base.split() , line)
if "slapgrid-sr" in command : self.slapgrid_sr = self.compare(self.slapgrid_sr_base.split() , line) if "slapgrid-ur" in command :
self.slapgrid_ur = self.compare(self.slapgrid_ur,
self.slapgrid_ur_base.split() , line)
if "slapgrid-cp" in command :
self.slapgrid_cp = self.compare(self.slapgrid_cp,
self.slapgrid_cp_base.split() , line)
if "slapgrid-sr" in command :
self.slapgrid_sr = self.compare(self.slapgrid_sr,
self.slapgrid_sr_base.split() , line)
def compare(self,reference,cron_line): def compare(self,state,reference,cron_line):
if not state == -1 :
return 2
for i in range(0,6): for i in range(0,6):
if not reference[i] == cron_line[i] : if not reference[i] == cron_line[i] :
return 0 return 0
...@@ -276,6 +302,9 @@ class CronFile: ...@@ -276,6 +302,9 @@ class CronFile:
logger.error("No line found for %s command" % key) logger.error("No line found for %s command" % key)
elif elements[key] == 1 : elif elements[key] == 1 :
logger.info("Line for %s command is good" % key) logger.info("Line for %s command is good" % key)
elif elements[key] == 2 :
logger.error("You have a duplicated line for %s command" % key)
def cron_check (config): def cron_check (config):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment