Ejemplo n.º 1
0
    def delete_container(self, name):
        if not self.http_conn:
            self.connect()

        swift.delete_container(url=self.swift_url, token=self.token, http_conn=self.http_conn, container=name)
        if self.debug:
            print("Container {0} deleted".format(name))
Ejemplo n.º 2
0
 def delete(self, container):
     """Container must be empty for deletion to succeed."""
     super(SwiftContainers, self).delete(container)
     swift_client.delete_container(self.endpoint,
                                   self.token,
                                   container,
                                   http_conn=self.http_conn)
Ejemplo n.º 3
0
    def test_missing_container(self):
        # In this test, we need to put container at handoff devices, so we
        # need container devices more than replica count
        if len(self.container_ring.devs) <= self.container_ring.replica_count:
            raise SkipTest("Need devices more that replica count")

        container = 'container-%s' % uuid4()
        cpart, cnodes = self.container_ring.get_nodes(self.account, container)

        # Kill all primary container servers
        for cnode in cnodes:
            kill_server((cnode['ip'], cnode['port']), self.ipport2server)

        # Create container, and all of its replicas are placed at handoff
        # device
        try:
            client.put_container(self.url, self.token, container)
        except ClientException as err:
            # if the cluster doesn't have enough devices, swift may return
            # error (ex. When we only have 4 devices in 3-replica cluster).
            self.assertEqual(err.http_status, 503)

        # Assert handoff device has a container replica
        another_cnode = next(self.container_ring.get_more_nodes(cpart))
        direct_client.direct_get_container(
            another_cnode, cpart, self.account, container)

        # Restart all primary container servers
        for cnode in cnodes:
            start_server((cnode['ip'], cnode['port']), self.ipport2server)

        # Create container/obj
        obj = 'object-%s' % uuid4()
        client.put_object(self.url, self.token, container, obj, '')

        # Run the object-updater
        Manager(['object-updater']).once()

        # Run the container-replicator, and now, container replicas
        # at handoff device get moved to primary servers
        Manager(['container-replicator']).once()

        # Assert container replicas in primary servers, just moved by
        # replicator don't know about the object
        for cnode in cnodes:
            self.assertFalse(direct_client.direct_get_container(
                cnode, cpart, self.account, container)[1])

        # since the container is empty - we can delete it!
        client.delete_container(self.url, self.token, container)

        # Re-run the object-updaters and now container replicas in primary
        # container servers should get updated
        Manager(['object-updater']).once()

        # Assert all primary container servers know about container/obj
        for cnode in cnodes:
            objs = [o['name'] for o in direct_client.direct_get_container(
                    cnode, cpart, self.account, container)[1]]
            self.assertIn(obj, objs)
Ejemplo n.º 4
0
 def delete_data(self, context, container_name):
     url = self._get_endpoint_url(context)
     token = context.auth_token
     try:
         headers, info = swift_client.get_container(
             url, token, container_name
         )
         for obj in info:
             try:
                 swift_client.delete_object(
                     url, token, container_name, obj['name']
                 )
             except swift_exceptions.ClientException as e:
                 LOG.error(e)
                 if e.http_status != 404:
                     raise
         swift_client.delete_container(url, token, container_name)
     except swift_exceptions.ClientException as e:
         LOG.error(e)
         if e.http_status == 404:
             raise exception.ContainerNotExists(
                 container_name=container_name
             )
         else:
             raise exception.ContainerDeletionError(
                 container_name=container_name
             )
Ejemplo n.º 5
0
 def delete_container(self, swift_url=None, swift_container=None):
     swift_connection = swift.HTTPConnection(url=swift_url, insecure=True)
     swift.delete_container(swift_url,
                            self.keystone.get_token('id'),
                            swift_container,
                            http_conn=(swift_connection.parsed_url,
                                       swift_connection))
def delete_container(request):
    auth_token = request.session['auth_token']
    storage_url = request.session['storage_url']

    container = ''
    if 'container' in request.GET.keys():
        container = request.GET['container']
    else:
        return redirect(containers)

    try:
        http_conn = (urlparse(storage_url),
                     client.HTTPConnection(storage_url, insecure=settings.SWIFT_SSL_INSECURE))
        _m, objects = client.get_container(storage_url, auth_token, container, http_conn=http_conn)
        for obj in objects:
            logger.info("Deleting object %s in container %s" % (obj['name'], container))
            client.delete_object(storage_url, auth_token,
                                 container, obj['name'], http_conn=http_conn)
        logger.info("Deleting container %s" % (container))
        client.delete_container(storage_url, auth_token, container, http_conn=http_conn)
        messages.add_message(request, messages.INFO, "Container deleted.")
    except client.ClientException:
        messages.add_message(request, messages.ERROR, "Access denied.")

    return redirect(containers)
Ejemplo n.º 7
0
 def test_one_node_fails(self):
     # Create container1
     # Kill container1 servers excepting two of the primaries
     # Delete container1
     # Restart other container1 primary server
     # Create container1/object1 (allowed because at least server thinks the
     #   container exists)
     # Get to a final state
     # Assert all container1 servers indicate container1 is alive and
     #   well with object1
     # Assert account level also indicates container1 is alive and
     #   well with object1
     container1 = 'container-%s' % uuid4()
     cpart, cnodes = self.container_ring.get_nodes(self.account, container1)
     client.put_container(self.url, self.token, container1)
     kill_nonprimary_server(cnodes, self.port2server, self.pids)
     kill_server(cnodes[0]['port'], self.port2server, self.pids)
     client.delete_container(self.url, self.token, container1)
     start_server(cnodes[0]['port'], self.port2server, self.pids)
     client.put_object(self.url, self.token, container1, 'object1', '123')
     get_to_final_state()
     for cnode in cnodes:
         self.assertEquals([
             o['name'] for o in direct_client.direct_get_container(
                 cnode, cpart, self.account, container1)[1]
         ], ['object1'])
     headers, containers = client.get_account(self.url, self.token)
     self.assertEquals(headers['x-account-container-count'], '1')
     self.assertEquals(headers['x-account-object-count'], '1')
     self.assertEquals(headers['x-account-bytes-used'], '3')
Ejemplo n.º 8
0
def delete_container(request, container, force=True):
    """
    Deletes a container. If force is True, it will deletes all objects first.
    """

    storage_url = get_endpoint(request, 'adminURL')
    auth_token = get_token_id(request)
    http_conn = client.http_connection(storage_url,
                                       insecure=settings.SWIFT_INSECURE)

    if force:
        try:
            _, objects = client.get_container(storage_url,
                                           auth_token,
                                           container,
                                           http_conn=http_conn)

        except client.ClientException as err:
            log.exception('{}{}'.format(ugettext('Exception:').encode('UTF-8'), err))
            return False

        for obj in objects:
            delete_object(request=request,
                          container=container,
                          objectname=obj['name'])

    try:
        client.delete_container(storage_url, auth_token,
                                container, http_conn=http_conn)
        actionlog.log(request.user.username, "delete", container)
    except client.ClientException as err:
        log.exception('{}{}'.format(ugettext('Exception:').encode('UTF-8'), err))
        return False

    return True
Ejemplo n.º 9
0
 def test_one_node_fails(self):
     # Create container1
     # Kill container1 servers excepting two of the primaries
     # Delete container1
     # Restart other container1 primary server
     # Create container1/object1 (allowed because at least server thinks the
     #   container exists)
     # Get to a final state
     # Assert all container1 servers indicate container1 is alive and
     #   well with object1
     # Assert account level also indicates container1 is alive and
     #   well with object1
     container1 = 'container-%s' % uuid4()
     cpart, cnodes = self.container_ring.get_nodes(self.account, container1)
     client.put_container(self.url, self.token, container1)
     kill_nonprimary_server(cnodes, self.port2server, self.pids)
     kill_server(cnodes[0]['port'], self.port2server, self.pids)
     client.delete_container(self.url, self.token, container1)
     start_server(cnodes[0]['port'], self.port2server, self.pids)
     client.put_object(self.url, self.token, container1, 'object1', '123')
     get_to_final_state()
     for cnode in cnodes:
         self.assertEquals(
             [o['name'] for o in direct_client.direct_get_container(
                 cnode, cpart, self.account, container1)[1]],
             ['object1'])
     headers, containers = client.get_account(self.url, self.token)
     self.assertEquals(headers['x-account-container-count'], '1')
     self.assertEquals(headers['x-account-object-count'], '1')
     self.assertEquals(headers['x-account-bytes-used'], '3')
Ejemplo n.º 10
0
 def _tearDown(self):
     client.delete_object(self.url, self.token, self.container_name,
                          'eran_swapped_mov.avi')
     client.delete_object(self.url, self.token, self.container_name,
                          'model')
     client.delete_object(self.url, self.token, self.container_name,
                          'recognized_mov.avi')
     client.delete_container(self.url, self.token, self.container_name)
Ejemplo n.º 11
0
 def delete(self):
     name = self.path.strip('/')
     try:
         client.delete_container(self.storage_url,
                                 self.auth_token,
                                 name,
                                 http_conn=self.http_connection)
     except client.ClientException:
         raise dav_error.DAVError(dav_error.HTTP_INTERNAL_ERROR)
Ejemplo n.º 12
0
    def test_missing_container(self):
        # In this test, we need to put container at handoff devices, so we
        # need container devices more than replica count
        if len(self.container_ring.devs) <= self.container_ring.replica_count:
            raise SkipTest("Need devices more that replica count")

        container = "container-%s" % uuid4()
        cpart, cnodes = self.container_ring.get_nodes(self.account, container)

        # Kill all primary container servers
        for cnode in cnodes:
            kill_server((cnode["ip"], cnode["port"]), self.ipport2server)

        # Create container, and all of its replicas are placed at handoff
        # device
        try:
            client.put_container(self.url, self.token, container)
        except ClientException as err:
            # if the cluster doesn't have enough devices, swift may return
            # error (ex. When we only have 4 devices in 3-replica cluster).
            self.assertEqual(err.http_status, 503)

        # Assert handoff device has a container replica
        another_cnode = self.container_ring.get_more_nodes(cpart).next()
        direct_client.direct_get_container(another_cnode, cpart, self.account, container)

        # Restart all primary container servers
        for cnode in cnodes:
            start_server((cnode["ip"], cnode["port"]), self.ipport2server)

        # Create container/obj
        obj = "object-%s" % uuid4()
        client.put_object(self.url, self.token, container, obj, "")

        # Run the object-updater
        Manager(["object-updater"]).once()

        # Run the container-replicator, and now, container replicas
        # at handoff device get moved to primary servers
        Manager(["container-replicator"]).once()

        # Assert container replicas in primary servers, just moved by
        # replicator don't know about the object
        for cnode in cnodes:
            self.assertFalse(direct_client.direct_get_container(cnode, cpart, self.account, container)[1])

        # since the container is empty - we can delete it!
        client.delete_container(self.url, self.token, container)

        # Re-run the object-updaters and now container replicas in primary
        # container servers should get updated
        Manager(["object-updater"]).once()

        # Assert all primary container servers know about container/obj
        for cnode in cnodes:
            objs = [o["name"] for o in direct_client.direct_get_container(cnode, cpart, self.account, container)[1]]
            self.assertIn(obj, objs)
Ejemplo n.º 13
0
 def tearDown(self):
     client.delete_object(self.url, self.token, self.container_name,
                          'small_eran.jpeg')
     client.delete_object(self.url, self.token, self.container_name,
                          'small_eran_2.jpeg')
     client.delete_object(self.url, self.token, self.container_name,
                          'small_eran_3.jpeg')
     client.delete_object(self.url, self.token, self.container_name,
                          'model')
     client.delete_container(self.url, self.token, self.container_name)
Ejemplo n.º 14
0
 def delete(self):
     name = self.path.strip('/')
     try:
         client.delete_container(
             self.storage_url,
             self.auth_token,
             name,
             http_conn=self.http_connection)
     except client.ClientException:
         raise dav_error.DAVError(dav_error.HTTP_INTERNAL_ERROR)
Ejemplo n.º 15
0
    def moveRecursive(self, newname):
        """ Move/rename a container.

        This is only working for empty containers and objects with a size of 0.
        Required by some clients because they create a folder "New folder" first.

        For all other requests this will simply return HTTP_FORBIDDEN. """
        oldname = self.path.lstrip('/')
        newname = newname.lstrip('/')

        if '/' not in oldname:
            try:
                # Container deletion will fail if not empty
                client.delete_container(self.storage_url,
                                        self.auth_token,
                                        oldname)
                client.put_container(self.storage_url,
                                     self.auth_token,
                                     newname)
            except client.ClientException:
                raise dav_error.DAVError(dav_error.HTTP_FORBIDDEN)

        else:
            old_container, _, old_object = oldname.partition('/')
            new_container, _, new_object = newname.partition('/')
            if old_container != new_container:
                raise dav_error.DAVError(dav_error.HTTP_FORBIDDEN)

            # If it is a pseudofolder, check that it is empty
            _, objects = client.get_container(
                self.storage_url,
                self.auth_token,
                container=self.container,
                delimiter='/',
                prefix=sanitize(old_object).rstrip('/') + '/',
                http_conn=self.http_connection)
            if len(objects) != 1:  # first object is the pseudofolder entry
                raise dav_error.DAVError(dav_error.HTTP_FORBIDDEN)

            # also check if the object is empty
            if objects[0].get('bytes') != 0:
                raise dav_error.DAVError(dav_error.HTTP_FORBIDDEN)

            # Do a COPY to preserve existing metadata and content-type
            client.put_object(self.storage_url,
								  self.auth_token,
								  self.container,
								  sanitize(new_object),
                                  headers = {'X-Copy-From': '/' + oldname + '/'},
								  http_conn=self.http_connection)
            client.delete_object(self.storage_url,
                                 self.auth_token,
                                 self.container,
                                 old_object + '/',
                                 http_conn=self.http_connection)
Ejemplo n.º 16
0
    def copyMoveSingle(self, destPath, isMove):
        src = '/'.join(self.path.split('/')[2:])
        dst = '/'.join(destPath.split('/')[2:])

        src_cont = self.path.split('/')[1]
        dst_cont = destPath.split('/')[1]

        # Make sure target container exists
        try:
            client.put_container(self.storage_url,
                                 self.auth_token,
                                 dst_cont,
                                 http_conn=self.http_connection)
        except:
            pass

        _, objects = client.get_container(self.storage_url,
                                          self.auth_token,
                                          container=src_cont,
                                          delimiter='/',
                                          prefix=src,
                                          http_conn=self.http_connection)

        for obj in objects:
            objname = obj.get('name', obj.get('subdir'))
            headers = {'X-Copy-From': '%s/%s' % (self.container, objname)}
            newname = objname.replace(src, dst)
            if newname[-1] == '/':
                newname = newname.rstrip('/') + '/'
            try:
                client.put_object(self.storage_url,
                                  self.auth_token,
                                  dst_cont,
                                  newname,
                                  headers=headers,
                                  http_conn=self.http_connection)
                if isMove:
                    client.delete_object(self.storage_url,
                                         self.auth_token,
                                         src_cont,
                                         objname,
                                         http_conn=self.http_connection)
            except client.ClientException:
                pass

        # will only succeed if container is empty
        if isMove:
            try:
                client.delete_container(self.storage_url,
                                        self.auth_token,
                                        self.container,
                                        http_conn=self.http_connection)
            except client.ClientException:
                pass
Ejemplo n.º 17
0
 def delete_account_cont(self, account_url, token):
     cnx = sclient.http_connection(account_url)
     al = sclient.get_account(None, token, http_conn=cnx, full_listing=True)
     for container in [c['name'] for c in al[1]]:
         ci = sclient.get_container(None,
                                    token,
                                    container,
                                    http_conn=cnx,
                                    full_listing=True)
         on = [od['name'] for od in ci[1]]
         for obj in on:
             sclient.delete_object('', token, container, obj, http_conn=cnx)
         sclient.delete_container('', token, container, http_conn=cnx)
Ejemplo n.º 18
0
 def run_test(num_locks, catch_503):
     container = 'container-%s' % uuid4()
     client.put_container(self.url, self.token, container)
     db_files = self._get_container_db_files(container)
     db_conns = []
     for i in range(num_locks):
         db_conn = sqlite3.connect(db_files[i])
         db_conn.execute('begin exclusive transaction')
         db_conns.append(db_conn)
     if catch_503:
         try:
             client.delete_container(self.url, self.token, container)
         except client.ClientException, e:
             self.assertEquals(e.http_status, 503)
Ejemplo n.º 19
0
 def delete_account_cont(self, account_url, token):
     cnx = sclient.http_connection(account_url)
     al = sclient.get_account(None, token,
                              http_conn=cnx,
                              full_listing=True)
     for container in [c['name'] for c in al[1]]:
         ci = sclient.get_container(None, token,
                                    container, http_conn=cnx,
                                    full_listing=True)
         on = [od['name'] for od in ci[1]]
         for obj in on:
             sclient.delete_object('', token, container,
                                   obj, http_conn=cnx)
         sclient.delete_container('', token, container, http_conn=cnx)
Ejemplo n.º 20
0
 def run_test(num_locks, catch_503):
     container = 'container-%s' % uuid4()
     client.put_container(self.url, self.token, container)
     db_files = self._get_container_db_files(container)
     db_conns = []
     for i in range(num_locks):
         db_conn = connect(db_files[i])
         db_conn.execute('begin exclusive transaction')
         db_conns.append(db_conn)
     if catch_503:
         exc = None
         try:
             client.delete_container(self.url, self.token, container)
         except client.ClientException, err:
             exc = err
         self.assertEquals(exc.http_status, 503)
Ejemplo n.º 21
0
def delete_container(request, container):
    """ Deletes a container """

    storage_url = request.session.get("storage_url", "")
    auth_token = request.session.get("auth_token", "")

    try:
        _m, objects = client.get_container(storage_url, auth_token, container)
        for obj in objects:
            client.delete_object(storage_url, auth_token, container, obj["name"])
        client.delete_container(storage_url, auth_token, container)
        messages.add_message(request, messages.INFO, _("Container deleted."))
    except client.ClientException:
        messages.add_message(request, messages.ERROR, _("Access denied."))

    return redirect(containerview)
Ejemplo n.º 22
0
    def test_first_two_nodes_fail(self):
        container = 'container-%s' % uuid4()
        client.put_container(self.url, self.token, container)
        self.assert_(container in [c['name'] for c in
                     client.get_account(self.url, self.token)[1]])

        object1 = 'object1'
        client.put_object(self.url, self.token, container, object1, 'test')
        self.assert_(container in [c['name'] for c in
                     client.get_account(self.url, self.token)[1]])
        self.assert_(object1 in [o['name'] for o in
                     client.get_container(self.url, self.token, container)[1]])

        cpart, cnodes = self.container_ring.get_nodes(self.account, container)
        for x in xrange(2):
            kill(self.pids[self.port2server[cnodes[x]['port']]], SIGTERM)

        client.delete_object(self.url, self.token, container, object1)
        self.assert_(container in [c['name'] for c in
                     client.get_account(self.url, self.token)[1]])
        self.assert_(object1 not in [o['name'] for o in
                     client.get_container(self.url, self.token, container)[1]])

        for x in xrange(2):
            self.pids[self.port2server[cnodes[x]['port']]] = \
                Popen(['swift-container-server',
                       '/etc/swift/container-server/%d.conf' %
                        ((cnodes[x]['port'] - 6001) / 10)]).pid
        sleep(2)
        self.assert_(container in [c['name'] for c in
                     client.get_account(self.url, self.token)[1]])
        # This okay because the first node hasn't got the update that the
        # object was deleted yet.
        self.assert_(object1 in [o['name'] for o in
                     direct_client.direct_get_container(cnodes[0], cpart,
                     self.account, container)[1]])

        # This fails because all three nodes have to indicate deletion before
        # we tell the user it worked. Since the first node 409s (it hasn't got
        # the update that the object was deleted yet), the whole must 503
        # (until every is synced up, then the delete would work).
        exc = None
        try:
            client.delete_container(self.url, self.token, container)
        except client.ClientException, err:
            exc = err
Ejemplo n.º 23
0
def delete_container(request, container):
    """ Deletes a container """

    storage_url = request.session.get('storage_url', '')
    auth_token = request.session.get('auth_token', '')

    try:
        _m, objects = client.get_container(storage_url, auth_token, container)
        for obj in objects:
            client.delete_object(storage_url, auth_token, container,
                                 obj['name'])
        client.delete_container(storage_url, auth_token, container)
        messages.add_message(request, messages.INFO, _("Container deleted."))
    except client.ClientException:
        messages.add_message(request, messages.ERROR, _("Access denied."))

    return redirect(containerview)
Ejemplo n.º 24
0
    def clean_container(self, container_name):
        _, objects = client.get_container(
            self.url, self.token, container_name,
            full_listing=True)

        # delete all objects inside the container
        # N.B. this cleanup could run in parallel but currently we have a few
        # objects in the user testing container so that, currently this does
        # as sequential simply
        for obj_dict in objects:
            client.delete_object(
                self.url, self.token,
                container_name, obj_dict['name'])
        client.get_container(self.url, self.token, container_name)

        # delete the container
        client.delete_container(self.url, self.token, container_name)    
Ejemplo n.º 25
0
def del_container(container):
    headers, objs = container_info(container)
    if len(objs) != 0:
        for i in objs:
            del_object(container, i['name'])
    return client.delete_container(url=storageURL,
                                   token=authtoken,
                                   container=container)
Ejemplo n.º 26
0
def update_container(request, project_id, container_name):

    if request.method == 'PUT':
        sp = JSONParser().parse(request)
        token = get_token_connection(request)
        url = settings.SWIFT_URL + "/AUTH_" + project_id

        headers, obj_list = swift_client.get_container(url, token,
                                                       container_name)
        headers['X-Storage-Policy'] = sp

        path_container = settings.SWIFT_CFG_TMP_DIR + "/" + container_name
        os.mkdir(path_container)

        for obj in obj_list:
            file = open(path_container + "/" + obj["name"], "w")
            obj_headers, obj_body = swift_client.get_object(
                url, token, container_name, obj["name"])
            file.write(obj_body)
            file.close()
            obj["headers"] = obj_headers
            swift_client.delete_object(url, token, container_name, obj["name"])

        swift_client.delete_container(url, token, container_name)
        swift_client.put_container(url, token, container_name, headers)

        for obj in obj_list:
            obj_path = os.path.join(path_container, obj["name"])
            obj_body = open(obj_path, "r")
            content_length = os.stat(obj_path).st_size
            swift_response = {}
            swift_client.put_object(url, token, container_name, obj["name"],
                                    obj_body, content_length, None, None,
                                    obj['content_type'], obj["headers"], None,
                                    None, None, swift_response)
            obj_body.close()
            os.remove(obj_path)

        os.rmdir(path_container)

        return JSONResponse("Container Policy updated correctly",
                            status=status.HTTP_201_CREATED)

    return JSONResponse('Method ' + str(request.method) + ' not allowed.',
                        status=status.HTTP_405_METHOD_NOT_ALLOWED)
Ejemplo n.º 27
0
 def run_test(num_locks, catch_503):
     container = "container-%s" % uuid4()
     client.put_container(self.url, self.token, container)
     db_files = self._get_container_db_files(container)
     db_conns = []
     for i in range(num_locks):
         db_conn = connect(db_files[i])
         db_conn.execute("begin exclusive transaction")
         db_conns.append(db_conn)
     if catch_503:
         try:
             client.delete_container(self.url, self.token, container)
         except client.ClientException as err:
             self.assertEquals(err.http_status, 503)
         else:
             self.fail("Expected ClientException but didn't get it")
     else:
         client.delete_container(self.url, self.token, container)
Ejemplo n.º 28
0
def del_container(container):
    headers, objs = container_info(container)
    if len(objs) != 0:
        for i in objs:
            del_object(container,i['name'])
    return client.delete_container(url = storageURL,
                                   token = authtoken,
                                   container = container
                                  )
 def run_test(num_locks, catch_503):
     container = 'container-%s' % uuid4()
     client.put_container(self.url, self.token, container)
     db_files = self._get_container_db_files(container)
     db_conns = []
     for i in range(num_locks):
         db_conn = connect(db_files[i])
         db_conn.execute('begin exclusive transaction')
         db_conns.append(db_conn)
     if catch_503:
         try:
             client.delete_container(self.url, self.token, container)
         except client.ClientException as err:
             self.assertEqual(err.http_status, 503)
         else:
             self.fail("Expected ClientException but didn't get it")
     else:
         client.delete_container(self.url, self.token, container)
Ejemplo n.º 30
0
    def delete_container(self, container_name):
        """

        :param container_name:
        :return:
        """
        ret = sc.delete_container(self.storage_location, self.ks.auth_token, container_name)
        if ret is None:
            print('Container: %s has been deleted.' % container_name)
Ejemplo n.º 31
0
def delete_container(request, container):
    """ Deletes a container """

    storage_url = request.session.get('storage_url', '')
    auth_token = request.session.get('auth_token', '')

    try:
        _m, objects = client.get_container(storage_url, auth_token, container)
        for obj in objects:
            client.delete_object(storage_url, auth_token,
                                 container, obj['name'])
        client.delete_container(storage_url, auth_token, container)
        messages.add_message(request, messages.INFO, _("Container deleted."))
    except client.ClientException:
        traceback.print_exc()
        messages.add_message(request, messages.ERROR, _("Access denied."))

    return redirect(containerview)
Ejemplo n.º 32
0
    def delete_container(self, container_name):
        """

        :param container_name:
        :return:
        """
        ret = sc.delete_container(self.storage_location, self.ks.auth_token,
                                  container_name)
        if ret is None:
            print('Container: %s has been deleted.' % container_name)
Ejemplo n.º 33
0
def delete_swift_account(storage_url, auth_token):
    insecure = settings.SWIFT_INSECURE

    try:
        # Criar container vazio para garantir que o account existe no swift
        http_conn = client.http_connection(storage_url, insecure=insecure)

        client.put_container(storage_url,
                             auth_token,
                             'dummy_container',
                             http_conn=http_conn)
        client.delete_container(storage_url,
                                auth_token,
                                'dummy_container',
                                http_conn=http_conn)
    except client.ClientException as err:
        log.exception(
            'Fail to create container "dummy_container": {0}'.format(err))
        return False

    try:
        # Deletar o account
        url = urlparse(storage_url)
        domain = '{}://{}'.format(url.scheme, url.netloc)
        path = url.path

        http_conn = client.HTTPConnection(domain, insecure=insecure)
        headers = {'X-Auth-Token': auth_token}

        resp = http_conn.request('DELETE', path, headers=headers)

        if resp.status_code != 204:
            error_msg = resp.json().get('error')
            log.exception(
                'Fail to delete account {}: {}, status code {}'.format(
                    storage_url, error_msg, resp.status_code))
            return False, error_msg

    except client.ClientException as err:
        log.exception(f'Exception: {err}')
        return False, str(err)

    return True, 'success'
Ejemplo n.º 34
0
    def delete(self):
        prefix = '/'.join(self.path.split('/')[2:])
        if '/' + self.container == self.path:
            try:
                client.delete_container(self.storage_url,
                                        self.auth_token,
                                        self.container,
                                        http_conn=self.http_connection)
            except client.ClientException:
                pass
        else:
            try:
                client.delete_object(self.storage_url,
                                     self.auth_token,
                                     self.container,
                                     prefix + '/',
                                     http_conn=self.http_connection)

            except client.ClientException:
                pass
Ejemplo n.º 35
0
    def delete(self):
        prefix = '/'.join(self.path.split('/')[2:])
        if '/' + self.container == self.path:
            try:
                client.delete_container(self.storage_url,
                                        self.auth_token,
                                        self.container,
                                        http_conn=self.http_connection)
            except client.ClientException:
                pass
        else:
            try:
                client.delete_object(self.storage_url,
                                     self.auth_token,
                                     self.container,
                                     prefix + '/',
                                     http_conn=self.http_connection)

            except client.ClientException:
                pass
Ejemplo n.º 36
0
def update_container(request, project_id, container_name):

    if request.method == 'PUT':
        sp = JSONParser().parse(request)
        token = get_token_connection(request)
        url = settings.SWIFT_URL + "/AUTH_" + project_id

        headers, obj_list = swift_client.get_container(url, token, container_name)
        headers['X-Storage-Policy'] = sp

        path_container = settings.SWIFT_CFG_TMP_DIR + "/" + container_name
        os.mkdir(path_container)

        for obj in obj_list:
            fle = open(path_container + "/" + obj["name"], "w")
            obj_headers, obj_body = swift_client.get_object(url, token, container_name, obj["name"])
            fle.write(obj_body)
            fle.close()
            obj["headers"] = obj_headers
            swift_client.delete_object(url, token, container_name, obj["name"])

        swift_client.delete_container(url, token, container_name)
        swift_client.put_container(url, token, container_name, headers)

        for obj in obj_list:
            obj_path = os.path.join(path_container, obj["name"])
            obj_body = open(obj_path, "r")
            content_length = os.stat(obj_path).st_size
            swift_response = {}
            swift_client.put_object(url, token, container_name, obj["name"],
                                    obj_body, content_length,
                                    None, None, obj['content_type'],
                                    obj["headers"], None, None, None, swift_response)
            obj_body.close()
            os.remove(obj_path)

        os.rmdir(path_container)

        return JSONResponse("Container Policy updated correctly", status=status.HTTP_201_CREATED)

    return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
Ejemplo n.º 37
0
def delete_container(request, container):
    """ Delete all the objects in the given container and delete the container
    itself. Return a JsonResponse if successful or 500 error. """

    storage_url = request.session.get('storage_url', '')
    auth_token = request.session.get('auth_token', '')

    try:
        _m, objects = client.get_container(
            storage_url, auth_token, container,
            headers={"X-Forwarded-For": request.META.get('REMOTE_ADDR')})
        for obj in objects:
            client.delete_object(
                storage_url, auth_token, container, obj['name'],
                headers={"X-Forwarded-For": request.META.get('REMOTE_ADDR')})
        client.delete_container(storage_url, auth_token, container,)
    except client.ClientException:
        return HttpResponse(e, status=500)

    return JsonResponse({
        'success': True,
    })
Ejemplo n.º 38
0
def delete_container(request, container, force=True):
    """
    Deletes a container. If force is True, it will deletes all objects first.
    """

    storage_url = get_endpoint(request, 'adminURL')
    auth_token = get_token_id(request)
    http_conn = client.http_connection(storage_url,
                                       insecure=settings.SWIFT_INSECURE)

    if force:
        try:
            _, objects = client.get_container(storage_url,
                                              auth_token,
                                              container,
                                              http_conn=http_conn)

        except client.ClientException as err:
            log.exception('{}{}'.format(
                ugettext('Exception:').encode('UTF-8'), err))
            return False

        for obj in objects:
            delete_object(request=request,
                          container=container,
                          objectname=obj['name'])

    try:
        client.delete_container(storage_url,
                                auth_token,
                                container,
                                http_conn=http_conn)
        actionlog.log(request.user.username, "delete", container)
    except client.ClientException as err:
        log.exception('{}{}'.format(
            ugettext('Exception:').encode('UTF-8'), err))
        return False

    return True
Ejemplo n.º 39
0
 def delete_data(self, context, container_name):
     url = self._get_endpoint_url(context)
     token = context.auth_token
     try:
         headers, info = swift_client.get_container(url, token,
                                                    container_name)
         for obj in info:
             try:
                 swift_client.delete_object(url, token, container_name,
                                            obj['name'])
             except swift_exceptions.ClientException as e:
                 LOG.error(e)
                 if e.http_status != 404:
                     raise
         swift_client.delete_container(url, token, container_name)
     except swift_exceptions.ClientException as e:
         LOG.error(e)
         if e.http_status == 404:
             raise exception.ContainerNotExists(
                 container_name=container_name)
         else:
             raise exception.ContainerDeletionError(
                 container_name=container_name)
Ejemplo n.º 40
0
    def test_one_node_fails(self):
        # Create container1
        container1 = "container-%s" % uuid4()
        cpart, cnodes = self.container_ring.get_nodes(self.account, container1)
        client.put_container(self.url, self.token, container1)

        # Kill container1 servers excepting two of the primaries
        kill_nonprimary_server(cnodes, self.port2server, self.pids)
        kill_server(cnodes[0]["port"], self.port2server, self.pids)

        # Delete container1
        client.delete_container(self.url, self.token, container1)

        # Restart other container1 primary server
        start_server(cnodes[0]["port"], self.port2server, self.pids)

        # Create container1/object1 (allowed because at least server thinks the
        #   container exists)
        client.put_object(self.url, self.token, container1, "object1", "123")

        # Get to a final state
        self.get_to_final_state()

        # Assert all container1 servers indicate container1 is alive and
        #   well with object1
        for cnode in cnodes:
            self.assertEquals(
                [o["name"] for o in direct_client.direct_get_container(cnode, cpart, self.account, container1)[1]],
                ["object1"],
            )

        # Assert account level also indicates container1 is alive and
        #   well with object1
        headers, containers = client.get_account(self.url, self.token)
        self.assertEquals(headers["x-account-container-count"], "1")
        self.assertEquals(headers["x-account-object-count"], "1")
        self.assertEquals(headers["x-account-bytes-used"], "3")
Ejemplo n.º 41
0
 def delete_container(self, swift_url=None, swift_container=None):
     swift.delete_container(swift_url, self.keystone.get_token('id'), swift_container)
Ejemplo n.º 42
0
    def test_main(self):
        container1 = 'container1'
        client.put_container(self.url, self.token, container1)
        container2 = 'container2'
        client.put_container(self.url, self.token, container2)
        headers, containers = client.get_account(self.url, self.token)
        self.assertEquals(headers['x-account-container-count'], '2')
        self.assertEquals(headers['x-account-object-count'], '0')
        self.assertEquals(headers['x-account-bytes-used'], '0')
        found1 = False
        found2 = False
        for c in containers:
            if c['name'] == container1:
                found1 = True
                self.assertEquals(c['count'], 0)
                self.assertEquals(c['bytes'], 0)
            elif c['name'] == container2:
                found2 = True
                self.assertEquals(c['count'], 0)
                self.assertEquals(c['bytes'], 0)
        self.assert_(found1)
        self.assert_(found2)

        client.put_object(self.url, self.token, container2, 'object1', '1234')
        headers, containers = client.get_account(self.url, self.token)
        self.assertEquals(headers['x-account-container-count'], '2')
        self.assertEquals(headers['x-account-object-count'], '0')
        self.assertEquals(headers['x-account-bytes-used'], '0')
        found1 = False
        found2 = False
        for c in containers:
            if c['name'] == container1:
                found1 = True
                self.assertEquals(c['count'], 0)
                self.assertEquals(c['bytes'], 0)
            elif c['name'] == container2:
                found2 = True
                self.assertEquals(c['count'], 0)
                self.assertEquals(c['bytes'], 0)
        self.assert_(found1)
        self.assert_(found2)

        get_to_final_state()
        headers, containers = client.get_account(self.url, self.token)
        self.assertEquals(headers['x-account-container-count'], '2')
        self.assertEquals(headers['x-account-object-count'], '1')
        self.assertEquals(headers['x-account-bytes-used'], '4')
        found1 = False
        found2 = False
        for c in containers:
            if c['name'] == container1:
                found1 = True
                self.assertEquals(c['count'], 0)
                self.assertEquals(c['bytes'], 0)
            elif c['name'] == container2:
                found2 = True
                self.assertEquals(c['count'], 1)
                self.assertEquals(c['bytes'], 4)
        self.assert_(found1)
        self.assert_(found2)

        apart, anodes = self.account_ring.get_nodes(self.account)
        kill(self.pids[self.port2server[anodes[0]['port']]], SIGTERM)

        client.delete_container(self.url, self.token, container1)
        client.put_object(self.url, self.token, container2, 'object2', '12345')
        headers, containers = client.get_account(self.url, self.token)
        self.assertEquals(headers['x-account-container-count'], '1')
        self.assertEquals(headers['x-account-object-count'], '1')
        self.assertEquals(headers['x-account-bytes-used'], '4')
        found1 = False
        found2 = False
        for c in containers:
            if c['name'] == container1:
                found1 = True
            elif c['name'] == container2:
                found2 = True
                self.assertEquals(c['count'], 1)
                self.assertEquals(c['bytes'], 4)
        self.assert_(not found1)
        self.assert_(found2)

        ps = []
        for n in xrange(1, 5):
            ps.append(Popen(['swift-container-updater',
                             '/etc/swift/container-server/%d.conf' % n,
                             'once']))
        for p in ps:
            p.wait()
        headers, containers = client.get_account(self.url, self.token)
        self.assertEquals(headers['x-account-container-count'], '1')
        self.assertEquals(headers['x-account-object-count'], '2')
        self.assertEquals(headers['x-account-bytes-used'], '9')
        found1 = False
        found2 = False
        for c in containers:
            if c['name'] == container1:
                found1 = True
            elif c['name'] == container2:
                found2 = True
                self.assertEquals(c['count'], 2)
                self.assertEquals(c['bytes'], 9)
        self.assert_(not found1)
        self.assert_(found2)

        self.pids[self.port2server[anodes[0]['port']]] = \
            Popen(['swift-account-server',
                   '/etc/swift/account-server/%d.conf' %
                    ((anodes[0]['port'] - 6002) / 10)]).pid
        sleep(2)
        # This is the earlier counts and bytes because the first node doesn't
        # have the newest udpates yet.
        headers, containers = \
            direct_client.direct_get_account(anodes[0], apart, self.account)
        self.assertEquals(headers['x-account-container-count'], '2')
        self.assertEquals(headers['x-account-object-count'], '1')
        self.assertEquals(headers['x-account-bytes-used'], '4')
        found1 = False
        found2 = False
        for c in containers:
            if c['name'] == container1:
                found1 = True
            elif c['name'] == container2:
                found2 = True
                # This is the earlier count and bytes because the first node
                # doesn't have the newest udpates yet.
                self.assertEquals(c['count'], 1)
                self.assertEquals(c['bytes'], 4)
        # This okay because the first node hasn't got the update that
        # container1 was deleted yet.
        self.assert_(found1)
        self.assert_(found2)

        get_to_final_state()
        headers, containers = \
            direct_client.direct_get_account(anodes[0], apart, self.account)
        self.assertEquals(headers['x-account-container-count'], '1')
        self.assertEquals(headers['x-account-object-count'], '2')
        self.assertEquals(headers['x-account-bytes-used'], '9')
        found1 = False
        found2 = False
        for c in containers:
            if c['name'] == container1:
                found1 = True
            elif c['name'] == container2:
                found2 = True
                self.assertEquals(c['count'], 2)
                self.assertEquals(c['bytes'], 9)
        self.assert_(not found1)
        self.assert_(found2)
    def test_main(self):
        # Create container1 and container2
        container1 = 'container1'
        client.put_container(self.url, self.token, container1)
        container2 = 'container2'
        client.put_container(self.url, self.token, container2)

        # Assert account level sees them
        headers, containers = client.get_account(self.url, self.token)
        self.assertEqual(headers['x-account-container-count'], '2')
        self.assertEqual(headers['x-account-object-count'], '0')
        self.assertEqual(headers['x-account-bytes-used'], '0')
        found1 = False
        found2 = False
        for container in containers:
            if container['name'] == container1:
                found1 = True
                self.assertEqual(container['count'], 0)
                self.assertEqual(container['bytes'], 0)
            elif container['name'] == container2:
                found2 = True
                self.assertEqual(container['count'], 0)
                self.assertEqual(container['bytes'], 0)
        self.assertTrue(found1)
        self.assertTrue(found2)

        # Create container2/object1
        client.put_object(self.url, self.token, container2, 'object1', '1234')

        # Assert account level doesn't see it yet
        headers, containers = client.get_account(self.url, self.token)
        self.assertEqual(headers['x-account-container-count'], '2')
        self.assertEqual(headers['x-account-object-count'], '0')
        self.assertEqual(headers['x-account-bytes-used'], '0')
        found1 = False
        found2 = False
        for container in containers:
            if container['name'] == container1:
                found1 = True
                self.assertEqual(container['count'], 0)
                self.assertEqual(container['bytes'], 0)
            elif container['name'] == container2:
                found2 = True
                self.assertEqual(container['count'], 0)
                self.assertEqual(container['bytes'], 0)
        self.assertTrue(found1)
        self.assertTrue(found2)

        # Get to final state
        self.get_to_final_state()

        # Assert account level now sees the container2/object1
        headers, containers = client.get_account(self.url, self.token)
        self.assertEqual(headers['x-account-container-count'], '2')
        self.assertEqual(headers['x-account-object-count'], '1')
        self.assertEqual(headers['x-account-bytes-used'], '4')
        found1 = False
        found2 = False
        for container in containers:
            if container['name'] == container1:
                found1 = True
                self.assertEqual(container['count'], 0)
                self.assertEqual(container['bytes'], 0)
            elif container['name'] == container2:
                found2 = True
                self.assertEqual(container['count'], 1)
                self.assertEqual(container['bytes'], 4)
        self.assertTrue(found1)
        self.assertTrue(found2)

        apart, anodes = self.account_ring.get_nodes(self.account)
        kill_nonprimary_server(anodes, self.ipport2server)
        kill_server((anodes[0]['ip'], anodes[0]['port']), self.ipport2server)
        # Kill account servers excepting two of the primaries

        # Delete container1
        client.delete_container(self.url, self.token, container1)

        # Put container2/object2
        client.put_object(self.url, self.token, container2, 'object2', '12345')

        # Assert account level knows container1 is gone but doesn't know about
        #   container2/object2 yet
        headers, containers = client.get_account(self.url, self.token)
        self.assertEqual(headers['x-account-container-count'], '1')
        self.assertEqual(headers['x-account-object-count'], '1')
        self.assertEqual(headers['x-account-bytes-used'], '4')
        found1 = False
        found2 = False
        for container in containers:
            if container['name'] == container1:
                found1 = True
            elif container['name'] == container2:
                found2 = True
                self.assertEqual(container['count'], 1)
                self.assertEqual(container['bytes'], 4)
        self.assertFalse(found1)
        self.assertTrue(found2)

        # Run container updaters
        Manager(['container-updater']).once()

        # Assert account level now knows about container2/object2
        headers, containers = client.get_account(self.url, self.token)
        self.assertEqual(headers['x-account-container-count'], '1')
        self.assertEqual(headers['x-account-object-count'], '2')
        self.assertEqual(headers['x-account-bytes-used'], '9')
        found1 = False
        found2 = False
        for container in containers:
            if container['name'] == container1:
                found1 = True
            elif container['name'] == container2:
                found2 = True
                self.assertEqual(container['count'], 2)
                self.assertEqual(container['bytes'], 9)
        self.assertFalse(found1)
        self.assertTrue(found2)

        # Restart other primary account server
        start_server((anodes[0]['ip'], anodes[0]['port']), self.ipport2server)

        # Assert that server doesn't know about container1's deletion or the
        #   new container2/object2 yet
        headers, containers = \
            direct_client.direct_get_account(anodes[0], apart, self.account)
        self.assertEqual(headers['x-account-container-count'], '2')
        self.assertEqual(headers['x-account-object-count'], '1')
        self.assertEqual(headers['x-account-bytes-used'], '4')
        found1 = False
        found2 = False
        for container in containers:
            if container['name'] == container1:
                found1 = True
            elif container['name'] == container2:
                found2 = True
                self.assertEqual(container['count'], 1)
                self.assertEqual(container['bytes'], 4)
        self.assertTrue(found1)
        self.assertTrue(found2)

        # Get to final state
        self.get_to_final_state()

        # Assert that server is now up to date
        headers, containers = \
            direct_client.direct_get_account(anodes[0], apart, self.account)
        self.assertEqual(headers['x-account-container-count'], '1')
        self.assertEqual(headers['x-account-object-count'], '2')
        self.assertEqual(headers['x-account-bytes-used'], '9')
        found1 = False
        found2 = False
        for container in containers:
            if container['name'] == container1:
                found1 = True
            elif container['name'] == container2:
                found2 = True
                self.assertEqual(container['count'], 2)
                self.assertEqual(container['bytes'], 9)
                self.assertEqual(container['bytes'], 9)
        self.assertFalse(found1)
        self.assertTrue(found2)
Ejemplo n.º 44
0
 def delete_container(self, container):
     return swift_client.delete_container(self.storage_url, self.token,
                                          container)
Ejemplo n.º 45
0
def projects(request, project_id=None):
    """
    GET: List all projects ordered by name
    PUT: Save a project (enable)
    DELETE: Delete a project (disable)
    POST: Check if a project exist or is enabled
    """
    try:
        r = get_redis_connection()
    except RedisError:
        return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR)

    if request.method == 'GET':
        enabled_projects = r.lrange('projects_crystal_enabled', 0, -1)
        return JSONResponse(enabled_projects, status=status.HTTP_200_OK)

    if request.method == 'PUT':
        project_list = get_project_list()
        project_name = project_list[project_id]
        if project_name == settings.MANAGEMENT_ACCOUNT:
            return JSONResponse("Management project could not be set as Crystal project",
                                status=status.HTTP_400_BAD_REQUEST)

        try:
            # Set Manager as admin of the Crystal Project
            keystone_client = get_keystone_admin_auth()
            admin_role_id, reseller_admin_role_id, admin_user_id = get_admin_role_user_ids(keystone_client)
            keystone_client.roles.grant(role=admin_role_id, user=admin_user_id, project=project_id)
            keystone_client.roles.grant(role=reseller_admin_role_id, user=admin_user_id, project=project_id)

            # Post Storlet and Dependency containers
            url, token = get_swift_url_and_token(project_name)
            swift_client.put_container(url, token, ".storlet")
            swift_client.put_container(url, token, ".dependency")
            headers = {'X-Account-Meta-Crystal-Enabled': True, 'X-Account-Meta-Storlet-Enabled': True}
            swift_client.post_account(url, token, headers)

            # Create project docker image
            create_docker_image(r, project_id)
            r.lpush('projects_crystal_enabled', project_id)
            return JSONResponse("Crystal Project correctly enabled", status=status.HTTP_201_CREATED)
        except:
            return JSONResponse("Error Enabling Crystal Project", status=status.HTTP_400_BAD_REQUEST)

    if request.method == 'DELETE':
        try:
            project_list = get_project_list()
            project_name = project_list[project_id]

            # Delete Storlet and Dependency containers
            try:
                url, token = get_swift_url_and_token(project_name)
                headers = {'X-Account-Meta-Crystal-Enabled': '', 'X-Account-Meta-Storlet-Enabled': ''}
                swift_client.post_account(url, token, headers)
                swift_client.delete_container(url, token, ".storlet")
                swift_client.delete_container(url, token, ".dependency")
            except:
                pass

            # Delete Manager as admin of the Crystal Project
            keystone_client = get_keystone_admin_auth()
            admin_role_id, reseller_admin_role_id, admin_user_id = get_admin_role_user_ids(keystone_client)
            try:
                keystone_client.roles.revoke(role=admin_role_id, user=admin_user_id, project=project_id)
                keystone_client.roles.revoke(role=reseller_admin_role_id, user=admin_user_id, project=project_id)
            except:
                pass

            # Delete project docker image
            delete_docker_image(r, project_id)

            r.lrem('projects_crystal_enabled', project_id)
            return JSONResponse("Crystal project correctly disabled.", status=status.HTTP_201_CREATED)
        except RedisError:
            return JSONResponse("Error inserting data", status=status.HTTP_400_BAD_REQUEST)

    if request.method == 'POST':
        enabled_projects = r.lrange('projects_crystal_enabled', 0, -1)
        if project_id in enabled_projects:
            return JSONResponse(project_id, status=status.HTTP_200_OK)
        return JSONResponse('The project with id:  ' + str(project_id) + ' does not exist.',
                            status=status.HTTP_404_NOT_FOUND)

    return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
Ejemplo n.º 46
0
    def test_main(self):
        # Create container1 and container2
        # Assert account level sees them
        # Create container2/object1
        # Assert account level doesn't see it yet
        # Get to final state
        # Assert account level now sees the container2/object1
        # Kill account servers excepting two of the primaries
        # Delete container1
        # Assert account level knows container1 is gone but doesn't know about
        #   container2/object2 yet
        # Put container2/object2
        # Run container updaters
        # Assert account level now knows about container2/object2
        # Restart other primary account server
        # Assert that server doesn't know about container1's deletion or the
        #   new container2/object2 yet
        # Get to final state
        # Assert that server is now up to date

        container1 = 'container1'
        client.put_container(self.url, self.token, container1)
        container2 = 'container2'
        client.put_container(self.url, self.token, container2)
        headers, containers = client.get_account(self.url, self.token)
        self.assertEquals(headers['x-account-container-count'], '2')
        self.assertEquals(headers['x-account-object-count'], '0')
        self.assertEquals(headers['x-account-bytes-used'], '0')
        found1 = False
        found2 = False
        for container in containers:
            if container['name'] == container1:
                found1 = True
                self.assertEquals(container['count'], 0)
                self.assertEquals(container['bytes'], 0)
            elif container['name'] == container2:
                found2 = True
                self.assertEquals(container['count'], 0)
                self.assertEquals(container['bytes'], 0)
        self.assert_(found1)
        self.assert_(found2)

        client.put_object(self.url, self.token, container2, 'object1', '1234')
        headers, containers = client.get_account(self.url, self.token)
        self.assertEquals(headers['x-account-container-count'], '2')
        self.assertEquals(headers['x-account-object-count'], '0')
        self.assertEquals(headers['x-account-bytes-used'], '0')
        found1 = False
        found2 = False
        for container in containers:
            if container['name'] == container1:
                found1 = True
                self.assertEquals(container['count'], 0)
                self.assertEquals(container['bytes'], 0)
            elif container['name'] == container2:
                found2 = True
                self.assertEquals(container['count'], 0)
                self.assertEquals(container['bytes'], 0)
        self.assert_(found1)
        self.assert_(found2)

        get_to_final_state()
        headers, containers = client.get_account(self.url, self.token)
        self.assertEquals(headers['x-account-container-count'], '2')
        self.assertEquals(headers['x-account-object-count'], '1')
        self.assertEquals(headers['x-account-bytes-used'], '4')
        found1 = False
        found2 = False
        for container in containers:
            if container['name'] == container1:
                found1 = True
                self.assertEquals(container['count'], 0)
                self.assertEquals(container['bytes'], 0)
            elif container['name'] == container2:
                found2 = True
                self.assertEquals(container['count'], 1)
                self.assertEquals(container['bytes'], 4)
        self.assert_(found1)
        self.assert_(found2)

        apart, anodes = self.account_ring.get_nodes(self.account)
        kill_nonprimary_server(anodes, self.port2server, self.pids)
        kill_server(anodes[0]['port'], self.port2server, self.pids)

        client.delete_container(self.url, self.token, container1)
        client.put_object(self.url, self.token, container2, 'object2', '12345')
        headers, containers = client.get_account(self.url, self.token)
        self.assertEquals(headers['x-account-container-count'], '1')
        self.assertEquals(headers['x-account-object-count'], '1')
        self.assertEquals(headers['x-account-bytes-used'], '4')
        found1 = False
        found2 = False
        for container in containers:
            if container['name'] == container1:
                found1 = True
            elif container['name'] == container2:
                found2 = True
                self.assertEquals(container['count'], 1)
                self.assertEquals(container['bytes'], 4)
        self.assert_(not found1)
        self.assert_(found2)

        processes = []
        for node in xrange(1, 5):
            processes.append(
                Popen([
                    'swift-container-updater',
                    '/etc/swift/container-server/%d.conf' % node, 'once'
                ]))
        for process in processes:
            process.wait()
        headers, containers = client.get_account(self.url, self.token)
        self.assertEquals(headers['x-account-container-count'], '1')
        self.assertEquals(headers['x-account-object-count'], '2')
        self.assertEquals(headers['x-account-bytes-used'], '9')
        found1 = False
        found2 = False
        for container in containers:
            if container['name'] == container1:
                found1 = True
            elif container['name'] == container2:
                found2 = True
                self.assertEquals(container['count'], 2)
                self.assertEquals(container['bytes'], 9)
        self.assert_(not found1)
        self.assert_(found2)

        start_server(anodes[0]['port'], self.port2server, self.pids)

        headers, containers = \
            direct_client.direct_get_account(anodes[0], apart, self.account)
        self.assertEquals(headers['x-account-container-count'], '2')
        self.assertEquals(headers['x-account-object-count'], '1')
        self.assertEquals(headers['x-account-bytes-used'], '4')
        found1 = False
        found2 = False
        for container in containers:
            if container['name'] == container1:
                found1 = True
            elif container['name'] == container2:
                found2 = True
                self.assertEquals(container['count'], 1)
                self.assertEquals(container['bytes'], 4)
        self.assert_(found1)
        self.assert_(found2)

        get_to_final_state()
        headers, containers = \
            direct_client.direct_get_account(anodes[0], apart, self.account)
        self.assertEquals(headers['x-account-container-count'], '1')
        self.assertEquals(headers['x-account-object-count'], '2')
        self.assertEquals(headers['x-account-bytes-used'], '9')
        found1 = False
        found2 = False
        for container in containers:
            if container['name'] == container1:
                found1 = True
            elif container['name'] == container2:
                found2 = True
                self.assertEquals(container['count'], 2)
                self.assertEquals(container['bytes'], 9)
        self.assert_(not found1)
        self.assert_(found2)
Ejemplo n.º 47
0
    def test_main(self):
        # Create container1 and container2
        container1 = 'container1'
        client.put_container(self.url, self.token, container1)
        container2 = 'container2'
        client.put_container(self.url, self.token, container2)

        # Assert account level sees them
        headers, containers = client.get_account(self.url, self.token)
        self.assertEquals(headers['x-account-container-count'], '2')
        self.assertEquals(headers['x-account-object-count'], '0')
        self.assertEquals(headers['x-account-bytes-used'], '0')
        found1 = False
        found2 = False
        for container in containers:
            if container['name'] == container1:
                found1 = True
                self.assertEquals(container['count'], 0)
                self.assertEquals(container['bytes'], 0)
            elif container['name'] == container2:
                found2 = True
                self.assertEquals(container['count'], 0)
                self.assertEquals(container['bytes'], 0)
        self.assertTrue(found1)
        self.assertTrue(found2)

        # Create container2/object1
        client.put_object(self.url, self.token, container2, 'object1', '1234')

        # Assert account level doesn't see it yet
        headers, containers = client.get_account(self.url, self.token)
        self.assertEquals(headers['x-account-container-count'], '2')
        self.assertEquals(headers['x-account-object-count'], '0')
        self.assertEquals(headers['x-account-bytes-used'], '0')
        found1 = False
        found2 = False
        for container in containers:
            if container['name'] == container1:
                found1 = True
                self.assertEquals(container['count'], 0)
                self.assertEquals(container['bytes'], 0)
            elif container['name'] == container2:
                found2 = True
                self.assertEquals(container['count'], 0)
                self.assertEquals(container['bytes'], 0)
        self.assertTrue(found1)
        self.assertTrue(found2)

        # Get to final state
        self.get_to_final_state()

        # Assert account level now sees the container2/object1
        headers, containers = client.get_account(self.url, self.token)
        self.assertEquals(headers['x-account-container-count'], '2')
        self.assertEquals(headers['x-account-object-count'], '1')
        self.assertEquals(headers['x-account-bytes-used'], '4')
        found1 = False
        found2 = False
        for container in containers:
            if container['name'] == container1:
                found1 = True
                self.assertEquals(container['count'], 0)
                self.assertEquals(container['bytes'], 0)
            elif container['name'] == container2:
                found2 = True
                self.assertEquals(container['count'], 1)
                self.assertEquals(container['bytes'], 4)
        self.assertTrue(found1)
        self.assertTrue(found2)

        apart, anodes = self.account_ring.get_nodes(self.account)
        kill_nonprimary_server(anodes, self.ipport2server, self.pids)
        kill_server((anodes[0]['ip'], anodes[0]['port']),
                    self.ipport2server, self.pids)
        # Kill account servers excepting two of the primaries

        # Delete container1
        client.delete_container(self.url, self.token, container1)

        # Put container2/object2
        client.put_object(self.url, self.token, container2, 'object2', '12345')

        # Assert account level knows container1 is gone but doesn't know about
        #   container2/object2 yet
        headers, containers = client.get_account(self.url, self.token)
        self.assertEquals(headers['x-account-container-count'], '1')
        self.assertEquals(headers['x-account-object-count'], '1')
        self.assertEquals(headers['x-account-bytes-used'], '4')
        found1 = False
        found2 = False
        for container in containers:
            if container['name'] == container1:
                found1 = True
            elif container['name'] == container2:
                found2 = True
                self.assertEquals(container['count'], 1)
                self.assertEquals(container['bytes'], 4)
        self.assertTrue(not found1)
        self.assertTrue(found2)

        # Run container updaters
        Manager(['container-updater']).once()

        # Assert account level now knows about container2/object2
        headers, containers = client.get_account(self.url, self.token)
        self.assertEquals(headers['x-account-container-count'], '1')
        self.assertEquals(headers['x-account-object-count'], '2')
        self.assertEquals(headers['x-account-bytes-used'], '9')
        found1 = False
        found2 = False
        for container in containers:
            if container['name'] == container1:
                found1 = True
            elif container['name'] == container2:
                found2 = True
                self.assertEquals(container['count'], 2)
                self.assertEquals(container['bytes'], 9)
        self.assertTrue(not found1)
        self.assertTrue(found2)

        # Restart other primary account server
        start_server((anodes[0]['ip'], anodes[0]['port']),
                     self.ipport2server, self.pids)

        # Assert that server doesn't know about container1's deletion or the
        #   new container2/object2 yet
        headers, containers = \
            direct_client.direct_get_account(anodes[0], apart, self.account)
        self.assertEquals(headers['x-account-container-count'], '2')
        self.assertEquals(headers['x-account-object-count'], '1')
        self.assertEquals(headers['x-account-bytes-used'], '4')
        found1 = False
        found2 = False
        for container in containers:
            if container['name'] == container1:
                found1 = True
            elif container['name'] == container2:
                found2 = True
                self.assertEquals(container['count'], 1)
                self.assertEquals(container['bytes'], 4)
        self.assertTrue(found1)
        self.assertTrue(found2)

        # Get to final state
        self.get_to_final_state()

        # Assert that server is now up to date
        headers, containers = \
            direct_client.direct_get_account(anodes[0], apart, self.account)
        self.assertEquals(headers['x-account-container-count'], '1')
        self.assertEquals(headers['x-account-object-count'], '2')
        self.assertEquals(headers['x-account-bytes-used'], '9')
        found1 = False
        found2 = False
        for container in containers:
            if container['name'] == container1:
                found1 = True
            elif container['name'] == container2:
                found2 = True
                self.assertEquals(container['count'], 2)
                self.assertEquals(container['bytes'], 9)
        self.assertTrue(not found1)
        self.assertTrue(found2)
Ejemplo n.º 48
0
 def test_ok(self):
     c.http_connection = self.fake_http_connection(200)
     value = c.delete_container('http://www.test.com', 'asdf', 'asdf')
     self.assertEqual(value, None)
Ejemplo n.º 49
0
 def test_ok(self):
     c.http_connection = self.fake_http_connection(200)
     value = c.delete_container('http://www.test.com', 'asdf', 'asdf')
     self.assertEquals(value, None)
Ejemplo n.º 50
0
 def delete_container(self, account_id, token, s_type, container):
     cnx = self.get_cnx(account_id, s_type)
     sclient.delete_container("", token, container, http_conn=cnx)
Ejemplo n.º 51
0
 def delete_container(self, container_name):
     return client.delete_container(self.url, self.token, container_name)
Ejemplo n.º 52
0
 def delete_container(self, account_id, token, s_type, container):
     cnx = self.get_cnx(account_id, s_type)
     sclient.delete_container("", token, container, http_conn=cnx)
Ejemplo n.º 53
0
 def tearDown(self):
     swift_client.delete_container(self.url,
                                   self.token,
                                   self.container)
 def test_ok(self):
     c.http_connection = self.fake_http_connection(200)
     value = c.delete_container("http://www.test.com", "asdf", "asdf")
     self.assertEquals(value, None)
Ejemplo n.º 55
0
            client.put_container(self.url, self.token, container)
            db_files = self._get_container_db_files(container)
            db_conns = []
            for i in range(num_locks):
                db_conn = connect(db_files[i])
                db_conn.execute('begin exclusive transaction')
                db_conns.append(db_conn)
            if catch_503:
                exc = None
                try:
                    client.delete_container(self.url, self.token, container)
                except client.ClientException, err:
                    exc = err
                self.assertEquals(exc.http_status, 503)
            else:
                client.delete_container(self.url, self.token, container)

        pool = GreenPool()
        try:
            with Timeout(15):
                pool.spawn(run_test, 1, False)
                pool.spawn(run_test, 2, True)
                pool.spawn(run_test, 3, True)
                pool.waitall()
        except Timeout, err:
            raise Exception(
                "The server did not return a 503 on container db locks, "
                "it just hangs: %s" % err)


if __name__ == '__main__':
Ejemplo n.º 56
0
 def delete(self, container):
     """Container must be empty for deletion to succeed."""
     super(SwiftContainers, self).delete(container)
     swift_client.delete_container(self.endpoint, self.token, container)
Ejemplo n.º 57
0
def projects(request, project_id=None):
    """
    GET: List all projects ordered by name
    PUT: Save a project (enable)
    DELETE: Delete a project (disable)
    POST: Check if a project exist or is enabled
    """
    try:
        r = get_redis_connection()
    except RedisError:
        return JSONResponse('Error connecting with DB',
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)

    if request.method == 'GET':
        enabled_projects = r.lrange('projects_crystal_enabled', 0, -1)
        return JSONResponse(enabled_projects, status=status.HTTP_200_OK)

    if request.method == 'PUT':
        project_list = get_project_list()
        project_name = project_list[project_id]
        if project_name == settings.MANAGEMENT_ACCOUNT:
            return JSONResponse(
                "Management project could not be set as Crystal project",
                status=status.HTTP_400_BAD_REQUEST)

        try:
            # Set Manager as admin of the Crystal Project
            keystone_client = get_keystone_admin_auth()
            admin_role_id, reseller_admin_role_id, admin_user_id = get_admin_role_user_ids(
                keystone_client)
            keystone_client.roles.grant(role=admin_role_id,
                                        user=admin_user_id,
                                        project=project_id)
            keystone_client.roles.grant(role=reseller_admin_role_id,
                                        user=admin_user_id,
                                        project=project_id)

            # Post Storlet and Dependency containers
            url, token = get_swift_url_and_token(project_name)
            swift_client.put_container(url, token, ".storlet")
            swift_client.put_container(url, token, ".dependency")
            headers = {
                'X-Account-Meta-Crystal-Enabled': True,
                'X-Account-Meta-Storlet-Enabled': True
            }
            swift_client.post_account(url, token, headers)

            # Create project docker image
            create_docker_image(r, project_id)
            r.lpush('projects_crystal_enabled', project_id)
            return JSONResponse("Crystal Project correctly enabled",
                                status=status.HTTP_201_CREATED)
        except:
            return JSONResponse("Error Enabling Crystal Project",
                                status=status.HTTP_400_BAD_REQUEST)

    if request.method == 'DELETE':
        try:
            project_list = get_project_list()
            project_name = project_list[project_id]

            # Delete Storlet and Dependency containers
            try:
                url, token = get_swift_url_and_token(project_name)
                headers = {
                    'X-Account-Meta-Crystal-Enabled': '',
                    'X-Account-Meta-Storlet-Enabled': ''
                }
                swift_client.post_account(url, token, headers)
                swift_client.delete_container(url, token, ".storlet")
                swift_client.delete_container(url, token, ".dependency")
            except:
                pass

            # Delete Manager as admin of the Crystal Project
            keystone_client = get_keystone_admin_auth()
            admin_role_id, reseller_admin_role_id, admin_user_id = get_admin_role_user_ids(
                keystone_client)
            try:
                keystone_client.roles.revoke(role=admin_role_id,
                                             user=admin_user_id,
                                             project=project_id)
                keystone_client.roles.revoke(role=reseller_admin_role_id,
                                             user=admin_user_id,
                                             project=project_id)
            except:
                pass

            # Delete project docker image
            delete_docker_image(r, project_id)

            r.lrem('projects_crystal_enabled', project_id)
            return JSONResponse("Crystal project correctly disabled.",
                                status=status.HTTP_201_CREATED)
        except RedisError:
            return JSONResponse("Error inserting data",
                                status=status.HTTP_400_BAD_REQUEST)

    if request.method == 'POST':
        enabled_projects = r.lrange('projects_crystal_enabled', 0, -1)
        if project_id in enabled_projects:
            return JSONResponse(project_id, status=status.HTTP_200_OK)
        return JSONResponse('The project with id:  ' + str(project_id) +
                            ' does not exist.',
                            status=status.HTTP_404_NOT_FOUND)

    return JSONResponse('Method ' + str(request.method) + ' not allowed.',
                        status=status.HTTP_405_METHOD_NOT_ALLOWED)
Ejemplo n.º 58
0
 def delete_container(self, container, *args):
     return swift_client.delete_container(self.storage_url, self.token, container)
Ejemplo n.º 59
0
 def delete_container(self):
     """
     delete container
     """
     client.delete_container(self.url, self.token, self.container_name)
Ejemplo n.º 60
0
 def delete_container(self):
     """
     delete container
     """
     client.delete_container(self.url, self.token, self.container_name)