Example #1
0
def do_body_upload(upload_id):

    try:
        log.debug("Incoming file upload request has files %s" % (request.files.keys()))
        request_copy = request.copy()
        new_body = request_copy.files.image_body.file
    except Exception as e:
        log.exception(e)
        raise HTTPResponse(status=400, output='Incoming POST did not contain a file named image_body')

    try:
        target_image_uuid = SecondaryDispatcher().target_image_for_upload_uuid(upload_id)
        log.debug("Got target_image UUID of %s" % (target_image_uuid))
        if not target_image_uuid:
            log.debug("Failed to find target image for upload UUID of %s" % (upload_id))
            raise HTTPResponse(status=404, output='Upload ID %s not found' % (upload_id))
        target_image = PersistentImageManager.default_manager().image_with_id(target_image_uuid)
        if not target_image:
            raise HTTPResponse(status=404, output='No target_image with ID %s found' % (target_image_uuid))
        log.debug("Got actual target_image")
        SecondaryDispatcher().update_target_image_body(target_image, new_body)
    finally:
        pass
        # Bottle resets the connection if we don't do this
        #new_body.close()

    response.status=202
    return { "status_text": "Finished without an exception" }
Example #2
0
    def builder_did_create_target_image(self, builder, target, image_id, template, parameters):
        self.log.info('builder_did_create_target_image() called in OVA plugin')
        self.status="BUILDING"

        # NOTE: This is unique to the OVA plugin at the moment
        # The ID passed in as the "base" image is actually a target image for the cloud
        # type that will be the ultimate target of the OVA.  This allows us to reuse the
        # existing target plugin code for disk format transformation and focus on the OVF
        # specific work in this plugin

        # The target image containing the disk in the correct format
        # Again, the builder things this is a base image but it is actually a target image
        self.target_image = builder.base_image

        # The base image that this origin target image was created from
        self.base_image = PersistentImageManager.default_manager().image_with_id(self.target_image.base_image_id)

        # A convenience variable pointing at the target image we are creating
        # Take note - self.target_image is the image we are sourcing from
        #             self.image is us, another target image
        self.image = builder.target_image
        self.parameters = parameters

        # This lets our logging helper know what image is being operated on
        self.active_image = self.image

        self.generate_ova()

        self.percent_complete=100
        self.status="COMPLETED"
Example #3
0
def list_images(image_collection, base_image_id=None, target_image_id=None, list_url=None):
    try:
        fetch_spec = {}
        if(image_collection == 'base_images'):
            fetch_spec['type'] = 'BaseImage'
        elif(image_collection == 'target_images'):
            fetch_spec['type'] = 'TargetImage'
            if base_image_id:
                fetch_spec['base_image_id'] = base_image_id
        elif(image_collection == 'provider_images'):
            fetch_spec['type'] = 'ProviderImage'
            if target_image_id:
                fetch_spec['target_image_id'] = target_image_id
        else:
            raise HTTPResponse(status=404, output='%s not found' % image_collection)

        fetched_images = PersistentImageManager.default_manager().images_from_query(fetch_spec)
        images = list()
        _url = list_url if list_url else request.url
        for image in fetched_images:
            resp_item = {image_collection[0:-1]:
                            {'_type':type(image).__name__,
                            'id':image.identifier,
                            'href':'%s/%s' % (_url, image.identifier)}
                        }
            images.append(resp_item)

        return converted_response({image_collection:images})
    except Exception as e:
        log.exception(e)
        raise HTTPResponse(status=500, output=e)
Example #4
0
def list_images(image_collection, base_image_id=None, target_image_id=None, list_url=None):
    try:
        _type = IMAGE_TYPES[image_collection]
        if _type:
            fetch_spec = {'type': _type}
            if base_image_id:
                fetch_spec['base_image_id'] = base_image_id
            if target_image_id:
                fetch_spec['target_image_id'] = target_image_id
        else:
            raise HTTPResponse(status=404, output='%s not found' % image_collection)

        fetched_images = PersistentImageManager.default_manager().images_from_query(fetch_spec)
        images = list()
        _url = list_url if list_url else request.url
        for image in fetched_images:
            resp_item = {image_collection[0:-1]:
                            {'_type':type(image).__name__,
                            'id':image.identifier,
                            'href':'%s/%s' % (_url, image.identifier)}
                        }
            images.append(resp_item)

        return converted_response({image_collection:images})
    except Exception as e:
        log.exception(e)
        raise HTTPResponse(status=500, output=e)
 def __init__(self):
     super(IndirectionCloud, self).__init__()
     self.app_config = ApplicationConfiguration().configuration
     self.log = logging.getLogger('%s.%s' %
                                  (__name__, self.__class__.__name__))
     self.pim = PersistentImageManager.default_manager()
     self.res_mgr = ReservationManager()
Example #6
0
    def builder_did_create_target_image(self, builder, target, image_id, template, parameters):
        self.log.info('builder_did_create_target_image() called in OVA plugin')
        self.status="BUILDING"

        # NOTE: This is unique to the OVA plugin at the moment
        # The ID passed in as the "base" image is actually a target image for the cloud
        # type that will be the ultimate target of the OVA.  This allows us to reuse the
        # existing target plugin code for disk format transformation and focus on the OVF
        # specific work in this plugin

        # The target image containing the disk in the correct format
        # Again, the builder things this is a base image but it is actually a target image
        self.target_image = builder.base_image

        # The base image that this origin target image was created from
        self.base_image = PersistentImageManager.default_manager().image_with_id(self.target_image.base_image_id)

        # A convenience variable pointing at the target image we are creating
        # Take note - self.target_image is the image we are sourcing from
        #             self.image is us, another target image
        self.image = builder.target_image
        self.parameters = parameters

        # This lets our logging helper know what image is being operated on
        self.active_image = self.image

        self.generate_ova()

        self.percent_complete=100
        self.status="COMPLETED"
Example #7
0
def list_images(image_collection, base_image_id=None, target_image_id=None):
    try:
        fetch_spec = {}
        if(image_collection == 'base_images'):
            fetch_spec['type'] = 'BaseImage'
        elif(image_collection == 'target_images'):
            fetch_spec['type'] = 'TargetImage'
            fetch_spec['base_image_id'] = base_image_id
        elif(image_collection == 'provider_images'):
            fetch_spec['type'] = 'ProviderImage'
            fetch_spec['target_image_id'] = target_image_id
        else:
            raise HTTPResponse(status=404, output='%s not found' % image_collection)

        fetched_images = PersistentImageManager.default_manager().images_from_query(fetch_spec)
        images = list()
        for image in fetched_images:
            resp_item = {'_type':type(image).__name__,
                         'id':image.identifier,
                         'href':'%s/%s' % (request.url, image.identifier)}
            for key in image.metadata:
                resp_item[key] = getattr(image, key, None)
            images.append(resp_item)

        return {image_collection:images}
    except Exception as e:
        log.exception(e)
        raise HTTPResponse(status=500, output=e)
Example #8
0
def image_with_id(collection_type, image_id, base_image_id=None, target_image_id=None, provider_image_id=None):
    try:
        img_class = IMAGE_TYPES[collection_type]
        if img_class:
            fetch_spec = {'type': img_class, 'identifier': image_id}
            try:
                image = PersistentImageManager.default_manager().images_from_query(fetch_spec)[0]
                _type = type(image).__name__
                _response = {'_type': _type,
                             'id': image.identifier,
                             'href': request.url}
                for key in image.metadata():
                    if key not in ('identifier', 'data', 'base_image_id', 'target_image_id'):
                        _response[key] = getattr(image, key, None)

                api_url = '%s://%s/imagefactory' % (request.urlparts[0], request.urlparts[1])

                if (_type == "BaseImage"):
                    _objtype = 'base_image'
                    _response['target_images'] = list_images('target_images',
                                                             base_image_id=image.identifier,
                                                             list_url='%s/target_images' % api_url)
                elif (_type == "TargetImage"):
                    _objtype = 'target_image'
                    base_image_id = image.base_image_id
                    if (base_image_id):
                        base_image_href = '%s/base_images/%s' % (api_url, base_image_id)
                        base_image_dict = {'_type': 'BaseImage', 'id': base_image_id, 'href': base_image_href}
                        _response['base_image'] = base_image_dict
                    else:
                        _response['base_image'] = None
                    _response['provider_images'] = list_images('provider_images',
                                                               target_image_id=image.identifier,
                                                               list_url='%s/provider_images' % api_url)
                elif (_type == "ProviderImage"):
                    _objtype = 'provider_image'
                    target_image_id = image.target_image_id
                    if (target_image_id):
                        target_image_href = '%s/target_images/%s' % (api_url, target_image_id)
                        target_image_dict = {'_type': 'TargetImage', 'id': target_image_id, 'href': target_image_href}
                        _response['target_image'] = target_image_dict
                    else:
                        _response['target_image'] = None
                else:
                    log.error("Returning HTTP status 500 due to unknown image type: %s" % _type)
                    raise HTTPResponse(status=500, output='Bad type for found object: %s' % _type)

                response.status = 200
                return converted_response({_objtype: _response})

            except IndexError as e:
                log.warning(e)
                raise HTTPResponse(status=404, output='No %s found with id: %s' % (img_class, image_id))
        else:
            raise HTTPResponse(status=404, output='Unknown resource type: %s' % collection_type)
    except Exception as e:
        log.exception(e)
        raise HTTPResponse(status=500, output=e)
Example #9
0
def get_image_file(image_id, base_image_id=None, target_image_id=None, provider_image_id=None):
    try:
        image = PersistentImageManager.default_manager().image_with_id(image_id)
        if(not image):
            raise HTTPResponse(status=404, output='No image found with id: %s' % image_id)
        path, filename = os.path.split(image.data)
        return static_file(filename, path, download=True)
    except Exception as e:
        log.exception(e)
        raise HTTPResponse(status=500, output=e)
Example #10
0
def get_image_file(image_id, base_image_id=None, target_image_id=None, provider_image_id=None):
    try:
        image = PersistentImageManager.default_manager().image_with_id(image_id)
        if(not image):
            raise HTTPResponse(status=404, output='No image found with id: %s' % image_id)
        path, filename = os.path.split(image.data)
        return static_file(filename, path, download=True)
    except Exception as e:
        log.exception(e)
        raise HTTPResponse(status=500, output=e)
Example #11
0
    def __init__(self, disk, path=None):
        if path:
            self.path = path
        else:
            storage_path = PersistentImageManager.default_manager().storage_path
            self.path = tempfile.mkdtemp(dir=storage_path)
            # this needs to be readable by others, e.g. the nfs user
            # when used in the RHEVHelper
            os.chmod(self.path, S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)

        self.disk = disk
Example #12
0
def delete_image_with_id(image_id,
                         collection_type=None,
                         base_image_id=None,
                         target_image_id=None,
                         provider_image_id=None):
    try:
        response.status = 204
        image = PersistentImageManager.default_manager().image_with_id(
            image_id)
        if (not image):
            raise HTTPResponse(status=404,
                               output='No image found with id: %s' % image_id)
        image_class = type(image).__name__
        builder = Builder()
        content_type = request.headers.get('Content-Type')
        form_data = form_data_for_content_type(content_type)
        required_values = set(['provider', 'credentials', 'target'])

        if form_data:
            root_object = form_data.get(IMAGE_TYPES.get(image_class))
            if root_object and (type(root_object) is dict):
                request_data = root_object
            else:
                request_data = form_data

            if image_class == 'ProviderImage':
                missing_values = required_values.difference(request_data)
                if len(missing_values) > 0:
                    raise HTTPResponse(status=400,
                                       output='Missing required values: %s' %
                                       missing_values)

            builder.delete_image(provider=request_data.get('provider'),
                                 credentials=request_data.get('credentials'),
                                 target=request_data.get('target'),
                                 image_object=image,
                                 parameters=request_data.get('parameters'))
        else:
            if image_class == 'ProviderImage':
                raise HTTPResponse(status=400,
                                   output='Missing required values:%s' %
                                   required_values)
            else:
                builder.delete_image(provider=None,
                                     credentials=None,
                                     target=None,
                                     image_object=image,
                                     parameters=None)
    except Exception as e:
        log.exception(e)
        raise HTTPResponse(status=500, output=e)
Example #13
0
def image_with_id(image_id, base_image_id=None, target_image_id=None, provider_image_id=None):
    try:
        image = PersistentImageManager.default_manager().image_with_id(image_id)
        if(not image):
            raise HTTPResponse(status=404, output='No image found with id: %s' % image_id)
        _type = type(image).__name__
        _response = {'_type':_type,
                     'id':image.identifier,
                     'href':request.url}
        for key in image.metadata():
            if key not in ('identifier', 'data', 'base_image_id', 'target_image_id'):
                _response[key] = getattr(image, key, None)

        api_url = '%s://%s/imagefactory' % (request.urlparts[0], request.urlparts[1])

        if(_type == "BaseImage"):
            _objtype = 'base_image'
            _response['target_images'] = list_images('target_images',
                                                     base_image_id = image.identifier,
                                                     list_url='%s/target_images' % api_url)
        elif(_type == "TargetImage"):
            _objtype = 'target_image'
            base_image_id = image.base_image_id
            if(base_image_id):
                base_image_href = '%s/base_images/%s' % (api_url, base_image_id)
                base_image_dict = {'_type': 'BaseImage', 'id': base_image_id, 'href': base_image_href}
                _response['base_image'] = base_image_dict
            else:
                _response['base_image'] = None
            _response['provider_images'] = list_images('provider_images',
                                                        target_image_id = image.identifier,
                                                        list_url = '%s/provider_images' % api_url)
        elif(_type == "ProviderImage"):
            _objtype = 'provider_image'
            target_image_id = image.target_image_id
            if(target_image_id):
                target_image_href = '%s/target_images/%s' % (api_url, target_image_id)
                target_image_dict = {'_type': 'TargetImage', 'id': target_image_id, 'href': target_image_href}
                _response['target_image'] = target_image_dict
            else:
                _response['target_image'] = None
        else:
            log.error("Returning HTTP status 500 due to unknown image type: %s" % _type)
            raise HTTPResponse(status=500, output='Bad type for found object: %s' % _type)

        response.status = 200
        return converted_response({_objtype:_response})
    except Exception as e:
        log.exception(e)
        raise HTTPResponse(status=500, output=e)
Example #14
0
def delete_image_with_id(image_id, base_image_id=None, target_image_id=None, provider_image_id=None):
    try:
        image = PersistentImageManager.default_manager().image_with_id(image_id)
        if(not image):
            raise HTTPResponse(status=404, output='No image found with id: %s' % image_id)
        builder = Builder()
        builder.delete_image(provider=request_data.get('provider'),
                             credentials=request_data.get('credentials'),
                             target=request_data.get('target'),
                             image_object=image,
                             parameters=request_data.get('parameters'))
        response.status = 204
    except Exception as e:
        log.exception(e)
        raise HTTPResponse(status=500, output=e)
Example #15
0
    def builder_did_create_target_image(self, builder, target, image_id, template, parameters):
        self.log.info('builder_did_create_target_image() called in OVA plugin')
        self.status="BUILDING"

        self.target_image = builder.base_image
        self.base_image = PersistentImageManager.default_manager().image_with_id(self.target_image.base_image_id)
        self.image = builder.target_image
        self.parameters = parameters

        # This lets our logging helper know what image is being operated on
        self.active_image = self.image

        self.generate_ova()

        self.percent_complete=100
        self.status="COMPLETED"
Example #16
0
def image_with_id(image_id, base_image_id=None, target_image_id=None, provider_image_id=None):
    try:
        image = PersistentImageManager.default_manager().image_with_id(image_id)
        if(not image):
            raise HTTPResponse(status=404, output='No image found with id: %s' % image_id)
        _response = {'_type':type(image).__name__,
                     'id':image.identifier,
                     'href':'%s/%s' % (request.url, image.identifier)}
        for key in image.metadata:
            _response[key] = getattr(image, key, None)

        response.status = 202
        return _response
    except Exception as e:
        log.exception(e)
        raise HTTPResponse(status=500, output=e)
Example #17
0
 def _singleton_init(self):
     self.log = logging.getLogger('%s.%s' % (__name__, self.__class__.__name__))
     self.pending_uploads = dict()
     self.pending_uploads_lock = BoundedSemaphore()
     self.pim = PersistentImageManager.default_manager()
     self.res = ReservationManager()
     self.secondaries = { } 
     if os.path.isfile(SECONDARIES):
         try:
             secs = open(SECONDARIES, "r")
             self.secondaries = json.load(secs)
             secs.close()
         except:
             self.log.warning("Unable to load JSON for secondaries from %s", SECONDARIES)
     if not 'targets' in self.secondaries:
         self.secondaries['targets'] = { }
     if not 'providers' in self.secondaries:
         self.secondaries['providers'] = { }
 def _singleton_init(self):
     self.log = logging.getLogger('%s.%s' % (__name__, self.__class__.__name__))
     self.pending_uploads = dict()
     self.pending_uploads_lock = BoundedSemaphore()
     self.pim = PersistentImageManager.default_manager()
     self.res = ReservationManager()
     self.secondaries = { } 
     if os.path.isfile(SECONDARIES):
         try:
             secs = open(SECONDARIES, "r")
             self.secondaries = json.load(secs)
             secs.close()
         except:
             self.log.warning("Unable to load JSON for secondaries from %s", SECONDARIES)
     if not 'targets' in self.secondaries:
         self.secondaries['targets'] = { }
     if not 'providers' in self.secondaries:
         self.secondaries['providers'] = { }
Example #19
0
    def builder_did_create_target_image(self, builder, target, image_id,
                                        template, parameters):
        self.log.info('builder_did_create_target_image() called in OVA plugin')
        self.status = "BUILDING"

        self.target_image = builder.base_image
        self.base_image = PersistentImageManager.default_manager(
        ).image_with_id(self.target_image.base_image_id)
        self.image = builder.target_image
        self.parameters = parameters

        # This lets our logging helper know what image is being operated on
        self.active_image = self.image

        self.generate_ova()

        self.percent_complete = 100
        self.status = "COMPLETED"
Example #20
0
def delete_image_with_id(image_id, base_image_id=None, target_image_id=None, provider_image_id=None):
    try:
        image = PersistentImageManager.default_manager().image_with_id(image_id)
        if(not image):
            raise HTTPResponse(status=404, output='No image found with id: %s' % image_id)
        builder = Builder()
        request_data = form_data_for_content_type(request.headers.get('Content-Type'))
        if(request_data and (len(request_data) > 0)):
            builder.delete_image(provider=request_data.get('provider'),
                                 credentials=request_data.get('credentials'),
                                 target=request_data.get('target'),
                                 image_object=image,
                                 parameters=request_data.get('parameters'))
        else:
            builder.delete_image(provider=None, credentials=None, target=None, image_object=image, parameters=None)
        response.status = 204
    except Exception as e:
        log.exception(e)
        raise HTTPResponse(status=500, output=e)
Example #21
0
def image_with_id(image_id, base_image_id=None, target_image_id=None, provider_image_id=None):
    try:
        image = PersistentImageManager.default_manager().image_with_id(image_id)
        if(not image):
            raise HTTPResponse(status=404, output='No image found with id: %s' % image_id)
        _type = type(image).__name__
        _response = {'_type':_type,
                     'id':image.identifier,
                     'href':request.url}
        for key in image.metadata():
            if key not in ('identifier', 'data', 'base_image_id', 'target_image_id'):
                _response[key] = getattr(image, key, None)

        if(_type == "BaseImage"):
            _response['target_images'] = self.list_images('target_images', base_image_id = image.identifier)
        elif(_type == "TargetImage"):
            base_image_id = image.metadata()['base_image_id']
            if(base_image_id):
                base_image_href = '%s://%s/imagefactory/base_images/%s' % (request.urlparts[0], request.urlparts[1], base_image_id)
                base_image_dict = {'_type': 'BaseImage', 'id': base_image_id, 'href': base_image_href}
                _response['base_image'] = base_image_dict
            else:
                _response['base_image'] = None
            _response['provider_images'] = self.list_images('provider_images', target_image_id = image.identifier)
        elif(_type == "ProviderImage"):
            target_image_id = image.metadata()['target_image_id']
            if(target_image_id):
                target_image_href = '%s://%s/imagefactory/target_images/%s' % (request.urlparts[0], request.urlparts[1], target_image_id)
                target_image_dict = {'_type': 'TargetImage', 'id': target_image_id, 'href': target_image_href}
                _response['target_image'] = target_image_dict
            else:
                _response['target_image'] = None
        else:
            log.warn("Unknown image type: %s" % _type)

        response.status = 202
        return _response
    except Exception as e:
        log.exception(e)
        raise HTTPResponse(status=500, output=e)
Example #22
0
def delete_image_with_id(image_id, base_image_id=None, target_image_id=None, provider_image_id=None):
    try:
        response.status = 204
        image = PersistentImageManager.default_manager().image_with_id(image_id)
        if(not image):
            raise HTTPResponse(status=404, output='No image found with id: %s' % image_id)
        image_class = type(image).__name__
        builder = Builder()
        content_type = request.headers.get('Content-Type')
        form_data = form_data_for_content_type(content_type)
        required_values = set(['provider', 'credentials', 'target'])

        if form_data:
            root_object = form_data.get(IMAGE_TYPES.get(image_class))
            if root_object and (type(root_object) is dict):
                request_data = root_object
            else:
                request_data = form_data

            if image_class == 'ProviderImage':
                missing_values = required_values.difference(request_data)
                if len(missing_values) > 0:
                    raise HTTPResponse(status=400, output='Missing required values: %s' % missing_values)

            builder.delete_image(provider=request_data.get('provider'),
                                 credentials=request_data.get('credentials'),
                                 target=request_data.get('target'),
                                 image_object=image,
                                 parameters=request_data.get('parameters'))
        else:
            if image_class == 'ProviderImage':
                raise HTTPResponse(status=400, output='Missing required values:%s' % required_values)
            else:
                builder.delete_image(provider=None, credentials=None, target=None, image_object=image, parameters=None)
    except Exception as e:
        log.exception(e)
        raise HTTPResponse(status=500, output=e)
Example #23
0
def image_with_id(collection_type,
                  image_id,
                  base_image_id=None,
                  target_image_id=None,
                  provider_image_id=None):
    try:
        img_class = IMAGE_TYPES[collection_type]
        if img_class:
            fetch_spec = {'type': img_class, 'identifier': image_id}
            try:
                image = PersistentImageManager.default_manager(
                ).images_from_query(fetch_spec)[0]
                _type = type(image).__name__
                _response = {
                    '_type': _type,
                    'id': image.identifier,
                    'href': request.url
                }
                for key in image.metadata():
                    if key not in ('identifier', 'data', 'base_image_id',
                                   'target_image_id'):
                        _response[key] = getattr(image, key, None)

                api_url = '%s://%s/imagefactory' % (request.urlparts[0],
                                                    request.urlparts[1])

                if (_type == "BaseImage"):
                    _objtype = 'base_image'
                    _response['target_images'] = list_images(
                        'target_images',
                        base_image_id=image.identifier,
                        list_url='%s/target_images' % api_url)
                elif (_type == "TargetImage"):
                    _objtype = 'target_image'
                    base_image_id = image.base_image_id
                    if (base_image_id):
                        base_image_href = '%s/base_images/%s' % (api_url,
                                                                 base_image_id)
                        base_image_dict = {
                            '_type': 'BaseImage',
                            'id': base_image_id,
                            'href': base_image_href
                        }
                        _response['base_image'] = base_image_dict
                    else:
                        _response['base_image'] = None
                    _response['provider_images'] = list_images(
                        'provider_images',
                        target_image_id=image.identifier,
                        list_url='%s/provider_images' % api_url)
                elif (_type == "ProviderImage"):
                    _objtype = 'provider_image'
                    target_image_id = image.target_image_id
                    if (target_image_id):
                        target_image_href = '%s/target_images/%s' % (
                            api_url, target_image_id)
                        target_image_dict = {
                            '_type': 'TargetImage',
                            'id': target_image_id,
                            'href': target_image_href
                        }
                        _response['target_image'] = target_image_dict
                    else:
                        _response['target_image'] = None
                else:
                    log.error(
                        "Returning HTTP status 500 due to unknown image type: %s"
                        % _type)
                    raise HTTPResponse(status=500,
                                       output='Bad type for found object: %s' %
                                       _type)

                response.status = 200
                return converted_response({_objtype: _response})

            except IndexError as e:
                log.warning(e)
                raise HTTPResponse(status=404,
                                   output='No %s found with id: %s' %
                                   (img_class, image_id))
        else:
            raise HTTPResponse(status=404,
                               output='Unknown resource type: %s' %
                               collection_type)
    except KeyError as e:
        if collection_type == 'plugins':
            return get_plugins(plugin_id=image_id)
        else:
            log.exception(e)
            raise HTTPResponse(status=500, output=e)
    except Exception as e:
        log.exception(e)
        raise HTTPResponse(status=500, output=e)
Example #24
0
def delete_image_with_id(image_id, base_image_id=None, target_image_id=None, provider_image_id=None):
    try:
        PersistentImageManager.default_manager().delete_image_with_id(image_id)
    except Exception as e:
        log.exception(e)
        raise HTTPResponse(status=500, output=e)
Example #25
0
    def create(self, installer_outputdir, args, cmd, profile, post=None):
        imgfunc = AbstractImageFactoryTask(args, cmd, profile)
        repos = self.getrepos(self.jsonfilename)
        util_xml = self.template_xml(
            repos, os.path.join(self.pkgdatadir,
                                'lorax-indirection-repo.tmpl'))
        lorax_repos = []
        if self.lorax_additional_repos:
            if getattr(self, 'yum_baseurl') not in self.lorax_additional_repos:
                self.lorax_additional_repos += ", {0}".format(
                    getattr(self, 'yum_baseurl'))
            for repourl in self.lorax_additional_repos.split(','):
                lorax_repos.extend(['-s', repourl.strip()])
        else:
            lorax_repos.extend(['-s', getattr(self, 'yum_baseurl')])

        port_file_path = self.workdir + '/repo-port'

        if not self.ostree_repo_is_remote:
            # Start trivial-httpd
            trivhttp = TrivialHTTP()
            trivhttp.start(self.ostree_repo)
            httpd_port = str(trivhttp.http_port)
            print "trivial httpd port=%s, pid=%s" % (httpd_port,
                                                     trivhttp.http_pid)
        else:
            httpd_port = str(self.httpd_port)
        substitutions = {
            'OSTREE_PORT': httpd_port,
            'OSTREE_REF': self.ref,
            'OSTREE_OSNAME': self.os_name,
            'LORAX_REPOS': " ".join(lorax_repos),
            'OS_PRETTY': self.os_pretty_name,
            'OS_VER': self.release
        }
        if '@OSTREE_HOSTIP@' in util_xml:
            if not self.ostree_repo_is_remote:
                host_ip = getDefaultIP()
            else:
                host_ip = self.httpd_host
            substitutions['OSTREE_HOSTIP'] = host_ip

        if '@OSTREE_PATH' in util_xml:
            substitutions['OSTREE_PATH'] = self.httpd_path

        print type(util_xml)
        for subname, subval in substitutions.iteritems():
            util_xml = util_xml.replace('@%s@' % (subname, ), subval)

        # Dump util_xml to workdir for logging
        self.dumpTempMeta(os.path.join(self.workdir, "lorax.xml"), util_xml)
        global verbosemode
        imgfacbuild = ImgFacBuilder(verbosemode=verbosemode)
        imgfacbuild.verbosemode = verbosemode
        imgfunc.checkoz("qcow2")
        util_ks = self.createUtilKS(self.tdl)

        # Building of utility image
        parameters = {
            "install_script": util_ks,
            "generate_icicle": False,
            "oz_overrides": json.dumps(imgfunc.ozoverrides)
        }
        if self.util_uuid is None:
            print "Starting Utility image build"
            util_image = imgfacbuild.build(template=open(self.util_tdl).read(),
                                           parameters=parameters)
            print "Created Utility Image: {0}".format(util_image.data)

        else:
            pim = PersistentImageManager.default_manager()
            util_image = pim.image_with_id(self.util_uuid)
            print "Re-using Utility Image: {0}".format(util_image.identifier)

        # Now lorax
        bd = BuildDispatcher()
        lorax_parameters = {
            "results_location": "/lorout/output.tar",
            "utility_image": util_image.identifier,
            "utility_customizations": util_xml,
            "oz_overrides": json.dumps(imgfunc.ozoverrides)
        }
        print "Building the lorax image"
        loraxiso_builder = bd.builder_for_target_image(
            "indirection",
            image_id=util_image.identifier,
            template=None,
            parameters=lorax_parameters)
        loraxiso_image = loraxiso_builder.target_image
        thread = loraxiso_builder.target_thread
        thread.join()

        # Extract the tarball of built images
        print "Extracting images to {0}/images".format(installer_outputdir)
        t = tarfile.open(loraxiso_image.data)
        t.extractall(path=installer_outputdir)
        if not self.ostree_repo_is_remote:
            trivhttp.stop()
Example #26
0
def operate_on_image_with_id(image_collection, image_id):
    log.debug("In the generic route (%s)" % request.method)
    image_type = image_collection[0:-1]

    if image_type not in [ 'target_image', 'provider_image' ]:
        raise HTTPResponse(status=500, output='Attempt to access invalid image type: %s' % image_type)

    if request.method == "GET":
        log.debug("In the generic GET")
	try:
	    image = PersistentImageManager.default_manager().image_with_id(image_id)
	    if(not image):
                log.error("Did search for image with id (%s) and got (%s)" % (image_id, image))
		raise HTTPResponse(status=404, output='No image found with id: %s' % (image_id))
            else:
                log.debug("Found image id (%s). Constructing response" % (image_id))
	    _type = type(image).__name__
	    _response = {'_type':_type,
			 'id':image.identifier,
			 'href':request.url}
	    for key in image.metadata():
		if key not in ('identifier', 'data', 'base_image_id', 'target_image_id'):
		    _response[key] = getattr(image, key, None)

	    api_url = '%s://%s/imagefactory' % (request.urlparts[0], request.urlparts[1])

	    if(_type == "TargetImage"):
		_objtype = 'target_image'
	    elif(_type == "ProviderImage"):
		_objtype = 'provider_image'
	    else:
		log.error("Returning HTTP status 500 due to unknown image type: %s" % _type)
		raise HTTPResponse(status=500, output='Bad type for found object: %s' % _type) 

            if _objtype != image_type:
                raise HTTPResponse(status=500, output='Requested image type %s got image of type %s' % (image_type, _objtype))

	    response.status = 200
	    return {_objtype:_response}
	except Exception as e:
	    log.exception(e)
	    raise HTTPResponse(status=500, output=e)

#@rest_api.get('/imagefactory/<image_collection>')
#@rest_api.get('/imagefactory/target_images/<target_image_id>/<image_collection>')
#@log_request
#@oauth_protect
#def list_images(image_collection, base_image_id=None, target_image_id=None, list_url=None):
#    try:
#        fetch_spec = {}
#        if(image_collection == 'target_images'):
#            fetch_spec['type'] = 'TargetImage'
#        elif(image_collection == 'provider_images'):
#            fetch_spec['type'] = 'ProviderImage'
#            if target_image_id:
#                fetch_spec['target_image_id'] = target_image_id
#        else:
#            raise HTTPResponse(status=404, output='%s not found' % image_collection)
#
#        fetched_images = PersistentImageManager.default_manager().images_from_query(fetch_spec)
#        images = list()
#        _url = list_url if list_url else request.url
#        for image in fetched_images:
#            resp_item = {image_collection[0:-1]:
#                            {'_type':type(image).__name__,
#                            'id':image.identifier,
#                            'href':'%s/%s' % (_url, image.identifier)}
#                        }
#            images.append(resp_item)
#
#        return images
#    except Exception as e:
#        log.exception(e)
#        raise HTTPResponse(status=500, output=e)

#@rest_api.post('/imagefactory/target_images/<target_image_id>')
#@log_request
#@oauth_protect
#def clone_target_image(target_image_id):
    elif request.method == "POST":
        try:
	    if image_type == 'target_image':
		request_data = RESTtools.form_data_for_content_type(request.headers.get('Content-Type'))    

		(target_image, upload_id) = SecondaryDispatcher().prep_target_image_clone(request_data, image_id)

		_response = { }
		_response['target_image'] = {'_type':type(target_image).__name__,
					     'id':target_image.identifier,
					     'href':'%s' % (request.url)}
		for key in target_image.metadata():
		    if key not in ('identifier', 'data'):
			_response['target_image'][key] = getattr(target_image, key, None)
		
		if upload_id:
		    _response['upload_id'] = upload_id

		response.status = 202
		return _response
	    else:
		request_data = RESTtools.form_data_for_content_type(request.headers.get('Content-Type'))
		if(not request_data):
		    raise HTTPResponse(status=400, output='%s not found in request.' % (image_type))

		req_target_img_id = request_data.get('target_image_id')
		target_img_id = req_target_img_id if req_target_img_id else target_image_id

		builder = BuildDispatcher().builder_for_provider_image(provider=request_data.get('provider'),
								       credentials=request_data.get('credentials'),
								       target=request_data.get('target'),
								       image_id=target_img_id,
								       template=request_data.get('template'),
								       parameters=request_data.get('parameters'),
								       my_image_id=image_id)
		image = builder.provider_image

		_response = {'_type':type(image).__name__,
			     'id':image.identifier,
			     'href':'%s/%s' % (request.url, image.identifier)}
		for key in image.metadata():
		    if key not in ('identifier', 'data'):
			_response[key] = getattr(image, key, None)

		response.status = 202
		return {image_collection[0:-1]:_response}
        except Exception as e:
            log.exception(e)
	    raise HTTPResponse(status=500, output=e)
    else:
	raise HTTPResponse(status=405)
 def __init__(self):
     super(IndirectionCloud, self).__init__()
     self.app_config = ApplicationConfiguration().configuration
     self.log = logging.getLogger('%s.%s' % (__name__, self.__class__.__name__))
     self.pim = PersistentImageManager.default_manager()
     self.res_mgr = ReservationManager()
    def create(self, outputdir, post=None):
        imgfunc = ImageFunctions()
        repos = self.getrepos(self.jsonfilename)
        util_xml = self.template_xml(repos, os.path.join(self.pkgdatadir, 'lorax-indirection-repo.tmpl'))
        lorax_repos = []
        if self.lorax_additional_repos:
            if getattr(self, 'yum_baseurl') not in self.lorax_additional_repos:
                self.lorax_additional_repos += ", {0}".format(getattr(self, 'yum_baseurl'))
            for repourl in self.lorax_additional_repos.split(','):
                lorax_repos.extend(['-s', repourl.strip()])
        else:
            lorax_repos.extend(['-s', getattr(self, 'yum_baseurl')])

        port_file_path = self.workdir + '/repo-port'

        # Start trivial-httpd

        trivhttp = TrivialHTTP()
        trivhttp.start(self.ostree_repo)
        httpd_port = str(trivhttp.http_port)
        print "trivial httpd port=%s, pid=%s" % (httpd_port, trivhttp.http_pid)
        substitutions = {'OSTREE_PORT': httpd_port,
                         'OSTREE_REF':  self.ref,
                         'OSTREE_OSNAME':  self.os_name,
                         'LORAX_REPOS': " ".join(lorax_repos),
                         'OS_PRETTY': self.os_pretty_name,
                         'OS_VER': self.release
                         }
        if '@OSTREE_HOSTIP@' in util_xml:
            host_ip = getDefaultIP()
            substitutions['OSTREE_HOSTIP'] = host_ip

        print type(util_xml)
        for subname, subval in substitutions.iteritems():
            util_xml = util_xml.replace('@%s@' % (subname, ), subval)

        # Dump util_xml to workdir for logging
        self.dumpTempMeta(os.path.join(self.workdir, "lorax.xml"), util_xml)
        global verbosemode
        imgfacbuild = ImgFacBuilder(verbosemode=verbosemode)
        imgfacbuild.verbosemode = verbosemode
        imgfunc.checkoz()
        util_ks = self.createUtilKS(self.tdl)

        # Building of utility image
        parameters = {"install_script": util_ks,
                      "generate_icicle": False,
                      "oz_overrides": json.dumps(imgfunc.ozoverrides)
                      }
        print "Starting build"
        if self.util_uuid is None:
            util_image = imgfacbuild.build(template=open(self.util_tdl).read(), parameters=parameters)
            print "Created Utility Image: {0}".format(util_image.data)

        else:
            pim = PersistentImageManager.default_manager()
            util_image = pim.image_with_id(self.util_uuid)
            print "Re-using Utility Image: {0}".format(util_image.identifier)

        # Now lorax
        bd = BuildDispatcher()
        lorax_parameters = {"results_location": "/lorout/output.tar",
                            "utility_image": util_image.identifier,
                            "utility_customizations": util_xml,
                            "oz_overrides": json.dumps(imgfunc.ozoverrides)
                            }
        print "Building the lorax image"
        loraxiso_builder = bd.builder_for_target_image("indirection", image_id=util_image.identifier, template=None, parameters=lorax_parameters)
        loraxiso_image = loraxiso_builder.target_image
        thread = loraxiso_builder.target_thread
        thread.join()

        # Extract the tarball of built images
        print "Extracting images to {0}/images".format(outputdir)
        t = tarfile.open(loraxiso_image.data)
        t.extractall(path=outputdir)
        trivhttp.stop()