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

Your cron file is to be perfect from now on

parent c111ebbd
...@@ -208,45 +208,50 @@ def slapos_conf_check (config): ...@@ -208,45 +208,50 @@ def slapos_conf_check (config):
check_networkcache(config,logger,configuration_parser) check_networkcache(config,logger,configuration_parser)
class CronLine: class CronFile:
""" """
Class to analyse each cron line individualy Class to analyse each cron line individualy
""" """
def __init__(self): def __init__(self):
""" Init all value to None""" """ Init all values"""
self.command = None self.slapformat = -1
self.pidfile = None self.slapgrid_cp = -1
self.logfile = None self.slapgrid_ur = -1
self.config = None self.slapgrid_sr = -1
# cron file from slapos documentation
self.slapgrid_sr_base = """*/5 * * * * root /opt/slapos/bin/slapgrid-sr --logfile=/opt/slapos/slapgrid-sr.log --pidfile=/opt/slapos/slapgrid-sr.pid /etc/opt/slapos/slapos.cfg >> /opt/slapos/slapgrid-sr.log 2>&1"""
self.slapgrid_cp_base = """*/5 * * * * root /opt/slapos/bin/slapgrid-cp --logfile=/opt/slapos/slapgrid-cp.log --pidfile=/opt/slapos/slapgrid-cp.pid /etc/opt/slapos/slapos.cfg >> /opt/slapos/slapgrid-cp.log 2>&1"""
self.slapgrid_ur_base = """0 0 * * * root /opt/slapos/bin/slapgrid-ur --logfile=/opt/slapos/slapgrid-ur.log --pidfile=/opt/slapos/slapgrid-ur.pid /etc/opt/slapos/slapos.cfg >> /opt/slapos/slapgrid-ur.log 2>&1"""
self.slapformat_base = """0 0 * * * root /opt/slapos/bin/slapformat --log_file=/opt/slapos/slapformat.log -c /etc/opt/slapos/slapos.cfg >> /opt/slapos/slapformat.log 2>&1"""
def parse(self,cron_line): def parse(self,cron_line):
""" Parse cron line and give value to attributes """ """ Parse cron line and give value to attributes """
line = cron_line.split() line = cron_line.split()
self.command = line[6] command = line[6]
for word in line: if "slapformat" in command : self.slapformat = self.compare(self.slapformat_base.split() , line)
if "slapos.cfg" in word : if "slapgrid-ur" in command : self.slapgrid_ur = self.compare(self.slapgrid_ur_base.split() , line)
self.config = word if "slapgrid-cp" in command : self.slapgrid_cp = self.compare(self.slapgrid_cp_base.split() , line)
if "--pidfile" in word : if "slapgrid-sr" in command : self.slapgrid_sr = self.compare(self.slapgrid_sr_base.split() , line)
self.pidfile = word[word.find("=")+1:]
if "--log_file" in word and "format" in self.command: def compare(self,reference,cron_line):
self.logfile = word[word.find("=")+1:] for i in range(0,6):
if "--logfile" in word and not "format" in self.command: if not reference[i] == cron_line[i] :
self.logfile = word[word.find("=")+1:] return 0
ref = len(reference[6:])
def check(self,config,logger): if not len(set(reference[6:]) & set(cron_line[6:])) == ref :
""" Check if all attributes are correctly set""" return 0
if not os.path.exists(self.config): else: return 1
logger.critical("For %s command: slapos.cfg is %s should be in %s"
% (self.command,self.config,config.slapos_configuration)) def check(self,logger):
elif not os.path.samefile(self.config,os.path.join(config.slapos_configuration,'slapos.cfg')): elements = {"slapformat":self.slapformat,"slapgrid-ur":self.slapgrid_ur,
logger.critical("For %s command: slapos.cfg is %s should be in %s" "slapgrid-sr":self.slapgrid_sr,"slapgrid-cp":self.slapgrid_cp}
% (self.command,self.config,config.slapos_configuration)) for key in elements :
if self.pidfile == None: if elements[key] == 0 :
logger.warning("For %s command: No pidfile" logger.error("Your line for %s command does not seem right" % key)
% (self.command)) elif elements[key] == -1 :
if self.logfile == None: logger.error("No line found for %s command" % key)
logger.warning("For %s command: No logfile" elif elements[key] == 1 :
% (self.command)) logger.info("Line for %s command is good" % key)
def cron_check (config): def cron_check (config):
...@@ -257,12 +262,12 @@ def cron_check (config): ...@@ -257,12 +262,12 @@ def cron_check (config):
logger = logging.getLogger('Checking slapos-node cron file:') logger = logging.getLogger('Checking slapos-node cron file:')
logger.setLevel(logging.INFO) logger.setLevel(logging.INFO)
logger.addHandler(ch) logger.addHandler(ch)
cron = open(config.slapos_cron,"r") cron = open(config.slapos_cron,"r")
cron_file = CronFile()
for line in cron : for line in cron :
if "/opt/slapos" in line and not line[0]=="#": if "/opt/slapos" in line and not line[0]=="#":
cron_line = CronLine() cron_file.parse(line)
cron_line.parse(line) cron_file.check(logger)
cron_line.check(config,logger)
def slapos_global_check (config): def slapos_global_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