import tarfile
import cStringIO
from flask import request, make_response
from data.database import configure
from config_app.c_app import app, config_provider
from config_app.config_endpoints.api import resource, ApiResource, nickname
@resource('/v1/configapp/tarconfig')
class TarConfigLoader(ApiResource):
""" Resource for validating a block of configuration against an external service. """
@nickname('uploadTarballConfig')
def post(self):
""" Loads tarball config into the config provider """
input_stream = request.stream
# since we're working with a tar file, shouldn't be larger than ~20KB, so just read the whole thing into mem
buf = input_stream.read()
config = tarfile.open(mode="r:gz", fileobj=cStringIO.StringIO(buf))
# TODO(sam): refactor config provider to accept a stream write to avoid loading into memory
config_provider.load_from_tarball(config)
# now try to connect to the db provided in their config
combined = dict(**app.config)
combined.update(config_provider.get_config())
configure(combined)
return make_response('OK')