def remove_host(self,hwaddr=None, hostname=None): """ Remove a registered SkoleSYS host from LDAP """ hostspath = "%s,%s" % (conf.get('LDAPSERVER','hosts_ou'),conf.get('LDAPSERVER','basedn')) if hwaddr: hwaddr = hostdef.check_hwaddr(hwaddr) if not hwaddr: return -1 res = self.l.search(hostspath,\ ldap.SCOPE_ONELEVEL,'(& (macAddress=%s)(objectclass=skoleSysHost))'%hwaddr,['dn']) sres = self.l.result(res,0) if sres[1]==[]: return -2 dn = sres[1][0][0] if hostname: res = self.l.search(hostspath,\ ldap.SCOPE_ONELEVEL,'(& (hostName=%s)(objectclass=skoleSysHost))'%hostname,['dn']) sres = self.l.result(res,0) if sres[1]==[]: return -3 dn = sres[1][0][0] self.bind(conf.get('LDAPSERVER','admin'),conf.get('LDAPSERVER','passwd')) self.delete(dn) return 0
def __init__(self,hwaddr=None): """ hwaddr The registered MAC of a host The constructor calls all the info collecting methods (conf_info(), host_info(), user_info()) """ self.data = {} self.hwaddr = hostdef.check_hwaddr(hwaddr) self.conf_info() self.host_info() self.user_info()
def host_info(self,hwaddr=None,hostname=None): hostspath = "%s,%s" % (conf.get('LDAPSERVER','hosts_ou'),conf.get('LDAPSERVER','basedn')) if hwaddr: hwaddr = hostdef.check_hwaddr(hwaddr) if not hwaddr: return None res = self.l.search(hostspath,\ ldap.SCOPE_ONELEVEL,'(& (macAddress=%s)(objectclass=skoleSysHost))'%hwaddr,['hostType','hostName','ipHostNumber','macAddress']) sres = self.l.result(res,0) if sres[1]==[]: return None return sres[1][0][1] if hostname: res = self.l.search(hostspath,\ ldap.SCOPE_ONELEVEL,'(& (hostName=%s)(objectclass=skoleSysHost))'%hostname,['hostType','hostName','ipHostNumber','macAddress']) sres = self.l.result(res,0) if sres[1]==[]: return None return sres[1][0][1]
def __init__(self,hosttype_id,dist_codename,hwaddr=None,context=None,context_only=False,pretend=False): """ Build the configuration for a certain hosttype or host. It works in a threaded or asynchronious environment by ensuring that a unique temp space is created in /tmp 1. Create temp directory. 2. Fetch system, domain and hosts information. 3. Initiate the configuration parser. """ self.hosttype = hostdef.hosttype_as_text(hosttype_id) self.hwaddr = hostdef.check_hwaddr(hwaddr) self.dist_codename = dist_codename self.context = context self.context_only = context_only # Create a temp directory self.tempdir = tempfile.mkdtemp(prefix='skolesys_') # Collect info about domain and host self.infocollection = InfoCollection(self.hwaddr) # Build the configuration self.build_config(pretend)
# Check if this hostname is already registered hm = hostmanager.HostManager() if hm.host_exists(hostname=hostname): print 'The hostname: "%s" has already been registered use hostmod instead' % hostname exit(0) if options.ipaddr: ipchk = hostdef.check_ipaddr(options.ipaddr) if not ipchk: print 'The ipaddress: "%s" entered is not valid' % options.ipaddr exit(0) print "Hostname: %s" % hostname while not options.hwaddr or not hostdef.check_hwaddr(options.hwaddr): options.hwaddr = raw_input("Input the hardware address of the host's network interface (mac-address): ") options.hwaddr = hostdef.check_hwaddr(options.hwaddr) # Check if this hwaddr is already registered if hm.host_exists(hwaddr=options.hwaddr): print 'The hwaddr: "%s" has already been registered use hostmod instead' % options.hwaddr exit(0) while not options.hosttype or not hostdef.hosttype_as_id(options.hosttype): options.hosttype = raw_input("Input the host type (%s): " % ','.join(hostdef.list_hosttypes_by_text())) hosttype_id = hostdef.hosttype_as_id(options.hosttype) try: hostadd_res = hm.register_host(options.hwaddr,hostname,hosttype_id,options.ipaddr)
def register_host(self,hwaddr,hostname,hosttype_id,ipaddr=None,update_hosts=True): """ Register a new host to the SkoleSYS network. The registrationid is the hwaddr but the hostname must be unique aswell. If no ipaddr is given or the given ipaddr is in use the system will pick one for the host. """ # check sanity hwaddr = hostdef.check_hwaddr(hwaddr) if not hwaddr: return -1 hostname = hostdef.check_hostname(hostname) if not hostname: return -2 if not hostdef.hosttype_as_text(hosttype_id): return -3 hosttype = hostdef.hosttype_as_text(hosttype_id) if ipaddr and not hostdef.check_ipaddr(ipaddr): return -4 # check if the hwaddr is already registered. if self.host_exists(hwaddr=hwaddr): return -5 # check if the hostname is already registered. if self.host_exists(hostname=hostname): return -6 if ipaddr and self.ipaddr_exists(ipaddr): return -7 if not ipaddr: ipaddr = self.fetch_next_ip(hosttype_id) # If still no ip address, there are no more ip addresses in the # ip range of the host type (expand the range in skolesys.conf) if not ipaddr: return -8 path = "%s,%s,%s" % \ ('cn=%s'%hostname,\ conf.get('LDAPSERVER','hosts_ou'),\ conf.get('LDAPSERVER','basedn')) host_info = {'cn': hostname, 'macAddress': hwaddr, 'hostType': hosttype, 'hostName': hostname, 'ipHostNumber': ipaddr, 'objectclass':('skoleSysHost','top')} self.bind(conf.get('LDAPSERVER','admin'),conf.get('LDAPSERVER','passwd')) self.touch_by_dict({path:host_info}) if update_hosts: # update-hosts=False is f.inst. used in seed-mainserver config scripts import skolesys.cfmachine.configbuilder as cb c = cb.ConfigBuilder(hostdef.hosttype_as_id('mainserver'),sysinfo.get_dist_codename(),'',context='update-hosts',context_only=True) curdir = os.getcwd() os.chdir(c.tempdir) os.system('./install.sh') os.chdir(curdir) del c return 1