29 lines
1.1 KiB
Python
Executable File
29 lines
1.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import base64
|
|
import os
|
|
from kubernetes import client, config
|
|
from kubernetes.client.api_client import ApiClient
|
|
from subprocess import Popen, PIPE
|
|
from flask import Flask, request, send_file
|
|
|
|
TOKEN = os.environ["TOKEN"]
|
|
app = Flask(__name__)
|
|
config.load_incluster_config()
|
|
|
|
@app.route("/stream/<namespace>/mongo/<user>/<database>")
|
|
def stream(namespace, user, database):
|
|
if request.headers.get("Authorization") != TOKEN:
|
|
raise
|
|
with ApiClient() as api:
|
|
v1 = client.CoreV1Api(api)
|
|
secret_name = "mongodb-%s-%s" % (user, database)
|
|
secret = v1.read_namespaced_secret(secret_name, namespace)
|
|
uri = base64.b64decode(secret.data["connectionString.standard"]).decode("ascii")
|
|
cmd = "/usr/bin/mongodump", "--uri", uri, "--gzip", "--archive"
|
|
print("Executing:", cmd)
|
|
process = Popen(cmd, stdout=PIPE, stdin=PIPE, close_fds=True, bufsize=4096 * 1024)
|
|
return send_file(process.stdout, "stream", "application/tar+gzip", download_name="%s.tar.gz" % secret_name)
|
|
|
|
|
|
app.run(host="0.0.0.0", debug=False, threaded=True)
|