def _connect(server=None): if server is None: server = cfg.get('io', 'server') url = urlparse.urlparse(server) if -1 == url.netloc.find(':'): port = url.port or 443 if 'https' == url.scheme else 80 else: port = None if 'https' == url.scheme: return httplib.HTTPSConnection(url.netloc, port) else: return httplib.HTTPConnection(url.netloc, port)
def secret(server): r = http.get('/secret', server=server) if 201 == r.status: secret = r.read().rstrip() logging.warning('created secret {0}'.format(secret)) logging.warning('to set as the default secret, store it in ~/.blueprint.cfg:') sys.stderr.write('\n[io]\nsecret = {0}\nserver = {1}\n\n'. format(secret, cfg.get('io', 'server'))) return secret elif 502 == r.status: logging.error('upstream storage service failed') return None else: logging.error('unexpected {0} creating secret'.format(r.status)) return None
def secret(server): """ Fetch a new secret from the configured server. """ r = http.get("/secret", server=server) if 201 == r.status: secret = r.read().rstrip() logging.warning("created secret {0}".format(secret)) logging.warning("to set as the default secret, store it in ~/.blueprint.cfg:") sys.stderr.write("\n[io]\nsecret = {0}\nserver = {1}\n\n".format(secret, cfg.get("io", "server"))) return secret elif 502 == r.status: logging.error("upstream storage service failed") return None else: logging.error("unexpected {0} creating secret".format(r.status)) return None
def secret(server): """ Fetch a new secret from the configured server. """ r = http.get('/secret', server=server) if 201 == r.status: secret = r.read().rstrip() logging.warning('created secret {0}'.format(secret)) logging.warning('to set as the default secret, store it in ~/.blueprint.cfg:') sys.stderr.write('\n[io]\nsecret = {0}\nserver = {1}\n\n'. format(secret, cfg.get('io', 'server'))) return secret elif 502 == r.status: logging.error('upstream storage service failed') return None else: logging.error('unexpected {0} creating secret'.format(r.status)) return None
""" Testing out Librato's metrics platform. """ from ConfigParser import NoOptionError, NoSectionError import base64 import httplib import urllib from blueprint import cfg try: token = cfg.get('librato', 'token') username = cfg.get('librato', 'username') auth = 'Basic {0}'.format( base64.b64encode('{0}:{1}'.format(username, token))) except (NoOptionError, NoSectionError): auth = None def count(name, value=1): """ Update a counter in Librato's metrics platform. """ if auth is None: return conn = httplib.HTTPSConnection('metrics-api.librato.com') conn.request('POST', '/v1/counters/{0}.json'.format(urllib.quote(name)), urllib.urlencode({'value': value}), { 'Authorization': auth, 'Content-Type': 'application/x-www-form-urlencoded'
import boto import boto.exception import httplib import socket from blueprint import cfg import librato import statsd import boto.s3 AWS_ACCESS_KEY_ID = cfg.get('s3', 'access_key') AWS_SECRET_ACCESS_KEY = cfg.get('s3', 'secret_key') bucket = cfg.get('s3', 'bucket') region = cfg.get('s3', 'region') s3_region = 's3' if 'US' == region else 's3-{0}'.format(region) ''' bucket_name = AWS_ACCESS_KEY_ID.lower() + '-dump' conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) bucket = conn.create_bucket(bucket_name, location=boto.s3.connection.Location.DEFAULT) testfile = "replace this with an actual filename" print 'Uploading %s to Amazon S3 bucket %s' % \ (testfile, bucket_name) def percent_cb(complete, total): sys.stdout.write('.')
import boto import boto.exception import httplib import socket from blueprint import cfg import librato import statsd import boto.s3 AWS_ACCESS_KEY_ID = cfg.get('s3', 'access_key') AWS_SECRET_ACCESS_KEY = cfg.get('s3', 'secret_key') bucket = cfg.get('s3', 'bucket') region = cfg.get('s3', 'region') s3_region = 's3' if 'US' == region else 's3-{0}'.format(region) ''' bucket_name = AWS_ACCESS_KEY_ID.lower() + '-dump' conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) bucket = conn.create_bucket(bucket_name, location=boto.s3.connection.Location.DEFAULT) testfile = "replace this with an actual filename" print 'Uploading %s to Amazon S3 bucket %s' % \ (testfile, bucket_name)
from ConfigParser import NoOptionError, NoSectionError import logging import random import socket import sys from blueprint import cfg try: host, port = cfg.get('statsd', 'host'), cfg.getint('statsd', 'port') except (NoOptionError, NoSectionError, ValueError): host = port = None def timing(stat, time, sample_rate=1): _send({stat: '{0}|ms'.format(time)}, sample_rate) def increment(stats, sample_rate=1): update(stats, 1, sample_rate) def decrement(stats, sample_rate=1): update(stats, -1, sample_rate) def update(stats, delta=1, sample_rate=1): if type(stats) is not list: stats = [stats] _send(dict([(stat, '{0}|c'.format(delta)) for stat in stats]), sample_rate)
import boto import boto.exception import httplib import socket from blueprint import cfg import librato import statsd access_key = cfg.get('s3', 'access_key') bucket = cfg.get('s3', 'bucket') protocol = 'https' if cfg.getboolean('s3', 'use_https') else 'http' region = cfg.get('s3', 'region') s3_region = 's3' if 'US' == region else 's3-{0}'.format(region) secret_key = cfg.get('s3', 'secret_key') def delete(key): """ Remove an object from S3. DELETE requests are free but this function still makes one billable request to account for freed storage. """ 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') c = boto.connect_s3(access_key, secret_key) b = c.get_bucket(bucket, validate=False) try:
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:
import base64 from flask import Flask, Response, abort, redirect, render_template, request import hashlib import json import os import re import sys import urlparse from blueprint import Blueprint from blueprint import cfg import librato import statsd backend = cfg.get('server', 'backend') if backend == "mongodb": import backend_mongodb as backend else: import backend app = Flask(__name__) def _blueprint(secret, name): """ Fetch a blueprint from S3 and turn it into a real Blueprint object. The name can't be given as a kwarg or Blueprint.__init__ will go looking for the JSON in Git. """
from ConfigParser import NoOptionError, NoSectionError import base64 import httplib import urllib from blueprint import cfg try: token = cfg.get('librato', 'token') username = cfg.get('librato', 'username') auth = 'Basic {0}'.format(base64.b64encode('{0}:{1}'.format(username, token))) except (NoOptionError, NoSectionError): auth = None def count(name, value=1): if auth is None: return conn = httplib.HTTPSConnection('metrics-api.librato.com') conn.request('POST', '/v1/counters/{0}.json'.format(urllib.quote(name)), urllib.urlencode({'value': value}), {'Authorization': auth, 'Content-Type': 'application/x-www-form-urlencoded'}) conn.getresponse() conn.close()