Ejemplo n.º 1
0
    def launch(self, inf, radl, requested_radl, num_vm, auth_data):
        server = ServerProxy(self.server_url, allow_none=True)
        session_id = self.getSessionID(auth_data)
        if session_id is None:
            return [(
                False,
                "Incorrect auth data, username and password must be specified for OpenNebula provider."
            )]

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

        system = radl.systems[0]
        # Currently ONE plugin prioritizes user-password credentials
        if system.getValue('disk.0.os.credentials.password'):
            system.delValue('disk.0.os.credentials.private_key')
            system.delValue('disk.0.os.credentials.public_key')

        res = []
        i = 0
        while i < num_vm:
            vm = VirtualMachine(inf, None, self.cloud, radl, requested_radl,
                                self)
            vm.destroy = True
            inf.add_vm(vm)
            template = self.getONETemplate(vm.info, sgs, auth_data, vm)

            self.log_debug("ONE Template: %s" % template)

            success, res_id = server.one.vm.allocate(session_id, template)[0:2]

            if success:
                vm.id = str(res_id)
                vm.info.systems[0].setValue('instance_id', str(res_id))
                vm.destroy = False
                res.append((success, vm))
            else:
                res.append((success, "ERROR: " + str(res_id)))
            i += 1

        return res
Ejemplo n.º 2
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
Ejemplo n.º 3
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
Ejemplo n.º 4
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)))
Ejemplo n.º 5
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)))
Ejemplo n.º 6
0
Archivo: Azure.py Proyecto: 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
Ejemplo n.º 7
0
Archivo: OCCI.py Proyecto: 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
Ejemplo n.º 8
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)))
Ejemplo n.º 9
0
    def launch(self, inf, radl, requested_radl, num_vm, auth_data):
        system = radl.systems[0]

        # Get the public network connected with this VM
        public_net = None
        for net in radl.networks:
            if net.isPublic() and system.getNumNetworkWithConnection(
                    net.id) is not None:
                public_net = net

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

        self._create_networks(inf, radl, auth_data)

        with inf._lock:
            self._create_volumes(system, auth_data)

        headers = {'Content-Type': 'application/json'}
        res = []
        i = 0
        while i < num_vm:
            try:
                i += 1
                # Create the VM to get the nodename
                vm = VirtualMachine(inf, None, self.cloud, radl,
                                    requested_radl, self)

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

                # The URI has this format: docker://image_name
                full_image_name = system.getValue("disk.0.image.url")[9:]

                # Create the container
                if self._is_swarm(auth_data):
                    cont_data = self._generate_create_svc_request_data(
                        full_image_name, outports, vm, ssh_port, auth_data)
                    resp = self.create_request('POST', "/services/create",
                                               auth_data, headers, cont_data)
                else:
                    # First we have to pull the image
                    image_parts = full_image_name.split(":")
                    image_name = image_parts[0]
                    if len(image_parts) < 2:
                        tag = "latest"
                    else:
                        tag = image_parts[1]
                    resp = self.create_request(
                        'POST', "/images/create?fromImage=%s&tag=%s" %
                        (image_name, tag), auth_data, headers)

                    if resp.status_code not in [201, 200]:
                        res.append(
                            (False, "Error pulling the image: " + resp.text))
                        continue

                    cont_data = self._generate_create_cont_request_data(
                        full_image_name, outports, vm, ssh_port, auth_data)
                    resp = self.create_request('POST', "/containers/create",
                                               auth_data, headers, cont_data)

                if resp.status_code != 201:
                    res.append(
                        (False, "Error creating the Container: " + resp.text))
                    continue

                output = json.loads(resp.text)
                # Set the cloud id to the VM
                if "Id" in output:
                    vm.id = output["Id"]
                elif "ID" in output:
                    vm.id = output["ID"]
                else:
                    res.append((False, "Error: response format not expected."))

                vm.info.systems[0].setValue('instance_id', str(vm.id))

                if not self._is_swarm(auth_data):
                    # In creation a container can only be attached to one one network
                    # so now we must attach to the rest of networks (if any)
                    success = self._attach_cont_to_networks(vm, auth_data)
                    if not success:
                        res.append(
                            (False,
                             "Error attaching to networks the Container"))
                        # Delete the container
                        resp = self.create_request('DELETE',
                                                   "/containers/" + vm.id,
                                                   auth_data)
                        continue

                    # Now start it
                    success, msg = self.start(vm, auth_data)
                    if not success:
                        res.append(
                            (False,
                             "Error starting the Container: " + str(msg)))
                        # Delete the container
                        resp = self.create_request('DELETE',
                                                   "/containers/" + vm.id,
                                                   auth_data)
                        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)))
Ejemplo n.º 10
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)))