Exemplo n.º 1
0
	def fetch_next_ip(self,hosttype_id):
		"""
		Fetch the first available ip address for a hosttype. The ip range
		is defined in skolesys.conf and the ip addresses already assigned
		are read in the ldap database
		"""
		if hosttype_id == hostdef.hosttype_as_id('mainserver'):
			return conf.get('DOMAIN','mainserver_ip')
	
		hosttype_text = hostdef.hosttype_as_text(hosttype_id)
		if not hosttype_text:
			return None
		iprange_str = conf.get('DOMAIN','%s_iprange' % hosttype_text)
		if not iprange_str:
			return None
		
		# Parse the iprange string
		c = re.compile('^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\s*(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$')
		m = c.match(iprange_str.strip())
		if m:
			ipstart = hostdef.iptoint('.'.join(m.groups()[:4]))
			ipend = hostdef.iptoint('.'.join(m.groups()[4:]))
			
			# Get already assigned ip addresses
			hostspath = "%s,%s" % (conf.get('LDAPSERVER','hosts_ou'),conf.get('LDAPSERVER','basedn'))
			res = self.l.search(hostspath,\
				ldap.SCOPE_ONELEVEL,'(objectclass=skoleSysHost)',['ipHostNumber'])
			sres = self.l.result(res)
			
			already_assigned = []
			for hidx in xrange(len(sres[1])):
				ipint = hostdef.iptoint(sres[1][hidx][1]['ipHostNumber'][0])
				if ipint!=None:
					already_assigned += [ipint]
					
			newip = None
			for tryip in range(ipstart,ipend+1):
				if already_assigned.count(tryip):
					continue
				newip = tryip
				break
			if newip:
				return hostdef.inttoip(newip)
		return None
	def conf_info(self):
		"""
		Read skolesys.conf and populate the member info dictionary. Many of
		the conf values are elabourated before they are stored in the dictionary.
		ie. subnets are registered with the following format in skolesys.conf:
			ltspserver_subnet = 192.168.0.0/16
		but this method expands that information to:
			ltspserver_subnet	192.168.0.0
			ltspserver_subnet_short	192.168
			ltspserver_subnetmask	255.255.0.0
		This kind of post elabouration is nessecary so cheetah can reach the right
		data when needed.
		"""
		from skolesys.lib.conf import conf
		# conf data
		self.data['conf'] = {}
		sections = conf.sections()
		for sec in sections:
			s = sec.lower()
			self.data['conf'][s] = {}
			for var,val in conf.items(sec):
				self.data['conf'][s][var.lower()] = val
		# TODO - There is to be made skolesys.conf sanity checker build into 
		# TODO - skolesys.lib.conf. That way the user can be presented with a 
		# TODO - configuration invalidity like missing mandetory variables:
		
		# Mainserver
		subnet_str = self.data['conf']['domain']['mainserver_subnet']
		res = hostdef.check_subnet(subnet_str)
		if not res:
			return -1
		subnet,subnetmask = res
		subnetmask_int = (2**32-1) - (2**(32-int(subnetmask))-1)
		subnet_int = hostdef.iptoint(subnet)
		subnetmask_address = '%d.%d.%d.%d' % ((subnetmask_int>>24)&255,(subnetmask_int>>16)&255,(subnetmask_int>>8)&255,subnetmask_int&255)
		self.data['conf']['domain']['mainserver_subnet'] = subnet
		self.data['conf']['domain']['mainserver_subnetmask'] = subnetmask_address
		subnet_short = ()
		bit_shifter = 24
		for ip_part in xrange(int(ceil(float(subnetmask)/8))):
			subnet_short += (str((subnet_int>>bit_shifter) & ((subnetmask_int>>bit_shifter) & 255)),)
			bit_shifter -= 8
		self.data['conf']['domain']['mainserver_subnet_short'] = '.'.join(subnet_short)
		
		# LTSP servers
		subnet_str = self.data['conf']['domain']['ltspserver_subnet']
		res = hostdef.check_subnet(subnet_str)
		if not res:
			return -2
		subnet,subnetmask = res
		subnetmask_int = (2**32-1) - (2**(32-int(subnetmask))-1)
		subnet_int = hostdef.iptoint(subnet)
		subnetmask_address = '%d.%d.%d.%d' % ((subnetmask_int>>24)&255,(subnetmask_int>>16)&255,(subnetmask_int>>8)&255,subnetmask_int&255)
		self.data['conf']['domain']['ltspserver_subnet'] = subnet
		self.data['conf']['domain']['ltspserver_subnetmask'] = subnetmask_address
		subnet_short = ()
		bit_shifter = 24
		for ip_part in xrange(int(ceil(float(subnetmask)/8))):
			subnet_short += (str((subnet_int>>bit_shifter) & ((subnetmask_int>>bit_shifter) & 255)),)
			bit_shifter -= 8
		self.data['conf']['domain']['ltspserver_subnet_short'] = '.'.join(subnet_short)