BVB Source Codes

blueprint Show backend.py Source code

Return Download blueprint: download backend.py Source code - Download blueprint Source code - Type:.py
  1. import boto
  2. import boto.exception
  3. import httplib
  4. import socket
  5.  
  6. from blueprint import cfg
  7. import librato
  8. import statsd
  9.  
  10.  
  11. access_key = cfg.get('s3', 'access_key')
  12. bucket = cfg.get('s3', 'bucket')
  13. protocol = 'https' if cfg.getboolean('s3', 'use_https') else 'http'
  14. region = cfg.get('s3', 'region')
  15. s3_region = 's3' if 'US' == region else 's3-{0}'.format(region)
  16. secret_key = cfg.get('s3', 'secret_key')
  17.  
  18.  
  19. def delete(key):
  20.     """
  21.    Remove an object from S3.  DELETE requests are free but this function
  22.    still makes one billable request to account for freed storage.
  23.    """
  24.     content_length = head(key)
  25.     if content_length is None:
  26.         return None
  27.     librato.count('blueprint-io-server.requests.delete')
  28.     statsd.increment('blueprint-io-server.requests.delete')
  29.     c = boto.connect_s3(access_key, secret_key)
  30.     b = c.get_bucket(bucket, validate=False)
  31.     try:
  32.         b.delete_key(key)
  33.         # TODO librato.something('blueprint-io-server.storage', -content_length)
  34.         statsd.update('blueprint-io-server.storage', -content_length)
  35.     except (boto.exception.BotoClientError,
  36.             boto.exception.BotoServerError,
  37.             boto.exception.S3ResponseError,
  38.             httplib.HTTPException,
  39.             socket.error,
  40.             socket.gaierror):
  41.         return False
  42.  
  43.  
  44. def delete_blueprint(secret, name):
  45.     return delete(key_for_blueprint(secret, name))
  46.  
  47.  
  48. def delete_tarball(secret, name, sha):
  49.     return delete(key_for_tarball(secret, name, sha))
  50.  
  51.  
  52. def get(key):
  53.     """
  54.    Fetch an object from S3.  This function makes one billable request.
  55.    """
  56.     librato.count('blueprint-io-server.requests.get')
  57.     statsd.increment('blueprint-io-server.requests.get')
  58.     c = boto.connect_s3(access_key, secret_key)
  59.     b = c.get_bucket(bucket, validate=False)
  60.     k = b.new_key(key)
  61.     try:
  62.         return k.get_contents_as_string()
  63.     except boto.exception.S3ResponseError:
  64.         return None
  65.     except (boto.exception.BotoClientError,
  66.             boto.exception.BotoServerError,
  67.             httplib.HTTPException,
  68.             socket.error,
  69.             socket.gaierror):
  70.         return False
  71.  
  72.  
  73. def get_blueprint(secret, name):
  74.     return get(key_for_blueprint(secret, name))
  75.  
  76.  
  77. def get_tarball(secret, name, sha):
  78.     return get(key_for_tarball(secret, name, sha))
  79.  
  80.  
  81. def head(key):
  82.     """
  83.    Make a HEAD request for an object in S3.  This is needed to find the
  84.    object's length so it can be accounted.  This function makes one
  85.    billable request and anticipates another.
  86.    """
  87.     librato.count('blueprint-io-server.requests.head')
  88.     statsd.increment('blueprint-io-server.requests.head')
  89.     c = boto.connect_s3(access_key, secret_key)
  90.     b = c.get_bucket(bucket, validate=False)
  91.     try:
  92.         k = b.get_key(key)
  93.         if k is None:
  94.             return None
  95.         return k.size
  96.     except (boto.exception.BotoClientError,
  97.             boto.exception.BotoServerError,
  98.             httplib.HTTPException,
  99.             socket.error,
  100.             socket.gaierror):
  101.         return False
  102.  
  103.  
  104. def head_blueprint(secret, name):
  105.     return head(key_for_blueprint(secret, name))
  106.  
  107.  
  108. def head_tarball(secret, name, sha):
  109.     return head(key_for_tarball(secret, name, sha))
  110.  
  111.  
  112. def key_for_blueprint(secret, name):
  113.     return '{0}/{1}/{2}'.format(secret,
  114.                                 name,
  115.                                 'blueprint.json')
  116.  
  117.  
  118. def key_for_tarball(secret, name, sha):
  119.     return '{0}/{1}/{2}.tar'.format(secret,
  120.                                     name,
  121.                                     sha)
  122.  
  123.  
  124. def list(key):
  125.     """
  126.    List objects in S3 whose keys begin with the given prefix.  This
  127.    function makes at least one billable request.
  128.    """
  129.     librato.count('blueprint-io-server.requests.list')
  130.     statsd.increment('blueprint-io-server.requests.list')
  131.     c = boto.connect_s3(access_key, secret_key)
  132.     b = c.get_bucket(bucket, validate=False)
  133.     return b.list(key)
  134.     try:
  135.         return True
  136.     except (boto.exception.BotoClientError,
  137.             boto.exception.BotoServerError,
  138.             httplib.HTTPException,
  139.             socket.error,
  140.             socket.gaierror):
  141.         return False
  142.  
  143.  
  144. def put(key, data):
  145.     """
  146.    Store an object in S3.  This function makes one billable request.
  147.    """
  148.     librato.count('blueprint-io-server.requests.put')
  149.     statsd.increment('blueprint-io-server.requests.put')
  150.     # TODO librato.something('blueprint-io-server.storage', len(data))
  151.     statsd.update('blueprint-io-server.storage', len(data))
  152.     c = boto.connect_s3(access_key, secret_key)
  153.     b = c.get_bucket(bucket, validate=False)
  154.     k = b.new_key(key)
  155.     try:
  156.         k.set_contents_from_string(data,
  157.                                    policy='public-read',
  158.                                    reduced_redundancy=True)
  159.         return True
  160.     except (boto.exception.BotoClientError,
  161.             boto.exception.BotoServerError,
  162.             httplib.HTTPException,
  163.             socket.error,
  164.             socket.gaierror):
  165.         return False
  166.  
  167.  
  168. def put_blueprint(secret, name, data):
  169.     return put(key_for_blueprint(secret, name), data)
  170.  
  171.  
  172. def put_tarball(secret, name, sha, data):
  173.     return put(key_for_tarball(secret, name, sha), data)
  174.  
  175.  
  176. def url_for(key):
  177.     return '{0}://{1}.{2}.amazonaws.com/{3}'.format(protocol,
  178.                                                     bucket,
  179.                                                     s3_region,
  180.                                                     key)
  181.  
  182.  
  183. def url_for_blueprint(secret, name):
  184.     return url_for(key_for_blueprint(secret, name))
  185.  
  186.  
  187. def url_for_tarball(secret, name, sha):
  188.     return url_for(key_for_tarball(secret, name, sha))
  189.  
downloadbackend.py Source code - Download blueprint Source code
Related Source Codes/Software:
PullToRefresh - A simple iPhone TableViewController for adding the... 2017-05-12
mrjob - Run MapReduce jobs on Hadoop or Amazon Web Service... 2017-05-11
wax - Wax is now being maintained by alibaba ... 2017-05-11
ledisdb - a high performance NoSQL powered by Go ... 2017-05-11
d3-cloud - Create word clouds in JavaScript. ... 2017-05-11
MTStatusBarOverlay - A custom iOS status bar overlay seen in Apps like ... 2017-05-12
www.html5rocks.com - ....a top-notch resource for web developer 2017-05-12
hologram - A markdown based documentation system for style gu... 2017-05-12
isso - a Disqus alternative https:... 2017-05-12
python3-cookbook - "The Python Cookbook" 3 rd Edition Translatio 2017-05-12
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top