2018-06-25 21:40:59 +00:00
|
|
|
import os
|
2018-06-28 17:45:26 +00:00
|
|
|
import tempfile
|
2018-06-18 20:01:30 +00:00
|
|
|
import tarfile
|
2018-06-15 19:42:10 +00:00
|
|
|
|
2018-06-25 21:40:59 +00:00
|
|
|
from flask import request, make_response, send_file
|
2018-06-18 15:01:14 +00:00
|
|
|
|
2018-06-19 17:46:34 +00:00
|
|
|
from data.database import configure
|
2018-06-25 21:40:59 +00:00
|
|
|
from util.config.validator import EXTRA_CA_DIRECTORY
|
2018-06-19 17:46:34 +00:00
|
|
|
|
|
|
|
from config_app.c_app import app, config_provider
|
2018-06-20 15:41:22 +00:00
|
|
|
from config_app.config_endpoints.api import resource, ApiResource, nickname
|
2018-06-15 19:42:10 +00:00
|
|
|
|
2018-06-28 17:45:26 +00:00
|
|
|
@resource('/v1/configapp/initialization')
|
|
|
|
class ConfigInitialization(ApiResource):
|
|
|
|
"""
|
|
|
|
Resource for dealing with any initialization logic for the config app
|
|
|
|
"""
|
|
|
|
|
|
|
|
@nickname('scStartNewConfig')
|
|
|
|
def get(self):
|
|
|
|
config_provider.new_config_dir()
|
|
|
|
|
|
|
|
return make_response('OK')
|
|
|
|
|
|
|
|
|
2018-06-15 19:42:10 +00:00
|
|
|
@resource('/v1/configapp/tarconfig')
|
|
|
|
class TarConfigLoader(ApiResource):
|
2018-06-25 21:40:59 +00:00
|
|
|
"""
|
|
|
|
Resource for dealing with configuration as a tarball,
|
|
|
|
including loading and generating functions
|
|
|
|
"""
|
2018-06-15 19:42:10 +00:00
|
|
|
|
2018-06-25 21:40:59 +00:00
|
|
|
@nickname('scGetConfigTarball')
|
|
|
|
def get(self):
|
2018-06-28 17:45:26 +00:00
|
|
|
config_path = config_provider.get_config_dir_path()
|
2018-06-25 21:40:59 +00:00
|
|
|
|
|
|
|
# remove the initial trailing / from the prefix path, and add the last dir one
|
|
|
|
tar_dir_prefix = config_path[1:] + '/'
|
|
|
|
|
|
|
|
def tarinfo_filter(tarinfo):
|
|
|
|
# remove leading directory info
|
|
|
|
tarinfo.name = tarinfo.name.replace(tar_dir_prefix, '')
|
|
|
|
|
|
|
|
# ignore any directory that isn't the specified extra ca one:
|
|
|
|
if tarinfo.isdir() and not tarinfo.name == EXTRA_CA_DIRECTORY:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return tarinfo
|
|
|
|
|
2018-06-28 17:45:26 +00:00
|
|
|
temp = tempfile.NamedTemporaryFile()
|
2018-06-25 21:40:59 +00:00
|
|
|
|
2018-06-28 17:45:26 +00:00
|
|
|
tar = tarfile.open(temp.name, mode="w|gz")
|
2018-06-25 21:40:59 +00:00
|
|
|
|
|
|
|
for name in os.listdir(config_path):
|
|
|
|
tar.add(os.path.join(config_path, name), filter=tarinfo_filter)
|
|
|
|
|
|
|
|
tar.close()
|
|
|
|
|
2018-06-28 17:45:26 +00:00
|
|
|
return send_file(temp.name, mimetype='application/gzip')
|
2018-06-25 21:40:59 +00:00
|
|
|
|
|
|
|
@nickname('scUploadTarballConfig')
|
2018-06-21 19:55:17 +00:00
|
|
|
def put(self):
|
2018-06-18 15:01:14 +00:00
|
|
|
""" Loads tarball config into the config provider """
|
2018-06-28 17:45:26 +00:00
|
|
|
# Generate a new empty dir to load the config into
|
|
|
|
config_provider.new_config_dir()
|
|
|
|
|
2018-06-18 15:01:14 +00:00
|
|
|
input_stream = request.stream
|
2018-06-25 19:23:30 +00:00
|
|
|
with tarfile.open(mode="r|gz", fileobj=input_stream) as tar_stream:
|
2018-06-28 17:45:26 +00:00
|
|
|
tar_stream.extractall(config_provider.get_config_dir_path())
|
2018-06-18 15:01:14 +00:00
|
|
|
|
2018-06-28 17:45:26 +00:00
|
|
|
# now try to connect to the db provided in their config to validate it works
|
2018-06-19 17:46:34 +00:00
|
|
|
combined = dict(**app.config)
|
|
|
|
combined.update(config_provider.get_config())
|
|
|
|
|
|
|
|
configure(combined)
|
|
|
|
|
2018-06-18 15:01:14 +00:00
|
|
|
return make_response('OK')
|