incubator-tashi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mry...@apache.org
Subject svn commit: r785723 - in /incubator/tashi/trunk/src/tashi: agents/dhcpdns.py client/tashi-client.py clustermanager/clustermanagerservice.py thrift/services.thrift
Date Wed, 17 Jun 2009 16:55:33 GMT
Author: mryan3
Date: Wed Jun 17 16:55:32 2009
New Revision: 785723

URL: http://svn.apache.org/viewvc?rev=785723&view=rev
Log:
Changed the NetworkConfiguration object to have an IP address in it.
The associated changes throughout the code to maintain the list of IP addresses is also in
this commit.


Modified:
    incubator/tashi/trunk/src/tashi/agents/dhcpdns.py
    incubator/tashi/trunk/src/tashi/client/tashi-client.py
    incubator/tashi/trunk/src/tashi/clustermanager/clustermanagerservice.py
    incubator/tashi/trunk/src/tashi/thrift/services.thrift

Modified: incubator/tashi/trunk/src/tashi/agents/dhcpdns.py
URL: http://svn.apache.org/viewvc/incubator/tashi/trunk/src/tashi/agents/dhcpdns.py?rev=785723&r1=785722&r2=785723&view=diff
==============================================================================
--- incubator/tashi/trunk/src/tashi/agents/dhcpdns.py (original)
+++ incubator/tashi/trunk/src/tashi/agents/dhcpdns.py Wed Jun 17 16:55:32 2009
@@ -19,6 +19,7 @@
 import os
 import socket
 from instancehook import InstanceHook
+from tashi.services.ttypes import Instance, NetworkConfiguration
 from tashi import boolean
 
 class DhcpDns(InstanceHook):
@@ -61,30 +62,41 @@
 			self.currentIP[k] = self.ipMin[k]
 		instances = self.client.getInstances()
 		for i in instances:
-			try:
-				ip = socket.gethostbyname(i.name)
-				ipNum = self.strToIp(ip)
-				self.log.info('Added %s->%s during reinitialization' % (i.name, ip))
-				self.usedIPs[ipNum] = ip
-			except Exception, e:
-				pass
+			for nic in i.nics:
+				try:
+					ip = nic.ip
+					ipNum = self.strToIp(ip)
+					self.log.info('Added %s->%s during reinitialization' % (i.name, ip))
+					self.usedIPs[ipNum] = ip
+				except Exception, e:
+					pass
 		
 	def strToIp(self, s):
-		ipNum = reduce(lambda x, y: x*256+y, map(int, s.split(".")))
+		ipNum = -1
+		try:
+			ipNum = reduce(lambda x, y: x*256+y, map(int, s.split(".")))
+		except:
+			pass
 		return ipNum
 	
 	def ipToStr(self, ip):
 		return "%d.%d.%d.%d" % (ip>>24, (ip>>16)%256, (ip>>8)%256, ip%256)
 	
-	def allocateIP(self, network):
-		while (self.currentIP[network] in self.usedIPs or self.currentIP[network] > self.ipMax[network]):
+	def allocateIP(self, nic):
+		network = nic.network
+		allocatedIP = None
+		requestedIP = self.strToIp(nic.ip)
+		if (requestedIP <= self.ipMax[network] and requestedIP >= self.ipMin[network] and
(requestedIP not in self.usedIPs)):
+			allocatedIP = requestedIP
+		while (allocatedIP == None):
 			if (self.currentIP[network] > self.ipMax[network]):
 				self.currentIP[network] = self.ipMin[network]
-			else:
+			elif (self.currentIP[network] in self.usedIPs):
 				self.currentIP[network] = self.currentIP[network] + 1
-		ipString = self.ipToStr(self.currentIP[network])
-		self.usedIPs[self.currentIP[network]] = ipString
-		self.currentIP[network] = self.currentIP[network] + 1
+			else:
+				allocatedIP = self.currentIP[network]
+		ipString = self.ipToStr(allocatedIP)
+		self.usedIPs[allocatedIP] = ipString
 		return ipString
 	
 	def addDhcp(self, name, ipaddr, hwaddr):
@@ -118,7 +130,7 @@
 		stdin.close()
 		output = stdout.read()
 		stdout.close()
-
+	
 	def addDns(self, name, ip):
 		if (self.dnsKeyFile != ""):
 			cmd = "nsupdate -k %s" % (self.dnsKeyFile)
@@ -158,31 +170,54 @@
 		output = stdout.read()
 		stdout.close()
 	
+	def doUpdate(self, instance):
+		newInstance = Instance()
+		newInstance.id = instance.id
+		newInstance.nics = instance.nics
+		self.client.vmUpdate(instance.id, newInstance, None)
+
 	def preCreate(self, instance):
 		if (len(instance.nics) < 1):
 			return
-		network = instance.nics[0].network
-		ip = self.allocateIP(network)
-		self.log.info("Adding %s:{%s->%s, %s->%s} to DHCP/DNS" % (instance.name, instance.nics[0].mac,
ip, instance.name, ip))
-		try:
-			self.addDhcp(instance.name, ip, instance.nics[0].mac)
-			self.addDns(instance.name, ip)
-		except Exception, e:
-			self.log.exception("Failed to add host %s to DHCP/DNS" % (instance.name))
+		for i in range(0, len(instance.nics)):
+			nic = instance.nics[i]
+			ip = self.allocateIP(nic)
+			nic.ip = ip
+			try:
+				if (i == 0):
+					self.log.info("Adding %s:{%s->%s} to DNS" % (instance.name, instance.name, ip))
+					self.addDns(instance.name, ip)
+				if (i == 0):
+					dhcpName = instance.name
+				else:
+					dhcpName = instance.name + "-nic%d" % (i)
+				self.log.info("Adding %s:{%s->%s} to DHCP" % (dhcpName, nic.mac, ip))
+				self.addDhcp(dhcpName, ip, nic.mac)
+			except Exception, e:
+				self.log.exception("Failed to add host %s to DHCP/DNS" % (instance.name))
+		self.doUpdate(instance)
 
 	def postDestroy(self, instance):
 		if (len(instance.nics) < 1):
 			return
-		try:
-			ip = socket.gethostbyname(instance.name)
-			ipNum = self.strToIp(ip)
-			del self.usedIPs[ipNum]
-		except Exception, e:
-			self.log.exception("Failed to remove host %s from pool of usedIPs" % (instance.name))
 		self.log.info("Removing %s from DHCP/DNS" % (instance.name))
+		for i in range(0, len(instance.nics)):
+			nic = instance.nics[i]
+			ip = nic.ip
+			try:
+				ipNum = self.strToIp(ip)
+				del self.usedIPs[ipNum]
+			except Exception, e:
+				self.log.exception("Failed to remove host %s, ip %s from pool of usedIPs" % (instance.name,
ip))
+			try:
+				if (i == 0):
+					dhcpName = instance.name
+				else:
+					dhcpName = instance.name + "-nic%d" % (i)
+				self.removeDhcp(dhcpName)
+			except Exception, e:
+				self.log.exception("Failed to remove host %s from DHCP" % (instance.name))
 		try:
 			self.removeDns(instance.name)
-			self.removeDhcp(instance.name)
 		except Exception, e:
-			self.log.exception("Failed to remove host %s from DHCP/DNS" % (instance.name))
-
+			self.log.exception("Failed to remove host %s from DNS" % (instance.name))

Modified: incubator/tashi/trunk/src/tashi/client/tashi-client.py
URL: http://svn.apache.org/viewvc/incubator/tashi/trunk/src/tashi/client/tashi-client.py?rev=785723&r1=785722&r2=785723&view=diff
==============================================================================
--- incubator/tashi/trunk/src/tashi/client/tashi-client.py (original)
+++ incubator/tashi/trunk/src/tashi/client/tashi-client.py Wed Jun 17 16:55:32 2009
@@ -76,13 +76,16 @@
 def randomMac():
 	return ("52:54:00:%2.2x:%2.2x:%2.2x" % (random.randint(0, 255), random.randint(0, 255),
random.randint(0, 255)))
 
-def randomNetwork():
+def getDefaultNetwork():
 	fetchNetworks()
 	networkId = 1
 	for network in networks:
 		if (networks[network].name == "default"):
 			networkId = network
-	return [NetworkConfiguration(d={'mac':randomMac(), 'network':networkId})]
+	return networkId
+
+def randomNetwork():
+	return [NetworkConfiguration(d={'mac':randomMac(), 'network':getDefaultNetwork()})]
 
 def parseDisks(arg):
 	try:
@@ -107,10 +110,18 @@
 		for strNic in strNics:
 			strNic = strNic.strip()
 			(l, s, r) = stringPartition(strNic, ":")
-			l = int(l)
-			if (r==''):
-				r = randomMac()
-			nic = NetworkConfiguration(d={'mac':r, 'network':l})
+			n = l
+			if (n == ''):
+				n = getDefaultNetwork()
+			n = int(n)
+			(l, s, r) = stringPartition(r, ":")
+			ip = l
+			if (ip == ''):
+				ip = None
+			m = r
+			if (m == ''):
+				m = randomMac()
+			nic = NetworkConfiguration(d={'mac':m, 'network':n, 'ip':ip})
 			nics.append(nic)
 		return nics
 	except:
@@ -245,7 +256,7 @@
 
 # Example use strings
 examples = {
-'createVm': ['--name foobar --disks i386-hardy.qcow2', '--userId 3 --name foobar --cores
8 --memory 7168 --disks mpi-hardy.qcow2:True,scratch.qcow2:False --nics 2:52:54:00:00:12:34,1:52:54:00:00:56:78
--hints enableDisplay=True'],
+'createVm': ['--name foobar --disks i386-hardy.qcow2', '--userId 3 --name foobar --cores
8 --memory 7168 --disks mpi-hardy.qcow2:True,scratch.qcow2:False --nics :1.2.3.4,1::52:54:00:00:56:78
--hints enableDisplay=True'],
 'createMany': ['--basename foobar --disks i386-hardy.qcow2 --count 4'],
 'shutdownVm': ['--instance 12345', '--instance foobar'],
 'destroyVm': ['--instance 12345', '--instance foobar'],

Modified: incubator/tashi/trunk/src/tashi/clustermanager/clustermanagerservice.py
URL: http://svn.apache.org/viewvc/incubator/tashi/trunk/src/tashi/clustermanager/clustermanagerservice.py?rev=785723&r1=785722&r2=785723&view=diff
==============================================================================
--- incubator/tashi/trunk/src/tashi/clustermanager/clustermanagerservice.py (original)
+++ incubator/tashi/trunk/src/tashi/clustermanager/clustermanagerservice.py Wed Jun 17 16:55:32
2009
@@ -440,6 +440,11 @@
 				oldInstance.vmId = instance.vmId
 			if (instance.hostId):
 				oldInstance.hostId = instance.hostId
+			for nic in instance.nics:
+				if (nic.ip):
+					for oldNic in oldInstance.nics:
+						if (oldNic.mac == nic.mac):
+							oldNic.ip = nic.ip
 			oldInstance.decayed = False
 			self.updateDecay(self.decayedInstances, oldInstance)
 			self.data.releaseInstance(oldInstance)

Modified: incubator/tashi/trunk/src/tashi/thrift/services.thrift
URL: http://svn.apache.org/viewvc/incubator/tashi/trunk/src/tashi/thrift/services.thrift?rev=785723&r1=785722&r2=785723&view=diff
==============================================================================
--- incubator/tashi/trunk/src/tashi/thrift/services.thrift (original)
+++ incubator/tashi/trunk/src/tashi/thrift/services.thrift Wed Jun 17 16:55:32 2009
@@ -89,8 +89,9 @@
 }
 
 struct NetworkConfiguration {
-	1:i32 network
-	2:string mac
+	1:i32 network,
+	2:string mac,
+	3:string ip
 }
 
 struct Instance {



Mime
View raw message