# npackage example https://svn.lliurex.net/pandora/n4d-ldap/trunk # jinja2 http://jinja.pocoo.org/docs/templates from jinja2 import Environment from jinja2.loaders import FileSystemLoader from jinja2 import Template import tempfile import shutil import os import subprocess import tarfile import time import glob class ApacheManager: def __init__(self): #Load template file self.tpl_env = Environment(loader=FileSystemLoader('/usr/share/n4d/templates/apache')) self.backup_files=[] self.backup_dirs=["/etc/apache2/","/var/www/","/net/server-sync/easy-sites"] #def init def startup(self,options): # executed when launching n4d pass #def startup def apt(self): # executed after apt operations pass #def apt # service test and backup functions # def test(self): pass #def makedir def makedir(self,dir=None): if not os.path.isdir(dir): os.makedirs(dir) return [True] #def timestamp backup def get_time(self): return get_backup_name("ApacheManager") #def backup def backup(self,dir="/backup"): try: self.makedir(dir) file_path=dir+"/"+self.get_time() tar=tarfile.open(file_path,"w:gz") for f in self.backup_files: if os.path.exists(f): tar.add(f) for d in self.backup_dirs: if os.path.exists(d): tar.add(d) self.list_easy_sites = glob.glob('/var/www/srv/'+'easy-'+'*') + glob.glob("/var/www/srv/links/easy-*") for j in self.list_easy_sites: if os.path.exists(j): tar.add(j) dir="/net/server-sync/easy-sites" if os.path.exists(dir): cmd="getfacl -R %s > /tmp/easy-sites.acl"%dir os.system(cmd) tar.add("/tmp/easy-sites.acl",arcname="easy-sites.acl") os.remove("/tmp/easy-sites.acl") tar.close() return [True,file_path] except Exception as e: return [False,str(e)] #def restore backup def restore(self,file_path=None): if file_path==None: dir="/backup" for f in sorted(os.listdir(dir),reverse=True): if "ApacheManager" in f: file_path=dir+"/"+f break try: if os.path.exists(file_path): tmp_dir=tempfile.mkdtemp() tar=tarfile.open(file_path) tar.extractall(tmp_dir) tar.close() for f in self.backup_files: tmp_path=tmp_dir+f if os.path.exists(tmp_path): shutil.copy(tmp_path,f) for d in self.backup_dirs: tmp_path=tmp_dir+d if os.path.exists(tmp_path): self.makedir(d) cmd="cp -r " + tmp_path +"/* " + d os.system(cmd) if os.path.exists("/net/server-sync/easy-sites") and os.path.exists(tmp_dir+"/easy-sites.acl"): os.system("setfacl -R --restore=%s/easy-sites.acl"%tmp_dir) os.system("service apache2 restart") return [True,""] else: return [False,"Backup file not found"] except Exception as e: return [False,str(e)] #def restore def load_exports(self): #Get template template = self.tpl_env.get_template("default") template_server = self.tpl_env.get_template("server") list_variables = {} #Inicialize INTERNAL_DOMAIN list_variables['INTERNAL_DOMAIN'] = objects['VariablesManager'].get_variable('INTERNAL_DOMAIN') #If INT_DOMAIN is not defined calculate it with args values if list_variables['INTERNAL_DOMAIN'] == None: return {'status':False,'msg':'Variable INTERNAL_DOMAIN not defined'} #Inicialize INTERNAL_DOMAIN list_variables['HOSTNAME'] = objects['VariablesManager'].get_variable('HOSTNAME') #If INT_DOMAIN is not defined calculate it with args values if list_variables['HOSTNAME'] == None: return {'status':False,'msg':'Variable HOSTNAME not defined'} ########################### #Setting VARS ########################### #Set HTTP_PATH list_variables['HTTP_PATH'] = objects['VariablesManager'].get_variable('HTTP_PATH') #If variable PROXY_ENABLED is not defined calculate it with args values if list_variables['HTTP_PATH'] == None: status,list_variables['HTTP_PATH'] = objects['VariablesManager'].init_variable('HTTP_PATH',{'PATH':'/var/www/'}) #Encode vars to UTF-8 string_template = template.render(list_variables).encode('UTF-8') #Open template file fd, tmpfilepath = tempfile.mkstemp() new_export_file = open(tmpfilepath,'w') new_export_file.write(string_template) new_export_file.close() os.close(fd) #Write template values n4d_mv(tmpfilepath,'/etc/apache2/sites-available/default.lliurex',True,'root','root','0644',False ) #Encode vars to UTF-8 string_template = template_server.render(list_variables).encode('UTF-8') #Open template file fd, tmpfilepath = tempfile.mkstemp() new_export_file = open(tmpfilepath,'w') new_export_file.write(string_template) new_export_file.close() os.close(fd) #Write template values n4d_mv(tmpfilepath,'/etc/apache2/sites-available/server',True,'root','root','0644',False ) #Restart service subprocess.Popen(['a2ensite','server'],stdout=subprocess.PIPE).communicate() subprocess.Popen(['/etc/init.d/apache2','reload'],stdout=subprocess.PIPE).communicate() return {'status':True,'msg':'Exports written'} #def load_exports def reboot_apache(self): #Restart apache service subprocess.Popen(['/etc/init.d/apache2','restart'],stdout=subprocess.PIPE).communicate() return {'status':True,'msg':'apache2 rebooted'} #def reboot_squid # ######################### # #class ApacheManager