def edit_host(self, **kw): ''' Process the edit_host request ''' # Confirm user authentication self.check_session() addr_type = self.address_types[kw['address_type']] is_dynamic = addr_type.has_key('pool') and addr_type['pool'] is not None and not addr_type['ranges'] changed_to_static = kw.has_key('did_change_ip') or (kw.has_key('was_dynamic') and not is_dynamic) self.webservice.change_registration( { 'old_mac' : kw['old_mac'], 'mac' : kw['mac'], 'hostname' : (kw['hostname'] if kw.has_key('hostname') else None), 'domain' : (int(kw['domain']) if kw.has_key('domain') else None), 'description' : kw['description'], 'expiration' : (int(kw['expiration']) if kw.has_key('did_renew_host') else None), 'is_dynamic' : kw['address_type'] == 'dynamic', 'pool' : addr_type['pool'] if addr_type.has_key('pool') else None, 'owners_list' : kw['owners_list'], 'network' : (kw['network'] if changed_to_static else None), 'address' : (kw['ip'] if changed_to_static else None), 'dhcp_group': (kw['dhcp_group'] if kw.has_key('dhcp_group') and kw['dhcp_group'] else None), }) self.redirect('/hosts/search/?q=%s' % misc.fix_mac(kw['mac'] if kw['mac'] else kw['old_mac']))
def add_attribute(self, mac, wrap=True, submit=False, attr_type_id=None, freeform_value=None, structured_value=None): # Confirm user authentication self.check_session() if wrap in ['0','f','False','no']: wrap = False vals = {} vals['wrap'] = wrap host = self.webservice.get_hosts({'mac':mac}) if len(host) != 1: raise Exception("Invalid host: %s (%r)" % (mac,host) ) host = host[0] host['clean_mac'] = misc.fix_mac(host['mac']) vals['host'] = host vals['valid_attributes'] = self.webservice.get_attributes({}) attrs = {} for i in vals['valid_attributes']: attrs[i['id']] = i filename = '%s/templates/add_host_attribute.tmpl'%frontend.static_dir if submit: if attrs[int(attr_type_id)]['structured']: self.webservice.add_structured_attribute_to_host( {'mac': mac, 'avid':int(structured_value)} ) else: self.webservice.add_freeform_attribute_to_host( {'mac': mac, 'aid': attr_type_id, 'value': freeform_value } ) status = "Success." if wrap: status = 'Success. <a href="javascript:window.close()">close window</a>' return status if wrap: return self.__template.wrap(leftcontent=self.get_leftnav(), filename=filename, values=vals) return str(framework.Template(file=filename, searchList=vals))
def add_host(self, **kw): ''' Process the add_host request ''' # Confirm user authentication self.check_session() addr_type = self.address_types[kw['address_type']] is_dynamic = addr_type.has_key('pool') and addr_type['pool'] is not None and not addr_type['ranges'] mac = self.webservice.register_host( { 'mac' : kw['mac'], 'hostname' : kw['hostname'], 'domain' : int(kw['domain']) if kw['domain'] else None, 'description' : kw['description'], 'expiration' : int(kw['expiration']), 'is_dynamic' : is_dynamic, 'pool' : addr_type['pool'] if addr_type.has_key('pool') else None, 'owners_list' : kw['owners_list'], 'network' : (kw['network'] if kw.has_key('network') and kw['network'] else None), 'add_host_to_my_group' : False, 'address' : (kw['ip'] if kw.has_key('ip') else None), 'dhcp_group': (kw['dhcp_group'] if kw.has_key('dhcp_group') and kw['dhcp_group'] else None), }) self.redirect('/hosts/search/?q=%s' % misc.fix_mac(mac))
def host_info(self, mac, wrap=True): # Confirm user authentication self.check_session() if wrap in ['0','f','False','no']: wrap = False # FIXME: it would be useful to get permissions on this host vals = {} print "get_hosts" host = self.webservice.get_hosts({'mac':mac}) if len(host) != 1: raise Exception("Invalid host: %s (%r)" % (mac,host) ) host = host[0] host['clean_mac'] = misc.fix_mac(host['mac']) vals['host'] = host vals['owners'] = self.webservice.find_ownernames_of_host({'mac':mac}) vals['attributes'] = self.webservice.get_attributes_to_hosts({'mac':mac}) vals['leased'] = self.webservice.get_leases({'mac':mac}) vals['static'] = self.webservice.get_addresses({'mac':mac}) vals['addresses'] = [ a['address'] for a in vals['static'] + vals['leased'] ] vals['pools'] = self.webservice.get_hosts_to_pools({'mac':mac}) ### if host['disabled']: vals['disabled'] = self.webservice.get_disabled( {'mac':mac} )[0] vals['dns_records'] = self.webservice.get_dns_records({'mac':mac,'order_by':'tid,name,ip_content,text_content'}) vals['enable_gul'] = frontend.enable_gul if frontend.enable_gul: vals['arp_bymac'] = self.webservice.get_gul_recent_arp_bymac({'mac':mac}) gul_byaddr = [] addrlist = [ addr['address'] for addr in vals['static']+vals['leased'] ] gul_byaddr = self.webservice.get_gul_recent_arp_byaddress({'address':addrlist}) byaddr = {} for i in gul_byaddr: if i.has_key('address'): byaddr[i['address']] = i else: print "No address field: %r" % i for addr in addrlist: if not byaddr.has_key(addr): byaddr[addr] = {'address':addr,'stopstamp':'no data','mac':'',} vals['arp_byaddress'] = byaddr filename = '%s/templates/host_info.tmpl'%frontend.static_dir if wrap: return self.__template.wrap(leftcontent=self.get_leftnav(), filename=filename, values=vals) return str(framework.Template(file=filename, searchList=vals))
def get_hosts_table(self, gid): '''Returns the table HTML @param gid: the database group id:''' text = [] # Query the database hosts= self.webservice.get_hosts( { 'gid' : gid } ) if not hosts: return "<p>This group does not contain any hosts.</p>" # The template HTML for every item item_template = '''<tr class="info" id="host%(clean_mac)s"> <td>%(hostname)s</td> <td>%(mac)s</td> <td class="actions"> <a href="javascript:;" id="delHost%(clean_mac)s" onclick="confirmHostRemove('%(clean_mac)s'); return false;">Remove</a> </td> </tr> ''' # Go through the query and make the table HTML using the template for host in hosts: host['gid'] = gid host['clean_mac'] = misc.fix_mac(host['mac']) text.append(item_template % (host)) # Combine all the parts into the table hosts_html = ''' <table class="infoTable"> <thead> <tr> <th width="25%%">Hostname</th> <th>Ethernet Address</th> <th> </th> </tr> </thead> <tbody> %s </tbody> </table> ''' % ''.join(text) return hosts_html
def get_results_table(self, search, gid): '''Returns the table of search results''' hosts = '' rows = [] HOSTS_LIMIT = 250 if len(search): hosts = self.webservice.get_hosts({ 'hostname' : '%%%s%%' % search, 'limit' : HOSTS_LIMIT }) used_hosts_temp = self.webservice.get_hosts({ 'gid' : gid }) # Put all used host names into a list for comparison used_hosts = [] for host in used_hosts_temp: used_hosts.append(host['hostname']) if not hosts: return '<p class="noResults">No hosts found.</p>' else: # The template HTML for every entry # If you change the <span id="add#"> below, be sure to update the regex in the table compliation below item_template = '''<tr class="info" id="host%(clean_mac)s"> <td>%(hostname)s</td> <td>%(mac)s</td> <td class="actions"> <span id="add%(clean_mac)s"> <a href="javascript:;" id="addLink%(clean_mac)s" onclick="addHostToGroup('%(clean_mac)s'); return false;">Add to group</a> </span> </td> </tr> ''' # Go through the query and make the table HTML using the template table_html = '' for host in hosts: host['clean_mac'] = misc.fix_mac(host['mac']) host['gid'] = gid if host['hostname'] in used_hosts: # This host already belongs to this group, so don't allow the user to select it (ie. remove the span containing the links) # FIXME: this regex could be better ... regex = re.compile('(<span).*?(<\\/span>)', re.IGNORECASE|re.DOTALL) rows.append(regex.sub('Already in group', (item_template % host))) else: # This host doesn't belong to this group yet, so keep all the normal links rows.append(item_template % (host)) limit = '' if len(hosts) == HOSTS_LIMIT: limit = '<div class="message"><div>Search results truncated to %s results. Try making your search more specific.</div></div>' % HOSTS_LIMIT # Combine all the parts into the table info = '''%s <input type="hidden" id="gid" value="%s" /> <table class="infoTable"> <thead> <tr> <th width="25%%">Hostname</th> <th>Ethernet Address</th> <th class="actions"> </th> </tr> </thead> <tbody> %s </tbody> </table> %s ''' % (limit, gid, ''.join(rows), limit) return info
def get_results_table(self, search, gid): '''Returns the table of search results''' hosts = '' rows = [] HOSTS_LIMIT = 250 if len(search): hosts = self.webservice.get_hosts({ 'hostname': '%%%s%%' % search, 'limit': HOSTS_LIMIT }) used_hosts_temp = self.webservice.get_hosts({'gid': gid}) # Put all used host names into a list for comparison used_hosts = [] for host in used_hosts_temp: used_hosts.append(host['hostname']) if not hosts: return '<p class="noResults">No hosts found.</p>' else: # The template HTML for every entry # If you change the <span id="add#"> below, be sure to update the regex in the table compliation below item_template = '''<tr class="info" id="host%(clean_mac)s"> <td>%(hostname)s</td> <td>%(mac)s</td> <td class="actions"> <span id="add%(clean_mac)s"> <a href="javascript:;" id="addLink%(clean_mac)s" onclick="addHostToGroup('%(clean_mac)s'); return false;">Add to group</a> </span> </td> </tr> ''' # Go through the query and make the table HTML using the template table_html = '' for host in hosts: host['clean_mac'] = misc.fix_mac(host['mac']) host['gid'] = gid if host['hostname'] in used_hosts: # This host already belongs to this group, so don't allow the user to select it (ie. remove the span containing the links) # FIXME: this regex could be better ... regex = re.compile('(<span).*?(<\\/span>)', re.IGNORECASE | re.DOTALL) rows.append( regex.sub('Already in group', (item_template % host))) else: # This host doesn't belong to this group yet, so keep all the normal links rows.append(item_template % (host)) limit = '' if len(hosts) == HOSTS_LIMIT: limit = '<div class="message"><div>Search results truncated to %s results. Try making your search more specific.</div></div>' % HOSTS_LIMIT # Combine all the parts into the table info = '''%s <input type="hidden" id="gid" value="%s" /> <table class="infoTable"> <thead> <tr> <th width="25%%">Hostname</th> <th>Ethernet Address</th> <th class="actions"> </th> </tr> </thead> <tbody> %s </tbody> </table> %s ''' % (limit, gid, ''.join(rows), limit) return info