Skip to content

Instantly share code, notes, and snippets.

@gilv
Created July 31, 2014 06:52
Show Gist options
  • Save gilv/64d43be25d0b4cb8ef29 to your computer and use it in GitHub Desktop.
Save gilv/64d43be25d0b4cb8ef29 to your computer and use it in GitHub Desktop.
migration_from_file_system
'''
Created on Dec 16, 2013
@author: Gil Vernik
'''
from swiftclient import client as c
import os
import tempfile
import random
local_files = ['f1.txt', 'f2.txt']
ACCOUNT = 'test'
USER = 'tester'
PASSWORD = 'testing'
TMP_FOLDER = '/home/gilv/migr_result/'
def get_token():
storage_url, token = c.get_auth('http://127.0.0.1:8080/auth/v1.0', ACCOUNT + ':' + USER, PASSWORD)
print storage_url, token
return storage_url, token
def create_container(storage_url, token, container_name, headers = None):
response_dict = dict()
c.put_container(storage_url, token, container_name, headers, None, response_dict)
print response_dict
def create_federation_fsystem(storage_url, token, container_name):
headers = {'X-Container-Migration-Active':'True',
'X-Container-Migration-Provider':'fsystem',
'X-Container-Migration-Path':tempfile.gettempdir(),
'X-Container-Migration-Source':'migsource'}
response_dict = dict()
c.put_container(storage_url, token, container_name, headers, None, response_dict)
print response_dict
def generate_fs_objects():
newpath = os.path.join(tempfile.gettempdir(), 'migsource')
if not os.path.exists(newpath):
os.makedirs(newpath)
for fname in local_files:
f = open(os.path.join(newpath, fname), 'w+')
f.write('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabc1234')
f.close()
def object_migration(storage_url, token, container_name, object_name):
resp_dict = {}
try:
resp_headers, gf = c.get_object(storage_url, token, container_name, object_name,None,None,None,resp_dict, None )
fn = open(TMP_FOLDER + object_name, 'w')
fn.write(gf)
fn.close()
print resp_dict
except Exception as e:
print resp_dict
raise e
if __name__ == '__main__':
swift_container = 'cont{0}'.format(random.randint(10, 10000))
storage_url, token = get_token()
response_dict = {}
print '1. Create local Swift container: {0}'.format(swift_container)
c.put_container(storage_url, token, swift_container, None, None, response_dict)
generate_fs_objects()
print ' Container created. Response status {0}'.format(response_dict['status'])
print '2. Show that object {0} does not exists in {1}'.format(local_files[0], swift_container)
try:
print ' GET ' + storage_url + '/' + swift_container + '/' + local_files
resp_headers, gf = c.get_object(storage_url, token, swift_container, local_files[0])
print resp_headers
except Exception as e:
print ' {0}'.format(e)
try:
print '3. Going to setup data migration for {0} from File System'.format(swift_container)
create_federation_fsystem(storage_url, token, swift_container)
print '4. Going to read container metadata'
ret = c.head_container(storage_url, token, swift_container)
print ret
print '4. Access {0} to read {1} one more time. Swift will migrate an object'.format(swift_container, local_files[0])
print ' GET ' + storage_url + '/' + swift_container + '/' + local_files[0]
source_listing = c.get_container(storage_url, token, swift_container)
for lfile in local_files:
object_migration(storage_url, token, swift_container, lfile)
except Exception as e:
print ' {0}'.format(e)
print ' Migration failed'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment