Skip to content

Instantly share code, notes, and snippets.

@obiyann
Created April 18, 2018 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save obiyann/7d3b04cee75259b4dfe0fe4ef9ecd26b to your computer and use it in GitHub Desktop.
Save obiyann/7d3b04cee75259b4dfe0fe4ef9ecd26b to your computer and use it in GitHub Desktop.
#!/usr/local/bin/python
# SquareBox CatDV Scality HTTP REST (Sproxyd) plugin
# Yannick Guillerm - Sales Engineer Scality
# yannick.guillerm@scality.com
# V1 - 04/09/14
#
# Description: use this plugin to archive assets from CatDV tier 1 storage to the Scality RING
# via the Scality RING Sproxyd (HTTP REST) connector. Two opcodes:
# - "upload" - HTTP PUT CatDV asset to the Scality RING via the S3 connector. If upload
# is successful, the Hi-Res asset is deleted from the CatDV tier 1 storage and asset
# metadata fields are upadated with S3 URL, RING objectID, status, archive date. Hi-Res
# asset in CatDV appears OFFLINE (specific offline icon), but proxy (low-res) is still
# available for preview
# - "download" - HTTP GET CatDV asset from the Scality RING via the S3 connector. If download
# is successful, the Hi-Res asset is restored to its original location on the CatDV tier 1
# storage and asset metadata fields are upadated with S3 URL, RING objectID, status, archive
# date. Hi-Res asset in CatDV appears ONLINE. Hi-Res asset accessible.
import requests
import sys, getopt
import argparse
import os
def upload_file(filepath, catalog, sproxyd_ip, sproxyd_driver):
# Upload a file to the Ring and update CatDV asset fields.
print "Uploading"
with open(filepath) as fh:
mydata = fh.read()
url = 'http://' + sproxyd_ip + ':81/proxy/' + sproxyd_driver + '/Archive' + filepath
response = requests.put(url,
data=mydata,
params={'file': filepath}
)
print "Uploading response code"
print response.status_code
if response.status_code == 200:
# Update object CatDV fields
Command = '/Applications/CatDV\ Worker\ 6.0.1/catdv -catalog ' + catalog +' -mediafile ' + filepath + ' -set ' + '\'User 1\'=Archived in Ring'
os.system(Command)
Command = '/Applications/CatDV\ Worker\ 6.0.1/catdv -catalog ' + catalog +' -mediafile ' + filepath + ' -set ' + '\'User 3\'=OFFLINE'
os.system(Command)
Command = '/Applications/CatDV\ Worker\ 6.0.1/catdv -catalog ' + catalog +' -mediafile ' + filepath + ' -set ' + '\'User 2\'=' + url
os.system(Command)
objectID = response.headers['x-scal-ring-key']
Command = '/Applications/CatDV\ Worker\ 6.0.1//catdv -catalog ' + catalog +' -mediafile ' + filepath + ' -set ' + '\'User 6\'=' + objectID
os.system(Command)
ArchiveDate = response.headers['date']
ArchiveDate = ArchiveDate.replace(",", "")
ArchiveDate = ArchiveDate.replace(" ", "\ ")
print ArchiveDate
Command = '/Applications/CatDV\ Worker\ 6.0.1//catdv -catalog ' + catalog +' -mediafile ' + filepath + ' -set ' + '\'User 7\'=' + ArchiveDate
os.system(Command)
# Delete file
os.remove(filepath)
def download_file(filepath, catalog, sproxyd_ip, sproxyd_driver):
# Download a file from the Ring and update CatDV asset fields.
url = 'http://' + sproxyd_ip + ':81/proxy/' + sproxyd_driver + '/Archive' + filepath
response = requests.get(url)
if response.status_code == 200:
with open(filepath, 'wb') as fh:
for chunk in response.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
fh.write(chunk)
print "Downloading response code"
print response.status_code
# Update object CatDV fields
Command = '/Applications/CatDV\ Worker\ 6.0.1/catdv -catalog ' + catalog +' -mediafile ' + filepath + ' -set ' + '\'User 1\'=Restored from Ring'
os.system(Command)
Command = '/Applications/CatDV\ Worker\ 6.0.1/catdv -catalog ' + catalog +' -mediafile ' + filepath + ' -set ' + '\'User 3\'=ONLINE'
os.system(Command)
def main(argv):
filepath = ""
op = ""
catalog = ""
sproxyd_ip = ""
sproxyd_driver = ""
# Parse arguments. File full path and op (upload/download)
parser = argparse.ArgumentParser()
parser.add_argument('--f', help='f help')
parser.add_argument('--o', help='o help')
parser.add_argument('--c', help='c help')
parser.add_argument('--ip', help='ip help')
parser.add_argument('--d', help='d help')
args = vars(parser.parse_args())
filepath = args['f']
op = args['o']
catalog = args['c']
sproxyd_ip = args['ip']
sproxyd_driver = args['d']
print sproxyd_ip
# Call proper operation according to op value
if op == "upload":
upload_file(filepath, catalog, sproxyd_ip, sproxyd_driver)
elif op == "download":
download_file(filepath, catalog, sproxyd_ip, sproxyd_driver)
else:
print "Incorrect op"
sys.exit(2)
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment