def load_exports(self): #Get template template_server = self.tpl_env.get_template("server") list_variables = {} #Inicialize INTERNAL_DOMAIN #Old n4d: list_variables['INTERNAL_DOMAIN'] = objects['VariablesManager'].get_variable('INTERNAL_DOMAIN') list_variables['INTERNAL_DOMAIN']=self.core.get_variable('INTERNAL_DOMAIN').get('return',None) #If INT_DOMAIN is not defined calculate it with args values if list_variables['INTERNAL_DOMAIN'] == None: #Old n4d: return {'status':False,'msg':'Variable INTERNAL_DOMAIN not defined'} return n4d.responses.build_failed_call_response('','Variable INTERNAL_DOMAIN not defined') #Inicialize INTERNAL_DOMAIN #Old n4d: list_variables['HOSTNAME'] = objects['VariablesManager'].get_variable('HOSTNAME') list_variables['HOSTNAME']=self.core.get_variable('HOSTNAME').get('return',None) #If INT_DOMAIN is not defined calculate it with args values if list_variables['HOSTNAME'] == None: #Old n4d: return {'status':False,'msg':'Variable HOSTNAME not defined'} return n4d.responses.build_failed_call_response('','Variable HOSTNAME not defined') ########################### #Setting VARS ########################### #Set HTTP_PATH #Old n4d: list_variables['HTTP_PATH'] = objects['VariablesManager'].get_variable('HTTP_PATH') list_variables['HTTP_PATH']=self.core.get_variable('HTTP_PATH').get('return',None) #If variable PROXY_ENABLED is not defined calculate it with args values if list_variables['HTTP_PATH'] == None: #Old n4d: status,list_variables['HTTP_PATH'] = objects['VariablesManager'].init_variable('HTTP_PATH',{'PATH':'/var/www/'}) ret=self.core.set_variable('HTTP_PATH','/var/www/') list_variables['HTTP_PATH']=ret['return'] if ret['status']==0: status=True else: status=False #Encode vars to UTF-8 #Old n4d: string_template = template_server.render(list_variables).encode('UTF-8') string_template = template_server.render(list_variables) #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.conf',True,'root','root','0644',False ) #Restart service subprocess.Popen(['a2ensite','server.conf'],stdout=subprocess.PIPE).communicate() subprocess.Popen(['/etc/init.d/apache2','reload'],stdout=subprocess.PIPE).communicate() #Old n4d: return {'status':True,'msg':'Exports written'} return n4d.responses.build_successful_call_response('','Exports written')
def remove_alias(self, alias): template_cname = self.tpl_env.get_template("cname-server") list_variables = {} #get INTERNAL_DOMAIN #list_variables['INTERNAL_DOMAIN'] = objects['VariablesManager'].get_variable('INTERNAL_DOMAIN') list_variables['INTERNAL_DOMAIN'] = self.n4dCore.get_variable( 'INTERNAL_DOMAIN').get('return', None) #If INT_DOMAIN is not defined return an error if list_variables['INTERNAL_DOMAIN'] == None: return n4d.responses.build_failed_call_response( INTERNAL_DOMAIN_VAR_ERROR) #get HOSTNAME #list_variables['HOSTNAME'] = objects['VariablesManager'].get_variable('HOSTNAME') list_variables['HOSTNAME'] = self.n4dCore.get_variable('HOSTNAME').get( 'return', None) #If INT_DOMAIN is not defined return an error if list_variables['HOSTNAME'] == None: return n4d.responses.build_failed_call_response(HOSTNAME_VAR_ERROR) #Add alias to SRV_ALIAS #Obtains actual SRV_ALIAS variable #list_variables['SRV_ALIAS'] = objects['VariablesManager'].get_variable('SRV_ALIAS') list_variables['SRV_ALIAS'] = self.n4dCore.get_variable( 'SRV_ALIAS').get('return', []) #Add new alias if list_variables['SRV_ALIAS'] == None: return n4d.responses.build_failed_call_response(SRV_ALIAS_ERROR) if alias in list_variables['SRV_ALIAS']: list_variables['SRV_ALIAS'].remove(alias) else: return n4d.responses.build_failed_call_response(ALIAS_ERROR) #Save new values #status,list_variables['SRV_ALIAS'] = objects['VariablesManager'].init_variable('SRV_ALIAS',{'ALIAS':list_variables['SRV_ALIAS']}) self.n4dCore.set_variable('SRV_ALIAS', list_variables['SRV_ALIAS']) list_variables['SRV_ALIAS'] = self.n4dCore.get_variable( 'SRV_ALIAS').get('return', []) #return {'status':True,'msg':'Set server name succesfully'} #Encode vars to UTF-8 string_template = template_cname.render(list_variables) #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 #shutil.move(tmpfilepath,'/etc/dnsmasq.conf') n4d_mv(tmpfilepath, '/var/lib/dnsmasq/config/cname-server', True, 'root', 'root', '0644', False) self.reboot_dhcpd() return n4d.responses.build_successful_call_response()
def configure_smb(self): template = self.tpl_env.get_template('smb.conf') self.core.set_variable("DEADTIME",15) self.core.set_variable("SAMBA_DOMAIN_NAME","lliurex") self.core.set_variable("NAME_SERVER_SAMBA","%h") environment_vars = self.core.get_variable_list(['SAMBA_DOMAIN_NAME','NAME_SERVER_SAMBA','DEADTIME','LDAP_BASE_DN'])["return"] string_template = template.render(environment_vars).encode('utf-8') tmp,filename=tempfile.mkstemp() f = open(filename,'w') f.writelines(string_template.decode("utf-8")) f.close() n4d_mv(filename,'/etc/samba/smb.conf') return n4d.responses.build_successful_call_response(ret_msg="smb.conf configured")
def configure_mount_on_boot(self, source, target, options=None): if options == None: options = self.default_mount_options if not os.path.isdir(target): try: os.makedirs(target) except: pass template_cname = self.tpl_env.get_template("mount.skel") list_variables = {} list_variables["SRC"] = source list_variables["DEST"] = target list_variables["OPTIONS"] = options string_template = template_cname.render( list_variables) #.encode('UTF-8') fd, tmpfilepath = tempfile.mkstemp() new_export_file = open(tmpfilepath, 'w') new_export_file.write(string_template) new_export_file.close() os.close(fd) p = subprocess.Popen( ["systemd-escape", target.lstrip("/")], stdout=subprocess.PIPE) file_name = p.communicate()[0].decode('utf-8').strip("\n") + ".mount" file_dest = "/lib/systemd/system/" + file_name n4d_mv(tmpfilepath, file_dest, True, 'root', 'root', '0644', False) os.system("systemctl daemon-reload") o = subprocess.Popen( ["systemctl", "enable", file_name], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode('utf-8') o2 = subprocess.Popen( ["systemctl", "start", file_name], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode('utf-8') ret = (o, o2) #Old n4d: return {"status":True,"msg":ret} return n4d.responses.build_successful_call_response(ret)
def set_dns_external(self, dnsexternal): list_variables = {} template_extradns = self.tpl_env.get_template("extra-dns") #list_variables['DNS_EXTERNAL'] = objects['VariablesManager'].get_variable('DNS_EXTERNAL') #list_variables['DNS_EXTERNAL'] = self.n4dCore.get_variable('DNS_EXTERNAL') #status,list_variables['DNS_EXTERNAL'] = objects['VariablesManager'].init_variable('DNS_EXTERNAL',{'DNS':dnsexternal}) self.n4dCore.set_variable('DNS_EXTERNAL', dnsexternal) list_variables['DNS_EXTERNAL'] = self.n4dCore.get_variable( 'DNS_EXTERNAL').get('return', None) with tempfile.NamedTemporaryFile('w', delete=False) as new_export_file: #new_export_file.write( template_extradns.render(list_variables).encode('UTF-8') ) new_export_file.write("%s" % template_extradns.render(list_variables)) tmpfilepath = new_export_file.name n4d_mv(tmpfilepath, self.extradnspath, True, 'root', 'root', '0644', False) return n4d.responses.build_successful_call_response()
def load_exports(self, restart_dnsmasq=False): #Get template template_cname = self.tpl_env.get_template("cname") list_variables = {} ########################### #Getting VARS ########################### try: list_variables.setdefault( 'INTERNAL_DOMAIN', self.n4d.get_variable('INTERNAL_DOMAIN').get('return', None)) list_variables.setdefault( 'HOSTNAME', self.n4d.get_variable('HOSTNAME').get('return', None)) except Exception as e: return n4d.responses.build_authentication_failed_response() if not list_variables.get('INTERNAL_DOMAIN', None): return n4d.responses.build_failed_call_response( ret_msg='Variable INTERNAL_DOMAIN not defined') if not list_variables.get('HOSTNAME', None): return n4d.responses.build_failed_call_response( ret_msg='Variable HOSTNAME not defined') #Encode vars to UTF-8 string_template = template_cname.render(list_variables) #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, '/var/lib/dnsmasq/config/cname-preseed', True, 'root', 'root', '0644', True) if restart_dnsmasq: subprocess.Popen(['systemctl', 'restart', 'dnsmasq'], stdout=subprocess.PIPE).communicate() return n4d.responses.build_successful_call_response('Exports written')
def load_exports(self): #Get template template_list = [] template_list.append({ 'template': self.tpl_env.get_template("dnsmasq.conf"), 'path': '/etc/dnsmasq.conf' }) template_list.append({ 'template': self.tpl_env.get_template("cname-server"), 'path': '/var/lib/dnsmasq/config/cname-server' }) template_list.append({ 'template': self.tpl_env.get_template("server"), 'path': '/var/lib/dnsmasq/hosts/server' }) template_list.append({ 'template': self.tpl_env.get_template("dhclient.conf"), 'path': '/etc/dhcp/dhclient.conf' }) template_cname = self.tpl_env.get_template("cname-server") template_server = self.tpl_env.get_template("server") template_dhclientconf = self.tpl_env.get_template("dhclient.conf") query_variables = [ 'INTERNAL_INTERFACE', 'INTERNAL_NETWORK', 'INTERNAL_MASK', 'INTERNAL_DOMAIN', 'SRV_IP', 'HOSTNAME', 'INTERFACE_REPLICATION' ] non_check_variables = ['INTERFACE_REPLICATION'] #list_variables = objects['VariablesManager'].get_variable_list(query_variables) list_variables = self.n4dCore.get_variable_list( query_variables)['return'] # Check exists variables for variable in query_variables: if variable in non_check_variables: continue if list_variables.get( variable, False ) == False: #variable in list_variables and list_variables[variable] is not None ): return n4d.responses.build_failed_call_response( UNDEFINED_VAR_ERROR) if list_variables.get('INTERFACE_REPLICATION', None): result = self.n4dCore.get_plugin( 'NetworkManager').get_replication_network() if result['status'] == 0: list_variables['REPLICATION_NETWORK'] = result['return'] dranges = dhcpranges.DhcpRanges() query_variables = { #'DHCP_ENABLE': {'ENABLE':'True'}, #'DHCP_LEASE_TIME': {'LEASE_TIME':12}, #'DHCP_DENY_UNKNOWN_CLIENTS': {'DENY_UNKNOWN':'no'}, #'DHCP_HOST_MAX': {'HOST_MAX':80}, #'DHCP_FIRST_IP': {'NETWORK':list_variables['INTERNAL_NETWORK'],'MASK':list_variables['INTERNAL_MASK']}, #'DHCP_LAST_IP': {'NETWORK':list_variables['INTERNAL_NETWORK'],'MASK':list_variables['INTERNAL_MASK']}, #'DNS_HOSTNAME_PREFIX': {'PREFIX':'llx-'}, #'DNS_UNREG_HOSTNAME_PREFIX':{'PREFIX':'host-'} 'DHCP_ENABLE': 'True', 'DHCP_LEASE_TIME': 12, 'DHCP_DENY_UNKNOWN_CLIENTS': 'no', 'DHCP_HOST_MAX': 80, 'DHCP_FIRST_IP': dranges.init_dhcp_first({ "NETWORK": list_variables["INTERNAL_NETWORK"], "MASK": list_variables["INTERNAL_MASK"] }), 'DHCP_LAST_IP': dranges.init_dhcp_last({ "NETWORK": list_variables["INTERNAL_NETWORK"], "MASK": list_variables["INTERNAL_MASK"] }), 'DNS_HOSTNAME_PREFIX': 'llx-', 'DNS_UNREG_HOSTNAME_PREFIX': 'host-', "SRV_ALIAS": [ "cups", "www", "ntp", "share", "srv", "servidor", "jclic-aula", "lliurexlab", "error", "ipxboot", "admin-center" ] } for variable in query_variables: self.n4dCore.set_variable(variable, query_variables[variable]) list_variables.update(query_variables) for template_info in template_list: with tempfile.NamedTemporaryFile('w', delete=False) as new_export_file: #new_export_file.write( template_info['template'].render(list_variables).encode('UTF-8') ) new_export_file.write( "%s" % template_info['template'].render(list_variables)) tmpfilepath = new_export_file.name n4d_mv(tmpfilepath, template_info['path'], True, 'root', 'root', '0644', False) return n4d.responses.build_successful_call_response()