Delete objects on dest if not on orig.
This commit is contained in:
parent
ac48c55687
commit
802cb45bf3
@ -57,11 +57,22 @@ class Accounts(object):
|
||||
orig_storage_cnx = swiftclient.http_connection(orig_storage_url)
|
||||
dest_storage_cnx = swiftclient.http_connection(dest_storage_url)
|
||||
|
||||
_, orig_containers = (
|
||||
orig_stats, orig_containers = (
|
||||
swiftclient.get_account(None, orig_token,
|
||||
http_conn=orig_storage_cnx,
|
||||
full_listing=True))
|
||||
|
||||
dest_stats, dest_containers = (
|
||||
swiftclient.get_account(None, dest_token,
|
||||
http_conn=dest_storage_cnx,
|
||||
full_listing=True))
|
||||
if int(dest_stats['x-account-container-count']) > \
|
||||
int(orig_stats['x-account-container-count']):
|
||||
self.container_cls.delete_container(dest_storage_cnx,
|
||||
dest_token,
|
||||
orig_containers,
|
||||
dest_containers)
|
||||
|
||||
for container in orig_containers:
|
||||
logging.info("Syncronizing %s: %s", container['name'], container)
|
||||
dt1 = datetime.datetime.fromtimestamp(time.time())
|
||||
|
@ -24,12 +24,39 @@ from swsync.utils import get_config
|
||||
|
||||
|
||||
class Containers(object):
|
||||
"""Containers syncornization."""
|
||||
"""Containers sync."""
|
||||
def __init__(self):
|
||||
self.max_gthreads = int(get_config("sync", "max_gthreads"))
|
||||
self.sync_object = sync_object
|
||||
self.delete_object = delete_object
|
||||
|
||||
def delete_container(self, dest_storage_cnx, dest_token,
|
||||
orig_containers,
|
||||
dest_containers):
|
||||
set1 = set((x['name']) for x in orig_containers)
|
||||
set2 = set((x['name']) for x in dest_containers)
|
||||
delete_diff = set2 - set1
|
||||
|
||||
pool = eventlet.GreenPool(size=self.max_gthreads)
|
||||
pile = eventlet.GreenPile(pool)
|
||||
for container in delete_diff:
|
||||
dest_container_stats, dest_objects = swiftclient.get_container(
|
||||
None, dest_token, container, http_conn=dest_storage_cnx,
|
||||
)
|
||||
for obj in dest_objects:
|
||||
logging.info("deleting obj: %s ts:%s", obj['name'],
|
||||
obj['last_modified'])
|
||||
pile.spawn(self.delete_object,
|
||||
dest_storage_cnx,
|
||||
dest_token,
|
||||
container,
|
||||
obj['name'])
|
||||
pool.waitall()
|
||||
logging.info("deleting container: %s", container)
|
||||
pile.spawn(swiftclient.delete_container,
|
||||
'', dest_token, container, http_conn=dest_storage_cnx)
|
||||
pool.waitall()
|
||||
|
||||
def sync(self, orig_storage_cnx, orig_storage_url,
|
||||
orig_token, dest_storage_cnx, dest_storage_url, dest_token,
|
||||
container_name):
|
||||
@ -79,7 +106,8 @@ class Containers(object):
|
||||
for obj in delete_diff:
|
||||
logging.info("deleting: %s ts:%s", obj[1], obj[0])
|
||||
pile.spawn(self.delete_object,
|
||||
dest_storage_url,
|
||||
dest_token, container_name,
|
||||
obj)
|
||||
dest_storage_cnx,
|
||||
dest_token,
|
||||
container_name,
|
||||
obj[1])
|
||||
pool.waitall()
|
||||
|
@ -77,13 +77,17 @@ def get_object(storage_url, token,
|
||||
return (resp_headers, object_body)
|
||||
|
||||
|
||||
def delete_object(dest_storage_url, dest_token,
|
||||
container_name, object_name_etag):
|
||||
|
||||
delete_object = "%s/%s/%s" % (dest_storage_url,
|
||||
container_name, object_name_etag[1])
|
||||
delete_headers = {'x-auth-token': dest_token}
|
||||
swiftclient.delete_object(delete_object, headers=delete_headers)
|
||||
def delete_object(dest_cnx,
|
||||
dest_token,
|
||||
container_name,
|
||||
object_name):
|
||||
parsed = dest_cnx[0]
|
||||
url = '%s://%s/%s' % (parsed.scheme, parsed.netloc, parsed.path)
|
||||
swiftclient.delete_object(url=url,
|
||||
token=dest_token,
|
||||
container=container_name,
|
||||
http_conn=dest_cnx,
|
||||
name=object_name)
|
||||
|
||||
|
||||
def sync_object(orig_storage_url, orig_token, dest_storage_url,
|
||||
|
@ -106,11 +106,6 @@ class FakeSWClient(object):
|
||||
def http_connection(url):
|
||||
return (urlparse.urlparse(url), None)
|
||||
|
||||
@staticmethod
|
||||
def get_account(*args, **kwargs):
|
||||
return (('x-foo', 'x-bar'),
|
||||
[x[0] for x in CONTAINERS_LIST])
|
||||
|
||||
|
||||
def fake_get_auth(auth_url, tenant, user, password):
|
||||
return FakeSWConnection(
|
||||
|
@ -30,11 +30,15 @@ class TestAccount(test_base.TestCase):
|
||||
self.accounts_cls = swsync.accounts.Accounts()
|
||||
self._stubs()
|
||||
|
||||
def get_account(self, *args, **kwargs):
|
||||
return ({'x-account-container-count': len(CONTAINERS_LIST)},
|
||||
[x[0] for x in CONTAINERS_LIST])
|
||||
|
||||
def _stubs(self):
|
||||
self.stubs.Set(keystoneclient.v2_0, 'client', FakeKS)
|
||||
self.stubs.Set(swiftclient.client, 'Connection', FakeSWConnection)
|
||||
self.stubs.Set(swsync.accounts, 'get_config', fake_get_config)
|
||||
self.stubs.Set(swiftclient, 'get_account', FakeSWClient.get_account)
|
||||
self.stubs.Set(swiftclient, 'get_account', self.get_account)
|
||||
self.stubs.Set(swiftclient, 'http_connection',
|
||||
FakeSWClient.http_connection)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user