示例#1
0
文件: TestRADL.py 项目: lxhiguera/im
	def test_empty_contextualize(self):
		radl = """
			system test (
			cpu.count>=1
			)
			
			deploy test 1
			
			contextualize ()
			"""
		r = parse_radl(radl)
		r.check()
		self.assertEqual(r.contextualize.items, {})
		
		radl_json = dump_radl_json(r)
		r = parse_radl_json(radl_json)
		r.check()
		self.assertEqual(r.contextualize.items, {})
		
		radl = """
			system test (
			cpu.count>=1
			)
			
			deploy test 1
			"""
		r = parse_radl(radl)
		r.check()
		self.assertEqual(r.contextualize.items, None)
		
		radl_json = dump_radl_json(r)
		r = parse_radl_json(radl_json)
		r.check()
		self.assertEqual(r.contextualize.items, None)
示例#2
0
文件: TestREST.py 项目: lxhiguera/im
 def test_34_get_radl(self):
     self.server.request('GET', "/infrastructures/" + self.inf_id + "/radl", headers = {'AUTHORIZATION' : self.auth_data})
     resp = self.server.getresponse()
     output = str(resp.read())
     self.assertEqual(resp.status, 200, msg="ERROR getting the infrastructure RADL:" + output)
     try:
         radl_parse.parse_radl(output)
     except Exception, ex:
         self.assertTrue(False, msg="ERROR parsing the RADL returned by GetInfrastructureRADL: " + str(ex))
示例#3
0
文件: TestIM.py 项目: lxhiguera/im
 def test_12_getradl(self):
     """
     Test the GetInfrastructureRADL IM function
     """
     (success, res) = self.server.GetInfrastructureRADL(self.inf_id, self.auth_data)
     self.assertTrue(success, msg="ERROR calling GetInfrastructureRADL: " + str(res))
     try:
         radl_parse.parse_radl(res)
     except Exception, ex:
         self.assertTrue(False, msg="ERROR parsing the RADL returned by GetInfrastructureRADL: " + str(ex))
示例#4
0
文件: TestRADL.py 项目: lxhiguera/im
	def test_ansible_host(self):

		radl = """
ansible ansible_master (host = 'host' and credentials.username = '******' and credentials.password = '******')
network net ()

system main (
ansible_host = 'ansible_master' and
net_interface.0.connection = 'net'
)		"""
		r = parse_radl(radl)
		self.radl_check(r)
		
		radl_json = dump_radl_json(r)
		r = parse_radl_json(radl_json)
		self.radl_check(r)

		radl = """
ansible ansible_master (host = 'host' and credentials.username = '******' and credentials.password = '******')
network net ()

system main (
ansible_host = 'ansible_master1' and
net_interface.0.connection = 'net'
)		"""
		r = parse_radl(radl)
		
		with self.assertRaises(RADLParseException):
			self.radl_check(r)
			
		radl = """
ansible ansible_master (credentials.username = '******' and credentials.password = '******')
network net ()

system main (
net_interface.0.connection = 'net'
)		"""
		r = parse_radl(radl)
		
		with self.assertRaises(RADLParseException):
			self.radl_check(r)
			
		radl = """
ansible ansible_master (host = 'host' and credentials.username = '******')
network net ()

system main (
net_interface.0.connection = 'net'
)		"""
		r = parse_radl(radl)
		
		with self.assertRaises(RADLParseException):
			self.radl_check(r)
示例#5
0
文件: TestIM.py 项目: lxhiguera/im
 def test_15_get_vm_info(self):
     """
     Test the GetVMInfo IM function
     """
     (success, vm_ids) = self.server.GetInfrastructureInfo(self.inf_id, self.auth_data)
     self.assertTrue(success, msg="ERROR calling GetInfrastructureInfo: " + str(vm_ids))
     (success, info)  = self.server.GetVMInfo(self.inf_id, vm_ids[0], self.auth_data)
     self.assertTrue(success, msg="ERROR calling GetVMInfo: " + str(info))
     try:
         radl_parse.parse_radl(info)
     except Exception, ex:
         self.assertTrue(False, msg="ERROR parsing the RADL returned by GetVMInfo: " + str(ex))       
示例#6
0
def get_launch_time(node):
	launch_time = 0
		
	(auth_data, server) = connect()
	
	(success, res) = server.GetInfrastructureList(auth_data)
	if success:
		infrastructure_id = res[0]
		logging.debug("Obtained infrastructure " + str(infrastructure_id))
	else:
		logging.error("ERROR listing the infrastructures: " + res)
		sys.exit(1) 
	
	(success, res) = server.GetInfrastructureInfo(infrastructure_id, auth_data)
	vm_ids = res['vm_list']
	if success:
		logging.debug("Successfully obtained infrastructure info")
		for vm_id in vm_ids:
			(success, info)  = server.GetVMInfo(infrastructure_id, vm_id, auth_data)
		
			if success:
				info_radl = radl_parse.parse_radl(info)
				node_name = info_radl.systems[0].name
				if node_name == node:
					launch_time = info_radl.systems[0].getValue('launch_time')
			else:
				logging.error("ERROR obtaining the node information: " + vm_id)
	return launch_time
示例#7
0
	def AlterVM(inf_id, vm_id, radl_data, auth):
		"""
		Get information about a virtual machine in an infrastructure.

		Args:

		- inf_id(str): infrastructure id.
		- vm_id(str): virtual machine id.
		- radl(str): RADL description.
		- auth(Authentication): parsed authentication tokens.

		Return: a str with the information about the VM
		"""

		InfrastructureManager.logger.info("Modifying the VM: '" + str(vm_id) + "' from inf: " + str(inf_id))
		vm = InfrastructureManager.get_vm_from_inf(inf_id, vm_id, auth)
		if not vm:
			InfrastructureManager.logger.info("VM does not exist or Access Error")
			raise Exception("VM does not exist or Access Error")
		
		if isinstance(radl_data, RADL):
			radl = radl_data
		else:
			radl = radl_parse.parse_radl(radl_data)

		exception = None
		try:
			(success, alter_res) = vm.alter(radl, auth)
		except Exception, e:
			exception = e
示例#8
0
def get_region(node):
	region = ""

	(auth_data, server) = connect()
	
	(success, res) = server.GetInfrastructureList(auth_data)
	if success:
		infrastructure_id = res[0]
		logging.debug("Obtained infrastructure " + str(infrastructure_id))
	else:
		logging.error("ERROR listing the infrastructures: " + res)
		sys.exit(1) 
	
	(success, res) = server.GetInfrastructureInfo(infrastructure_id, auth_data)
	vm_ids = res['vm_list']
	if success:
		logging.debug("Successfully obtained infrastructure info")
		for vm_id in vm_ids:
			(success, info)  = server.GetVMInfo(infrastructure_id, vm_id, auth_data)
		
			if success:
				info_radl = radl_parse.parse_radl(info)
				node_name = info_radl.systems[0].name
				if node_name == node:
					image_url = info_radl.systems[0].getValue('disk.0.image.url')
					# Parse the URL to get the region (for example 'aws://us-east-1/ami-e50e888c')
					region = image_url.split('/')[2]
			else:
				logging.error("ERROR obtaining the instance type: " + vm_id)
	
	return region
示例#9
0
    def test_10_concrete(self):
        radl_data = """
            network net ()
            system test (
            cpu.arch='x86_64' and
            cpu.count>=1 and
            memory.size>=512m and
            #disks.free_size>=2g and
            net_interface.0.connection = 'net' and
            net_interface.0.dns_name = 'test' and
            disk.0.os.flavour='ubuntu' and
            disk.0.os.version>='12.04'
            #disk.0.os.flavour='centos' and
            #disk.0.os.version>='6'
            )"""
        radl = radl_parse.parse_radl(radl_data)
        radl_system = radl.systems[0]

        for cloud_id, cloud in cloud_list.items():
            if self.connectors_to_test == "all" or cloud_id in self.connectors_to_test:
                systems = self.concrete_systems_with_vmrc(radl_system)
                concrete_systems = []
                for s in systems:
                    concrete_systems.extend(cloud.concreteSystem(s, auth))
                self.assertTrue(len(concrete_systems) > 0, msg="ERROR: no system returned by concreteSystems for cloud: " + cloud_id)
示例#10
0
def get_user_spot_bid(node):
	bid = 0.0
		
	(auth_data, server) = connect()
	
	(success, res) = server.GetInfrastructureList(auth_data)
	if success:
		infrastructure_id = res[0]
		logging.debug("Obtained infrastructure " + str(infrastructure_id))
	else:
		logging.error("ERROR listing the infrastructures: " + res)
		sys.exit(1) 
	
	(success, res) = server.GetInfrastructureInfo(infrastructure_id, auth_data)
	vm_ids = res['vm_list']
	if success:
		logging.debug("Successfully obtained infrastructure info")
		for vm_id in vm_ids:
			(success, info)  = server.GetVMInfo(infrastructure_id, vm_id, auth_data)
		
			if success:
				info_radl = radl_parse.parse_radl(info)
				node_name = info_radl.systems[0].name
				if node_name == node:
					bid = info_radl.systems[0].getValue('price')
			else:
				logging.error("ERROR obtaining the user bid for the spot instance: " + vm_id)
	
	return bid
示例#11
0
文件: TestRADL.py 项目: lxhiguera/im
	def test_references(self):

		r = parse_radl(TESTS_PATH + "/test_radl_ref.radl")
		self.radl_check(r, [2, 2, 0, 2, 2])
		
		radl_json = dump_radl_json(r)
		r = parse_radl_json(radl_json)
		self.radl_check(r, [2, 2, 0, 2, 2])
示例#12
0
文件: TestIM.py 项目: lxhiguera/im
    def test_80_create_ansible_host(self):
        """
        Test the CreateInfrastructure IM function
        """
        ansible_radl = """
            network publicnet (outbound = 'yes')
            network net ()

            system node (
             cpu.arch='x86_64' and
             cpu.count>=1 and
             memory.size>=512m and
             net_interface.0.connection = 'publicnet' and
             net_interface.1.connection = 'net' and
             disk.0.os.flavour='ubuntu' and
             disk.0.os.version>='12.04'
            )

            deploy node 1
            """
            
        (success, inf_id) = self.server.CreateInfrastructure(ansible_radl, self.auth_data)
        self.assertTrue(success, msg="ERROR calling CreateInfrastructure to create ansible master: " + str(inf_id))
        self.__class__.inf_id = [inf_id]

        all_configured = self.wait_inf_state(inf_id, VirtualMachine.CONFIGURED, 600)
        self.assertTrue(all_configured, msg="ERROR waiting the ansible master to be configured (timeout).")
        
        (success, info)  = self.server.GetVMInfo(inf_id, "0", self.auth_data)
        self.assertTrue(success, msg="ERROR getting ansible master info: " + str(info))
        master_radl = radl_parse.parse_radl(info)
        
        host = master_radl.systems[0].getValue("net_interface.0.ip")
        username = master_radl.systems[0].getValue("disk.0.os.credentials.username")
        password = master_radl.systems[0].getValue("disk.0.os.credentials.password")
        
        radl = """
        	ansible ansible_master (host = '%s' and credentials.username='******' and credentials.password='******')
            network net ()

            system node (
             cpu.arch='x86_64' and
             cpu.count>=1 and
             memory.size>=512m and
             net_interface.0.connection = 'net' and
             disk.0.os.flavour='ubuntu' and
             disk.0.os.version>='12.04'
            )

            deploy node 1
            """ % (host, username, password)

        (success, inf_id) = self.server.CreateInfrastructure(radl, self.auth_data)
        self.assertTrue(success, msg="ERROR calling CreateInfrastructure: " + str(inf_id))
        self.__class__.inf_id.append(inf_id) 

        all_configured = self.wait_inf_state(inf_id, VirtualMachine.CONFIGURED, 300)
        self.assertTrue(all_configured, msg="ERROR waiting the infrastructure to be configured (timeout).")
示例#13
0
文件: TestRADL.py 项目: lxhiguera/im
	def test_check_newline(self):
		radl = """
system test (
auth = 'asd asd asd asd asd asd asd as dasd asd as das dasd as das d                            asd \n' and
otra = 1
)
		"""
		r = parse_radl(radl)
		r.check()
示例#14
0
文件: TestRADL.py 项目: lxhiguera/im
	def radl_check(self, radl, expected_lengths=None, check_output=True):
		self.assertIsInstance(radl, RADL)
		radl.check()
		if expected_lengths:
			lengths = [len(l) for l in [radl.networks, radl.systems, radl.deploys,
			                            radl.configures, radl.contextualize]]
			self.assertEqual(lengths, expected_lengths)
		if check_output:
			self.radl_check(parse_radl(str(radl)), expected_lengths, check_output=False)
示例#15
0
文件: TestRADL.py 项目: lxhiguera/im
	def test_check_password(self):

		radl = """
network publica ()

system main (
disk.0.os.credentials.new.password = '******'
)		"""
		r = parse_radl(radl)
		with self.assertRaises(RADLParseException):
			r.check()
			
		radl = """
network publica ()

system main (
disk.0.os.credentials.new.password = '******'
)		"""
		r = parse_radl(radl)
		r.check()
示例#16
0
文件: TestRADL.py 项目: lxhiguera/im
	def test_outports(self):

		radl = """
network publica (outbound = 'yes' and outports='8899a-8899,22-22')

system main (
net_interface.0.connection = 'publica'
)		"""
		r = parse_radl(radl)
		with self.assertRaises(RADLParseException):
			self.radl_check(r)
示例#17
0
	def Reconfigure(inf_id, radl_data, auth, vm_list = None):
		"""
		Add and update RADL definitions and reconfigure the infrastructure.

		Args:

		- inf_id(str): infrastructure id.
		- radl_data(str): RADL description, it can be empty.
		- auth(Authentication): parsed authentication tokens.
		- vm_list(list of int): List of VM ids to reconfigure. If None all VMs will be reconfigured.

		Return: "" if success.
		"""

		InfrastructureManager.logger.info("Reconfiguring the inf: " + str(inf_id))
		if isinstance(radl_data, RADL):
			radl = radl_data
		else:
			radl = radl_parse.parse_radl(radl_data)
		InfrastructureManager.logger.debug(radl)

		sel_inf = InfrastructureManager.get_infrastructure(inf_id, auth)

		# Update infrastructure RADL with this new RADL
		# Add or update configures
		for s in radl.configures:
			sel_inf.radl.add(s.clone(), "replace")
			InfrastructureManager.logger.info("(Re)definition of %s %s" % (type(s), s.getId()))
		
		# and update contextualize
		sel_inf.radl.add(radl.contextualize)
		
		# Check if the user want to set a new password to any system:
		for system in sel_inf.radl.systems:
			new_system = radl.get_system_by_name(system.name)
			if new_system:
				new_creds = new_system.getCredentialValues(new = True)
				# The user has specified a credential:
				if len(list(set(new_creds))) > 1 or list(set(new_creds))[0] != None:
					creds = system.getCredentialValues()
					if cmp(new_creds,creds) != 0:
						# The credentials have changed
						(_, password, public_key, private_key) = new_creds
						system.setCredentialValues(password=password, public_key=public_key, private_key=private_key, new=True)
		
		InfrastructureManager.save_data(inf_id)

		# Stick all virtual machines to be reconfigured
		InfrastructureManager.logger.info("Contextualize the inf.")
		sel_inf.Contextualize(auth, vm_list)

		return ""
示例#18
0
文件: TestRADL.py 项目: lxhiguera/im
	def test_basic0(self):

		r = parse_radl(TESTS_PATH + "/test_radl_1.radl")
		self.radl_check(r, [2, 2, 0, 0, 0])
		s = r.get_system_by_name("main")
		self.assertEqual(s.getValue("cpu.arch"), "x86_64")
		self.assertEqual(s.getValue("net_interface.0.connection"), "publica")
		
		radl_json = dump_radl_json(r)
		r = parse_radl_json(radl_json)
		self.radl_check(r, [2, 2, 0, 0, 0])
		s = r.get_system_by_name("main")
		self.assertEqual(s.getValue("cpu.arch"), "x86_64")
		self.assertEqual(s.getValue("net_interface.0.connection"), "publica")
示例#19
0
文件: TestRADL.py 项目: lxhiguera/im
	def test_basic(self):
		r = parse_radl(TESTS_PATH + "/test_radl_0.radl")
		self.radl_check(r, [1, 1, 1, 1, 0])
		s = r.get_system_by_name("cursoaws")
		self.assertIsInstance(s, system)
		self.assertEqual(len(s.features), 17)
		self.assertEqual(s.getValue("disk.0.os.name"), "linux")
		
		radl_json = dump_radl_json(r)
		r = parse_radl_json(radl_json)
		s = r.get_system_by_name("cursoaws")
		self.assertIsInstance(s, system)
		self.assertEqual(len(s.features), 17)
		self.assertEqual(s.getValue("disk.0.os.name"), "linux")
示例#20
0
 def test_55_alter(self):
     radl_data = """
         system test (
         cpu.count>=2 and
         memory.size>=1024m
         )"""
     radl = radl_parse.parse_radl(radl_data)
     for vm in self.vm_list:
         cl = vm.cloud.getCloudConnector()
         (success, msg) = cl.alterVM(vm, radl, auth)
         self.assertTrue(success, msg="ERROR: updating VM for cloud: " + vm.cloud.id + ": " + str(msg))
         # get the updated vm
         (success, new_vm) = cl.updateVMInfo(vm, auth)
         new_cpu = new_vm.info.systems[0].getValue('cpu.count')
         new_memory = new_vm.info.systems[0].getFeature('memory.size').getValue('M')
         self.assertEqual(new_cpu, 2, msg="ERROR: updating VM for cloud: " + vm.cloud.id + ". CPU num must be 2.")
         self.assertEqual(new_memory, 1024, msg="ERROR: updating VM for cloud: " + vm.cloud.id + ". Memory must be 1024.")
示例#21
0
def getInfrastructureInfo():
	node_list = {}
	is_spot = False
	state = ""
	
	(auth_data, server) = connect()
	
	(success, res) = server.GetInfrastructureList(auth_data)
	if success:
		infrastructure_id = res[0]
		logging.debug("Obtained infrastructure " + str(infrastructure_id))
	else:
		logging.error("ERROR listing the infrastructures: " + res)
		sys.exit(1) 
	
	(success, res) = server.GetInfrastructureInfo(infrastructure_id, auth_data)
	vm_ids = res['vm_list']
	if success:
		logging.debug("Successfully obtained infrastructure info")
		for vm_id in vm_ids:
			(success, info)  = server.GetVMInfo(infrastructure_id, vm_id, auth_data)
			if success:
				info_radl = radl_parse.parse_radl(info)
				is_spot = info_radl.systems[0].getValue('spot')
				node_name = info_radl.systems[0].name
				#ignore nodes in state 'off' or 'failed' and delete them from the infrastructure
				state = info_radl.systems[0].getValue('state')
				if state != 'off' and state != 'failed':
					logging.info("state of node " + node_name + " is " + state)
					if is_spot == 'yes':
						node_list[node_name] = 'spot'
					else:
						node_list[node_name] = 'ondemand'
				else:
					(success, info) = server.RemoveResource(infrastructure_id, vm_id, auth_data)
					if success:
						logging.info("Successfully removed VM " + vm_id)
					else:
						logging.warn('Error removing the VM with ID: %s. Error: %s' % (vm_id, info))
			else:
				logging.error("ERROR obtaining the node information: " + vm_id)
	
	logging.info("The node list that compose the infrastructure is: ") 
	logging.info(node_list)
	return node_list
示例#22
0
文件: TestRADL.py 项目: lxhiguera/im
	def test_concrete(self):

		r = parse_radl(TESTS_PATH + "/test_radl_conc.radl")
		self.radl_check(r)
		s = r.get_system_by_name("main")
		self.assertIsInstance(s, system)
		concrete_s, score = s.concrete()
		self.assertIsInstance(concrete_s, system)
		self.assertEqual(score, 201)
		
		radl_json = dump_radl_json(r)
		r = parse_radl_json(radl_json)
		self.radl_check(r)
		s = r.get_system_by_name("main")
		self.assertIsInstance(s, system)
		concrete_s, score = s.concrete()
		self.assertIsInstance(concrete_s, system)
		self.assertEqual(score, 201)
示例#23
0
    def test_20_launch(self):
        radl_data = """
            network net (outbound='yes' and outports='8899/tcp,22/tcp-22/tcp')
            system test (
            cpu.arch='x86_64' and
            cpu.count>=1 and
            memory.size>=512m and
            #disks.free_size>=2g and
            net_interface.0.connection = 'net' and
            net_interface.0.dns_name = 'test' and
            disk.0.os.flavour='ubuntu' and
            disk.0.os.version>='12.04' and
            disk.0.os.credentials.new.password = '******' and
            #disk.0.os.flavour='centos' and
            #disk.0.os.version>='6' and
            disk.1.size=1GB and
            disk.1.device='hdb' and
            disk.1.mount_path='/mnt/path'
            )"""
        radl = radl_parse.parse_radl(radl_data)
        radl.check()
        radl_system = radl.systems[0]

        for cloud_id, cloud in cloud_list.items():
            if self.connectors_to_test == "all" or cloud_id in self.connectors_to_test:
                systems = self.concrete_systems_with_vmrc(radl_system)
                concrete_systems = []
                for s in systems:
                    concrete_systems.extend(cloud.concreteSystem(s, auth))
                self.assertTrue(len(concrete_systems) > 0, msg="ERROR: no system returned by concreteSystems for cloud: " + cloud_id)
    
                launch_radl = radl.clone()
                launch_radl.systems = [concrete_systems[0]]
                res = cloud.launch(InfrastructureInfo(), launch_radl, launch_radl, 1, auth)
                for success, vm in res:
                    self.assertTrue(success, msg="ERROR: launching a VM for cloud: " + cloud_id)
                    self.__class__.vm_list.append(vm) 
示例#24
0
文件: TestIM.py 项目: lxhiguera/im
    def test_70_create_cloud_init(self):
        """
        Test the CreateInfrastructure IM function
        """
        radl = """
			network net ()

			system node (
             cpu.arch='x86_64' and
             cpu.count>=1 and
             memory.size>=512m and
             net_interface.0.connection = 'net' and
             disk.0.os.flavour='ubuntu' and
             disk.0.os.version>='12.04'
            )

            deploy node 1

			configure node (
@begin
#!/bin/bash
echo "Hello World" >> /tmp/data.txt
@end
			)

			contextualize (
              system node configure node with cloud_init
            )
            """

        a = radl_parse.parse_radl(radl)
        (success, inf_id) = self.server.CreateInfrastructure(radl, self.auth_data)
        self.assertTrue(success, msg="ERROR calling CreateInfrastructure: " + str(inf_id))
        self.__class__.inf_id = inf_id

        all_configured = self.wait_inf_state(self.inf_id, VirtualMachine.CONFIGURED, 300)
        self.assertTrue(all_configured, msg="ERROR waiting the infrastructure to be configured (timeout).")
示例#25
0
	def AddResource(inf_id, radl_data, auth, context = True, failed_clouds = []):
		"""
		Add the resources in the RADL to the infrastructure.

		Args:

		- inf_id(str): infrastructure id.
		- radl(str): RADL description.
		- auth(Authentication): parsed authentication tokens.
		- context(bool): Flag to specify if the ctxt step will be made
		- failed_clouds(list of CloudInfo): A list of failed Cloud providers to avoid launching the VMs in them.

		Return(list of int): ids of the new virtual machine created.
		"""

		InfrastructureManager.logger.info("Adding resources to inf: " + str(inf_id))
		
		if isinstance(radl_data, RADL):
			radl = radl_data
		else:
			radl = radl_parse.parse_radl(radl_data)
		
		InfrastructureManager.logger.debug(radl)
		radl.check()
		
		sel_inf = InfrastructureManager.get_infrastructure(inf_id, auth)
		
		# Update infrastructure RADL with this new RADL
		sel_inf.complete_radl(radl)

		# If any deploy is defined, only update definitions.
		if not radl.deploys:
			sel_inf.update_radl(radl, [])
			InfrastructureManager.logger.warn("Infrastructure without any deploy. Exiting.")
			return []

		for system in radl.systems:
			# Add apps requirements to the RADL
			apps_to_install = system.getApplications()
			for app_to_install in apps_to_install:
				for app_avail, _, _, _, requirements in Recipe.getInstallableApps():
					if requirements and app_avail.isNewerThan(app_to_install):
						# This app must be installed and it has special requirements
						try:
							requirements_radl = radl_parse.parse_radl(requirements).systems[0]
							system.applyFeatures(requirements_radl, conflict="other", missing="other")
						except Exception:
							InfrastructureManager.logger.exception("Error in the requirements of the app: " + app_to_install.getValue("name") + ". Ignore them.")
							InfrastructureManager.logger.debug(requirements)
						break				

		# Get VMRC credentials
		vmrc_list = []
		for vmrc_elem in auth.getAuthInfo('VMRC'):
			if ('host' in vmrc_elem and 'username' in vmrc_elem and
			    'password' in vmrc_elem):
				vmrc_list.append(VMRC(vmrc_elem['host'], vmrc_elem['username'],
				                      vmrc_elem['password']))

		# Concrete systems using VMRC
		# NOTE: consider not-fake deploys (vm_number > 0)
		systems_with_vmrc = {}
		for system_id in set([ d.id for d in radl.deploys if d.vm_number > 0 ]):
			s = radl.get_system_by_name(system_id)
			
			if not s.getValue("disk.0.image.url") and len(vmrc_list) == 0:
				raise Exception("No correct VMRC auth data provided nor image URL")
			
			# Remove the requested apps from the system
			s_without_apps = radl.get_system_by_name(system_id).clone()
			s_without_apps.delValue("disk.0.applications")
			
			# Set the default values for cpu, memory
			defaults = (Feature("cpu.count", ">=", Config.DEFAULT_VM_CPUS),
			            Feature("memory.size", ">=", Config.DEFAULT_VM_MEMORY,
			                    Config.DEFAULT_VM_MEMORY_UNIT),
			            Feature("cpu.arch", "=", Config.DEFAULT_VM_CPU_ARCH))
			for f in defaults:
				if not s_without_apps.hasFeature(f.prop, check_softs=True):
					s_without_apps.addFeature(f)

			vmrc_res = [ s0 for vmrc in vmrc_list for s0 in vmrc.search_vm(s) ]
			# Check that now the image URL is in the RADL
			if not s.getValue("disk.0.image.url") and not vmrc_res:
				raise Exception("No VMI obtained from VMRC to system: " + system_id)
			
			n = [ s_without_apps.clone().applyFeatures(s0, conflict="other", missing="other")
			                         for s0 in vmrc_res ]
			systems_with_vmrc[system_id] = n if n else [s_without_apps]

		# Concrete systems with cloud providers and select systems with the greatest score
		# in every cloud
		cloud_list = dict([ (c.id, c.getCloudConnector()) for c in CloudInfo.get_cloud_list(auth) if c not in failed_clouds ])
		concrete_systems = {}
		for cloud_id, cloud in cloud_list.items():
			for system_id, systems in systems_with_vmrc.items():				
				s1 = [InfrastructureManager._compute_score(s.clone().applyFeatures(s0, conflict="other", missing="other").concrete(), radl.get_system_by_name(system_id))
				                for s in systems for s0 in cloud.concreteSystem(s, auth)]
				# Store the concrete system with largest score
				concrete_systems.setdefault(cloud_id, {})[system_id] = (
					max(s1, key=lambda x: x[1]) if s1 else (None, -1e9) )

		# Group virtual machines to deploy by network dependencies
		deploy_groups = InfrastructureManager._compute_deploy_groups(radl)
		InfrastructureManager.logger.debug("Groups of VMs with dependencies")
		InfrastructureManager.logger.debug(deploy_groups)

		# Sort by score the cloud providers
		# NOTE: consider fake deploys (vm_number == 0)
		deploys_group_cloud_list = {}
		for deploy_group in deploy_groups:
			suggested_cloud_ids = list(set([ d.cloud_id for d in deploy_group if d.cloud_id ]))
			if len(suggested_cloud_ids) > 1:
				raise Exception("Two deployments that have to be launched in the same cloud provider are asked to be deployed in different cloud providers: %s" % deploy_group)
			elif len(suggested_cloud_ids) == 1:
				if suggested_cloud_ids[0] not in cloud_list:
					InfrastructureManager.logger.debug("Cloud Provider list:")
					InfrastructureManager.logger.debug(cloud_list)
					raise Exception("No auth data for cloud with ID: %s" % suggested_cloud_ids[0])
				else:
					cloud_list0 = [ (suggested_cloud_ids[0], cloud_list[suggested_cloud_ids[0]]) ]
			else:
				cloud_list0 = cloud_list.items()
			if d.vm_number:
				scored_clouds = [ (cloud_id, sum([ d.vm_number*concrete_systems[cloud_id][d.id][1]
			                         for d in deploy_group ])) for cloud_id, _ in cloud_list0 ]
			else:
				scored_clouds = [ (cloud_id, 1) for cloud_id, _ in cloud_list0 ]
			
			ordered_cloud_list = [ c.id for c in CloudInfo.get_cloud_list(auth) ]
			# reverse the list to use the reverse order in the sort function
			ordered_cloud_list.reverse()
			# Order the clouds first by the score and then using the cloud order in the auth data 
			sorted_scored_clouds = sorted(scored_clouds, key=lambda x: (x[1], ordered_cloud_list.index(x[0])), reverse=True)
			deploys_group_cloud_list[id(deploy_group)] = [ c[0] for c in sorted_scored_clouds ]

		# Launch every group in the same cloud provider
		deployed_vm = {}
		cancel_deployment = []
		try:
			if Config.MAX_SIMULTANEOUS_LAUNCHES > 1:
				pool = ThreadPool(processes=Config.MAX_SIMULTANEOUS_LAUNCHES)
				pool.map(
					lambda ds: InfrastructureManager._launch_group(sel_inf,
						ds, deploys_group_cloud_list[id(ds)], cloud_list, concrete_systems,
						radl, auth, deployed_vm, cancel_deployment), deploy_groups)
			else:
				for ds in deploy_groups:
					InfrastructureManager._launch_group(sel_inf,
						ds, deploys_group_cloud_list[id(ds)], cloud_list, concrete_systems,
						radl, auth, deployed_vm, cancel_deployment)
		except Exception, e:
			# Please, avoid exception to arrive to this level, because some virtual
			# machine may lost.
			cancel_deployment.append(e)
示例#26
0
文件: TestRADL.py 项目: lxhiguera/im
	def test_dup_features(self):

		radl = """
system main (
cpu.count>=1 and
cpu.count<=0
)		"""

		with self.assertRaises(RADLParseException) as ex:
			parse_radl(radl)
		self.assertEqual(ex.exception.line, 4)

		radl = """
system main (
cpu.count=1 and
cpu.count=2
)		"""

		with self.assertRaises(RADLParseException) as ex:
			parse_radl(radl)
		self.assertEqual(ex.exception.line, 4)

		radl = """
system main (
cpu.count>=1 and
cpu.count>=5 and
cpu.count>=0
)		"""

		parse_radl(radl)

		radl = """
system main (
cpu.count=1 and
cpu.count>=0
)		"""

		parse_radl(radl)

		radl = """
system main (
cpu.count>=1 and
cpu.count<=5
)		"""

		parse_radl(radl)

		radl = """
system main (
cpu.count>=5 and
cpu.count<=5
)		"""

		parse_radl(radl)