Example #1
0
File: EC2.py Project: lxhiguera/im
	def launch(self, inf, radl, requested_radl, num_vm, auth_data):
		
		im_username = "******"
		if auth_data.getAuthInfo('InfrastructureManager'):
			im_username = auth_data.getAuthInfo('InfrastructureManager')[0]['username']

		system = radl.systems[0]
		
		user_data = self.get_cloud_init_data(radl)
		
		# Currently EC2 plugin uses first private_key credentials
		if system.getValue('disk.0.os.credentials.private_key'):
			system.delValue('disk.0.os.credentials.password')
		
		(region_name, ami) = self.getAMIData(system.getValue("disk.0.image.url"))
		
		self.logger.debug("Connecting with the region: " + region_name)
		conn = self.get_connection(region_name, auth_data)
		
		res = []
		if not conn:
			for i in range(num_vm):
				res.append((False, "Error connecting with EC2, check the credentials"))
			return res
		
		image = conn.get_image(ami)
		
		if not image:
			for i in range(num_vm):
				res.append((False, "Incorrect AMI selected"))
			return res
		else:
			block_device_name = None
			for name, device in image.block_device_mapping.iteritems():
				if device.snapshot_id or device.volume_id:
					block_device_name = name

			if not block_device_name:
				self.logger.error("Error getting correct block_device name from AMI: " + str(ami))
				for i in range(num_vm):
					res.append((False, "Error getting correct block_device name from AMI: " + str(ami)))
				return res
			
			# Create the security group for the VMs
			provider_id = self.get_net_provider_id(radl)
			if provider_id:
				vpc, subnet = provider_id
				sg_names = None
				sg_ids = self.create_security_group(conn, inf, radl, vpc)
				if not sg_ids:
					vpc = None
					subnet = None
					sg_ids = None
					sg_names = ['default']
			else:
				# Check the default VPC and get the first subnet with a connection with a gateway
				# If there are no default VPC, use EC2-classic
				vpc, subnet = self.get_default_subnet(conn)
				if vpc:
					self.set_net_provider_id(radl, vpc, subnet)
					sg_names = None
					sg_ids = self.create_security_group(conn, inf, radl, vpc)
				else:
					sg_ids = None
					sg_names = self.create_security_group(conn, inf, radl, vpc)
					if not sg_names:
						sg_names = ['default']
			
			# Now create the keypair
			(created_keypair, keypair_name) = self.create_keypair(system, conn)
			if not keypair_name:
				self.logger.error("Error managing the keypair.")
				for i in range(num_vm):
					res.append((False, "Error managing the keypair."))
				return res
			
			all_failed = True

			i = 0
			while i < num_vm:
				try:
					spot = False
					if system.getValue("spot") == "yes":
						spot = True
	
					if spot:
						self.logger.debug("Launching a spot instance")
						instance_type = self.get_instance_type(system)
						if not instance_type:
							self.logger.error("Error launching the VM, no instance type available for the requirements.")
							self.logger.debug(system)
							res.append((False, "Error launching the VM, no instance type available for the requirements."))
						else:
							price = system.getValue("price")
							#Realizamos el request de spot instances
							if system.getValue("disk.0.os.name"):
								operative_system = system.getValue("disk.0.os.name")
								if operative_system == "linux":
									operative_system = 'Linux/UNIX'
									#TODO: diferenciar entre cuando sea 'Linux/UNIX', 'SUSE Linux' o 'Windows' teniendo en cuenta tambien el atributo "flavour" del RADL
							else:
								res.append((False, "Error launching the image: spot instances need the OS defined in the RADL"))
								#operative_system = 'Linux/UNIX'
							
							if system.getValue('availability_zone'):
								availability_zone = system.getValue('availability_zone')
							else:
								availability_zone = 'us-east-1c'
								historical_price = 1000.0
								availability_zone_list = conn.get_all_zones()
								for zone in availability_zone_list:
									history = conn.get_spot_price_history(instance_type=instance_type.name, product_description=operative_system, availability_zone=zone.name, max_results=1)
									self.logger.debug("Spot price history for the region " + zone.name)
									self.logger.debug(history)
									if history and history[0].price < historical_price:
										historical_price = history[0].price
										availability_zone = zone.name
							self.logger.debug("Launching the spot request in the zone " + availability_zone)
							
							# Force to use magnetic volumes
							bdm = boto.ec2.blockdevicemapping.BlockDeviceMapping(conn)
							bdm[block_device_name] = boto.ec2.blockdevicemapping.BlockDeviceType(volume_type="standard")
							request = conn.request_spot_instances(price=price, image_id=image.id, count=1, type='one-time', instance_type=instance_type.name, placement=availability_zone, key_name=keypair_name, security_groups=sg_names, security_group_ids=sg_ids, block_device_map=bdm, subnet_id=subnet, user_data=user_data)
							
							if request:
								ec2_vm_id = region_name + ";" + request[0].id
								
								self.logger.debug("RADL:")
								self.logger.debug(system)
							
								vm = VirtualMachine(inf, ec2_vm_id, self.cloud, radl, requested_radl, self)
								vm.info.systems[0].setValue('instance_id', str(vm.id))
								# Add the keypair name to remove it later 
								vm.keypair_name = keypair_name
								self.logger.debug("Instance successfully launched.")
								all_failed = False
								res.append((True, vm))
							else: 
								res.append((False, "Error launching the image"))
							
					else:
						self.logger.debug("Launching ondemand instance")
						instance_type = self.get_instance_type(system)
						if not instance_type:
							self.logger.error("Error launching the VM, no instance type available for the requirements.")
							self.logger.debug(system)
							res.append((False, "Error launching the VM, no instance type available for the requirements."))
						else:
							placement = system.getValue('availability_zone')
							# Force to use magnetic volumes
							bdm = boto.ec2.blockdevicemapping.BlockDeviceMapping(conn)
							bdm[block_device_name] = boto.ec2.blockdevicemapping.BlockDeviceType(volume_type="standard")
							# Check if the user has specified the net provider id 
							reservation = image.run(min_count=1,max_count=1,key_name=keypair_name,instance_type=instance_type.name,security_groups=sg_names,security_group_ids=sg_ids,placement=placement,block_device_map=bdm,subnet_id=subnet,user_data=user_data)
		
							if len(reservation.instances) == 1:
								instance = reservation.instances[0]
								instance.add_tag("IM-USER", im_username)
								ec2_vm_id = region_name + ";" + instance.id

								self.logger.debug("RADL:")
								self.logger.debug(system)
								
								vm = VirtualMachine(inf, ec2_vm_id, self.cloud, radl, requested_radl, self)
								vm.info.systems[0].setValue('instance_id', str(vm.id))
								# Add the keypair name to remove it later 
								vm.keypair_name = keypair_name
								self.logger.debug("Instance successfully launched.")
								res.append((True, vm))
								all_failed = False
							else:
								res.append((False, "Error launching the image"))
					
				except Exception, ex:
					self.logger.exception("Error launching instance.")
					res.append((False, "Error launching the instance: " + str(ex)))
					
				i += 1
Example #2
0
    def test_60_finalize(self, sleep, get_connection):
        radl_data = """
            network net (outbound = 'yes')
            system test (
            cpu.arch='x86_64' and
            cpu.count=1 and
            memory.size=512m and
            net_interface.0.connection = 'net' and
            net_interface.0.ip = '158.42.1.1' and
            net_interface.0.dns_name = 'test' and
            disk.0.os.name = 'linux' and
            disk.0.image.url = 'one://server.com/1' and
            disk.0.os.credentials.username = '******' and
            disk.0.os.credentials.password = '******' and
            disk.1.size=1GB and
            disk.1.device='hdb' and
            disk.1.mount_path='/mnt/path'
            )"""
        radl = radl_parse.parse_radl(radl_data)

        auth = Authentication([{
            'id': 'ec2',
            'type': 'EC2',
            'username': '******',
            'password': '******'
        }])
        ec2_cloud = self.get_ec2_cloud()

        inf = MagicMock()
        inf.id = "1"
        inf.get_next_vm_id.return_value = 1
        vm = VirtualMachine(inf, "us-east-1;id-1", ec2_cloud.cloud, radl, radl,
                            ec2_cloud)
        vm.keypair_name = "key"

        conn = MagicMock()
        get_connection.return_value = conn

        reservation = MagicMock()
        instance = MagicMock()
        device = MagicMock()
        instance.update.return_value = True
        instance.terminate.return_value = True
        instance.block_device_mapping = {"device": device}
        device.volume_id = "volid"
        reservation.instances = [instance]
        conn.get_all_instances.return_value = [reservation]

        conn.delete_key_pair.return_value = True

        address = MagicMock()
        address.public_ip = "158.42.1.1"
        address.instance_id = "id-1"
        address.disassociate.return_value = True
        address.release.return_value = True
        conn.get_all_addresses.return_value = [address]

        conn.get_all_spot_instance_requests.return_value = []

        volume = MagicMock()
        volume.attachment_state.return_value = None
        conn.get_all_volumes.return_value = [volume]
        conn.delete_volume.return_value = True

        sg = MagicMock()
        sg.name = "im-1"
        sg.instances.return_value = []
        sg.revoke.return_value = True
        sg.delete.return_value = True
        conn.get_all_security_groups.return_value = [sg]

        success, _ = ec2_cloud.finalize(vm, auth)

        self.assertTrue(success, msg="ERROR: finalizing VM info.")
        self.assertNotIn("ERROR",
                         self.log.getvalue(),
                         msg="ERROR found in log: %s" % self.log.getvalue())
Example #3
0
File: EC2.py Project: indigo-dc/im
    def test_60_finalize(self, sleep, get_connection):
        radl_data = """
            network net (outbound = 'yes')
            system test (
            cpu.arch='x86_64' and
            cpu.count=1 and
            memory.size=512m and
            net_interface.0.connection = 'net' and
            net_interface.0.ip = '158.42.1.1' and
            net_interface.0.dns_name = 'test' and
            disk.0.os.name = 'linux' and
            disk.0.image.url = 'one://server.com/1' and
            disk.0.os.credentials.username = '******' and
            disk.0.os.credentials.password = '******' and
            disk.1.size=1GB and
            disk.1.device='hdb' and
            disk.1.mount_path='/mnt/path'
            )"""
        radl = radl_parse.parse_radl(radl_data)

        auth = Authentication([{'id': 'ec2', 'type': 'EC2', 'username': '******', 'password': '******'}])
        ec2_cloud = self.get_ec2_cloud()

        inf = MagicMock()
        inf.id = "1"
        inf.get_next_vm_id.return_value = 1
        vm = VirtualMachine(inf, "us-east-1;id-1", ec2_cloud.cloud, radl, radl, ec2_cloud)
        vm.keypair_name = "key"

        conn = MagicMock()
        get_connection.return_value = conn

        reservation = MagicMock()
        instance = MagicMock()
        device = MagicMock()
        instance.update.return_value = True
        instance.terminate.return_value = True
        instance.block_device_mapping = {"device": device}
        device.volume_id = "volid"
        reservation.instances = [instance]
        conn.get_all_instances.return_value = [reservation]

        conn.delete_key_pair.return_value = True

        address = MagicMock()
        address.public_ip = "158.42.1.1"
        address.instance_id = "id-1"
        address.disassociate.return_value = True
        address.release.return_value = True
        conn.get_all_addresses.return_value = [address]

        conn.get_all_spot_instance_requests.return_value = []

        volume = MagicMock()
        volume.attachment_state.return_value = None
        conn.get_all_volumes.return_value = [volume]
        conn.delete_volume.return_value = True

        sg = MagicMock()
        sg.name = "im-1"
        sg.instances.return_value = []
        sg.revoke.return_value = True
        sg.delete.return_value = True
        conn.get_all_security_groups.return_value = [sg]

        success, _ = ec2_cloud.finalize(vm, auth)

        self.assertTrue(success, msg="ERROR: finalizing VM info.")
        self.assertNotIn("ERROR", self.log.getvalue(), msg="ERROR found in log: %s" % self.log.getvalue())
        self.clean_log()