import sys import os import xmlrpclib import copy import time class ShutdownerManager: def __init__(self): self.cron_file="/etc/cron.d/lliurex-shutdowner" self.thinclient_cron_file="/etc/cron.d/lliurex-shutdowner-thinclients" self.server_cron_file="/etc/cron.d/lliurex-shutdowner-server" self.variables_dir="/var/lib/n4d/variables-dir/" #def init def startup(self,options): check_client=objects["VariablesManager"].get_variable("REMOTE_VARIABLES_SERVER") if check_client!=None: varFile=os.path.join(self.variables_dir,"SHUTDOWNER") if os.path.exists(varFile): try: objects["VariablesManager"].variables.pop("SHUTDOWNER") os.remove(varFile) except: pass else: self.internal_variable=copy.deepcopy(objects["VariablesManager"].get_variable("SHUTDOWNER")) if self.internal_variable==None: try: self.initialize_variable() objects["VariablesManager"].add_variable("SHUTDOWNER",copy.deepcopy(self.internal_variable),"","Shutdowner internal variable","lliurex-shutdowner") except Exception as e: print e self.check_server_shutodown() #def startup def initialize_variable(self): self.internal_variable={} self.internal_variable["cron_enabled"]=False self.internal_variable["cron_content"]="" self.internal_variable["shutdown_signal"]=0.0 self.internal_variable["cron_values"]={} self.internal_variable["cron_values"]["hour"]=0 self.internal_variable["cron_values"]["minute"]=0 self.internal_variable["cron_values"]["weekdays"]=[True,True,True,True,True] self.internal_variable["cron_values"]["server_shutdown"]=False self.internal_variable["server_cron"]={} self.internal_variable["server_cron"]["custom_shutdown"]=False self.internal_variable["server_cron"]["cron_server_content"]="" self.internal_variable["server_cron"]["cron_server_values"]={} self.internal_variable["server_cron"]["cron_server_values"]["hour"]=0 self.internal_variable["server_cron"]["cron_server_values"]["minute"]=0 self.internal_variable["server_cron"]["cron_server_values"]["weekdays"]=[True,True,True,True,True] #def initialize_variable def check_variable(self,variable): try: if not type(variable)==dict: return False if not type(variable["cron_enabled"])==bool: return False if not type(variable["cron_content"])==str: return False if not type(variable["shutdown_signal"])==float: return False if not type(variable["cron_values"])==dict: return False if not type(variable["cron_values"]["hour"])==int: return False if not type(variable["cron_values"]["minute"])==int: return False if not type(variable["cron_values"]["server_shutdown"])==bool: return False if not type(variable["cron_values"]["weekdays"])==list: if len(variable["cron_values"]["weekdays"])!=5: return False if not type(variable["server_cron"])==dict: return False if not type(variable["server_cron"]["custom_shutdown"])==bool: return False if not type(variable["server_cron"]["cron_server_content"])==str: return False if not type(variable["server_cron"]["cron_server_values"])==dict: return False if not type(variable["server_cron"]["cron_server_values"]["hour"])==int: return False if not type(variable["server_cron"]["cron_server_values"]["minute"])==int: return False if not type(variable["server_cron"]["cron_server_values"]["weekdays"])==list: if len(variable["server_cron"]["cron_server_values"]["weekdays"])!=5: return False except: return False return True #def check_variable def manual_client_list_check(self): objects["VariablesManager"].manual_client_list_check() return True #def manual_client_list_check def is_cron_enabled(self): ret={} ret["status"]=self.internal_variable["cron_enabled"] ret["msg"]=self.internal_variable["cron_content"] if ret["status"]: ret["cli_support"]="enabled" else: ret["cli_support"]="disabled" return ret #def is_cron_enabled def is_server_shutdown_enabled(self): ret={} ret["status"]=self.internal_variable["cron_values"]["server_shutdown"] try: ret["msg"]=self.internal_variable["server_cron"]["cron_server_content"] except: ret["msg"]="" pass if ret["status"]: ret["cli_support"]="enabled" else: ret["cli_support"]="disabled" try: ret["custom_shutdown"]=self.internal_variable["server_cron"]["custom_shutdown"] except: ret["custom_shutdown"]=False return ret #def is_server_shutdown_enabled def update_shutdown_signal(self): self.internal_variable["shutdown_signal"]=time.time() return self.save_variable() #def update_shutdown_signal def save_variable(self,variable=None): if variable==None: variable=copy.deepcopy(self.internal_variable) else: if not self.check_variable(variable): return {"status":False,"msg":"Variable does not have the expected structure"} self.internal_variable=copy.deepcopy(variable) objects["VariablesManager"].set_variable("SHUTDOWNER",variable) self.check_server_shutodown() return {"status":True,"msg":""} #def save_variable def check_server_shutodown(self): if self.internal_variable["cron_enabled"] and self.internal_variable["cron_values"]["server_shutdown"]: if not self.internal_variable["server_cron"]["custom_shutdown"]: f=open(self.cron_file,"w") f.write(self.internal_variable["cron_content"]) f.close() if os.path.exists(self.server_cron_file): os.remove(self.server_cron_file) else: shutdown_cmd="/usr/sbin/shutdown-server-lliurex" cron_content="%s %s * * %s root %s >> /var/log/syslog\n" minute=self.internal_variable["server_cron"]["cron_server_values"]["minute"] hour=self.internal_variable["server_cron"]["cron_server_values"]["hour"] days="" count=1 for day in self.internal_variable["server_cron"]["cron_server_values"]["weekdays"]: if day: days+="%s,"%count count+=1 days=days.rstrip(",") server_cron=cron_content%(minute,hour,days,shutdown_cmd) f=open(self.server_cron_file,"w") f.write(server_cron) f.close() if os.path.exists(self.cron_file): os.remove(self.cron_file) else: if os.path.exists(self.cron_file): os.remove(self.cron_file) if os.path.exists(self.server_cron_file): os.remove(self.server_cron_file) self.build_thinclient_cron() return True #def check_server_shutdown def build_thinclient_cron(self): if self.internal_variable["cron_enabled"] and self.internal_variable["cron_values"]["server_shutdown"]: if not self.internal_variable["server_cron"]["custom_shutdown"]: # server will handle dialog calls its shutdown if os.path.exists(self.thinclient_cron_file): os.remove(self.thinclient_cron_file) return True if self.internal_variable["cron_enabled"]: # server will only handle thin clients dialogs shutdown_cmd="/usr/sbin/shutdown-lliurex" cron_content="%s %s * * %s root %s >> /var/log/syslog\n" minute=self.internal_variable["cron_values"]["minute"] hour=self.internal_variable["cron_values"]["hour"] days="" count=1 for day in self.internal_variable["cron_values"]["weekdays"]: if day: days+="%s,"%count count+=1 days=days.rstrip(",") thinclient_cron=cron_content%(minute,hour,days,shutdown_cmd) f=open(self.thinclient_cron_file,"w") f.write(thinclient_cron) f.close() return True else: # nothing to do if os.path.exists(self.thinclient_cron_file): os.remove(self.thinclient_cron_file) return True #def build_thinclient_cron