示例#1
0
文件: cdn_local.py 项目: fzhaw/cdn
def delete_object(global_id, container, object_name):
    """
    Deletes an Object

    :param global_id: user id
    :param container: container name
    :return: HTTP 201 if created
    """
    password = request.headers.get('X-Password')
    user = StorageUser.objects(global_id=global_id, global_pwd=password).first()
    if user is not None:
        try:
            conn = swiftclient.Connection(auth_url, global_id, password, auth_version=auth_version,
                                          tenant_name=global_id, insecure=True)
            conn.delete_object(container, object_name)
            conn.close()

            # Delete entry from Mongo
            obj = StorageObject.objects(tenant_name=global_id, container_name=container,
                                        object_name=object_name).first()
            if obj is not None:
                obj.delete()

            return HTTPResponse(status=204)
        except ClientException:
            traceback.print_exc(sys.stderr)
            abort(500, "Could not delete file")
        except AttributeError:
            traceback.print_exc(sys.stderr)
            abort(400, "Request malformed")
    else:
        abort(401, 'Unknown user')
示例#2
0
文件: cdn_local.py 项目: fzhaw/cdn
def post_object(global_id, container):
    """
    Uploads an Object, must include a file with id=file_content and a header X-Password=global_id user's password
    :param global_id: user id
    :param container: container name
    :return: HTTP 201 if created
    """
    password = request.headers.get('X-Password')
    user = StorageUser.objects(global_id=global_id, global_pwd=password).first()
    if user is not None:
        tenant_id = user.tenant_id
        fil = request.files.get('file_content')
        try:
            conn = swiftclient.Connection(auth_url, global_id, password, auth_version=auth_version,
                                          tenant_name=global_id, insecure=True)
            conn.put_object(container, fil.filename, fil.file)
            conn.close()

            # Save to Mongo
            obj = StorageObject(tenant_id=tenant_id, tenant_name=global_id, container_name=container,
                                object_name=fil.filename, user=user)
            obj.save()

            return HTTPResponse(status=201)
        except ClientException:
            traceback.print_exc(sys.stderr)
            abort(500, "Could not upload file")
        except AttributeError:
            abort(400, "Request malformed, probably object is not sent with the file_content identifier")
    else:
        abort(401, 'Unknown user')
示例#3
0
def post_object(global_id, container):
    """
    Uploads an Object, must include a file with id=file_content and a header X-Password=global_id user's password
    :param global_id: user id
    :param container: container name
    :return: HTTP 201 if created
    """
    password = request.headers.get('X-Password')
    user = StorageUser.objects(global_id=global_id, global_pwd=password).first()
    if user is not None:
        tenant_id = user.tenant_id
        fil = request.files.get('file_content')
        try:
            conn = swiftclient.Connection(auth_url, global_id, password, auth_version=auth_version,
                                          tenant_name=global_id, insecure=True)
            conn.put_object(container, fil.filename, fil.file)
            conn.close()

            # Save to Mongo
            obj = StorageObject(tenant_id=tenant_id, tenant_name=global_id, container_name=container,
                                object_name=fil.filename, user=user)
            obj.save()

            return HTTPResponse(status=201)
        except ClientException:
            traceback.print_exc(sys.stderr)
            abort(500, "Could not upload file")
        except AttributeError:
            abort(400, "Request malformed, probably object is not sent with the file_content identifier")
    else:
        abort(401, 'Unknown user')
示例#4
0
文件: cdn_local.py 项目: fzhaw/cdn
def delete_container(global_id, container_name):
    """
    Deletes a container for specific tenant
    :param global_id: user id
    :param container_name: container name
    :return: HTTP 204 if deleted
    """
    password = request.headers.get('X-Password')
    if StorageUser.objects(global_id=global_id, global_pwd=password).first() is not None:
        try:
            conn = swiftclient.Connection(auth_url, global_id, password, auth_version=auth_version,
                                          tenant_name=global_id,
                                          insecure=True)
            for obj in conn.get_container(container_name)[1]:
                # delete objects one by one before deleting the container
                conn.delete_object(container_name, obj['name'])
            # now deletes container
            conn.delete_container(container_name)
            conn.close()
            return HTTPResponse(status=204)
        except:
            traceback.print_exc(sys.stdout)
            abort(500, 'Server Error, Please check logs')
    else:
        abort(401, 'Unknown user')
示例#5
0
def delete_object(global_id, container, object_name):
    """
    Deletes an Object

    :param global_id: user id
    :param container: container name
    :return: HTTP 201 if created
    """
    password = request.headers.get('X-Password')
    user = StorageUser.objects(global_id=global_id, global_pwd=password).first()
    if user is not None:
        try:
            conn = swiftclient.Connection(auth_url, global_id, password, auth_version=auth_version,
                                          tenant_name=global_id, insecure=True)
            conn.delete_object(container, object_name)
            conn.close()

            # Delete entry from Mongo
            obj = StorageObject.objects(tenant_name=global_id, container_name=container,
                                        object_name=object_name).first()
            if obj is not None:
                obj.delete()

            return HTTPResponse(status=204)
        except ClientException:
            traceback.print_exc(sys.stderr)
            abort(500, "Could not delete file")
        except AttributeError:
            traceback.print_exc(sys.stderr)
            abort(400, "Request malformed")
    else:
        abort(401, 'Unknown user')
示例#6
0
def delete_container(global_id, container_name):
    """
    Deletes a container for specific tenant
    :param global_id: user id
    :param container_name: container name
    :return: HTTP 204 if deleted
    """
    password = request.headers.get('X-Password')
    if StorageUser.objects(global_id=global_id, global_pwd=password).first() is not None:
        try:
            conn = swiftclient.Connection(auth_url, global_id, password, auth_version=auth_version,
                                          tenant_name=global_id,
                                          insecure=True)
            for obj in conn.get_container(container_name)[1]:
                # delete objects one by one before deleting the container
                conn.delete_object(container_name, obj['name'])
            # now deletes container
            conn.delete_container(container_name)
            conn.close()
            return HTTPResponse(status=204)
        except:
            traceback.print_exc(sys.stdout)
            abort(500, 'Server Error, Please check logs')
    else:
        abort(401, 'Unknown user')
示例#7
0
文件: cdn_local.py 项目: fzhaw/cdn
def post_account():
    """
    Creates an account on underlying keystone
    Requires body in the request: {"global_id":"xxxx", "global_password":"******"}
    :return: HTTP 201 if created

    """
    try:
        # TODO: Add X-Username and X-Password for admin authentication
        jreq = request.json
        global_id = jreq.get('global_id')
        password = jreq.get('global_password')
        if global_id is not None and password is not None:
            # User exists?
            user = StorageUser.objects(global_id=global_id).first()
            if user is None:
                # Create tenant
                tenant_id = admin_ks_conn.tenants.create(tenant_name=global_id).id #Possibility of failure here if same name already registered
                # Create user
                user_id = admin_ks_conn.users.create(name=global_id, password=password, tenant_id=tenant_id)
                # Set admin role in tenant
                admin_role_id = admin_ks_conn.roles.find(name='admin').id
                admin_ks_conn.roles.add_user_role(user_id, admin_role_id, tenant_id)

                # Save to Mongo
                new_user = StorageUser(global_id=global_id, global_pwd=password, tenant_id=tenant_id)
                new_user.save()

                return HTTPResponse(status=201)
            else:
                abort(409, 'User already exists')
        else:
            abort(400, 'JSON incomplete')
    except JSONDecodeError:
        abort(500, 'JSON received invalid')
    except ClientException, ex:
        abort(500, ex.message)
示例#8
0
def post_account():
    """
    Creates an account on underlying keystone
    Requires body in the request: {"global_id":"xxxx", "global_password":"******"}
    :return: HTTP 201 if created

    """
    try:
        # TODO: Add X-Username and X-Password for admin authentication
        jreq = request.json
        global_id = jreq.get('global_id')
        password = jreq.get('global_password')
        if global_id is not None and password is not None:
            # User exists?
            user = StorageUser.objects(global_id=global_id).first()
            if user is None:
                # Create tenant
                tenant_id = admin_ks_conn.tenants.create(tenant_name=global_id).id #Possibility of failure here if same name already registered
                # Create user
                user_id = admin_ks_conn.users.create(name=global_id, password=password, tenant_id=tenant_id)
                # Set admin role in tenant
                admin_role_id = admin_ks_conn.roles.find(name='admin').id
                admin_ks_conn.roles.add_user_role(user_id, admin_role_id, tenant_id)

                # Save to Mongo
                new_user = StorageUser(global_id=global_id, global_pwd=password, tenant_id=tenant_id)
                new_user.save()

                return HTTPResponse(status=201)
            else:
                abort(409, 'User already exists')
        else:
            abort(400, 'JSON incomplete')
    except JSONDecodeError:
        abort(500, 'JSON received invalid')
    except ClientException, ex:
        abort(500, ex.message)
示例#9
0
文件: cdn_local.py 项目: fzhaw/cdn
def delete_account():
    """
    Deletes an account on underlying keystone
    :return: HTTP 204 if deleted

    """
    try:
        # TODO: Add X-Username and X-Password for admin authentication
        jreq = request.json
        global_id = jreq.get('global_id')
        password = jreq.get('global_password')
        if global_id is not None and password is not None:
            # User exists?
            user = StorageUser.objects(global_id=global_id).first()
            if user is not None:
                # Delete swift content manually (otherwise unrecoverable)
                conn = swiftclient.Connection(auth_url, global_id, password, auth_version=auth_version,
                                              tenant_name=global_id, insecure=True)
                for container in conn.get_account()[1]:
                    container_name = container['name']
                    for obj in conn.get_container(container_name)[1]:
                        object_name = obj['name']
                        conn.delete_object(container_name, object_name)
                    conn.delete_container(container_name)

                conn.close()

                # Delete KS user
                admin_ks_conn.users.delete(global_id)

                # Delete tenant
                admin_ks_conn.tenants.delete(global_id)

                # Delete from Mongo
                user.delete()

                return HTTPResponse(status=204)
            else:
                abort(409, 'User does not exist')
        else:
            abort(400, 'JSON incomplete')
    except JSONDecodeError:
        abort(400, 'JSON received invalid')
示例#10
0
def delete_account():
    """
    Deletes an account on underlying keystone
    :return: HTTP 204 if deleted

    """
    try:
        # TODO: Add X-Username and X-Password for admin authentication
        jreq = request.json
        global_id = jreq.get('global_id')
        password = jreq.get('global_password')
        if global_id is not None and password is not None:
            # User exists?
            user = StorageUser.objects(global_id=global_id).first()
            if user is not None:
                # Delete swift content manually (otherwise unrecoverable)
                conn = swiftclient.Connection(auth_url, global_id, password, auth_version=auth_version,
                                              tenant_name=global_id, insecure=True)
                for container in conn.get_account()[1]:
                    container_name = container['name']
                    for obj in conn.get_container(container_name)[1]:
                        object_name = obj['name']
                        conn.delete_object(container_name, object_name)
                    conn.delete_container(container_name)

                conn.close()

                # Delete KS user
                admin_ks_conn.users.delete(global_id)

                # Delete tenant
                admin_ks_conn.tenants.delete(global_id)

                # Delete from Mongo
                user.delete()

                return HTTPResponse(status=204)
            else:
                abort(409, 'User does not exist')
        else:
            abort(400, 'JSON incomplete')
    except JSONDecodeError:
        abort(400, 'JSON received invalid')
示例#11
0
文件: cdn_local.py 项目: fzhaw/cdn
def get_object(global_id, container_name, object_name):
    """
    Redirects to an object already stored in Swift
    If object missing, raise 404
    :param global_id: global user id
    :param container_name: container name
    :param object_name: file name
    :return: redirection (HTTP 303) to the actual object
    """

    #if file exists
    obj = StorageObject.objects(tenant_name=global_id, container_name=container_name,
                                object_name=object_name).first()
    if obj is not None:
        return redirect(swift_root + "%s/%s/%s" % (obj.tenant_id, container_name, object_name), 303)
    else:
        # let's try to retrieve it... if it exists
        # Before checking anything, verify that user global_id exists!
        usr = StorageUser.objects(global_id=global_id).first()
        if usr is not None and cdn_central_address is not None:
            # find origin for user from central or from the cache
            origin_object = StorageOrigin.objects(user=usr).first()
            origin_address = None
            if origin_object is not None:
                origin_address = origin_object.url
            else:
                r = get(cdn_central_address + '/origin/' + global_id)
                jresp = r.json()
                origin_address = jresp['origin_address']

            # If current instance of cdnlocal IS the origin, then file does not exist, returns a 404
            if origin_address == local_address or origin_address is None:
                return abort(404, 'File does not exist or is unavailable')

            # Cache object, but should redirect to origin this time so user does not wait for file retrieval
            t = Thread(target=cache_object, args=(origin_address, usr, container_name, object_name,))
            t.start()
            print "redirect to: %s/%s/%s/%s" % (origin_address, global_id, container_name, object_name)
            return redirect("%s/%s/%s/%s" % (origin_address, global_id, container_name, object_name), 303)
        else:
            return abort(404, 'User does not exist or Central CDN server undefined')
示例#12
0
def get_object(global_id, container_name, object_name):
    """
    Redirects to an object already stored in Swift
    If object missing, raise 404
    :param global_id: global user id
    :param container_name: container name
    :param object_name: file name
    :return: redirection (HTTP 303) to the actual object
    """

    #if file exists
    obj = StorageObject.objects(tenant_name=global_id, container_name=container_name,
                                object_name=object_name).first()
    if obj is not None:
        return redirect(swift_root + "%s/%s/%s" % (obj.tenant_id, container_name, object_name), 303)
    else:
        # let's try to retrieve it... if it exists
        # Before checking anything, verify that user global_id exists!
        usr = StorageUser.objects(global_id=global_id).first()
        if usr is not None and cdn_central_address is not None:
            # find origin for user from central or from the cache
            origin_object = StorageOrigin.objects(user=usr).first()
            origin_address = None
            if origin_object is not None:
                origin_address = origin_object.url
            else:
                r = get(cdn_central_address + '/origin/' + global_id)
                jresp = r.json()
                origin_address = jresp['origin_address']

            # If current instance of cdnlocal IS the origin, then file does not exist, returns a 404
            if origin_address == local_address or origin_address is None:
                return abort(404, 'File does not exist or is unavailable')

            # Cache object, but should redirect to origin this time so user does not wait for file retrieval
            t = Thread(target=cache_object, args=(origin_address, usr, container_name, object_name))
            t.start()

            return redirect("%s/%s/%s/%s" % (origin_address, global_id, container_name, object_name), 303)
        else:
            return abort(404, 'User does not exist or Central CDN server undefined')
示例#13
0
文件: cdn_local.py 项目: fzhaw/cdn
def create_container(global_id, container_name):
    """
    Creates a container for specific tenant
    :param global_id: user id
    :param container_name: container name
    :return: HTTP 201 if created
    """
    password = request.headers.get('X-Password')
    if StorageUser.objects(global_id=global_id, global_pwd=password).first() is not None:
        try:
            conn = swiftclient.Connection(auth_url, global_id, password, auth_version=auth_version,
                                          tenant_name=global_id,
                                          insecure=True)
            conn.put_container(container_name, {"X-Container-Read": ".r:*"})
            conn.close()
            return HTTPResponse(status=201)
        except:
            traceback.print_exc(sys.stdout)
            abort(500, 'Server Error, Please check logs')
    else:
        abort(401, 'Unknown user')
示例#14
0
def create_container(global_id, container_name):
    """
    Creates a container for specific tenant
    :param global_id: user id
    :param container_name: container name
    :return: HTTP 201 if created
    """
    password = request.headers.get('X-Password')
    if StorageUser.objects(global_id=global_id, global_pwd=password).first() is not None:
        try:
            conn = swiftclient.Connection(auth_url, global_id, password, auth_version=auth_version,
                                          tenant_name=global_id,
                                          insecure=True)
            conn.put_container(container_name, {"X-Container-Read": ".r:*"})
            conn.close()
            return HTTPResponse(status=201)
        except:
            traceback.print_exc(sys.stdout)
            abort(500, 'Server Error, Please check logs')
    else:
        abort(401, 'Unknown user')