Ejemplo n.º 1
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.º 2
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.º 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()

        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.º 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()

        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)))