Exemplo n.º 1
0
def job_binary_internal_create(context, values):
    """Returns a JobBinaryInternal that does not contain a data field

    The data column uses deferred loading.
    """
    values["datasize"] = len(values["data"])
    datasize_KB = values["datasize"] / 1024.0
    if datasize_KB > CONF.job_binary_max_KB:
        raise ex.DataTooBigException(
            round(datasize_KB, 1), CONF.job_binary_max_KB,
            _("Size of internal binary (%(size)sKB) is greater "
              "than the maximum (%(maximum)sKB)"))

    job_binary_int = m.JobBinaryInternal()
    job_binary_int.update(values)

    session = get_session()
    with session.begin():
        try:
            job_binary_int.save(session=session)
        except db_exc.DBDuplicateEntry as e:
            raise ex.DBDuplicateEntry(
                _("Duplicate entry for JobBinaryInternal: %s") % e.columns)

    return job_binary_internal_get(context, job_binary_int.id)
Exemplo n.º 2
0
def job_binary_internal_get_raw_data(context, job_binary_internal_id):
    """Returns only the data field for the specified JobBinaryInternal."""
    query = model_query(m.JobBinaryInternal, context)
    res = query.filter_by(id=job_binary_internal_id).first()

    if res is not None:
        datasize_KB = res.datasize / 1024.0
        if datasize_KB > CONF.job_binary_max_KB:
            raise ex.DataTooBigException(
                round(datasize_KB, 1), CONF.job_binary_max_KB,
                _("Size of internal binary (%(size)sKB) is greater than the "
                  "maximum (%(maximum)sKB)"))

        # This assignment is sufficient to load the deferred column
        res = res.data
    return res
Exemplo n.º 3
0
def get_raw_data(job_binary, proxy_configs=None):
    conn_kwargs = {}
    if proxy_configs:
        conn_kwargs.update(username=proxy_configs.get('proxy_username'),
                           password=proxy_configs.get('proxy_password'),
                           trust_id=proxy_configs.get('proxy_trust_id'))
    else:
        conn_kwargs.update(username=job_binary.extra.get('user'),
                           password=job_binary.extra.get('password'))

    conn = sw.client(**conn_kwargs)

    if not (job_binary.url.startswith(su.SWIFT_INTERNAL_PREFIX)):
        # This should have been guaranteed already,
        # but we'll check just in case.
        raise ex.BadJobBinaryException(
            _("Url for binary in internal swift must start with %s") %
            su.SWIFT_INTERNAL_PREFIX)

    names = job_binary.url[job_binary.url.index("://") + 3:].split("/", 1)
    if len(names) == 1:
        # a container has been requested, this is currently unsupported
        raise ex.BadJobBinaryException(
            _('Url for binary in internal swift must specify an object not '
              'a container'))
    else:
        container, obj = names

        # if container name has '.sahara' suffix we need to strip it
        container = _strip_sahara_suffix(container)

        try:
            # First check the size
            headers = conn.head_object(container, obj)
            total_KB = int(headers.get('content-length', 0)) / 1024.0
            if total_KB > CONF.job_binary_max_KB:
                raise ex.DataTooBigException(
                    round(total_KB, 1), CONF.job_binary_max_KB,
                    _("Size of swift object (%(size)sKB) is greater "
                      "than maximum (%(maximum)sKB)"))

            headers, body = conn.get_object(container, obj)
        except swiftclient.ClientException as e:
            raise ex.SwiftClientException(six.text_type(e))

    return body
Exemplo n.º 4
0
def _get_raw_job_binary_data(job_binary, conn):
    names = _get_names_from_job_binary_url(job_binary.url)
    bucket, obj = names
    try:
        size = conn.head_object(Bucket=bucket, Key=obj)['ContentLength']
        # We have bytes, but want kibibytes:
        total_KB = size / 1024.0
        if total_KB > CONF.job_binary_max_KB:
            raise ex.DataTooBigException(
                round(total_KB, 1), CONF.job_binary_max_KB,
                _("Size of S3 object (%(size)sKB) is greater "
                  "than maximum (%(maximum)sKB)"))
        body = conn.get_object(Bucket=bucket, Key=obj)['Body'].read()
    except ex.DataTooBigException:
        raise
    except Exception:
        raise ex.S3ClientException("Couldn't get object from s3")
    return body
Exemplo n.º 5
0
    def _get_raw_data(self, job_binary, conn):
        names = self._get_names_from_url(job_binary.url)
        container, obj = names

        # if container name has '.sahara' suffix we need to strip it
        if container.endswith(su.SWIFT_URL_SUFFIX):
            container = container[:-len(su.SWIFT_URL_SUFFIX)]

        try:
            # First check the size
            headers = conn.head_object(container, obj)
            total_KB = int(headers.get('content-length', 0)) / 1024.0
            if total_KB > CONF.job_binary_max_KB:
                raise ex.DataTooBigException(
                    round(total_KB, 1), CONF.job_binary_max_KB,
                    _("Size of swift object (%(size)sKB) is greater "
                      "than maximum (%(maximum)sKB)"))

            headers, body = conn.get_object(container, obj)
        except swiftclient.ClientException as e:
            raise ex.SwiftClientException(six.text_type(e))

        return body
Exemplo n.º 6
0
def get_raw_data(context, job_binary, proxy_configs=None):
    if proxy_configs:
        conn = _get_conn_for_proxy_user(proxy_configs)
    else:
        user = job_binary.extra["user"]
        password = job_binary.extra["password"]

        conn = _get_conn(user, password)

    if not (job_binary.url.startswith(su.SWIFT_INTERNAL_PREFIX)):
        # This should have been guaranteed already,
        # but we'll check just in case.
        raise ex.BadJobBinaryException(
            _("Url for binary in internal swift must start with %s")
            % su.SWIFT_INTERNAL_PREFIX)

    names = job_binary.url[job_binary.url.index("://") + 3:].split("/", 1)
    if len(names) == 1:
        # We are getting a whole container, return as a dictionary.
        container = names[0]

        # if container name has '.sahara' suffix we need to strip it
        container = _strip_sahara_suffix(container)

        # First check the size...
        try:
            headers = conn.head_container(container)
            total_KB = int(headers.get('x-container-bytes-used', 0)) / 1024.0
            if total_KB > CONF.job_binary_max_KB:
                raise ex.DataTooBigException(
                    round(total_KB, 1), CONF.job_binary_max_KB,
                    _("Size of swift container (%(size)sKB) is greater "
                      "than maximum (%(maximum)sKB)"))

            body = {}
            headers, objects = conn.get_container(container)
            for item in objects:
                headers, obj = conn.get_object(container, item["name"])
                body[item["name"]] = obj
        except swiftclient.ClientException as e:
            raise ex.SwiftClientException(six.text_type(e))

    else:
        container, obj = names

        # if container name has '.sahara' suffix we need to strip it
        container = _strip_sahara_suffix(container)

        try:
            # First check the size
            headers = conn.head_object(container, obj)
            total_KB = int(headers.get('content-length', 0)) / 1024.0
            if total_KB > CONF.job_binary_max_KB:
                raise ex.DataTooBigException(
                    round(total_KB, 1), CONF.job_binary_max_KB,
                    _("Size of swift object (%(size)sKB) is greater "
                      "than maximum (%(maximum)sKB)"))

            headers, body = conn.get_object(container, obj)
        except swiftclient.ClientException as e:
            raise ex.SwiftClientException(six.text_type(e))

    return body