Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDB support for storage #152

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion blueprint/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def pull(server, secret, name):
r = http.get('/{0}/{1}'.format(secret, name), server=server)
if 200 == r.status:
b = Blueprint.load(r, name)

for filename in b.sources.itervalues():
logging.info('fetching source tarballs - this may take a while')
r = http.get('/{0}/{1}/{2}'.format(secret, name, filename),
Expand Down
15 changes: 13 additions & 2 deletions blueprint/io/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@

from blueprint import Blueprint
from blueprint import cfg
import backend

import librato
import statsd

backend = cfg.get('server', 'backend')
if backend == "mongodb":
import backend_mongodb as backend
else:
import backend

app = Flask(__name__)

app = Flask(__name__)

def _blueprint(secret, name):
"""
Expand Down Expand Up @@ -114,6 +119,12 @@ def secret():

browser_pattern = re.compile(r'Chrome|Gecko|Microsoft|Mozilla|Safari|WebKit')

@app.route('/<secret>/<name>/blueprint.json', methods=['GET'])
def get_json(secret, name):
validate_secret(secret)
validate_name(name)

return backend.get(backend.key_for_blueprint(secret, name))

@app.route('/<secret>/<name>', methods=['GET'])
def get_blueprint(secret, name):
Expand Down
166 changes: 166 additions & 0 deletions blueprint/io/server/backend_mongodb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
from pymongo import MongoClient
import httplib
import socket

from blueprint import cfg
import librato
import statsd

username = None
password = None

address = cfg.get('mongodb', 'address')
port = cfg.get('mongodb', 'port')
database = cfg.get('mongodb', 'database')
collection = cfg.get('mongodb', 'collection')
url = cfg.get('server', 'address')
protocol = 'https' if cfg.getboolean('server', 'use_https') else 'http'
try:
username = cfg.get('mongodb', 'user')
password = cfg.get('mongodb', 'password')
except:
pass

client = MongoClient(address, int(port))
db = client[database]
if username is not None:
if password is not None:
db.authenticate(username, password)
collection = db[collection]

class StoredObject:
key = None
tarball = None
def __init__(self, key, tarball):
self.key = key
self.tarball = tarball

def delete(key):
"""
Remove an object from MongoDB.
"""
content_length = head(key)
if content_length is None:
return None
librato.count('blueprint-io-server.requests.delete')
statsd.increment('blueprint-io-server.requests.delete')
try:
collection.delete({"key" : key})
except:
return False

def delete_blueprint(secret, name):
return delete(key_for_blueprint(secret, name))


def delete_tarball(secret, name, sha):
return delete(key_for_tarball(secret, name, sha))


def get(key):
"""
Fetch an object from MongoDB.
"""
librato.count('blueprint-io-server.requests.get')
statsd.increment('blueprint-io-server.requests.get')
try:
k = collection.find_one({"key" : key})
if k is None:
return None
except:
return False
return k['tarball']


def get_blueprint(secret, name):
return get(key_for_blueprint(secret, name))


def get_tarball(secret, name, sha):
return get(key_for_tarball(secret, name, sha))


def head(key):
"""
Make a HEAD request for an object in MongoDB.
This returns the size of the tarball.
"""
librato.count('blueprint-io-server.requests.head')
statsd.increment('blueprint-io-server.requests.head')
try:
k = collection.find_one({"key" : key})
if k is None:
return None
except:
return False
return len(k['tarball'])


def head_blueprint(secret, name):
return head(key_for_blueprint(secret, name))


def head_tarball(secret, name, sha):
return head(key_for_tarball(secret, name, sha))


def key_for_blueprint(secret, name):
return '{0}/{1}/{2}'.format(secret,
name,
'blueprint.json')


def key_for_tarball(secret, name, sha):
return '{0}/{1}/{2}.tar'.format(secret,
name,
sha)


def list(key):
"""
List objects in MongoDB whose key begins with the given prefix
"""
librato.count('blueprint-io-server.requests.list')
statsd.increment('blueprint-io-server.requests.list')
try:
result = collection.find({"key" : '^%s' % (key)})
except:
return False
return result


def put(key, data):
"""
Store an object in MongoDB.
"""
librato.count('blueprint-io-server.requests.put')
statsd.increment('blueprint-io-server.requests.put')
# TODO librato.something('blueprint-io-server.storage', len(data))
statsd.update('blueprint-io-server.storage', len(data))
element = StoredObject(key, data)
try:
collection.insert(element.__dict__)
except:
return False
return True



def put_blueprint(secret, name, data):
return put(key_for_blueprint(secret, name), data)


def put_tarball(secret, name, sha, data):
return put(key_for_tarball(secret, name, sha), data)


def url_for(key):
return '{0}://{1}/{2}'.format(protocol, url, key)


def url_for_blueprint(secret, name):
return url_for(key_for_blueprint(secret, name))


def url_for_tarball(secret, name, sha):
return url_for(key_for_tarball(secret, name, sha))