Ejemplo n.º 1
0
 def __real_check__(self, req):
     """
     The real health check for swift object store:
     It will simulate the client to download a file so that all the layers are checked
     """
     try:
         swift_client = Connection(self.auth_url, self.check_account, self.check_secrete, retries=2)
         (head, content) = swift_client.get_object(self.check_account, self.check_file)
         return Response(request=req, body= content, content_type="text/plain")
     except ClientException as ce:
         body = "Object not found : %s/%s\n" % (self.check_account, self.check_file)
         return Response(request=req, body = body, status = ce.http_status, content_type="text/plain")
     except Exception as e:
         return Response(request=req, body= e.msg, status = "506 Other Exception", content_type="text/plain")
# Parse any command line arguments. Currently just --debug flag
parser = argparse.ArgumentParser(description='Load files to Rackspace cloud or potentially other openstack "Swift" (storage) compatible cloud providers.')
parser.add_argument('-debug','--debug',action="store_true", help='true for noisy helpful execution, false or omitted for quiet.')
parser.add_argument('-container','--container', action="store", dest="container", help='Container name at Rackspace cloudfiles')
parser.add_argument('-files','--files', action="store", dest="file_pattern", help='File name or pattern, e.g. logo.jpg or *.jpg')
parser.add_argument('-content_type','--content_type', action="store", help='content-type which will be applied to all the files on the CDN, e.g. image/jpeg, text/html')
args = parser.parse_args()
debug_mode = (args.debug)

print 'Loading files which match pattern%s to container %s with content_type=%s' % (args.file_pattern, args.container, args.content_type)

import glob
    
    
# Open a connection to the rackspace Cloud with the user and ke.
conn = Connection('https://auth.api.rackspacecloud.com/v1.0',secrets.RACKSPACE_USER, secrets.RACKSPACE_KEY)
# Get some data about the account, including a list of storage containers.
acc = conn.get_account()
#first_container_name = acc[1][0]['name']
#pprint(acc)
# Redundant here, but get details of the container. 
#container_object = conn.get_container(first_container_name )
#pprint(container_object)
# Now create  new file within the first container and upload some text contents.

# version which uploads a piece of text.

#destination_name='openstacktest.txt' 
#contents = "blah blah blah"
#content_type = 'text/html'
# Code samples for blog post
# http://mikethetechie.com/post/6975966936/controlling-the-environment-cloud-control-apis
#
#Import the Openstack swift (1.3.0) module to access the Rackspace cloudfiles api.
# Download from https://launchpad.net/swift/
# Then use setup.py install.

import secrets # import user name RACKSPACE_USER and API key RACKSPACE_KEY
import swift.common.client
from pprint import pprint
from swift.common.client import  get_auth, get_account, get_container, put_object, Connection


# Open a connection to the rackspace Cloud with the user and ke.
conn = Connection('https://auth.api.rackspacecloud.com/v1.0',secrets.RACKSPACE_USER, secrets.RACKSPACE_KEY)
# Get some data about the account, including a list of storage containers.
acc = conn.get_account()
first_container_name = acc[1][0]['name']
pprint(acc)
# Redundant here, but get details of the container. 
container_object = conn.get_container(first_container_name )
pprint(container_object)
# Now create  new file within the first container and upload some text contents.
destination_name='openstacktest.txt' 
contents = "blah blah blah"
content_type = 'text/html'

# version to load a file
#contents = open('xd_city_sunrise.jpg','r')
#destination_name='xd_city_sunrise.jpg'