Example #1
0
	def gen_launch_res(self, inf, radl, requested_radl, num_vm, auth_data):
		res = []
		for i in range(num_vm):
			cloud = CloudInfo()
			cloud.type = "DeployedNode"
			vm = VirtualMachine(inf, "1234", cloud, radl, requested_radl)
			# create the mock for the vm finalize function
			vm.finalize = Mock(return_value=(True, vm))
			res.append((True, vm))
		return res
Example #2
0
	def launch(self, inf, radl, requested_radl, num_vm, auth_data):
		res = []
		for _ in range(num_vm):
			now = str(int(time.time()*100))
			vm = VirtualMachine(inf, now, self.cloud, requested_radl, requested_radl)
			vm.info.systems[0].setValue('provider.type', self.type)
			vm.state = VirtualMachine.RUNNING
			res.append((True, vm))
		
		return res
Example #3
0
 def gen_launch_res(self, inf, radl, requested_radl, num_vm, auth_data):
     res = []
     for _ in range(num_vm):
         cloud = CloudInfo()
         cloud.type = "Dummy"
         vm = VirtualMachine(inf, "1234", cloud, radl, requested_radl)
         vm.get_ssh = Mock(side_effect=self.get_dummy_ssh)
         vm.state = VirtualMachine.RUNNING
         res.append((True, vm))
     return res
Example #4
0
    def test_60_finalize(self, sleep, get_driver):
        auth = Authentication([{'id': 'ost', 'type': 'OpenStack', 'username': '******',
                                'password': '******', 'tenant': 'tenant', 'host': 'https://server.com:5000'}])
        ost_cloud = self.get_ost_cloud()

        radl_data = """
            system test (
            cpu.count>=2 and
            memory.size>=2048m
            )"""
        radl = radl_parse.parse_radl(radl_data)

        inf = MagicMock()
        inf.get_next_vm_id.return_value = 1
        vm = VirtualMachine(inf, "1", ost_cloud.cloud, radl, radl, ost_cloud)

        driver = MagicMock()
        driver.name = "OpenStack"
        get_driver.return_value = driver

        node = MagicMock()
        node.id = "1"
        node.state = "running"
        node.extra = {'flavorId': 'small'}
        node.public_ips = ['158.42.1.1']
        node.private_ips = ['10.0.0.1']
        node.driver = driver
        node.destroy.return_value = True
        driver.list_nodes.return_value = [node]

        sg = MagicMock()
        sg.id = sg.name = "sg1"
        driver.ex_get_node_security_groups.return_value = [sg]

        keypair = MagicMock()
        driver.get_key_pair.return_value = keypair
        vm.keypair = keypair

        driver.delete_key_pair.return_value = True

        driver.delete_security_group.return_value = True

        driver.ex_list_floating_ips.return_value = []

        success, _ = ost_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()
Example #5
0
    def test_60_finalize(self, get_driver):
        auth = Authentication([{'id': 'libcloud', 'type': 'LibCloud', 'username': '******',
                                'password': '******', 'driver': 'EC2'}])
        lib_cloud = self.get_lib_cloud()

        radl_data = """
            system test (
            cpu.count>=2 and
            memory.size>=2048m
            )"""
        radl = radl_parse.parse_radl(radl_data)

        inf = MagicMock()
        inf.get_next_vm_id.return_value = 1
        vm = VirtualMachine(inf, "1", lib_cloud.cloud, radl, radl, lib_cloud)
        vm.keypair = ""

        driver = MagicMock()
        driver.name = "Amazon EC2"
        get_driver.return_value = driver

        node = MagicMock()
        node.id = "1"
        node.state = "running"
        node.driver = driver
        node.destroy.return_value = True
        driver.list_nodes.return_value = [node]

        sg = MagicMock()
        sg.id = sg.name = "sg1"
        driver.ex_get_node_security_groups.return_value = [sg]

        keypair = MagicMock()
        driver.get_key_pair.return_value = keypair
        vm.keypair = keypair
        volume = MagicMock()
        volume.id = "id"
        vm.volumes = [volume]

        driver.delete_key_pair.return_value = True

        driver.ex_describe_addresses_for_node.return_value = ["ip"]
        driver.ex_disassociate_address.return_value = True

        success, _ = lib_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()
Example #6
0
	def launch(self, inf, radl, requested_radl, num_vm, auth_data):
		res = []
		for _ in range(num_vm):
			now = str(int(time.time()*100))
			vm = VirtualMachine(inf, now, self.cloud, requested_radl, requested_radl)
			
			vm.info.systems[0].setValue('provider.type', self.type)
			vm.state = VirtualMachine.RUNNING
			
			vm.info.systems[0].setValue("net_interface.0.ip","10.0.0.1")
			vm.info.systems[0].setValue("disk.0.os.credentials.username", "username")
			vm.info.systems[0].setValue("disk.0.os.credentials.password", "password")
			
			res.append((True, vm))
		
		return res
Example #7
0
 def test_tosca_get_outputs(self):
     """Test TOSCA get_outputs function"""
     tosca_data = read_file_as_string('../files/tosca_create.yml')
     tosca = Tosca(tosca_data)
     _, radl = tosca.to_radl()
     radl.systems[0].setValue("net_interface.0.ip", "158.42.1.1")
     radl.systems[0].setValue("disk.0.os.credentials.username", "ubuntu")
     radl.systems[0].setValue("disk.0.os.credentials.password", "pass")
     inf = InfrastructureInfo()
     vm = VirtualMachine(inf, "1", None, radl, radl, None)
     vm.requested_radl = radl
     inf.vm_list = [vm]
     outputs = tosca.get_outputs(inf)
     self.assertEqual(outputs, {'server_url': ['158.42.1.1'],
                                'server_creds': {'token_type': 'password',
                                                 'token': 'pass',
                                                 'user': '******'}})
Example #8
0
    def launch(self, inf, radl, requested_radl, num_vm, auth_data):
        driver = self.get_driver(auth_data)

        system = radl.systems[0]
        image_id = self.get_image_id(system.getValue("disk.0.image.url"))
        image = NodeImage(id=image_id, name=None, driver=driver)

        instance_type = self.get_instance_type(driver.list_sizes(), system)

        name = system.getValue("instance_name")
        if not name:
            name = system.getValue("disk.0.image.name")
        if not name:
            name = "userimage"

        args = {
            'size': instance_type,
            'image': image,
            'name': "%s-%s" % (name, int(time.time() * 100))
        }

        keypair = None
        public_key = system.getValue("disk.0.os.credentials.public_key")
        if self.driver_uses_keypair(driver):
            if public_key:
                keypair = driver.get_key_pair(public_key)
                if keypair:
                    system.setUserKeyCredentials(
                        system.getCredentials().username, None,
                        keypair.private_key)
                else:
                    if "ssh_key" in driver.features.get("create_node", []):
                        args["auth"] = NodeAuthSSHKey(public_key)
                    else:
                        args["ex_keyname"] = keypair.name
            elif not system.getValue("disk.0.os.credentials.password"):
                keypair_name = "im-%d" % int(time.time() * 100.0)
                keypair = driver.create_key_pair(keypair_name)
                system.setUserKeyCredentials(system.getCredentials().username,
                                             None, keypair.private_key)

                if keypair.public_key and "ssh_key" in driver.features.get(
                        "create_node", []):
                    args["auth"] = NodeAuthSSHKey(keypair.public_key)
                else:
                    args["ex_keyname"] = keypair_name

        res = []
        i = 0
        while i < num_vm:
            self.log_debug("Creating node")

            node = driver.create_node(**args)

            if node:
                vm = VirtualMachine(inf, node.id, self.cloud, radl,
                                    requested_radl,
                                    self.cloud.getCloudConnector(inf))
                vm.info.systems[0].setValue('instance_id', str(node.id))
                vm.info.systems[0].setValue('instance_name', str(node.name))
                # Add the keypair name to remove it later
                vm.keypair = keypair_name
                self.log_debug("Node successfully created.")
                res.append((True, vm))
            else:
                res.append((False, "Error creating the node"))

            i += 1

        return res
Example #9
0
    def launch(self, inf, radl, requested_radl, num_vm, auth_data):
        system = radl.systems[0]

        public_net = None
        for net in radl.networks:
            if net.isPublic():
                public_net = net

        outports = None
        if public_net:
            outports = public_net.getOutPorts()

        apiVersion = self.get_api_version(auth_data)

        res = []
        namespace = inf.id
        headers = {'Content-Type': 'application/json'}
        uri = "/api/" + apiVersion + "/namespaces"
        with inf._lock:
            resp = self.create_request('GET', uri + "/" + namespace, auth_data,
                                       headers)
            if resp.status_code != 200:
                namespace_data = {
                    'apiVersion': apiVersion,
                    'kind': 'Namespace',
                    'metadata': {
                        'name': namespace
                    }
                }
                body = json.dumps(namespace_data)
                resp = self.create_request('POST', uri, auth_data, headers,
                                           body)

                if resp.status_code != 201:
                    for _ in range(num_vm):
                        res.append(
                            (False,
                             "Error creating the Namespace: " + resp.text))
                        return res

        i = 0
        while i < num_vm:
            try:
                i += 1

                vm = VirtualMachine(inf, None, self.cloud, radl,
                                    requested_radl, self)
                (nodename, _) = vm.getRequestedName(
                    default_hostname=Config.DEFAULT_VM_NAME,
                    default_domain=Config.DEFAULT_DOMAIN)
                pod_name = nodename

                # Do not use the Persistent volumes yet
                volumes = self._create_volumes(apiVersion, namespace, system,
                                               pod_name, auth_data)

                ssh_port = (KubernetesCloudConnector._port_base_num +
                            KubernetesCloudConnector._port_counter) % 65535
                KubernetesCloudConnector._port_counter += 1
                pod_data = self._generate_pod_data(apiVersion, namespace,
                                                   pod_name, outports, system,
                                                   ssh_port, volumes)
                body = json.dumps(pod_data)

                uri = "/api/" + apiVersion + "/namespaces/" + namespace + "/pods"
                resp = self.create_request('POST', uri, auth_data, headers,
                                           body)

                if resp.status_code != 201:
                    res.append(
                        (False, "Error creating the Container: " + resp.text))
                else:
                    output = json.loads(resp.text)
                    vm.id = output["metadata"]["name"]
                    # Set SSH port in the RADL info of the VM
                    vm.setSSHPort(ssh_port)
                    # Set the default user and password to access the container
                    vm.info.systems[0].setValue(
                        'disk.0.os.credentials.username', 'root')
                    vm.info.systems[0].setValue(
                        'disk.0.os.credentials.password', self._root_password)
                    vm.info.systems[0].setValue('instance_id', str(vm.id))
                    vm.info.systems[0].setValue('instance_name', str(vm.id))

                    res.append((True, vm))

            except Exception as ex:
                self.log_exception(
                    "Error connecting with Kubernetes API server")
                res.append((False, "ERROR: " + str(ex)))

        return res
Example #10
0
File: GCE.py Project: bbsyaya/im
    def test_30_updateVMInfo(self, get_driver):
        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.dns_name = 'test' and
            disk.0.os.name = 'linux' and
            disk.0.image.url = 'gce://us-central1-a/centos-6' and
            disk.0.os.credentials.username = '******' 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()

        auth = Authentication([{
            'id': 'one',
            'type': 'GCE',
            'username': '******',
            'password': '******',
            'project': 'proj'
        }])
        gce_cloud = self.get_gce_cloud()

        inf = MagicMock()
        vm = VirtualMachine(inf, "1", gce_cloud.cloud, radl, radl, gce_cloud,
                            1)

        driver = MagicMock()
        get_driver.return_value = driver

        node = MagicMock()
        zone = MagicMock()
        node.id = "1"
        node.state = "running"
        node.extra = {'flavorId': 'small'}
        node.public_ips = []
        node.public_ips = ['158.42.1.1']
        node.private_ips = ['10.0.0.1']
        node.driver = driver
        zone.name = 'us-central1-a'
        node.extra = {'zone': zone}
        node.size = NodeSize("1", "name1", 512, 1, None, None, driver)
        driver.ex_get_node.return_value = node

        volume = MagicMock()
        volume.id = "vol1"
        volume.attach.return_value = True
        volume.extra = {'status': 'READY'}
        driver.create_volume.return_value = volume

        success, vm = gce_cloud.updateVMInfo(vm, auth)

        self.assertTrue(success, msg="ERROR: updating VM info.")
        self.assertNotIn("ERROR",
                         self.log.getvalue(),
                         msg="ERROR found in log: %s" % self.log.getvalue())
Example #11
0
    def create_vms(self, inf, radl, requested_radl, num_vm, location, storage_account_name,
                   subnets, credentials, subscription_id):
        """
        Creates a set of VMs
        """
        resource_client = ResourceManagementClient(credentials, subscription_id)
        vms = []
        i = 0
        while i < num_vm:
            uid = str(uuid.uuid1())

            vm_name = radl.systems[0].getValue("instance_name")
            if vm_name:
                vm_name = "%s-%s" % (vm_name, uid)
            else:
                vm_name = "userimage-%s" % uid

            group_name = "rg-%s" % (vm_name)

            try:
                tags = {}
                if radl.systems[0].getValue('instance_tags'):
                    keypairs = radl.systems[0].getValue('instance_tags').split(",")
                    for keypair in keypairs:
                        parts = keypair.split("=")
                        key = parts[0].strip()
                        value = parts[1].strip()
                        tags[key] = value

                args = {'location': location}
                if tags:
                    args['tags'] = tags

                # Create resource group for the VM
                resource_client.resource_groups.create_or_update(group_name, args)
                compute_client = ComputeManagementClient(credentials, subscription_id)

                vm = VirtualMachine(inf, group_name + '/' + vm_name, self.cloud, radl, requested_radl, self)
                vm.info.systems[0].setValue('instance_id', group_name + '/' + vm_name)

                nics = self.create_nics(radl, credentials, subscription_id, group_name, subnets)

                custom_data = self.get_cloud_init_data(radl, vm)
                instance_type = self.get_instance_type(radl.systems[0], credentials, subscription_id)
                vm_parameters = self.get_azure_vm_create_json(group_name, storage_account_name, vm_name,
                                                              nics, radl, instance_type, custom_data,
                                                              compute_client)

                async_vm_creation = compute_client.virtual_machines.create_or_update(group_name,
                                                                                     vm_name,
                                                                                     vm_parameters)

                self.log_info("VM ID: %s created." % vm.id)
                inf.add_vm(vm)
                vms.append((True, (vm, async_vm_creation)))
            except Exception as ex:
                vms.append((False, "Error creating the VM: %s" % str(ex)))
                self.log_exception("Error creating the VM")

                # Delete Resource group and everything in it
                if group_name:
                    self.delete_resource_group(group_name, resource_client)

            i += 1

        return vms
Example #12
0
    def launch(self, inf, radl, requested_radl, num_vm, auth_data):
        driver = self.get_driver(auth_data)

        system = radl.systems[0]
        image_id = self.get_image_id(system.getValue("disk.0.image.url"))
        image = NodeImage(id=image_id, name=None, driver=driver)

        instance_type = self.get_instance_type(driver.list_sizes(), system)

        name = system.getValue("instance_name")
        if not name:
            name = system.getValue("disk.0.image.name")
        if not name:
            name = "userimage"

        nets = self.get_networks(driver, radl)

        sgs = self.create_security_group(driver, inf, radl)

        args = {
            'size': instance_type,
            'image': image,
            'networks': nets,
            'ex_security_groups': sgs,
            'name': "%s-%s" % (name, int(time.time() * 100))
        }

        cloud_init = self.get_cloud_init_data(radl)
        if cloud_init:
            args['ex_userdata'] = cloud_init

        keypair = None
        public_key = system.getValue("disk.0.os.credentials.public_key")
        if public_key:
            keypair = driver.get_key_pair(public_key)
            if keypair:
                system.setUserKeyCredentials(system.getCredentials().username,
                                             None, keypair.private_key)
            else:
                if "ssh_key" in driver.features.get("create_node", []):
                    args["auth"] = NodeAuthSSHKey(public_key)
                else:
                    args["ex_keyname"] = keypair.name
        elif not system.getValue("disk.0.os.credentials.password"):
            keypair_name = "im-%d" % int(time.time() * 100.0)
            keypair = driver.create_key_pair(keypair_name)
            system.setUserKeyCredentials(system.getCredentials().username,
                                         None, keypair.private_key)

            if keypair.public_key and "ssh_key" in driver.features.get(
                    "create_node", []):
                args["auth"] = NodeAuthSSHKey(keypair.public_key)
            else:
                args["ex_keyname"] = keypair_name

        res = []
        i = 0
        all_failed = True
        while i < num_vm:
            self.logger.debug("Creating node")

            node = driver.create_node(**args)

            if node:
                vm = VirtualMachine(inf, node.id, self.cloud, radl,
                                    requested_radl,
                                    self.cloud.getCloudConnector())
                vm.info.systems[0].setValue('instance_id', str(node.id))
                vm.info.systems[0].setValue('instance_name', str(node.name))
                # Add the keypair name to remove it later
                vm.keypair = keypair_name
                self.logger.debug("Node successfully created.")
                all_failed = False
                res.append((True, vm))
            else:
                res.append((False, "Error creating the node"))
            i += 1

        # if all the VMs have failed, remove the sg and keypair
        if all_failed:
            if (public_key is None or len(public_key) == 0 or
                (len(public_key) >= 1
                 and public_key.find('-----BEGIN CERTIFICATE-----') != -1)):
                # only delete in case of the user do not specify the keypair
                # name
                driver.delete_key_pair(keypair)
            if sgs:
                driver.ex_delete_security_group(sgs[0])

        return res
Example #13
0
File: Azure.py Project: nakos/im
    def launch(self, inf, radl, requested_radl, num_vm, auth_data):
        location = self.DEFAULT_LOCATION
        if radl.systems[0].getValue('availability_zone'):
            location = radl.systems[0].getValue('availability_zone')
        else:
            radl.systems[0].setValue('availability_zone', location)

        credentials, subscription_id = self.get_credentials(auth_data)

        resource_client = ResourceManagementClient(credentials,
                                                   subscription_id)

        with inf._lock:
            # Create resource group for the Infrastructure
            inf_rg = None
            try:
                inf_rg = resource_client.resource_groups.get("rg-%s" % inf.id)
            except Exception:
                pass
            if not inf_rg:
                resource_client.resource_groups.create_or_update(
                    "rg-%s" % inf.id, {'location': location})

            subnets = self.create_nets(inf, radl, credentials, subscription_id,
                                       "rg-%s" % inf.id)

        res = []
        i = 0
        while i < num_vm:
            try:
                # Create the VM to get the nodename
                now = int(time.time() * 100)
                vm = VirtualMachine(inf, None, self.cloud, radl,
                                    requested_radl, self)
                group_name = "rg-%s-%d" % (inf.id, vm.im_id)
                storage_account_name = "st%d%d" % (now, vm.im_id)

                vm_name = radl.systems[0].getValue("instance_name")
                if vm_name:
                    vm_name = "%s%d" % (vm_name, now)
                else:
                    vm_name = "userimage%d" % now

                # Create resource group for the VM
                resource_client.resource_groups.create_or_update(
                    group_name, {'location': location})

                # Create storage account
                storage_account, error_msg = self.create_storage_account(
                    group_name, storage_account_name, credentials,
                    subscription_id, location)

                if not storage_account:
                    res.append((False, error_msg))
                    resource_client.resource_groups.delete(group_name)
                    break

                nics = self.create_nics(inf, radl, credentials,
                                        subscription_id, group_name, subnets)

                instance_type = self.get_instance_type(radl.systems[0],
                                                       credentials,
                                                       subscription_id)
                vm_parameters = self.get_azure_vm_create_json(
                    storage_account_name, vm_name, nics, radl, instance_type)

                compute_client = ComputeManagementClient(
                    credentials, subscription_id)
                async_vm_creation = compute_client.virtual_machines.create_or_update(
                    group_name, vm_name, vm_parameters)
                azure_vm = async_vm_creation.result()

                # Set the cloud id to the VM
                vm.id = group_name + '/' + vm_name
                vm.info.systems[0].setValue('instance_id',
                                            group_name + '/' + vm_name)

                self.attach_data_disks(vm, storage_account_name, credentials,
                                       subscription_id, location)

                res.append((True, vm))
            except Exception as ex:
                self.log_exception("Error creating the VM")
                res.append((False, "Error creating the VM: " + str(ex)))

                # Delete Resource group and everything in it
                resource_client.resource_groups.delete(group_name)

            i += 1

        return res
Example #14
0
    def test_30_updateVMInfo(self, credentials, dns_client, compute_client,
                             network_client):
        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.dns_name = 'test.domain.com' and
            disk.0.os.name = 'linux' and
            disk.0.image.url = 'azr://Canonical/UbuntuServer/16.04.0-LTS/latest' and
            disk.0.os.credentials.username = '******' and
            disk.0.os.credentials.password = '******'
            )"""
        radl = radl_parse.parse_radl(radl_data)
        radl.check()

        auth = Authentication([{
            'id': 'azure',
            'type': 'Azure',
            'subscription_id': 'subscription_id',
            'username': '******',
            'password': '******'
        }])
        azure_cloud = self.get_azure_cloud()

        inf = MagicMock()
        vm = VirtualMachine(inf, "rg0/im0", azure_cloud.cloud, radl, radl,
                            azure_cloud, 1)

        instace_type = MagicMock()
        instace_type.name = "instance_type1"
        instace_type.number_of_cores = 1
        instace_type.memory_in_mb = 1024
        instace_type.resource_disk_size_in_mb = 102400
        instace_types = [instace_type]
        cclient = MagicMock()
        compute_client.return_value = cclient
        cclient.virtual_machine_sizes.list.return_value = instace_types

        avm = MagicMock()
        avm.provisioning_state = "Succeeded"
        avm.hardware_profile.vm_size = "instance_type1"
        avm.location = "northeurope"
        status1 = MagicMock()
        status1.code = "ProvisioningState/succeeded"
        status2 = MagicMock()
        status2.code = "PowerState/running"
        avm.instance_view.statuses = [status1, status2]
        ni = MagicMock()
        ni.id = "/subscriptions/subscription-id/resourceGroups/rg0/providers/Microsoft.Network/networkInterfaces/ni-0"
        avm.network_profile.network_interfaces = [ni]
        cclient.virtual_machines.get.return_value = avm

        nclient = MagicMock()
        network_client.return_value = nclient
        ni_res = MagicMock()
        ip_conf = MagicMock()
        ip_conf.private_ip_address = "10.0.0.1"
        ip_conf.public_ip_address.id = (
            "/subscriptions/subscription-id/resourceGroups/rg0/"
            "providers/Microsoft.Network/networkInterfaces/ip-0")
        ni_res.ip_configurations = [ip_conf]
        nclient.network_interfaces.get.return_value = ni_res

        pub_ip_res = MagicMock()
        pub_ip_res.ip_address = "13.0.0.1"
        nclient.public_ip_addresses.get.return_value = pub_ip_res

        dclient = MagicMock()
        dns_client.return_value = dclient
        dclient.zones.get.return_value = None
        dclient.record_sets.get.return_value = None

        success, vm = azure_cloud.updateVMInfo(vm, auth)

        self.assertTrue(success, msg="ERROR: updating VM info.")
        self.assertEquals(dclient.zones.create_or_update.call_args_list,
                          [call('rg0', 'domain.com', {'location': 'global'})])
        self.assertEquals(
            dclient.record_sets.create_or_update.call_args_list, [
                call('rg0', 'domain.com', 'test', 'A', {
                    'arecords': [{
                        'ipv4_address': '13.0.0.1'
                    }],
                    'ttl': 300
                })
            ])
        self.assertNotIn("ERROR",
                         self.log.getvalue(),
                         msg="ERROR found in log: %s" % self.log.getvalue())
Example #15
0
    def test_55_alter(self, credentials, network_client, compute_client,
                      storage_client, resource_client):
        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.dns_name = 'test' and
            disk.0.os.name = 'linux' and
            disk.0.image.url = 'azr://image-id' and
            disk.0.os.credentials.username = '******' and
            disk.0.os.credentials.password = '******'
            )"""
        radl = radl_parse.parse_radl(radl_data)

        new_radl_data = """
            system test (
            cpu.count>=2 and
            memory.size>=2048m
            )"""
        new_radl = radl_parse.parse_radl(new_radl_data)

        auth = Authentication([{
            'id': 'azure',
            'type': 'Azure',
            'subscription_id': 'subscription_id',
            'username': '******',
            'password': '******'
        }])
        azure_cloud = self.get_azure_cloud()

        instace_type = MagicMock()
        instace_type.name = "instance_type2"
        instace_type.number_of_cores = 2
        instace_type.memory_in_mb = 2048
        instace_type.resource_disk_size_in_mb = 102400
        instace_types = [instace_type]
        cclient = MagicMock()
        compute_client.return_value = cclient
        cclient.virtual_machine_sizes.list.return_value = instace_types

        vm = MagicMock()
        vm.provisioning_state = "Succeeded"
        vm.hardware_profile.vm_size = "instance_type2"
        vm.location = "northeurope"
        ni = MagicMock()
        ni.id = "/subscriptions/subscription-id/resourceGroups/rg0/providers/Microsoft.Network/networkInterfaces/ni-0"
        vm.network_profile.network_interfaces = [ni]
        cclient.virtual_machines.get.return_value = vm

        nclient = MagicMock()
        network_client.return_value = nclient
        ni_res = MagicMock()
        ip_conf = MagicMock()
        ip_conf.private_ip_address = "10.0.0.1"
        ip_conf.public_ip_address.id = (
            "/subscriptions/subscription-id/resourceGroups/rg0/"
            "providers/Microsoft.Network/networkInterfaces/ip-0")
        ni_res.ip_configurations = [ip_conf]
        nclient.network_interfaces.get.return_value = ni_res

        pub_ip_res = MagicMock()
        pub_ip_res.ip_address = "13.0.0.1"
        nclient.public_ip_addresses.get.return_value = pub_ip_res

        inf = MagicMock()
        vm = VirtualMachine(inf, "rg0/vm0", azure_cloud.cloud, radl, radl,
                            azure_cloud, 1)

        success, _ = azure_cloud.alterVM(vm, new_radl, auth)

        self.assertTrue(success, msg="ERROR: modifying VM info.")
        self.assertNotIn("ERROR",
                         self.log.getvalue(),
                         msg="ERROR found in log: %s" % self.log.getvalue())
Example #16
0
File: Fogbow.py Project: vigial/im
    def test_30_updateVMInfo(self, sleep, requests):
        radl_data = """
            network net (outbound = 'yes' and outports = '8080,9000:9010')
            system test (
            cpu.arch='x86_64' and
            cpu.count=1 and
            memory.size=512m and
            net_interface.0.connection = 'net' and
            net_interface.0.dns_name = 'test' and
            disk.0.os.name = 'linux' and
            disk.0.image.url = 'fbw://server.com/fogbow-ubuntu' 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)
        radl.check()

        auth = Authentication([{
            'id': 'fogbow',
            'type': 'FogBow',
            'token': 'user',
            'host': 'server.com'
        }])
        fogbow_cloud = self.get_fogbow_cloud()

        inf = MagicMock()
        vm = VirtualMachine(inf, "1", fogbow_cloud.cloud, radl, radl,
                            fogbow_cloud, 1)

        requests.side_effect = self.get_response

        success, vm = fogbow_cloud.updateVMInfo(vm, auth)

        self.assertTrue(success, msg="ERROR: updating VM info.")
        self.assertNotIn("ERROR",
                         self.log.getvalue(),
                         msg="ERROR found in log: %s" % self.log.getvalue())
        self.assertEquals(vm.info.systems[0].getValue("net_interface.1.ip"),
                          "10.0.0.1")
        self.assertEquals(vm.info.systems[0].getValue("net_interface.0.ip"),
                          "8.8.8.8")
        self.assertEquals(vm.info.systems[0].getValue("memory.size"),
                          1073741824)
        self.assertEquals(vm.info.systems[0].getValue("disk.1.device"),
                          "/dev/sdb")

        data = json.loads(requests.call_args_list[1][1]["data"])
        self.assertEqual(data["computeId"], '1')
        data = json.loads(requests.call_args_list[4][1]["data"])
        self.assertEqual(
            data, {
                'direction': 'IN',
                'protocol': 'TCP',
                'etherType': 'IPv4',
                'portTo': 8080,
                'portFrom': 8080,
                'cidr': '0.0.0.0/0'
            })
        data = json.loads(requests.call_args_list[5][1]["data"])
        self.assertEqual(
            data, {
                'direction': 'IN',
                'protocol': 'TCP',
                'etherType': 'IPv4',
                'portTo': 9000,
                'portFrom': 9010,
                'cidr': '0.0.0.0/0'
            })
        data = json.loads(requests.call_args_list[6][1]["data"])
        self.assertEqual(
            data, {
                'direction': 'IN',
                'protocol': 'TCP',
                'etherType': 'IPv4',
                'portTo': 22,
                'portFrom': 22,
                'cidr': '0.0.0.0/0'
            })
        data = json.loads(requests.call_args_list[7][1]["data"])
        self.assertEqual(data["volumeSize"], 1)
        data = json.loads(requests.call_args_list[9][1]["data"])
        self.assertEqual(data, {
            "computeId": "1",
            "device": "/dev/hdb",
            "volumeId": "1"
        })
Example #17
0
    def launch(self, inf, radl, requested_radl, num_vm, auth_data):
        system = radl.systems[0]
        auth_headers = self.get_auth_headers(auth_data)

        # name = system.getValue("disk.0.image.name")

        res = []
        i = 0
        conn = self.get_http_connection()

        url = uriparse(system.getValue("disk.0.image.url"))
        if url[1].startswith('http'):
            os_tpl = url[1] + url[2]
        else:
            os_tpl = url[1]

        # set the credentials the FogBow default username: fogbow
        system.delValue('disk.0.os.credentials.username')
        system.setValue('disk.0.os.credentials.username', 'fogbow')

        public_key = system.getValue('disk.0.os.credentials.public_key')

        if not public_key:
            # We must generate them
            (public_key, private_key) = self.keygen()
            system.setValue('disk.0.os.credentials.private_key', private_key)

        while i < num_vm:
            try:
                conn.putrequest('POST', "/fogbow_request/")
                conn.putheader('Content-Type', 'text/occi')
                # conn.putheader('Accept', 'text/occi')
                if auth_headers:
                    for k, v in auth_headers.iteritems():
                        conn.putheader(k, v)

                conn.putheader(
                    'Category', 'fogbow_request; scheme="http://schemas.fogbowcloud.org/request#"; class="kind"')

                conn.putheader('X-OCCI-Attribute',
                               'org.fogbowcloud.request.instance-count=1')
                conn.putheader('X-OCCI-Attribute',
                               'org.fogbowcloud.request.type="one-time"')
                conn.putheader('X-OCCI-Attribute',
                               'org.fogbowcloud.order.resource-kind="compute"')

                requirements = ""
                if system.getValue('instance_type'):
                    conn.putheader('Category', system.getValue('instance_type') +
                                   '; scheme="http://schemas.fogbowcloud.org/template/resource#"; class="mixin"')
                else:
                    cpu = system.getValue('cpu.count')
                    memory = system.getFeature('memory.size').getValue('M')
                    if cpu:
                        requirements += "Glue2vCPU >= %d" % cpu
                    if memory:
                        if requirements:
                            requirements += " && "
                        requirements += "Glue2RAM >= %d" % memory

                conn.putheader(
                    'Category', os_tpl + '; scheme="http://schemas.fogbowcloud.org/template/os#"; class="mixin"')
                conn.putheader(
                    'Category', 'fogbow_public_key; scheme="http://schemas.fogbowcloud/credentials#"; class="mixin"')
                conn.putheader(
                    'X-OCCI-Attribute', 'org.fogbowcloud.credentials.publickey.data="' + public_key.strip() + '"')

                if system.getValue('availability_zone'):
                    if requirements:
                        requirements += ' && '
                        requirements += 'Glue2CloudComputeManagerID == "%s"' % system.getValue(
                            'availability_zone')

                if requirements:
                    conn.putheader(
                        'X-OCCI-Attribute', 'org.fogbowcloud.request.requirements=' + requirements)

                conn.endheaders()

                resp = conn.getresponse()

                # With this format: X-OCCI-Location:
                # http://158.42.104.75:8182/fogbow_request/436e76ef-9980-4fdb-87fe-71e82655f578
                output = resp.read()

                if resp.status != 201:
                    res.append((False, resp.reason + "\n" + output))
                else:
                    occi_vm_id = os.path.basename(resp.msg.dict['location'])
                    # occi_vm_id = os.path.basename(output)
                    vm = VirtualMachine(
                        inf, occi_vm_id, self.cloud, radl, requested_radl)
                    vm.info.systems[0].setValue('instance_id', str(vm.id))
                    res.append((True, vm))

            except Exception, ex:
                self.logger.exception("Error connecting with FogBow manager")
                res.append((False, "ERROR: " + str(ex)))

            i += 1
Example #18
0
    def test_60_finalize(self, sleep, get_driver):
        auth = Authentication([{
            'id': 'ost',
            'type': 'OpenStack',
            'username': '******',
            'password': '******',
            'tenant': 'tenant',
            'host': 'https://server.com:5000'
        }])
        ost_cloud = self.get_ost_cloud()

        radl_data = """
            network public (outboud = 'yes')
            network private (create = 'yes' and provider_id = ' im-infid-private')
            system test (
            cpu.count>=2 and
            memory.size>=2048m and
            net_interface.0.connection = 'public' and
            net_interface.1.connection = 'private'
            )"""
        radl = radl_parse.parse_radl(radl_data)

        inf = MagicMock()
        inf.id = "infid"
        inf.radl = radl
        vm = VirtualMachine(inf, "1", ost_cloud.cloud, radl, radl, ost_cloud,
                            1)

        driver = MagicMock()
        driver.name = "OpenStack"
        get_driver.return_value = driver

        node = MagicMock()
        node.id = "1"
        node.state = "running"
        node.extra = {'flavorId': 'small'}
        node.public_ips = ['158.42.1.1']
        node.private_ips = ['10.0.0.1']
        node.driver = driver
        node.destroy.return_value = True
        driver.ex_get_node_details.return_value = node

        driver.delete_security_group.return_value = True

        fip = MagicMock()
        fip.node_id = node.id
        fip.ip_address = '158.42.1.1'
        fip.delete.return_value = True
        driver.ex_list_floating_ips.return_value = [fip]
        driver.ex_detach_floating_ip_from_node.return_value = True

        sg1 = MagicMock()
        sg1.name = "im-infid-private"
        sg1.description = "Security group created by the IM"
        sg2 = MagicMock()
        sg2.name = "im-infid-public"
        sg2.description = "Security group created by the IM"
        sg3 = MagicMock()
        sg3.name = "im-infid"
        sg3.description = ""
        driver.ex_list_security_groups.return_value = [sg1, sg2, sg3]

        net1 = MagicMock()
        net1.name = "im-infid-private"
        net1.cidr = None
        net1.extra = {'subnets': ["subnet1"]}
        net2 = MagicMock()
        net2.name = "public"
        net2.cidr = None
        net2.extra = {'subnets': [], 'router:external': True}
        driver.ex_list_networks.return_value = [net1, net2]

        router = MagicMock()
        router.id = "id"
        router.name = "name"
        router.extra = {'external_gateway_info': {'network_id': net2.id}}
        driver.ex_list_routers.return_value = [router]
        driver.ex_add_router_subnet.return_value = True

        success, _ = ost_cloud.finalize(vm, True, 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.assertEqual(node.destroy.call_args_list, [call()])
        self.assertEqual(driver.ex_del_router_subnet.call_args_list[0][0][0],
                         router)
        self.assertEqual(
            driver.ex_del_router_subnet.call_args_list[0][0][1].id, "subnet1")
        self.assertEqual(driver.ex_delete_network.call_args_list[0][0][0],
                         net1)
        self.assertEqual(
            driver.ex_delete_security_group.call_args_list[0][0][0], sg2)
        self.assertEqual(
            driver.ex_delete_security_group.call_args_list[1][0][0], sg1)
        self.assertEqual(fip.delete.call_args_list, [call()])
        self.assertEqual(
            driver.ex_detach_floating_ip_from_node.call_args_list[0][0],
            (node, fip))
Example #19
0
    def test_30_updateVMInfo(self, get_driver):
        radl_data = """
            network net (outbound = 'yes' and provider_id = 'pool1')
            network net1 (provider_id = 'os-lan' and router='10.0.0.0/16,vrouter1')
            system test (
            cpu.arch='x86_64' and
            cpu.count=1 and
            memory.size=512m and
            net_interface.0.connection = 'net' and
            net_interface.0.dns_name = 'test' and
            net_interface.1.connection = 'net1' and
            disk.0.os.name = 'linux' and
            disk.0.image.url = 'ost://server.com/ami-id' 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' and
            disk.2.image.url='ost://server.com/ami-id1' and
            disk.2.mount_path='/mnt/path'
            )"""
        radl = radl_parse.parse_radl(radl_data)
        radl.check()

        auth = Authentication([{
            'id': 'ost',
            'type': 'OpenStack',
            'username': '******',
            'password': '******',
            'tenant': 'tenant',
            'host': 'https://server.com:5000'
        }])
        ost_cloud = self.get_ost_cloud()

        inf = MagicMock()
        vm = VirtualMachine(inf, "1", ost_cloud.cloud, radl, radl, ost_cloud,
                            1)

        vm2 = MagicMock()
        syst = MagicMock()
        syst.name = "vrouter1"
        vm2.info.systems = [syst]
        vm2.getIfaceIP.return_value = "10.0.0.1"
        inf.vm_list = [vm2, vm]

        driver = MagicMock()
        get_driver.return_value = driver

        node = MagicMock()
        node.id = "1"
        node.state = "running"
        node.extra = {
            'flavorId': 'small',
            'addresses': {
                'os-lan': [{
                    'addr': '10.0.0.1',
                    'OS-EXT-IPS:type': 'fixed'
                }]
            }
        }
        node.public_ips = []
        node.private_ips = ['10.0.0.1']
        node.driver = driver
        driver.ex_get_node_details.return_value = node

        node_size = MagicMock()
        node_size.ram = 512
        node_size.price = 1
        node_size.disk = 1
        node_size.vcpus = 1
        node_size.name = "small"
        driver.ex_get_size.return_value = node_size

        volume = MagicMock()
        volume.id = "vol1"
        volume.attach.return_value = True
        driver.create_volume.return_value = volume

        pool = MagicMock()
        pool.name = "pool1"
        floating_ip = MagicMock()
        floating_ip.ip_address = "8.8.8.8"
        pool.list_floating_ips.return_value = []
        pool.create_floating_ip.return_value = floating_ip
        driver.ex_list_floating_ip_pools.return_value = [pool]

        net1 = MagicMock()
        net1.id = 'net1id'
        net1.name = "os-lan"
        net1.cidr = None
        net1.extra = {'subnets': ["subnet1"]}
        net2 = MagicMock()
        net2.id = 'net2id'
        net2.name = "public"
        net2.cidr = None
        net2.extra = {'subnets': [], 'router:external': True}
        driver.ex_list_networks.return_value = [net1, net2]

        port = MagicMock()
        port.extra = {'device_id': net1.id}
        driver.ex_list_ports.return_value = [port]

        success, vm = ost_cloud.updateVMInfo(vm, auth)

        self.assertTrue(success, msg="ERROR: updating VM info.")
        self.assertEquals(vm.info.systems[0].getValue("net_interface.1.ip"),
                          "10.0.0.1")
        self.assertEquals(driver.ex_update_subnet.call_args_list[0][0][0].id,
                          "subnet1")
        self.assertEquals(driver.ex_update_subnet.call_args_list[0][1], {
            'host_routes': [{
                'nexthop': '10.0.0.1',
                'destination': '10.0.0.0/16'
            }]
        })

        # In this case the Node has the float ip assigned
        # node.public_ips = ['8.8.8.8']
        floating_ip.node_id = node.id
        pool.list_floating_ips.return_value = [floating_ip]
        driver.ex_list_floating_ip_pools.return_value = [pool]

        success, vm = ost_cloud.updateVMInfo(vm, auth)

        self.assertTrue(success, msg="ERROR: updating VM info.")
        self.assertEquals(vm.info.systems[0].getValue("net_interface.1.ip"),
                          "10.0.0.1")
        self.assertEquals(vm.info.systems[0].getValue("net_interface.0.ip"),
                          "8.8.8.8")

        # In this case the Node addresses are not available and it uses the old method
        node.extra = {'flavorId': 'small'}
        success, vm = ost_cloud.updateVMInfo(vm, auth)
        self.assertEquals(vm.info.systems[0].getValue("net_interface.1.ip"),
                          "10.0.0.1")
        self.assertEquals(vm.info.systems[0].getValue("net_interface.0.ip"),
                          "8.8.8.8")

        self.assertTrue(success, msg="ERROR: updating VM info.")

        # the node has a IPv6 IP
        node = MagicMock()
        node.id = "2"
        node.state = "running"
        node.extra = {'flavorId': 'small'}
        node.public_ips = ['8.8.8.8', '2001:630:12:581:f816:3eff:fe92:2146']
        node.private_ips = ['10.0.0.1']
        node.driver = driver
        driver.ex_get_node_details.return_value = node

        success, vm = ost_cloud.updateVMInfo(vm, auth)
        self.assertTrue(success, msg="ERROR: updating VM info.")
        self.assertEquals(vm.info.systems[0].getValue("net_interface.0.ip"),
                          "8.8.8.8")
        self.assertEquals(vm.info.systems[0].getValue("net_interface.0.ipv6"),
                          "2001:630:12:581:f816:3eff:fe92:2146")
        self.assertNotIn("ERROR",
                         self.log.getvalue(),
                         msg="ERROR found in log: %s" % self.log.getvalue())
Example #20
0
File: OCCI.py Project: amcaar/im
    def launch(self, inf, radl, requested_radl, num_vm, auth_data):
        system = radl.systems[0]
        auth_header = self.get_auth_header(auth_data)

        cpu = system.getValue('cpu.count')
        memory = None
        if system.getFeature('memory.size'):
            memory = system.getFeature('memory.size').getValue('G')
        name = system.getValue("instance_name")
        if not name:
            name = system.getValue("disk.0.image.name")
        if not name:
            name = "im_userimage"
        arch = system.getValue('cpu.arch')

        if arch.find('64'):
            arch = 'x64'
        else:
            arch = 'x86'

        res = []
        i = 0

        public_key = system.getValue('disk.0.os.credentials.public_key')
        password = system.getValue('disk.0.os.credentials.password')

        if public_key:
            if password:
                system.delValue('disk.0.os.credentials.password')
            password = None
        else:
            if not password:
                # We must generate them
                (public_key, private_key) = self.keygen()
                system.setValue('disk.0.os.credentials.private_key', private_key)

        user = system.getValue('disk.0.os.credentials.username')
        if not user:
            user = self.DEFAULT_USER
            system.setValue('disk.0.os.credentials.username', user)

        user_data = ""
        if public_key:
            # Add user cloud init data
            cloud_config_str = self.get_cloud_init_data(radl)
            cloud_config = self.gen_cloud_config(public_key, user, cloud_config_str)
            user_data = base64.b64encode(cloud_config).replace("\n", "")
            self.logger.debug("Cloud init: " + cloud_config)

        # Get the info about the OCCI server (GET /-/)
        occi_info = self.query_occi(auth_data)

        # Parse the info to get the os_tpl scheme
        url = uriparse(system.getValue("disk.0.image.url"))
        # Get the Image ID from the last part of the path
        os_tpl = os.path.basename(url[2])
        os_tpl_scheme = self.get_os_tpl_scheme(occi_info, os_tpl)
        if not os_tpl_scheme:
            raise Exception(
                "Error getting os_tpl scheme. Check that the image specified is supported in the OCCI server.")

        # Parse the info to get the instance_type (resource_tpl) scheme
        instance_type_uri = None
        if system.getValue('instance_type'):
            instance_type = self.get_instance_type_uri(
                occi_info, system.getValue('instance_type'))
            instance_type_uri = uriparse(instance_type)
            if not instance_type_uri[5]:
                raise Exception("Error getting Instance type URI. Check that the instance_type specified is "
                                "supported in the OCCI server.")
            else:
                instance_name = instance_type_uri[5]
                instance_scheme = instance_type_uri[
                    0] + "://" + instance_type_uri[1] + instance_type_uri[2] + "#"

        while i < num_vm:
            volumes = []
            try:
                # First create the volumes
                volumes = self.create_volumes(system, auth_data)

                body = 'Category: compute; scheme="http://schemas.ogf.org/occi/infrastructure#"; class="kind"\n'
                body += 'Category: ' + os_tpl + '; scheme="' + \
                    os_tpl_scheme + '"; class="mixin"\n'
                body += 'Category: user_data; scheme="http://schemas.openstack.org/compute/instance#"; class="mixin"\n'
                # body += 'Category: public_key;
                # scheme="http://schemas.openstack.org/instance/credentials#";
                # class="mixin"\n'

                if instance_type_uri:
                    body += 'Category: ' + instance_name + '; scheme="' + \
                        instance_scheme + '"; class="mixin"\n'
                else:
                    # Try to use this OCCI attributes (not supported by
                    # openstack)
                    if cpu:
                        body += 'X-OCCI-Attribute: occi.compute.cores=' + \
                            str(cpu) + '\n'
                    # body += 'X-OCCI-Attribute: occi.compute.architecture=' + arch +'\n'
                    if memory:
                        body += 'X-OCCI-Attribute: occi.compute.memory=' + \
                            str(memory) + '\n'

                compute_id = "im." + str(int(time.time() * 100))
                body += 'X-OCCI-Attribute: occi.core.id="' + compute_id + '"\n'
                body += 'X-OCCI-Attribute: occi.core.title="' + name + '"\n'

                # Set the hostname defined in the RADL
                # Create the VM to get the nodename
                vm = VirtualMachine(inf, None, self.cloud, radl, requested_radl, self)
                (nodename, _) = vm.getRequestedName(default_hostname=Config.DEFAULT_VM_NAME,
                                                    default_domain=Config.DEFAULT_DOMAIN)

                body += 'X-OCCI-Attribute: occi.compute.hostname="' + nodename + '"\n'
                # See: https://wiki.egi.eu/wiki/HOWTO10
                # body += 'X-OCCI-Attribute: org.openstack.credentials.publickey.name="my_key"'
                # body += 'X-OCCI-Attribute: org.openstack.credentials.publickey.data="ssh-rsa BAA...zxe ==user@host"'
                if user_data:
                    body += 'X-OCCI-Attribute: org.openstack.compute.user_data="' + user_data + '"\n'

                # Add volume links
                for device, volume_id in volumes:
                    body += ('Link: <%s/storage/%s>;rel="http://schemas.ogf.org/occi/infrastructure#storage";'
                             'category="http://schemas.ogf.org/occi/infrastructure#storagelink";'
                             'occi.core.target="%s/storage/%s";occi.core.source="%s/compute/%s"'
                             '' % (self.cloud.path, volume_id,
                                   self.cloud.path, volume_id,
                                   self.cloud.path, compute_id))
                    if device:
                        body += ';occi.storagelink.deviceid="/dev/%s"\n' % device
                    body += '\n'

                self.logger.debug(body)

                headers = {'Accept': 'text/plain', 'Connection': 'close', 'Content-Type': 'text/plain,text/occi'}
                if auth_header:
                    headers.update(auth_header)
                resp = self.create_request('POST', self.cloud.path + "/compute/", auth_data, headers, body)

                # some servers return 201 and other 200
                if resp.status_code != 201 and resp.status_code != 200:
                    res.append((False, resp.reason + "\n" + resp.text))
                    for _, volume_id in volumes:
                        self.delete_volume(volume_id, auth_data)
                else:
                    occi_vm_id = os.path.basename(resp.text)
                    if occi_vm_id:
                        vm.id = occi_vm_id
                        vm.info.systems[0].setValue('instance_id', str(occi_vm_id))
                        res.append((True, vm))
                    else:
                        res.append((False, 'Unknown Error launching the VM.'))

            except Exception, ex:
                self.logger.exception("Error connecting with OCCI server")
                res.append((False, "ERROR: " + str(ex)))
                for _, volume_id in volumes:
                    self.delete_volume(volume_id, auth_data)

            i += 1
Example #21
0
    def launch(self, inf, radl, requested_radl, num_vm, auth_data):
        service_name = None
        region = self.DEFAULT_LOCATION
        if radl.systems[0].getValue('availability_zone'):
            region = radl.systems[0].getValue('availability_zone')
        else:
            radl.systems[0].setValue('availability_zone', region)

        res = []
        i = 0
        while i < num_vm:
            try:
                subscription_id = self.get_subscription_id(auth_data)

                # Create storage account
                storage_account_name = self.get_storage_name(
                    subscription_id, region)
                storage_account = self.get_storage_account(
                    storage_account_name, auth_data)
                if not storage_account:
                    storage_account_name, error_msg = self.create_storage_account(
                        storage_account_name, auth_data, region)
                    if not storage_account_name:
                        res.append((False, error_msg))
                        break
                else:
                    # if the user has specified the region
                    if radl.systems[0].getValue('availability_zone'):
                        # Check that the region of the storage account is the
                        # same of the service
                        if region != storage_account.GeoPrimaryRegion:
                            res.append(
                                (False,
                                 ("Error creating the service. The specified "
                                  "region is not the same of the service.")))
                            break
                    else:
                        # Otherwise use the storage account region
                        region = storage_account.GeoPrimaryRegion

                # and the service
                service_name, error_msg = self.create_service(
                    auth_data, region)
                if service_name is None:
                    res.append((False, error_msg))
                    break

                self.logger.debug("Creating the VM with id: " + service_name)

                # Create the VM to get the nodename
                vm = VirtualMachine(inf, service_name, self.cloud, radl,
                                    requested_radl, self)
                vm.info.systems[0].setValue('instance_id', str(vm.id))

                # Generate the XML to create the VM
                vm_create_xml = self.get_azure_vm_create_xml(
                    vm, storage_account_name, radl, i, auth_data)

                if vm_create_xml is None:
                    self.delete_service(service_name, auth_data)
                    res.append((False, "Incorrect image or auth data"))
                    break

                uri = "/services/hostedservices/%s/deployments" % service_name
                headers = {
                    'x-ms-version': '2013-03-01',
                    'Content-Type': 'application/xml'
                }
                resp = self.create_request('POST', uri, auth_data, headers,
                                           vm_create_xml)

                if resp.status_code != 202:
                    self.delete_service(service_name, auth_data)
                    self.logger.error("Error creating the VM: Error Code " +
                                      str(resp.status_code) + ". Msg: " +
                                      resp.text)
                    res.append((False, "Error creating the VM: Error Code " +
                                str(resp.status_code) + ". Msg: " + resp.text))
                else:
                    # Call the GET OPERATION STATUS until sea 200 (OK)
                    request_id = resp.headers['x-ms-request-id']
                    success = self.wait_operation_status(request_id, auth_data)
                    if success:
                        res.append((True, vm))
                    else:
                        self.logger.exception("Error waiting the VM creation")
                        self.delete_service(service_name, auth_data)
                        res.append((False, "Error waiting the VM creation"))

            except Exception, ex:
                self.logger.exception("Error creating the VM")
                if service_name:
                    self.delete_service(service_name, auth_data)
                res.append((False, "Error creating the VM: " + str(ex)))

            i += 1
Example #22
0
    def launch(self, inf, radl, requested_radl, num_vm, auth_data):
        driver = self.get_driver(auth_data)

        system = radl.systems[0]
        image_id = self.get_image_id(system.getValue("disk.0.image.url"))
        image = NodeImage(id=image_id, name=None, driver=driver)

        instance_type = self.get_instance_type(driver.list_sizes(), system)
        if not instance_type:
            raise Exception("No flavor found for the specified VM requirements.")

        name = system.getValue("instance_name")
        if not name:
            name = system.getValue("disk.0.image.name")
        if not name:
            name = "userimage"

        nets = self.get_networks(driver, radl)

        sgs = self.create_security_group(driver, inf, radl)

        args = {'size': instance_type,
                'image': image,
                'networks': nets,
                'ex_security_groups': sgs,
                'name': "%s-%s" % (name, int(time.time() * 100))}

        keypair = None
        keypair_name = None
        keypair_created = False
        public_key = system.getValue("disk.0.os.credentials.public_key")
        if public_key:
            keypair = driver.get_key_pair(public_key)
            if keypair:
                system.setUserKeyCredentials(
                    system.getCredentials().username, None, keypair.private_key)
            else:
                if "ssh_key" in driver.features.get("create_node", []):
                    args["auth"] = NodeAuthSSHKey(public_key)

        elif not system.getValue("disk.0.os.credentials.password"):
            keypair_name = "im-%d" % int(time.time() * 100.0)
            self.logger.debug("Create keypair: %s" % keypair_name)
            keypair = driver.create_key_pair(keypair_name)
            keypair_created = True
            public_key = keypair.public_key
            system.setUserKeyCredentials(
                system.getCredentials().username, None, keypair.private_key)

            if keypair.public_key and "ssh_key" in driver.features.get("create_node", []):
                args["auth"] = NodeAuthSSHKey(keypair.public_key)
            else:
                args["ex_keyname"] = keypair_name

        user = system.getValue('disk.0.os.credentials.username')
        if not user:
            user = self.DEFAULT_USER
            system.setValue('disk.0.os.credentials.username', user)

        cloud_init = self.get_cloud_init_data(radl)
        if public_key:
            cloud_init = self.gen_cloud_config(public_key, user, cloud_init)

        if cloud_init:
            args['ex_userdata'] = cloud_init

        res = []
        i = 0
        all_failed = True
        while i < num_vm:
            self.logger.debug("Creating node")

            node = None
            msg = "Error creating the node. "
            try:
                node = driver.create_node(**args)
            except Exception, ex:
                msg += str(ex)

            if node:
                vm = VirtualMachine(inf, node.id, self.cloud, radl, requested_radl, self.cloud.getCloudConnector())
                vm.info.systems[0].setValue('instance_id', str(node.id))
                vm.info.systems[0].setValue('instance_name', str(node.name))
                # Add the keypair name to remove it later
                if keypair_name:
                    vm.keypair = keypair_name
                self.logger.debug("Node successfully created.")
                all_failed = False
                res.append((True, vm))
            else:
                res.append((False, msg))
            i += 1
Example #23
0
    def test_55_alter(self, get_driver):
        radl_data = """
            network net ()
            system test (
            cpu.arch='x86_64' and
            cpu.count=1 and
            memory.size=512m and
            net_interface.0.connection = 'net' 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 = '******'
            )"""
        radl = radl_parse.parse_radl(radl_data)

        new_radl_data = """
            system test (
            cpu.count>=2 and
            memory.size>=2048m
            )"""
        new_radl = radl_parse.parse_radl(new_radl_data)

        auth = Authentication([{
            'id': 'ost',
            'type': 'OpenStack',
            'username': '******',
            'password': '******',
            'tenant': 'tenant',
            'host': 'https://server.com:5000'
        }])
        ost_cloud = self.get_ost_cloud()

        inf = MagicMock()
        inf.get_next_vm_id.return_value = 1
        vm = VirtualMachine(inf, "1", ost_cloud.cloud, radl, radl, ost_cloud)

        driver = MagicMock()
        get_driver.return_value = driver

        node = MagicMock()
        node.id = "1"
        node.state = "running"
        node.extra = {'flavorId': 'small'}
        node.public_ips = ['158.42.1.1']
        node.private_ips = ['10.0.0.1']
        node.driver = driver
        driver.list_nodes.return_value = [node]

        node_size = MagicMock()
        node_size.ram = 2048
        node_size.price = 1
        node_size.disk = 1
        node_size.vcpus = 2
        node_size.name = "small"
        driver.list_sizes.return_value = [node_size]

        driver.ex_resize.return_value = True

        success, _ = ost_cloud.alterVM(vm, new_radl, auth)

        self.assertTrue(success, msg="ERROR: modifying VM info.")
        self.assertNotIn("ERROR",
                         self.log.getvalue(),
                         msg="ERROR found in log: %s" % self.log.getvalue())
        self.clean_log()
Example #24
0
    def launch(self, inf, radl, requested_radl, num_vm, auth_data):
        driver = self.get_driver(auth_data)

        system = radl.systems[0]
        region, image_id = self.get_image_data(
            system.getValue("disk.0.image.url"))

        image = driver.ex_get_image(image_id)
        if not image:
            return [(False, "Incorrect image name") for _ in range(num_vm)]

        if system.getValue('availability_zone'):
            region = system.getValue('availability_zone')

        instance_type = self.get_instance_type(driver.list_sizes(region),
                                               system)

        name = system.getValue("instance_name")
        if not name:
            name = system.getValue("disk.0.image.name")
        if not name:
            name = "userimage"

        args = {
            'size': instance_type,
            'image': image,
            'external_ip': 'ephemeral',
            'location': region
        }

        if self.request_external_ip(radl):
            if num_vm:
                raise Exception(
                    "A fixed IP cannot be specified to a set of nodes (deploy is higher than 1)"
                )
            fixed_ip = self.request_external_ip(radl)
            args['external_ip'] = driver.ex_create_address(name="im-" +
                                                           fixed_ip,
                                                           region=region,
                                                           address=fixed_ip)

        # include the SSH_KEYS
        username = system.getValue('disk.0.os.credentials.username')
        private = system.getValue('disk.0.os.credentials.private_key')
        public = system.getValue('disk.0.os.credentials.public_key')

        if not public or not private:
            # We must generate them
            self.logger.debug("No keys. Generating key pair.")
            (public, private) = self.keygen()
            system.setValue('disk.0.os.credentials.private_key', private)

        metadata = {}
        if private and public:
            metadata = {"sshKeys": username + ":" + public}
            self.logger.debug("Setting ssh for user: "******"default")

        res = []
        if num_vm > 1:
            args['number'] = num_vm
            args['base_name'] = "%s-%s" % (name.lower().replace(
                "_", "-"), int(time.time() * 100))
            nodes = driver.ex_create_multiple_nodes(**args)
        else:
            args['name'] = "%s-%s" % (name.lower().replace(
                "_", "-"), int(time.time() * 100))
            nodes = [driver.create_node(**args)]

        for node in nodes:
            vm = VirtualMachine(inf, node.extra['name'], self.cloud, radl,
                                requested_radl, self.cloud.getCloudConnector())
            vm.info.systems[0].setValue('instance_id', str(vm.id))
            vm.info.systems[0].setValue('instance_name', str(vm.id))
            self.logger.debug("Node successfully created.")
            res.append((True, vm))

        for _ in range(len(nodes), num_vm):
            res.append((False, "Error launching VM."))

        return res
Example #25
0
    def launch(self, inf, radl, requested_radl, num_vm, auth_data):
        system = radl.systems[0]

        public_net = None
        for net in radl.networks:
            if net.isPublic():
                public_net = net

        outports = None
        if public_net:
            outports = public_net.getOutPorts()

        conn = self.get_http_connection(auth_data)
        res = []
        i = 0
        while i < num_vm:
            try:
                i += 1

                ssh_port = 22
                if public_net:
                    ssh_port = (DockerCloudConnector._port_base_num +
                                DockerCloudConnector._port_counter) % 65535
                    DockerCloudConnector._port_counter += 1

                # Create the VM to get the nodename
                vm = VirtualMachine(inf, None, self.cloud, radl,
                                    requested_radl, self)

                # Create the container
                conn.putrequest('POST', "/containers/create")
                conn.putheader('Content-Type', 'application/json')

                cont_data = self._generate_create_request_data(
                    outports, system, vm, ssh_port)
                body = json.dumps(cont_data)

                conn.putheader('Content-Length', len(body))
                conn.endheaders(body)

                resp = conn.getresponse()
                output = resp.read()
                if resp.status != 201:
                    res.append(
                        (False, "Error creating the Container: " + output))
                    continue

                output = json.loads(output)
                # Set the cloud id to the VM
                vm.id = output["Id"]
                vm.info.systems[0].setValue('instance_id', str(vm.id))

                # Now start it
                success, _ = self.start(vm, auth_data)
                if not success:
                    res.append(
                        (False,
                         "Error starting the Container: " + str(output)))
                    # Delete the container
                    conn.request('DELETE', "/containers/" + vm.id)
                    resp = conn.getresponse()
                    resp.read()
                    continue

                # Set the default user and password to access the container
                vm.info.systems[0].setValue('disk.0.os.credentials.username',
                                            'root')
                vm.info.systems[0].setValue('disk.0.os.credentials.password',
                                            self._root_password)

                # Set ssh port in the RADL info of the VM
                vm.setSSHPort(ssh_port)

                res.append((True, vm))

            except Exception, ex:
                self.logger.exception("Error connecting with Docker server")
                res.append((False, "ERROR: " + str(ex)))
Example #26
0
    def test_30_updateVMInfo(self, get_dns_driver, get_driver):
        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.dns_name = 'test.domain.com' and
            disk.0.os.name = 'linux' and
            disk.0.image.url = 'gce://us-central1-a/centos-6' and
            disk.0.os.credentials.username = '******'
            )"""
        radl = radl_parse.parse_radl(radl_data)
        radl.check()

        auth = Authentication([{
            'id': 'one',
            'type': 'GCE',
            'username': '******',
            'password': '******',
            'project': 'proj'
        }])
        gce_cloud = self.get_gce_cloud()

        inf = MagicMock()
        vm = VirtualMachine(inf, "1", gce_cloud.cloud, radl, radl, gce_cloud,
                            1)

        driver = MagicMock()
        get_driver.return_value = driver
        dns_driver = MagicMock()
        get_dns_driver.return_value = dns_driver

        node = MagicMock()
        zone = MagicMock()
        node.id = "1"
        node.state = "running"
        node.extra = {'flavorId': 'small'}
        node.public_ips = ['158.42.1.1']
        node.private_ips = ['10.0.0.1']
        node.driver = driver
        zone.name = 'us-central1-a'
        node.extra = {'zone': zone}
        node.size = NodeSize("1", "name1", 512, 1, None, None, driver)
        driver.ex_get_node.return_value = node

        dns_driver.iterate_zones.return_value = []
        dns_driver.iterate_records.return_value = []

        success, vm = gce_cloud.updateVMInfo(vm, auth)

        self.assertTrue(success, msg="ERROR: updating VM info.")

        self.assertEquals(dns_driver.create_zone.call_count, 1)
        self.assertEquals(dns_driver.create_record.call_count, 1)
        self.assertEquals(dns_driver.create_zone.call_args_list[0],
                          call('domain.com.'))
        self.assertEquals(dns_driver.create_record.call_args_list[0][0][0],
                          'test.domain.com.')
        self.assertEquals(dns_driver.create_record.call_args_list[0][0][2],
                          'A')
        self.assertEquals(dns_driver.create_record.call_args_list[0][0][3], {
            'rrdatas': ['158.42.1.1'],
            'ttl': 300
        })

        self.assertNotIn("ERROR",
                         self.log.getvalue(),
                         msg="ERROR found in log: %s" % self.log.getvalue())
Example #27
0
    def launch(self, inf, radl, requested_radl, num_vm, auth_data):
        driver = self.get_driver(auth_data)

        system = radl.systems[0]
        image_id = self.get_image_id(system.getValue("disk.0.image.url"))
        image = NodeImage(id=image_id, name=None, driver=driver)

        instance_type = self.get_instance_type(driver.list_sizes(), system)
        if not instance_type:
            raise Exception(
                "No flavor found for the specified VM requirements.")

        name = system.getValue("instance_name")
        if not name:
            name = system.getValue("disk.0.image.name")
        if not name:
            name = "userimage"

        nets = self.get_networks(driver, radl)

        sgs = self.create_security_group(driver, inf, radl)

        args = {
            'size': instance_type,
            'image': image,
            'networks': nets,
            'ex_security_groups': sgs,
            'name': "%s-%s" % (name, int(time.time() * 100))
        }

        keypair = None
        keypair_name = None
        keypair_created = False
        public_key = system.getValue("disk.0.os.credentials.public_key")
        if public_key:
            keypair = driver.get_key_pair(public_key)
            if keypair:
                system.setUserKeyCredentials(system.getCredentials().username,
                                             None, keypair.private_key)
            else:
                if "ssh_key" in driver.features.get("create_node", []):
                    args["auth"] = NodeAuthSSHKey(public_key)

        elif not system.getValue("disk.0.os.credentials.password"):
            keypair_name = "im-%d" % int(time.time() * 100.0)
            self.log_debug("Create keypair: %s" % keypair_name)
            keypair = driver.create_key_pair(keypair_name)
            keypair_created = True
            public_key = keypair.public_key
            system.setUserKeyCredentials(system.getCredentials().username,
                                         None, keypair.private_key)

            if keypair.public_key and "ssh_key" in driver.features.get(
                    "create_node", []):
                args["auth"] = NodeAuthSSHKey(keypair.public_key)
            else:
                args["ex_keyname"] = keypair_name

        user = system.getValue('disk.0.os.credentials.username')
        if not user:
            user = self.DEFAULT_USER
            system.setValue('disk.0.os.credentials.username', user)

        cloud_init = self.get_cloud_init_data(radl)
        if public_key:
            cloud_init = self.gen_cloud_config(public_key, user, cloud_init)

        if cloud_init:
            args['ex_userdata'] = cloud_init

        res = []
        i = 0
        all_failed = True
        while i < num_vm:
            self.log_debug("Creating node")

            node = None
            msg = "Error creating the node. "
            try:
                node = driver.create_node(**args)
            except Exception as ex:
                msg += str(ex)

            if node:
                vm = VirtualMachine(inf, node.id, self.cloud, radl,
                                    requested_radl,
                                    self.cloud.getCloudConnector(inf))
                vm.info.systems[0].setValue('instance_id', str(node.id))
                vm.info.systems[0].setValue('instance_name', str(node.name))
                # Add the keypair name to remove it later
                if keypair_name:
                    vm.keypair = keypair_name
                self.log_debug("Node successfully created.")
                all_failed = False
                res.append((True, vm))
            else:
                res.append((False, msg))
            i += 1

        # if all the VMs have failed, remove the sg and keypair
        if all_failed:
            if keypair_created:
                # only delete in case of the user do not specify the keypair
                # name
                self.log_debug("Deleting keypair: %s." % keypair_name)
                driver.delete_key_pair(keypair)
            if sgs:
                self.log_debug("Deleting security group: %s." % sgs[0].id)
                driver.ex_delete_security_group(sgs[0])

        return res
Example #28
0
    def test_60_finalize(self, sleep, get_dns_driver, get_driver):
        auth = Authentication([{
            'id': 'one',
            'type': 'GCE',
            'username': '******',
            'password': '******',
            'project': 'proj'
        }])
        gce_cloud = self.get_gce_cloud()

        radl_data = """
            network net (outbound = 'yes')
            system test (
            cpu.count=2 and
            memory.size=2048m and
            net_interface.0.connection = 'net' and
            net_interface.0.dns_name = 'test.domain.com' and
            net_interface.0.ip = '158.42.1.1'
            )"""
        radl = radl_parse.parse_radl(radl_data)

        inf = MagicMock()
        inf.id = "infid"
        vm = VirtualMachine(inf, "1", gce_cloud.cloud, radl, radl, gce_cloud,
                            1)

        driver = MagicMock()
        get_driver.return_value = driver
        dns_driver = MagicMock()
        get_dns_driver.return_value = dns_driver

        node = MagicMock()
        node.destroy.return_value = True
        node.extra = {'disks': [{'source': 'vol'}]}
        node.driver = driver
        driver.ex_get_node.return_value = node

        volume = MagicMock()
        volume.detach.return_value = True
        volume.destroy.return_value = True
        driver.ex_get_volume.return_value = volume

        zone = MagicMock()
        zone.domain = "domain.com."
        dns_driver.iterate_zones.return_value = [zone]
        record = MagicMock()
        record.name = 'test.domain.com.'
        record.data = {'rrdatas': ['158.42.1.1'], 'ttl': 300}
        dns_driver.iterate_records.return_value = [record]

        net = MagicMock()
        net.name = "im-infid-id"
        net.destroy.return_value = True
        driver.ex_list_networks.return_value = [net]

        fw = MagicMock()
        fw.name = "im-infid-id"
        fw.destroy.return_value = True
        driver.ex_list_firewalls.return_value = [fw]

        route = MagicMock()
        route.name = "im-infid-id"
        route.destroy.return_value = True
        driver.ex_list_routes.return_value = [route]

        success, _ = gce_cloud.finalize(vm, True, 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.assertEquals(dns_driver.delete_record.call_count, 1)
        self.assertEquals(
            dns_driver.delete_record.call_args_list[0][0][0].name,
            'test.domain.com.')
        self.assertEquals(node.destroy.call_args_list, [call()])
        self.assertEquals(net.destroy.call_args_list, [call()])
        self.assertEquals(fw.destroy.call_args_list, [call()])
        self.assertEquals(route.destroy.call_args_list, [call()])
Example #29
0
    def launch(self, inf, radl, requested_radl, num_vm, auth_data):
        system = radl.systems[0]

        public_net = None
        for net in radl.networks:
            if net.isPublic():
                public_net = net

        outports = None
        if public_net:
            outports = public_net.getOutPorts()

        auth_header = self.get_auth_header(auth_data)
        conn = self.get_http_connection()
        apiVersion = self.get_api_version(auth_data)

        res = []
        i = 0
        while i < num_vm:
            try:
                i += 1

                namespace = "im%d" % int(time.time() * 100)
                vm = VirtualMachine(inf, None, self.cloud,
                                    radl, requested_radl, self)
                (nodename, _) = vm.getRequestedName(
                    default_hostname=Config.DEFAULT_VM_NAME, default_domain=Config.DEFAULT_DOMAIN)
                pod_name = nodename

                # Do not use the Persistent volumes yet
                volumes = self._create_volumes(
                    apiVersion, namespace, system, pod_name, auth_data)

                # Create the pod
                conn.putrequest('POST', "/api/" + apiVersion +
                                "/namespaces/" + namespace + "/pods")
                conn.putheader('Content-Type', 'application/json')
                if auth_header:
                    conn.putheader(auth_header.keys()[
                                   0], auth_header.values()[0])

                ssh_port = (KubernetesCloudConnector._port_base_num +
                            KubernetesCloudConnector._port_counter) % 65535
                KubernetesCloudConnector._port_counter += 1
                pod_data = self._generate_pod_data(
                    apiVersion, namespace, pod_name, outports, system, ssh_port, volumes)
                body = json.dumps(pod_data)
                conn.putheader('Content-Length', len(body))
                conn.endheaders(body)

                resp = conn.getresponse()
                output = resp.read()
                if resp.status != 201:
                    res.append(
                        (False, "Error creating the Container: " + output))
                else:
                    output = json.loads(output)
                    vm.id = output["metadata"]["namespace"] + \
                        "/" + output["metadata"]["name"]
                    # Set SSH port in the RADL info of the VM
                    vm.setSSHPort(ssh_port)
                    # Set the default user and password to access the container
                    vm.info.systems[0].setValue(
                        'disk.0.os.credentials.username', 'root')
                    vm.info.systems[0].setValue(
                        'disk.0.os.credentials.password', self._root_password)
                    vm.info.systems[0].setValue('instance_id', str(vm.id))
                    vm.info.systems[0].setValue('instance_name', str(vm.id))

                    res.append((True, vm))

            except Exception, ex:
                self.logger.exception(
                    "Error connecting with Kubernetes API server")
                res.append((False, "ERROR: " + str(ex)))
Example #30
0
    def launch(self, inf, radl, requested_radl, num_vm, auth_data):
        system = radl.systems[0]
        res = []
        i = 0

        image = os.path.basename(system.getValue("disk.0.image.url"))

        # set the credentials the FogBow default username: fogbow
        system.delValue('disk.0.os.credentials.username')
        system.setValue('disk.0.os.credentials.username', 'fogbow')

        public_key = system.getValue('disk.0.os.credentials.public_key')

        if not public_key:
            # We must generate them
            (public_key, private_key) = self.keygen()
            system.setValue('disk.0.os.credentials.private_key', private_key)

        cpu = system.getValue('cpu.count')
        memory = system.getFeature('memory.size').getValue('M')
        name = system.getValue("instance_name")
        if not name:
            name = system.getValue("disk.0.image.name")
        if not name:
            name = "userimage"

        with inf._lock:
            self.create_nets(inf, radl, auth_data)

        while i < num_vm:
            try:
                headers = {'Content-Type': 'application/json'}

                nets = []
                fed_net = None
                for net in radl.networks:
                    if not net.isPublic() and radl.systems[0].getNumNetworkWithConnection(net.id) is not None:
                        provider_id = net.getValue('provider_id')
                        if provider_id:
                            if net.getValue("federated") == "yes":
                                fed_net = provider_id
                            else:
                                nets.append(provider_id)

                body = {"compute":
                        {"imageId": image,
                         "memory": memory,
                         "name": "%s-%s" % (name.lower().replace("_", "-"), str(uuid1())),
                         "publicKey": public_key,
                         "vCPU": cpu}
                        }

                if nets:
                    body["compute"]["networkIds"] = nets
                if fed_net:
                    body["federatedNetworkId"] = fed_net

                if system.getValue('availability_zone'):
                    if '@' in system.getValue('availability_zone'):
                        parts = system.getValue('availability_zone').split('@')
                        body["compute"]['cloudName'] = parts[0]
                        body["compute"]['provider'] = parts[1]
                    else:
                        body["compute"]['provider'] = system.getValue('availability_zone')

                self.log_debug(body)

                resp = self.create_request('POST', '/computes/', auth_data, headers, json.dumps(body))

                if resp.status_code not in [201, 200]:
                    res.append((False, resp.reason + "\n" + resp.text))
                else:
                    vm = VirtualMachine(inf, str(resp.json()['id']), self.cloud, radl, requested_radl)
                    vm.info.systems[0].setValue('instance_id', str(vm.id))
                    inf.add_vm(vm)
                    res.append((True, vm))

            except Exception as ex:
                self.log_exception("Error connecting with FogBow manager")
                res.append((False, "ERROR: " + str(ex)))

            i += 1

        return res
Example #31
0
    def launch(self, inf, radl, requested_radl, num_vm, auth_data):
        system = radl.systems[0]

        public_net = None
        for net in radl.networks:
            if net.isPublic():
                public_net = net

        outports = None
        if public_net:
            outports = public_net.getOutPorts()

        conn = self.get_http_connection(auth_data)
        res = []
        i = 0
        while i < num_vm:
            try:
                i += 1

                ssh_port = 22
                if public_net:
                    ssh_port = (DockerCloudConnector._port_base_num +
                                DockerCloudConnector._port_counter) % 65535
                    DockerCloudConnector._port_counter += 1

                # Create the VM to get the nodename
                vm = VirtualMachine(inf, None, self.cloud,
                                    radl, requested_radl, self)

                # Create the container
                conn.putrequest('POST', "/containers/create")
                conn.putheader('Content-Type', 'application/json')

                cont_data = self._generate_create_request_data(
                    outports, system, vm, ssh_port)
                body = json.dumps(cont_data)

                conn.putheader('Content-Length', len(body))
                conn.endheaders(body)

                resp = conn.getresponse()
                output = resp.read()
                if resp.status != 201:
                    res.append(
                        (False, "Error creating the Container: " + output))
                    continue

                output = json.loads(output)
                # Set the cloud id to the VM
                vm.id = output["Id"]
                vm.info.systems[0].setValue('instance_id', str(vm.id))

                # Now start it
                success, _ = self.start(vm, auth_data)
                if not success:
                    res.append(
                        (False, "Error starting the Container: " + str(output)))
                    # Delete the container
                    conn.request('DELETE', "/containers/" + vm.id)
                    resp = conn.getresponse()
                    resp.read()
                    continue

                # Set the default user and password to access the container
                vm.info.systems[0].setValue(
                    'disk.0.os.credentials.username', 'root')
                vm.info.systems[0].setValue(
                    'disk.0.os.credentials.password', self._root_password)

                # Set ssh port in the RADL info of the VM
                vm.setSSHPort(ssh_port)

                res.append((True, vm))

            except Exception, ex:
                self.logger.exception("Error connecting with Docker server")
                res.append((False, "ERROR: " + str(ex)))
Example #32
0
    def test_30_updateVMInfo(self, record_sets, connect_to_region, get_connection):
        radl_data = """
            network net (outbound = 'yes')
            network net2 (router = '10.0.10.0/24,vrouter')
            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.domain.com' and
            net_interface.1.connection = 'net2' 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)
        radl.check()

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

        inf = MagicMock()
        vm1 = MagicMock()
        system1 = MagicMock()
        system1.name = 'vrouter'
        vm1.info.systems = [system1]
        vm1.id = "region;int-id"
        inf.vm_list = [vm1]
        vm = VirtualMachine(inf, "us-east-1;id-1", ec2_cloud.cloud, radl, radl, ec2_cloud, 1)

        conn = MagicMock()
        get_connection.return_value = conn

        reservation = MagicMock()
        instance = MagicMock()
        instance.update.return_value = True
        instance.tags = []
        instance.virtualization_type = "vt"
        instance.placement = "us-east-1"
        instance.state = "running"
        instance.instance_type = "t1.micro"
        instance.launch_time = "2016-12-31T00:00:00"
        instance.ip_address = "158.42.1.1"
        instance.private_ip_address = "10.0.0.1"
        instance.connection = conn
        reservation.instances = [instance]
        conn.get_all_instances.return_value = [reservation]

        address = MagicMock()
        address.public_ip = "158.42.1.1"
        conn.get_all_addresses.return_value = [address]

        dns_conn = MagicMock()
        connect_to_region.return_value = dns_conn

        dns_conn.get_zone.return_value = None
        zone = MagicMock()
        zone.get_a.return_value = None
        dns_conn.create_zone.return_value = zone
        changes = MagicMock()
        record_sets.return_value = changes
        change = MagicMock()
        changes.add_change.return_value = change

        vpc = MagicMock()
        vpc.id = "vpc-id"
        conn.get_all_vpcs.return_value = [vpc]

        subnet = MagicMock()
        subnet.id = "subnet-id"
        conn.get_all_subnets.return_value = [subnet]

        routet = MagicMock()
        routet.id = "routet-id"
        conn.get_all_route_tables.return_value = [routet]

        success, vm = ec2_cloud.updateVMInfo(vm, auth)

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

        self.assertEquals(dns_conn.create_zone.call_count, 1)
        self.assertEquals(dns_conn.create_zone.call_args_list[0][0][0], "domain.com.")
        self.assertEquals(changes.add_change.call_args_list, [call('CREATE', 'test.domain.com.', 'A')])
        self.assertEquals(change.add_value.call_args_list, [call('158.42.1.1')])
        self.assertEquals(conn.create_route.call_args_list, [call('routet-id', '10.0.10.0/24', instance_id='int-id')])
Example #33
0
    def test_55_alter(self, get_keystone_uri, requests):
        radl_data = """
            network net ()
            system test (
            cpu.arch='x86_64' and
            cpu.count=1 and
            memory.size=512m and
            net_interface.0.connection = 'net' and
            net_interface.0.dns_name = 'test' and
            disk.0.os.name = 'linux' and
            disk.0.os.credentials.username = '******' and
            disk.0.os.credentials.password = '******'
            )"""
        radl = radl_parse.parse_radl(radl_data)

        new_radl_data = """
            network net ()
            network net1 (outbound = 'yes')
            system test (
            net_interface.0.connection = 'net' and
            net_interface.1.connection = 'net1' and
            disk.1.size=1GB and
            disk.1.device='hdc' and
            disk.1.fstype='ext4' and
            disk.1.mount_path='/mnt/disk'
            )"""
        new_radl = radl_parse.parse_radl(new_radl_data)

        auth = Authentication([{
            'id': 'occi',
            'type': 'OCCI',
            'proxy': 'proxy',
            'host': 'https://server.com:11443'
        }])
        occi_cloud = self.get_occi_cloud()

        inf = MagicMock()
        vm = VirtualMachine(inf, "1", occi_cloud.cloud, radl, radl, occi_cloud,
                            1)

        requests.side_effect = self.get_response

        get_keystone_uri.return_value = None, None

        success, _ = occi_cloud.alterVM(vm, new_radl, auth)

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

        # Now test to delete the public IP
        radl_data = """
            network net ()
            network net1 (outbound = 'yes')
            system test (
            net_interface.0.connection = 'net' and
            net_interface.1.connection = 'net1' and
            net_interface.1.ip = '8.8.8.8' and
            cpu.arch='x86_64' and
            cpu.count=1 and
            memory.size=512m and
            disk.0.os.name = 'linux'
            )"""
        radl = radl_parse.parse_radl(radl_data)

        new_radl_data = """
            network net ()
            system test (
            net_interface.0.connection = 'net' and
            disk.1.size=1GB and
            disk.1.device='hdc' and
            disk.1.fstype='ext4' and
            disk.1.mount_path='/mnt/disk'
            )"""
        new_radl = radl_parse.parse_radl(new_radl_data)

        vm = VirtualMachine(inf, "1", occi_cloud.cloud, radl, radl, occi_cloud,
                            1)
        success, _ = occi_cloud.alterVM(vm, new_radl, auth)
        self.assertTrue(success, msg="ERROR: modifying VM info.")
        self.assertNotIn("ERROR",
                         self.log.getvalue(),
                         msg="ERROR found in log: %s" % self.log.getvalue())

        self.assertEqual(
            vm.requested_radl.systems[0].getValue(
                "net_interface.0.connection"), "net")
Example #34
0
    def test_60_finalize(self, record_sets, connect_to_region, sleep, get_connection):
        radl_data = """
            network net (outbound = 'yes')
            network net2 (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.domain.com' and
            net_interface.1.connection = 'net2' 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"
        vm = VirtualMachine(inf, "us-east-1;id-1", ec2_cloud.cloud, radl, radl, ec2_cloud, 1)

        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]

        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 = []

        sg = MagicMock()
        sg.name = "im-1"
        sg.description = "Security group created by the IM"
        sg.instances.return_value = []
        sg.revoke.return_value = True
        sg.delete.return_value = True
        sg1 = MagicMock()
        sg1.name = "im-1-net"
        sg1.description = ""
        sg1.instances.return_value = []
        sg1.revoke.return_value = True
        sg1.delete.return_value = True
        sg2 = MagicMock()
        sg2.name = "im-1-net2"
        sg2.description = "Security group created by the IM"
        sg2.instances.return_value = []
        sg2.revoke.return_value = True
        sg2.delete.return_value = True
        conn.get_all_security_groups.return_value = [sg, sg1, sg2]

        dns_conn = MagicMock()
        connect_to_region.return_value = dns_conn

        zone = MagicMock()
        record = MagicMock()
        zone.id = "zid"
        zone.get_a.return_value = record
        dns_conn.get_all_rrsets.return_value = []
        dns_conn.get_zone.return_value = zone
        changes = MagicMock()
        record_sets.return_value = changes
        change = MagicMock()
        changes.add_change.return_value = change

        subnet = MagicMock()
        subnet.id = "subnet-id"
        conn.get_all_subnets.return_value = [subnet]

        vpc = MagicMock()
        vpc.id = "vpc-id"
        conn.get_all_vpcs.return_value = [vpc]

        ig = MagicMock()
        ig.id = "ig-id"
        conn.get_all_internet_gateways.return_value = [ig]

        success, _ = ec2_cloud.finalize(vm, True, 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.assertEquals(dns_conn.delete_hosted_zone.call_count, 1)
        self.assertEquals(dns_conn.delete_hosted_zone.call_args_list[0][0][0], zone.id)
        self.assertEquals(changes.add_change.call_args_list, [call('DELETE', 'test.domain.com.', 'A')])
        self.assertEquals(change.add_value.call_args_list, [call('158.42.1.1')])
        self.assertEquals(sg.delete.call_args_list, [call()])
        self.assertEquals(sg1.delete.call_args_list, [])
        self.assertEquals(sg2.delete.call_args_list, [call()])
        self.assertEquals(conn.delete_subnet.call_args_list, [call('subnet-id')])
        self.assertEquals(conn.delete_vpc.call_args_list, [call('vpc-id')])
        self.assertEquals(conn.delete_internet_gateway.call_args_list, [call('ig-id')])
        self.assertEquals(conn.detach_internet_gateway.call_args_list, [call('ig-id', 'vpc-id')])
Example #35
0
    def launch(self, inf, radl, requested_radl, num_vm, auth_data):
        driver = self.get_driver(auth_data)

        system = radl.systems[0]
        image_id = self.get_image_id(system.getValue("disk.0.image.url"))
        image = NodeImage(id=image_id, name=None, driver=driver)

        instance_type = self.get_instance_type(driver.list_sizes(), system)

        name = system.getValue("instance_name")
        if not name:
            name = system.getValue("disk.0.image.name")
        if not name:
            name = "userimage"

        sgs = self.create_security_groups(driver, inf, radl)

        args = {
            'size': instance_type,
            'image': image,
            'ex_security_groups': sgs,
            'name': "%s-%s" % (name, str(uuid.uuid1()))
        }

        if system.getValue('availability_zone'):
            args['location'] = system.getValue('availability_zone')

        keypair = None
        public_key = system.getValue("disk.0.os.credentials.public_key")

        if public_key and public_key.find('-----BEGIN CERTIFICATE-----') == -1:
            public_key = None
            keypair = driver.get_key_pair(public_key)
            if keypair:
                system.setUserKeyCredentials(system.getCredentials().username,
                                             None, keypair.private_key)
            else:
                args["ex_keyname"] = keypair.name
        else:
            public_key, private_key = self.keygen()
            system.setUserKeyCredentials(system.getCredentials().username,
                                         None, private_key)

        user = system.getValue('disk.0.os.credentials.username')
        if not user:
            user = self.DEFAULT_USER
            system.setValue('disk.0.os.credentials.username', user)

        tags = self.get_instance_tags(system)

        res = []
        i = 0
        while i < num_vm:
            self.log_debug("Creating node")

            vm = VirtualMachine(inf, None, self.cloud, radl, requested_radl,
                                self.cloud.getCloudConnector(inf))
            vm.destroy = True
            inf.add_vm(vm)
            cloud_init = self.get_cloud_init_data(radl, vm, public_key, user)

            if cloud_init:
                args['ex_userdata'] = cloud_init

            msg = "Error creating the node"
            try:
                node = driver.create_node(**args)
            except Exception as ex:
                msg += ": %s" % str(ex)
                self.log_exception("Error creating node.")
                node = None

            if node:
                if tags:
                    try:
                        driver.ex_create_tags([node.id], tags)
                    except:
                        self.log_exception("Error adding tags to node %s." %
                                           node.id)

                vm.id = node.id
                vm.info.systems[0].setValue('instance_id', str(node.id))
                vm.info.systems[0].setValue('instance_name', str(node.name))
                if 'zone_name' in node.extra:
                    vm.info.systems[0].setValue('availability_zone',
                                                node.extra["zone_name"])
                self.log_debug("Node successfully created.")
                vm.destroy = False
                inf.add_vm(vm)
                res.append((True, vm))
            else:
                res.append((False, msg))

            i += 1

        return res
Example #36
0
    def launch(self, inf, radl, requested_radl, num_vm, auth_data):
        driver = self.get_driver(auth_data)

        system = radl.systems[0]
        image_id = self.get_image_id(system.getValue("disk.0.image.url"))
        image = NodeImage(id=image_id, name=None, driver=driver)

        instance_type = self.get_instance_type(driver.list_sizes(), system)

        name = system.getValue("instance_name")
        if not name:
            name = system.getValue("disk.0.image.name")
        if not name:
            name = "userimage"

        nets = self.get_networks(driver, radl)

        sgs = self.create_security_group(driver, inf, radl)

        args = {'size': instance_type,
                'image': image,
                'networks': nets,
                'ex_security_groups': sgs,
                'name': "%s-%s" % (name, int(time.time() * 100))}

        cloud_init = self.get_cloud_init_data(radl)
        if cloud_init:
            args['ex_userdata'] = cloud_init

        keypair = None
        public_key = system.getValue("disk.0.os.credentials.public_key")
        if public_key:
            keypair = driver.get_key_pair(public_key)
            if keypair:
                system.setUserKeyCredentials(
                    system.getCredentials().username, None, keypair.private_key)
            else:
                if "ssh_key" in driver.features.get("create_node", []):
                    args["auth"] = NodeAuthSSHKey(public_key)
                else:
                    args["ex_keyname"] = keypair.name
        elif not system.getValue("disk.0.os.credentials.password"):
            keypair_name = "im-%d" % int(time.time() * 100.0)
            keypair = driver.create_key_pair(keypair_name)
            system.setUserKeyCredentials(
                system.getCredentials().username, None, keypair.private_key)

            if keypair.public_key and "ssh_key" in driver.features.get("create_node", []):
                args["auth"] = NodeAuthSSHKey(keypair.public_key)
            else:
                args["ex_keyname"] = keypair_name

        res = []
        i = 0
        all_failed = True
        while i < num_vm:
            self.logger.debug("Creating node")

            node = driver.create_node(**args)

            if node:
                vm = VirtualMachine(
                    inf, node.id, self.cloud, radl, requested_radl, self.cloud.getCloudConnector())
                vm.info.systems[0].setValue('instance_id', str(node.id))
                vm.info.systems[0].setValue('instance_name', str(node.name))
                # Add the keypair name to remove it later
                vm.keypair = keypair_name
                self.logger.debug("Node successfully created.")
                all_failed = False
                res.append((True, vm))
            else:
                res.append((False, "Error creating the node"))
            i += 1

        # if all the VMs have failed, remove the sg and keypair
        if all_failed:
            if (public_key is None or len(public_key) == 0 or
                    (len(public_key) >= 1 and public_key.find('-----BEGIN CERTIFICATE-----') != -1)):
                # only delete in case of the user do not specify the keypair
                # name
                driver.delete_key_pair(keypair)
            if sgs:
                driver.ex_delete_security_group(sgs[0])

        return res
Example #37
0
    def test_get_remote_port(self):
        radl_data = """
            system test (
            disk.0.os.name = 'linux'
            )"""
        radl = radl_parse.parse_radl(radl_data)
        vm = VirtualMachine(None, "1", None, radl, radl)
        port = vm.getRemoteAccessPort()
        self.assertEqual(port, 22)

        radl_data = """
            network net1 (outbound = 'yes' and
                          outports = '1022-22')
            system test (
            net_interface.0.connection = 'net1' and
            disk.0.os.name = 'linux'
            )"""
        radl = radl_parse.parse_radl(radl_data)
        vm = VirtualMachine(None, "1", None, radl, radl)
        port = vm.getRemoteAccessPort()
        self.assertEqual(port, 1022)

        radl_data = """
            system test (
            disk.0.os.name = 'windows'
            )"""
        radl = radl_parse.parse_radl(radl_data)
        vm = VirtualMachine(None, "1", None, radl, radl)
        port = vm.getRemoteAccessPort()
        self.assertEqual(port, 5986)

        radl_data = """
            network net1 (outbound = 'yes' and
                          outports = '105986-5986')
            system test (
            net_interface.0.connection = 'net1' and
            disk.0.os.name = 'windows'
            )"""
        radl = radl_parse.parse_radl(radl_data)
        vm = VirtualMachine(None, "1", None, radl, radl)
        port = vm.getRemoteAccessPort()
        self.assertEqual(port, 105986)
Example #38
0
    def test_30_updateVMInfo_spot(self, 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.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)
        radl.check()

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

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

        conn = MagicMock()
        get_connection.return_value = conn

        reservation = MagicMock()
        instance = MagicMock()
        instance.update.return_value = True
        instance.tags = []
        instance.virtualization_type = "vt"
        instance.placement = "us-east-1"
        instance.state = "running"
        instance.instance_type = "t1.micro"
        instance.launch_time = "2016-12-31T00:00:00"
        instance.ip_address = "158.42.1.1"
        instance.private_ip_address = "10.0.0.1"
        instance.connection = conn
        reservation.instances = [instance]
        conn.get_all_instances.return_value = [reservation]

        conn.get_all_addresses.return_value = []

        sir = MagicMock()
        sir.state = ""
        sir.id = "id"
        conn.get_all_spot_instance_requests.return_value = [sir]

        volume = MagicMock()
        volume.status = "available"
        volume.id = "volid"
        conn.create_volume.return_value = volume
        conn.attach_volume.return_value = True

        success, vm = ec2_cloud.updateVMInfo(vm, auth)

        self.assertTrue(success, msg="ERROR: updating VM info.")
        self.assertNotIn("ERROR",
                         self.log.getvalue(),
                         msg="ERROR found in log: %s" % self.log.getvalue())
Example #39
0
    def test_30_updateVMInfo(self, get_driver):
        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.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 = '******'
            )"""
        radl = radl_parse.parse_radl(radl_data)
        radl.check()

        auth = Authentication([{
            'id': 'ost',
            'type': 'OpenStack',
            'username': '******',
            'password': '******',
            'tenant': 'tenant',
            'host': 'https://server.com:5000'
        }])
        ost_cloud = self.get_ost_cloud()

        inf = MagicMock()
        inf.get_next_vm_id.return_value = 1
        vm = VirtualMachine(inf, "1", ost_cloud.cloud, radl, radl, ost_cloud)

        driver = MagicMock()
        get_driver.return_value = driver

        node = MagicMock()
        node.id = "1"
        node.state = "running"
        node.extra = {
            'flavorId': 'small',
            'addresses': {
                'os-lan': [{
                    'addr': '10.0.0.1',
                    'OS-EXT-IPS:type': 'fixed'
                }]
            }
        }
        node.public_ips = []
        node.private_ips = ['10.0.0.1']
        node.driver = driver
        driver.list_nodes.return_value = [node]

        node_size = MagicMock()
        node_size.ram = 512
        node_size.price = 1
        node_size.disk = 1
        node_size.vcpus = 1
        node_size.name = "small"
        driver.ex_get_size.return_value = node_size

        volume = MagicMock()
        volume.id = "vol1"
        volume.attach.return_value = True
        driver.create_volume.return_value = volume

        pool = MagicMock()
        pool.name = "pool1"
        pool.list_floating_ips.return_value = []
        pool.create_floating_ip.return_value = True
        driver.ex_list_floating_ip_pools.return_value = [pool]

        success, vm = ost_cloud.updateVMInfo(vm, auth)

        self.assertTrue(success, msg="ERROR: updating VM info.")
        self.assertNotIn("ERROR",
                         self.log.getvalue(),
                         msg="ERROR found in log: %s" % self.log.getvalue())
        self.clean_log()
Example #40
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 #41
0
    def test_30_updateVMInfo(self, get_driver):
        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 = 'aws://ami-id' and
            disk.0.os.credentials.username = '******' and
            disk.0.os.credentials.password = '******'
            )"""
        radl = radl_parse.parse_radl(radl_data)
        radl.check()

        auth = Authentication([{
            'id': 'libcloud',
            'type': 'LibCloud',
            'username': '******',
            'password': '******',
            'driver': 'EC2'
        }])
        lib_cloud = self.get_lib_cloud()

        inf = MagicMock()
        vm = VirtualMachine(inf, "1", lib_cloud.cloud, radl, radl, lib_cloud,
                            1)

        driver = MagicMock()
        driver.name = "Amazon EC2"
        get_driver.return_value = driver

        node = MagicMock()
        node.id = "1"
        node.state = "running"
        node.extra = {'availability': 'use-east-1'}
        node.public_ips = []
        node.private_ips = ['10.0.0.1']
        node.driver = driver
        node.size = MagicMock()
        node.size.ram = 512
        node.size.price = 1
        node.size.disk = 1
        node.size.vcpus = 1
        node.size.name = "small"
        driver.list_nodes.return_value = [node]

        volume = MagicMock()
        volume.id = "vol1"
        volume.extra = {"state": "available"}
        volume.attach.return_value = True
        driver.create_volume.return_value = volume

        driver.ex_allocate_address.return_value = "10.0.0.1"

        success, vm = lib_cloud.updateVMInfo(vm, auth)

        self.assertTrue(success, msg="ERROR: updating VM info.")
        self.assertNotIn("ERROR",
                         self.log.getvalue(),
                         msg="ERROR found in log: %s" % self.log.getvalue())
Example #42
0
    def launch(self, inf, radl, requested_radl, num_vm, auth_data):
        driver = self.get_driver(auth_data)

        system = radl.systems[0]
        image_id = self.get_image_id(system.getValue("disk.0.image.url"))
        image = NodeImage(id=image_id, name=None, driver=driver)

        instance_type = self.get_instance_type(driver.list_sizes(), system)

        name = system.getValue("instance_name")
        if not name:
            name = system.getValue("disk.0.image.name")
        if not name:
            name = "userimage"

        args = {'size': instance_type,
                'image': image,
                'name': "%s-%s" % (name, int(time.time() * 100))}

        keypair = None
        public_key = system.getValue("disk.0.os.credentials.public_key")
        if self.driver_uses_keypair(driver):
            if public_key:
                keypair = driver.get_key_pair(public_key)
                if keypair:
                    system.setUserKeyCredentials(
                        system.getCredentials().username, None, keypair.private_key)
                else:
                    if "ssh_key" in driver.features.get("create_node", []):
                        args["auth"] = NodeAuthSSHKey(public_key)
                    else:
                        args["ex_keyname"] = keypair.name
            elif not system.getValue("disk.0.os.credentials.password"):
                keypair_name = "im-%d" % int(time.time() * 100.0)
                keypair = driver.create_key_pair(keypair_name)
                system.setUserKeyCredentials(
                    system.getCredentials().username, None, keypair.private_key)

                if keypair.public_key and "ssh_key" in driver.features.get("create_node", []):
                    args["auth"] = NodeAuthSSHKey(keypair.public_key)
                else:
                    args["ex_keyname"] = keypair_name

        res = []
        i = 0
        while i < num_vm:
            self.logger.debug("Creating node")

            node = driver.create_node(**args)

            if node:
                vm = VirtualMachine(
                    inf, node.id, self.cloud, radl, requested_radl, self.cloud.getCloudConnector())
                vm.info.systems[0].setValue('instance_id', str(node.id))
                vm.info.systems[0].setValue('instance_name', str(node.name))
                # Add the keypair name to remove it later
                vm.keypair = keypair_name
                self.logger.debug("Node successfully created.")
                res.append((True, vm))
            else:
                res.append((False, "Error creating the node"))

            i += 1

        return res
Example #43
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()
Example #44
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