Пример #1
0
    def test_create_disk_snapshot(self):
        """Test CreateDiskSnapshot """
        radl = RADL()
        radl.add(
            system("s0", [
                Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"),
                Feature("disk.0.os.credentials.username", "=", "user"),
                Feature("disk.0.os.credentials.password", "=", "pass")
            ]))
        radl.add(deploy("s0", 1))

        new_url = "mock0://linux.for.ev.er/test"

        cloud0 = self.get_cloud_connector_mock("MyMock0")
        cloud0.create_snapshot = Mock(return_value=(True, new_url))
        self.register_cloudconnector("Mock0", cloud0)
        auth0 = self.getAuth([0], [], [("Mock0", 0)])

        infId = IM.CreateInfrastructure(str(radl), auth0)

        InfrastructureList.infrastructure_list[infId].vm_list[
            0].cloud_connector = cloud0

        res = IM.CreateDiskSnapshot(infId, 0, 0, "test", True, auth0)
        self.assertEqual(res, new_url)

        self.assertEqual(cloud0.create_snapshot.call_count, 1)
Пример #2
0
 def test_tosca_add_hybrid2(self):
     tosca_data = read_file_as_string('../files/tosca_add_hybrid.yml')
     tosca = Tosca(tosca_data)
     inf_info = MagicMock()
     vm1 = MagicMock()
     system1 = system("lrms_server", [
         Feature("disk.0.image.url", "=", "ost://cloud1.com/image1"),
         Feature("net_interface.0.connection", "=", "private_net")
     ])
     vm1.info.systems = [system1]
     vm2 = MagicMock()
     system2 = system("lrms_wn", [
         Feature("disk.0.image.url", "=", "ost://cloud3.com/image1"),
         Feature("net_interface.0.connection", "=", "private.cloud3.com")
     ])
     vm2.info.systems = [system2]
     inf_info.get_vm_list_by_system_name.return_value = {
         "lrms_server": [vm1],
         "lrms_wn": [vm2]
     }
     net = MagicMock()
     net.isPublic.return_value = False
     inf_info.radl.get_network_by_id.return_value = net
     _, radl = tosca.to_radl(inf_info)
     print(radl)
     radl = parse_radl(str(radl))
     lrms_wn = radl.get_system_by_name("lrms_wn")
     self.assertEqual("private.cloud2.com",
                      lrms_wn.getValue("net_interface.0.connection"))
Пример #3
0
    def update_system_info_from_instance(self, system, instance_type):
        """
        Update the features of the system with the information of the instance_type
        """
        if isinstance(instance_type, NodeSize):
            system.addFeature(Feature("memory.size", "=", instance_type.ram,
                                      'M'),
                              conflict="other",
                              missing="other")
            if instance_type.disk:
                system.addFeature(Feature("disk.0.free_size", "=",
                                          instance_type.disk, 'G'),
                                  conflict="other",
                                  missing="other")
            if instance_type.price:
                system.addFeature(Feature("price", "=", instance_type.price),
                                  conflict="me",
                                  missing="other")
            if 'guestCpus' in instance_type.extra:
                system.addFeature(Feature("cpu.count", "=",
                                          instance_type.extra['guestCpus']),
                                  conflict="other",
                                  missing="other")

            system.addFeature(Feature("instance_type", "=",
                                      instance_type.name),
                              conflict="other",
                              missing="other")
Пример #4
0
    def test_altervm(self):
        """Test AlterVM"""
        radl = RADL()
        radl.add(
            system("s0", [
                Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"),
                Feature("disk.0.os.credentials.username", "=", "user"),
                Feature("cpu.count", "=", 1),
                Feature("memory.size", "=", 512, "M"),
                Feature("disk.0.os.credentials.password", "=", "pass")
            ]))
        radl.add(deploy("s0", 1))

        auth0 = self.getAuth([0], [], [("Dummy", 0)])
        infId = IM.CreateInfrastructure(str(radl), auth0)

        radl = RADL()
        radl.add(
            system("s0", [
                Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"),
                Feature("disk.0.os.credentials.username", "=", "user"),
                Feature("cpu.count", "=", 2),
                Feature("memory.size", "=", 1024, "M"),
                Feature("disk.0.os.credentials.password", "=", "pass")
            ]))
        radl.add(deploy("s0", 1))

        radl_info = IM.AlterVM(infId, "0", str(radl), auth0)
        parsed_radl_info = parse_radl(str(radl_info))
        self.assertEqual(parsed_radl_info.systems[0].getValue("cpu.count"), 2)
        self.assertEqual(
            parsed_radl_info.systems[0].getFeature('memory.size').getValue(
                'M'), 1024)

        IM.DestroyInfrastructure(infId, auth0)
Пример #5
0
    def test_create_async(self):
        """Create Inf. async"""

        radl = RADL()
        radl.add(
            system("s0", [
                Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"),
                Feature("disk.0.os.credentials.username", "=", "user"),
                Feature("disk.0.os.credentials.password", "=", "pass")
            ]))
        radl.add(deploy("s0", 1))

        cloud = type("MyMock0", (CloudConnector, object), {})
        cloud.launch = Mock(side_effect=self.sleep_and_create_vm)
        cloud.finalize = Mock(return_value=(True, ""))
        self.register_cloudconnector("Mock", cloud)
        auth0 = self.getAuth([0], [], [("Mock", 0)])

        before = int(time.time())
        infId = IM.CreateInfrastructure(str(radl), auth0, True)
        delay = int(time.time()) - before

        self.assertLess(delay, 2)

        time.sleep(6)

        IM.DestroyInfrastructure(infId, auth0)
Пример #6
0
    def test_start_stop(self):
        """Test Start and Stop operations"""
        radl = RADL()
        radl.add(
            system("s0", [
                Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"),
                Feature("disk.0.os.credentials.username", "=", "user"),
                Feature("disk.0.os.credentials.password", "=", "pass")
            ]))
        radl.add(deploy("s0", 1))

        auth0 = self.getAuth([0], [], [("Dummy", 0)])
        infId = IM.CreateInfrastructure(str(radl), auth0)

        res = IM.StopInfrastructure(infId, auth0)
        self.assertEqual(res, "")
        res = IM.StartInfrastructure(infId, auth0)
        self.assertEqual(res, "")

        res = IM.StartVM(infId, "0", auth0)
        self.assertEqual(res, "")
        res = IM.StopVM(infId, "0", auth0)
        self.assertEqual(res, "")

        IM.DestroyInfrastructure(infId, auth0)
Пример #7
0
    def concreteSystem(self, radl_system, auth_data):
        image_urls = radl_system.getValue("disk.0.image.url")
        if not image_urls:
            return [radl_system.clone()]
        else:
            if not isinstance(image_urls, list):
                image_urls = [image_urls]

            res = []
            for str_url in image_urls:
                url = uriparse(str_url)
                protocol = url[0]
                if protocol in ['fbw']:
                    res_system = radl_system.clone()

                    res_system.addFeature(
                        Feature("disk.0.image.url", "=", str_url), conflict="other", missing="other")

                    res_system.addFeature(
                        Feature("provider.type", "=", self.type), conflict="other", missing="other")
                    res_system.addFeature(Feature(
                        "provider.host", "=", self.cloud.server), conflict="other", missing="other")
                    res_system.addFeature(Feature(
                        "provider.port", "=", self.cloud.port), conflict="other", missing="other")

                    res_system.delValue('disk.0.os.credentials.username')
                    res_system.setValue('disk.0.os.credentials.username', 'fogbow')

                    res.append(res_system)

            return res
Пример #8
0
    def test_inf_addresources0(self):
        """Deploy single virtual machines and test reference."""
        radl = RADL()
        radl.add(
            system("s0", [
                Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"),
                Feature("disk.0.os.credentials.username", "=", "user"),
                Feature("disk.0.os.credentials.password", "=", "pass")
            ]))
        radl.add(deploy("s0", 1))

        auth0 = self.getAuth([0], [], [("Dummy", 0)])
        infId = IM.CreateInfrastructure("", auth0)

        vms = IM.AddResource(infId, str(radl), auth0)
        self.assertEqual(vms, [0])

        # Test references
        radl = RADL()
        radl.add(system("s0", reference=True))
        radl.add(deploy("s0", 1))
        vms = IM.AddResource(infId, str(radl), auth0)
        self.assertEqual(vms, [1])

        IM.DestroyInfrastructure(infId, auth0)
Пример #9
0
    def test_get_vm_info(self):
        """
        Test GetVMInfo and GetVMProperty and GetVMContMsg and GetInfrastructureRADL and
        GetInfrastructureContMsg and GetInfrastructureState.
        """
        radl = RADL()
        radl.add(
            system("s0", [
                Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"),
                Feature("disk.0.os.credentials.username", "=", "user"),
                Feature("disk.0.os.credentials.password", "=", "pass")
            ]))
        radl.add(deploy("s0", 1))

        auth0 = self.getAuth([0], [], [("Dummy", 0)])
        infId = IM.CreateInfrastructure(str(radl), auth0)

        radl_info = IM.GetVMInfo(infId, "0", auth0)
        parsed_radl_info = parse_radl(str(radl_info))
        self.assertEqual(parsed_radl_info.systems[0].getValue("state"),
                         "running")

        radl_info = IM.GetVMInfo(infId, "0", auth0, True)
        parsed_radl_info = parse_radl_json(radl_info)
        self.assertEqual(parsed_radl_info.systems[0].getValue("state"),
                         "running")

        state = IM.GetVMProperty(infId, "0", "state", auth0)
        self.assertEqual(state, "running")

        contmsg = IM.GetVMContMsg(infId, "0", auth0)
        self.assertEqual(contmsg, "")

        InfrastructureList.infrastructure_list[infId].cont_out = "Header"
        InfrastructureList.infrastructure_list[infId].vm_list[
            0].cloud_connector = MagicMock()
        InfrastructureList.infrastructure_list[infId].vm_list[
            0].cloud_connector.error_messages = "TESTMSG"
        contmsg = IM.GetInfrastructureContMsg(infId, auth0)
        header_contmsg = IM.GetInfrastructureContMsg(infId, auth0, True)
        InfrastructureList.infrastructure_list[infId].vm_list[
            0].cloud_connector = None

        self.assertIn("TESTMSG", contmsg)
        self.assertNotIn("TESTMSG", header_contmsg)
        self.assertIn("Header", header_contmsg)

        state = IM.GetInfrastructureState(infId, auth0)
        self.assertEqual(state["state"], "running")
        self.assertEqual(state["vm_states"]["0"], "running")

        radl_info = IM.GetInfrastructureRADL(infId, auth0)
        parsed_radl_info = parse_radl(str(radl_info))
        self.assertEqual(
            parsed_radl_info.systems[0].getValue(
                "disk.0.os.credentials.username"), "user")

        IM.DestroyInfrastructure(infId, auth0)
Пример #10
0
    def concreteSystem(self, radl_system, auth_data):
        image_urls = radl_system.getValue("disk.0.image.url")
        if not image_urls:
            return [radl_system.clone()]
        else:
            if not isinstance(image_urls, list):
                image_urls = [image_urls]

            res = []
            for str_url in image_urls:
                url = uriparse(str_url)
                protocol = url[0]

                protocol = url[0]
                driver = self.get_driver(auth_data)

                PROTOCOL_MAP = {
                    "Amazon EC2": "aws",
                    "OpenNebula": "one",
                    "OpenStack": "ost",
                    "LibVirt": "file"
                }

                req_protocol = PROTOCOL_MAP.get(driver.name, None)

                if req_protocol is None or protocol == req_protocol:
                    res_system = radl_system.clone()
                    instance_type = self.get_instance_type(
                        driver.list_sizes(), res_system)
                    self.update_system_info_from_instance(
                        res_system, instance_type)

                    res_system.addFeature(Feature("disk.0.image.url", "=",
                                                  str_url),
                                          conflict="other",
                                          missing="other")

                    res_system.addFeature(Feature("provider.type", "=",
                                                  self.type),
                                          conflict="other",
                                          missing="other")
                    if self.cloud.server:
                        res_system.addFeature(Feature("provider.host", "=",
                                                      self.cloud.server),
                                              conflict="other",
                                              missing="other")
                    if self.cloud.port != -1:
                        res_system.addFeature(Feature("provider.port", "=",
                                                      self.cloud.port),
                                              conflict="other",
                                              missing="other")

                    res.append(res_system)
            return res
Пример #11
0
 def update_system_info_from_instance(self, system, instance_type):
     """
     Update the features of the system with the information of the instance_type
     """
     system.addFeature(Feature("cpu.count", "=", instance_type.Cores),
                       conflict="other", missing="other")
     system.addFeature(Feature("memory.size", "=", instance_type.MemoryInMb, 'M'),
                       conflict="other", missing="other")
     system.addFeature(Feature("disks.free_size", "=", instance_type.VirtualMachineResourceDiskSizeInMb, 'M'),
                       conflict="other", missing="other")
     system.addFeature(Feature("instance_type", "=", instance_type.Name),
                       conflict="other", missing="other")
Пример #12
0
    def concreteSystem(self, radl_system, auth_data):
        image_urls = radl_system.getValue("disk.0.image.url")
        if not image_urls:
            return [radl_system.clone()]
        else:
            if not isinstance(image_urls, list):
                image_urls = [image_urls]

            res = []
            for str_url in image_urls:
                url = uriparse(str_url)
                protocol = url[0]
                src_host = url[1].split(':')[0]
                # TODO: check the port
                if (protocol == "one") and self.cloud.server == src_host:
                    # Check the space in image and compare with disks.free_size
                    if radl_system.getValue('disks.free_size'):
                        disk_free = int(
                            radl_system.getFeature('disks.free_size').getValue(
                                'M'))
                        # The VMRC specified the value in MB
                        disk_size = int(radl_system.getValue("disk.0.size"))

                        if disk_size < disk_free:
                            # if the image do not have enough space, discard it
                            return []

                    res_system = radl_system.clone()

                    res_system.getFeature("cpu.count").operator = "="
                    res_system.getFeature("memory.size").operator = "="

                    res_system.addFeature(Feature("disk.0.image.url", "=",
                                                  str_url),
                                          conflict="other",
                                          missing="other")

                    res_system.addFeature(Feature("provider.type", "=",
                                                  self.type),
                                          conflict="other",
                                          missing="other")
                    res_system.addFeature(Feature("provider.host", "=",
                                                  self.cloud.server),
                                          conflict="other",
                                          missing="other")
                    res_system.addFeature(Feature("provider.port", "=",
                                                  self.cloud.port),
                                          conflict="other",
                                          missing="other")

                    res.append(res_system)

            return res
Пример #13
0
 def update_system_info_from_instance(system, instance_type):
     """
     Update the features of the system with the information of the instance_type
     """
     system.addFeature(Feature("cpu.count", "=", instance_type.number_of_cores),
                       conflict="other", missing="other")
     system.addFeature(Feature("memory.size", "=", instance_type.memory_in_mb, 'M'),
                       conflict="other", missing="other")
     system.addFeature(Feature("disks.free_size", "=", instance_type.resource_disk_size_in_mb, 'M'),
                       conflict="other", missing="other")
     system.addFeature(Feature("instance_type", "=", instance_type.name),
                       conflict="other", missing="other")
Пример #14
0
    def concreteSystem(self, radl_system, auth_data):
        image_urls = radl_system.getValue("disk.0.image.url")
        if not image_urls:
            return [radl_system.clone()]
        else:
            if not isinstance(image_urls, list):
                image_urls = [image_urls]

            res = []
            for str_url in image_urls:
                url = uriparse(str_url)
                protocol = url[0]

                src_host = url[1].split(':')[0]
                # TODO: check the port
                if protocol == "ost" and self.cloud.server == src_host:
                    driver = self.get_driver(auth_data)

                    res_system = radl_system.clone()
                    instance_type = self.get_instance_type(
                        driver.list_sizes(), res_system)
                    self.update_system_info_from_instance(
                        res_system, instance_type)

                    res_system.addFeature(Feature("disk.0.image.url", "=",
                                                  str_url),
                                          conflict="other",
                                          missing="other")

                    res_system.addFeature(Feature("provider.type", "=",
                                                  self.type),
                                          conflict="other",
                                          missing="other")
                    res_system.addFeature(Feature("provider.host", "=",
                                                  self.cloud.server),
                                          conflict="other",
                                          missing="other")
                    res_system.addFeature(Feature("provider.port", "=",
                                                  self.cloud.port),
                                          conflict="other",
                                          missing="other")

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

                    res.append(res_system)

            return res
Пример #15
0
    def test_inf_cloud_order(self, suds_cli):
        """Test cloud selection in base of the auth data order."""

        n0, n1 = 1, 1  # Machines to deploy
        radl = RADL()
        radl.add(
            system("s0", [
                Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"),
                Feature("cpu.count", "=", 1),
                Feature("disk.0.os.credentials.username", "=", "user"),
                Feature("disk.0.os.credentials.password", "=", "pass")
            ]))
        radl.add(deploy("s0", n0))
        radl.add(
            system("s1", [
                Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"),
                Feature("cpu.count", "=", 1),
                Feature("disk.0.os.credentials.username", "=", "user"),
                Feature("disk.0.os.credentials.password", "=", "pass")
            ]))
        radl.add(deploy("s1", n1))

        cloud0 = self.get_cloud_connector_mock("MyMock0")
        self.register_cloudconnector("Mock0", cloud0)
        cloud1 = self.get_cloud_connector_mock("MyMock1")
        self.register_cloudconnector("Mock1", cloud1)
        auth0 = self.getAuth([0], [0], [("Mock0", 0), ("Mock1", 1)])
        infId = IM.CreateInfrastructure(str(radl), auth0)
        self.assertEqual(cloud0.launch.call_count, n0 + n1)
        IM.DestroyInfrastructure(infId, auth0)
Пример #16
0
    def concreteSystem(self, radl_system, auth_data):
        image_urls = radl_system.getValue("disk.0.image.url")
        if not image_urls:
            return [radl_system.clone()]
        else:
            if not isinstance(image_urls, list):
                image_urls = [image_urls]

            res = []
            for str_url in image_urls:
                url = uriparse(str_url)
                protocol = url[0]
                if protocol == 'docker' and url[1]:
                    res_system = radl_system.clone()

                    res_system.addFeature(Feature("virtual_system_type", "=",
                                                  "docker"),
                                          conflict="other",
                                          missing="other")

                    res_system.getFeature("cpu.count").operator = "="
                    res_system.getFeature("memory.size").operator = "="

                    res_system.setValue('disk.0.os.credentials.username',
                                        'root')
                    res_system.setValue('disk.0.os.credentials.password',
                                        self._root_password)

                    res_system.addFeature(Feature("disk.0.image.url", "=",
                                                  str_url),
                                          conflict="other",
                                          missing="other")

                    res_system.addFeature(Feature("provider.type", "=",
                                                  self.type),
                                          conflict="other",
                                          missing="other")
                    res_system.addFeature(Feature("provider.host", "=",
                                                  self.cloud.server),
                                          conflict="other",
                                          missing="other")
                    res_system.addFeature(Feature("provider.port", "=",
                                                  self.cloud.port),
                                          conflict="other",
                                          missing="other")

                    res.append(res_system)

            return res
Пример #17
0
    def update_system_info_from_instance(self, system, instance_type):
        """
        Update the features of the system with the information of the instance_type
        """
        system.addFeature(Feature(
            "memory.size", "=", instance_type.ram, 'M'), conflict="other", missing="other")
        if instance_type.disk:
            system.addFeature(Feature(
                "disk.0.free_size", "=", instance_type.disk, 'G'), conflict="other", missing="other")
        if instance_type.price:
            system.addFeature(
                Feature("price", "=", instance_type.price), conflict="me", missing="other")

        system.addFeature(Feature("instance_type", "=",
                                  instance_type.name), conflict="other", missing="other")
Пример #18
0
Файл: Azure.py Проект: nakos/im
    def concreteSystem(self, radl_system, auth_data):
        image_urls = radl_system.getValue("disk.0.image.url")
        if not image_urls:
            return [radl_system.clone()]
        else:
            if not isinstance(image_urls, list):
                image_urls = [image_urls]

            res = []
            for str_url in image_urls:
                url = uriparse(str_url)
                protocol = url[0]

                protocol = url[0]
                if protocol == "azr":
                    credentials, subscription_id = self.get_credentials(
                        auth_data)

                    res_system = radl_system.clone()
                    instance_type = self.get_instance_type(
                        res_system, credentials, subscription_id)
                    if not instance_type:
                        self.log_error(
                            "Error generating the RADL of the VM, no instance type available for the requirements."
                        )
                        self.log_debug(res_system)
                    else:
                        res_system.addFeature(Feature("disk.0.image.url", "=",
                                                      str_url),
                                              conflict="other",
                                              missing="other")
                        self.update_system_info_from_instance(
                            res_system, instance_type)
                        res_system.addFeature(Feature("provider.type", "=",
                                                      self.type),
                                              conflict="other",
                                              missing="other")

                        username = res_system.getValue(
                            'disk.0.os.credentials.username')
                        if not username:
                            res_system.setValue(
                                'disk.0.os.credentials.username', 'azureuser')

                        res_system.updateNewCredentialValues()

                        res.append(res_system)
            return res
Пример #19
0
    def concreteSystem(self, radl_system, auth_data):
        image_urls = radl_system.getValue("disk.0.image.url")
        if not image_urls:
            return [radl_system.clone()]
        else:
            if not isinstance(image_urls, list):
                image_urls = [image_urls]

            res = []
            for str_url in image_urls:
                url = uriparse(str_url)
                protocol = url[0]
                if protocol == "gce":
                    driver = self.get_driver(auth_data)

                    res_system = radl_system.clone()
                    res_system.addFeature(Feature("disk.0.image.url", "=",
                                                  str_url),
                                          conflict="other",
                                          missing="other")

                    if res_system.getValue('availability_zone'):
                        region = res_system.getValue('availability_zone')
                    else:
                        region, _ = self.get_image_data(str_url)

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

                    if not instance_type:
                        return []

                    self.update_system_info_from_instance(
                        res_system, instance_type)

                    username = res_system.getValue(
                        'disk.0.os.credentials.username')
                    if not username:
                        res_system.setValue('disk.0.os.credentials.username',
                                            'gceuser')
                    res_system.addFeature(Feature("provider.type", "=",
                                                  self.type),
                                          conflict="other",
                                          missing="other")

                    res.append(res_system)

            return res
Пример #20
0
    def test_inf_addresources_without_credentials(self):
        """Deploy single virtual machine without credentials to check that it raises the correct exception."""

        radl = RADL()
        radl.add(
            system(
                "s0",
                [Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er")]))
        radl.add(deploy("s0", 1))

        auth0 = self.getAuth([0], [], [("Dummy", 0)])
        infId = IM.CreateInfrastructure("", auth0)

        vms = IM.AddResource(infId, str(radl), auth0)

        self.assertEqual(vms, [0])

        res = IM.GetInfrastructureState(infId, auth0)
        self.assertEqual(res['state'], VirtualMachine.FAILED)

        res = IM.GetVMContMsg(infId, 0, auth0)
        self.assertEqual(res, (
            "Error launching the VMs of type s0 to cloud ID cloud0 of type Dummy."
            " No username for deploy: s0\n"))

        IM.DestroyInfrastructure(infId, auth0)
Пример #21
0
    def test_db(self):
        """ Test DB data access """
        inf = InfrastructureInfo()
        inf.id = "1"
        inf.auth = self.getAuth([0], [], [("Dummy", 0)])
        cloud = CloudInfo()
        cloud.type = "Dummy"
        radl = RADL()
        radl.add(
            system(
                "s0",
                [Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er")]))
        radl.add(deploy("s0", 1))
        vm1 = VirtualMachine(inf, "1", cloud, radl, radl)
        vm2 = VirtualMachine(inf, "2", cloud, radl, radl)
        inf.vm_list = [vm1, vm2]
        inf.vm_master = vm1
        # first create the DB table
        Config.DATA_DB = "sqlite:///tmp/ind.dat"
        InfrastructureList.load_data()

        success = InfrastructureList._save_data_to_db(Config.DATA_DB,
                                                      {"1": inf})
        self.assertTrue(success)

        res = InfrastructureList._get_data_from_db(Config.DATA_DB)
        self.assertEqual(len(res), 1)
        self.assertEqual(len(res['1'].vm_list), 2)
        self.assertEqual(res['1'].vm_list[0], res['1'].vm_master)
        self.assertEqual(
            res['1'].vm_master.info.systems[0].getValue("disk.0.image.url"),
            "mock0://linux.for.ev.er")
        self.assertTrue(res['1'].auth.compare(inf.auth,
                                              "InfrastructureManager"))
Пример #22
0
    def complete_radl(self, radl):
        """
        Update passed radl with the stored RADL.
        """

        with self._lock:
            # Replace references of systems, networks and configures by its
            # definitions
            for s in radl.networks + radl.systems + radl.configures + radl.ansible_hosts:
                if s.reference:
                    aspect = self.radl.get(s)
                    if aspect is None:
                        raise Exception(
                            "Unknown reference in RADL to %s '%s'" %
                            (type(s), s.getId()))
                    radl.add(aspect.clone(), "replace")

            # Add fake deploys to indicate the cloud provider associated to a
            # private network.
            system_counter = 0
            for n in radl.networks:
                if n.id in self.private_networks:
                    system_id = self.FAKE_SYSTEM + str(system_counter)
                    system_counter += 1
                    radl.add(
                        system(
                            system_id,
                            [Feature("net_interface.0.connection", "=", n.id)
                             ]))
                    radl.add(deploy(system_id, 0, self.private_networks[n.id]))

        # Check the RADL
        radl.check()
Пример #23
0
    def concrete_system(self, radl_system, str_url, auth_data):
        url = urlparse(str_url)
        protocol = url[0]

        if protocol == "gce":
            driver = self.get_driver(auth_data)

            res_system = radl_system.clone()
            res_system.addFeature(Feature("disk.0.image.url", "=", str_url),
                                  conflict="other",
                                  missing="other")

            if res_system.getValue('availability_zone'):
                region = res_system.getValue('availability_zone')
            else:
                region, _ = self.get_image_data(str_url)

            instance_type = self.get_instance_type(driver.list_sizes(region),
                                                   res_system)
            if not instance_type:
                return None

            self.update_system_info_from_instance(res_system, instance_type)

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

            return res_system
        else:
            return None
Пример #24
0
    def concreteSystem(self, radl_system, auth_data):
        """
        Return a list of compatible systems with the cloud

        Arguments:

           - radl_system(:py:class:`radl.system`): a system.
           - auth_data(:py:class:`dict` of str objects): Authentication data to access cloud provider.

        Returns(list of system): list of compatible systems.
        """
        image_urls = radl_system.getValue("disk.0.image.url")
        if not image_urls:
            return [radl_system.clone()]
        else:
            if not isinstance(image_urls, list):
                image_urls = [image_urls]

            res = []

            for str_url in image_urls:
                res_system = self.concrete_system(radl_system, str_url,
                                                  auth_data)
                if res_system:
                    res_system.addFeature(Feature("disk.0.image.url", "=",
                                                  str_url),
                                          conflict="other",
                                          missing="other")
                    res_system.addFeature(Feature("provider.type", "=",
                                                  self.type),
                                          conflict="other",
                                          missing="other")
                    if self.cloud.server:
                        res_system.addFeature(Feature("provider.host", "=",
                                                      self.cloud.server),
                                              conflict="other",
                                              missing="other")
                    if self.cloud.port != -1:
                        res_system.addFeature(Feature("provider.port", "=",
                                                      self.cloud.port),
                                              conflict="other",
                                              missing="other")
                    res.append(res_system)

            return res
Пример #25
0
    def test_export_import(self):
        """Test ExportInfrastructure and ImportInfrastructure operations"""
        radl = RADL()
        radl.add(
            system("s0", [
                Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"),
                Feature("disk.0.os.credentials.username", "=", "user"),
                Feature("disk.0.os.credentials.password", "=", "pass")
            ]))
        radl.add(deploy("s0", 1))

        auth0 = self.getAuth([0], [], [("Dummy", 0)])
        infId = IM.CreateInfrastructure(str(radl), auth0)

        res = IM.ExportInfrastructure(infId, True, auth0)
        new_inf_id = IM.ImportInfrastructure(res, auth0)

        IM.DestroyInfrastructure(new_inf_id, auth0)
Пример #26
0
 def update_system_info_from_instance(self, system, instance_type):
     """
     Update the features of the system with the information of the instance_type
     """
     if instance_type:
         LibCloudCloudConnector.update_system_info_from_instance(self, system, instance_type)
         if instance_type.vcpus:
             system.addFeature(
                 Feature("cpu.count", "=", instance_type.vcpus), conflict="me", missing="other")
Пример #27
0
    def alterVM(self, vm, radl, auth_data):
        # This function is correctly implemented
        # But kubernetes does not permit cpu to be updated yet
        system = radl.systems[0]

        apiVersion = self.get_api_version(auth_data)

        try:
            pod_data = []

            cpu = vm.info.systems[0].getValue('cpu.count')
            memory = vm.info.systems[0].getFeature('memory.size').getValue('B')

            new_cpu = system.getValue('cpu.count')
            new_memory = system.getFeature('memory.size').getValue('B')

            changed = False
            if new_cpu and new_cpu != cpu:
                pod_data.append(
                    {"op": "replace", "path": "/spec/containers/0/resources/limits/cpu", "value": new_cpu})
                changed = True
            if new_memory and new_memory != memory:
                pod_data.append(
                    {"op": "replace", "path": "/spec/containers/0/resources/limits/memory", "value": new_memory})
                changed = True

            if not changed:
                self.log_debug(
                    "Nothing changes in the kubernetes pod: " + str(vm.id))
                return (True, vm)

            # Create the container
            namespace = vm.inf.id
            pod_name = vm.id

            headers = {'Content-Type': 'application/json-patch+json'}
            uri = "/api/" + apiVersion + "/namespaces/" + namespace + "/pods/" + pod_name
            body = json.dumps(pod_data)
            resp = self.create_request('PATCH', uri, auth_data, headers, body)

            if resp.status_code != 201:
                return (False, "Error updating the Pod: " + resp.text)
            else:
                if new_cpu:
                    vm.info.systems[0].setValue('cpu.count', new_cpu)
                if new_memory:
                    vm.info.systems[0].addFeature(
                        Feature("memory.size", "=", new_memory, 'B'), conflict="other", missing="other")
                return (True, self.updateVMInfo(vm, auth_data))

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

        return (False, "Not supported")
Пример #28
0
    def _toRADLSystem(vmi):

        # Pass common features
        VMRC_RADL_MAP = {
            'hypervisor': ('virtual_system_type', str),
            'diskSize': ('disk.0.size', int),
            'arch': ('cpu.arch', str),
            'location': ('disk.0.image.url', str),
            'name': ('disk.0.image.name', str),
            'userLogin': ('disk.0.os.credentials.username', str),
            'userPassword': ('disk.0.os.credentials.password', str)
        }
        fs = [
            Feature(VMRC_RADL_MAP[prop][0], "=",
                    VMRC_RADL_MAP[prop][1](getattr(vmi, prop)))
            for prop in VMRC_RADL_MAP
            if hasattr(vmi, prop) and getattr(vmi, prop)
        ]
        fs.extend([
            Feature("disk.0.os." + prop, "=", getattr(vmi.os, prop))
            for prop in ['name', "flavour", "version"]
        ])

        if not hasattr(vmi, 'applications'):
            return system("", fs)

        # vmi.applications can store the attributes of a single application
        # or can be a list of objects.
        if vmi.applications and isinstance(vmi.applications[0], str):
            apps = [vmi.applications]
        else:
            apps = vmi.applications

        for app in apps:
            OS_VMRC_RADL_PROPS = ["name", "version", "path"]
            fs.append(
                Feature(
                    "disk.0.applications", "contains",
                    FeaturesApp([
                        Feature(prop, "=", getattr(app, prop))
                        for prop in OS_VMRC_RADL_PROPS
                    ] + [Feature("preinstalled", "=", "yes")])))
        return system("", fs)
Пример #29
0
    def test_reconfigure(self):
        """Reconfigure."""
        radl = RADL()
        radl.add(
            system("s0", [
                Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"),
                Feature("disk.0.os.credentials.username", "=", "user"),
                Feature("disk.0.os.credentials.password", "=", "pass")
            ]))
        radl.add(deploy("s0", 1))

        auth0 = self.getAuth([0], [], [("Dummy", 0)])
        infId = IM.CreateInfrastructure(str(radl), auth0)

        reconf_radl = """configure test (\n@begin\n---\n  - tasks:\n      - debug: msg="RECONFIGURERADL"\n@end\n)"""
        IM.Reconfigure(infId, reconf_radl, auth0)
        IM.Reconfigure(infId, reconf_radl, auth0, ['0'])

        IM.DestroyInfrastructure(infId, auth0)
Пример #30
0
    def test_inf_removeresources(self):
        """Deploy 4 VMs and remove 2"""
        radl = RADL()
        radl.add(
            system("s0", [
                Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"),
                Feature("disk.0.os.credentials.username", "=", "user"),
                Feature("disk.0.os.credentials.password", "=", "pass")
            ]))
        radl.add(deploy("s0", 4))

        auth0 = self.getAuth([0], [], [("Dummy", 0)])
        infId = IM.CreateInfrastructure(str(radl), auth0)
        cont = IM.RemoveResource(infId, ['0', '1'], auth0)
        self.assertEqual(cont, 2)
        vms = IM.GetInfrastructureInfo(infId, auth0)
        self.assertEqual(sorted(vms), ['2', '3'])

        IM.DestroyInfrastructure(infId, auth0)