2018-03-10 15:05:49 +02:00
|
|
|
import flask
|
|
|
|
import flask_bootstrap
|
|
|
|
import redis
|
2018-04-22 11:04:11 +02:00
|
|
|
import json
|
2018-03-10 15:05:49 +02:00
|
|
|
import os
|
|
|
|
import jinja2
|
2018-04-22 11:04:11 +02:00
|
|
|
import uuid
|
2018-04-22 11:54:10 +02:00
|
|
|
import string
|
|
|
|
import random
|
2018-10-23 14:42:19 +02:00
|
|
|
import ipaddress
|
2018-11-27 19:36:24 +02:00
|
|
|
import hashlib
|
2018-04-22 11:04:11 +02:00
|
|
|
|
2018-03-10 15:05:49 +02:00
|
|
|
|
2019-01-16 20:50:52 +02:00
|
|
|
version = os.getenv("this_version", "master")
|
2018-12-30 20:52:34 +02:00
|
|
|
static_url_path = "/" + version + "/static"
|
2019-01-02 20:11:12 +02:00
|
|
|
app = flask.Flask(__name__, static_url_path=static_url_path)
|
2018-03-10 15:05:49 +02:00
|
|
|
flask_bootstrap.Bootstrap(app)
|
2018-04-22 11:04:11 +02:00
|
|
|
db = redis.StrictRedis(host='redis', port=6379, db=0)
|
|
|
|
|
|
|
|
|
2018-04-22 11:54:10 +02:00
|
|
|
def render_flavor(flavor, template, data):
|
2018-04-22 11:04:11 +02:00
|
|
|
return flask.render_template(
|
|
|
|
os.path.join(flavor, template),
|
2018-04-22 11:54:10 +02:00
|
|
|
**data
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-04-22 16:08:43 +02:00
|
|
|
@app.add_template_global
|
2018-04-22 11:54:10 +02:00
|
|
|
def secret(length=16):
|
|
|
|
charset = string.ascii_uppercase + string.digits
|
|
|
|
return ''.join(
|
|
|
|
random.SystemRandom().choice(charset)
|
|
|
|
for _ in range(length)
|
2018-04-22 11:04:11 +02:00
|
|
|
)
|
2018-03-10 15:05:49 +02:00
|
|
|
|
|
|
|
|
2018-04-22 16:08:43 +02:00
|
|
|
def build_app(path):
|
|
|
|
|
2018-04-22 11:54:10 +02:00
|
|
|
app.jinja_env.trim_blocks = True
|
|
|
|
app.jinja_env.lstrip_blocks = True
|
|
|
|
|
2018-04-22 16:08:43 +02:00
|
|
|
@app.context_processor
|
|
|
|
def app_context():
|
2018-12-16 16:26:45 +02:00
|
|
|
return dict(versions=os.getenv("VERSIONS","master").split(','))
|
|
|
|
|
2018-12-19 17:15:45 +02:00
|
|
|
prefix_bp = flask.Blueprint(version, __name__)
|
|
|
|
prefix_bp.jinja_loader = jinja2.ChoiceLoader([
|
2018-12-16 16:26:45 +02:00
|
|
|
jinja2.FileSystemLoader(os.path.join(path, "templates")),
|
|
|
|
jinja2.FileSystemLoader(os.path.join(path, "flavors"))
|
|
|
|
])
|
|
|
|
|
2018-12-19 17:15:45 +02:00
|
|
|
root_bp = flask.Blueprint("root", __name__)
|
|
|
|
root_bp.jinja_loader = jinja2.ChoiceLoader([
|
2018-12-16 16:26:45 +02:00
|
|
|
jinja2.FileSystemLoader(os.path.join(path, "templates")),
|
|
|
|
jinja2.FileSystemLoader(os.path.join(path, "flavors"))
|
|
|
|
])
|
|
|
|
|
2018-12-19 17:15:45 +02:00
|
|
|
@prefix_bp.context_processor
|
|
|
|
@root_bp.context_processor
|
2018-12-16 16:26:45 +02:00
|
|
|
def bp_context(version=version):
|
|
|
|
return dict(version=version)
|
|
|
|
|
2018-12-19 17:15:45 +02:00
|
|
|
@prefix_bp.route("/")
|
|
|
|
@root_bp.route("/")
|
2018-12-16 16:26:45 +02:00
|
|
|
def wizard():
|
|
|
|
return flask.render_template('wizard.html')
|
|
|
|
|
2018-12-19 17:15:45 +02:00
|
|
|
@prefix_bp.route("/submit_flavor", methods=["POST"])
|
|
|
|
@root_bp.route("/submit_flavor", methods=["POST"])
|
2018-12-16 16:26:45 +02:00
|
|
|
def submit_flavor():
|
|
|
|
data = flask.request.form.copy()
|
|
|
|
steps = sorted(os.listdir(os.path.join(path, "templates", "steps", data["flavor"])))
|
|
|
|
return flask.render_template('wizard.html', flavor=data["flavor"], steps=steps)
|
|
|
|
|
2018-12-19 17:15:45 +02:00
|
|
|
@prefix_bp.route("/submit", methods=["POST"])
|
|
|
|
@root_bp.route("/submit", methods=["POST"])
|
2018-12-16 16:26:45 +02:00
|
|
|
def submit():
|
|
|
|
data = flask.request.form.copy()
|
|
|
|
data['uid'] = str(uuid.uuid4())
|
|
|
|
data['dns'] = str(ipaddress.IPv4Network(data['subnet'])[-2])
|
|
|
|
db.set(data['uid'], json.dumps(data))
|
|
|
|
return flask.redirect(flask.url_for('.setup', uid=data['uid']))
|
|
|
|
|
2018-12-19 17:15:45 +02:00
|
|
|
@prefix_bp.route("/setup/<uid>", methods=["GET"])
|
|
|
|
@root_bp.route("/setup/<uid>", methods=["GET"])
|
2018-12-16 16:26:45 +02:00
|
|
|
def setup(uid):
|
|
|
|
data = json.loads(db.get(uid))
|
|
|
|
flavor = data.get("flavor", "compose")
|
|
|
|
rendered = render_flavor(flavor, "setup.html", data)
|
|
|
|
return flask.render_template("setup.html", contents=rendered)
|
|
|
|
|
2018-12-19 17:15:45 +02:00
|
|
|
@prefix_bp.route("/file/<uid>/<filepath>", methods=["GET"])
|
|
|
|
@root_bp.route("/file/<uid>/<filepath>", methods=["GET"])
|
2018-12-16 16:26:45 +02:00
|
|
|
def file(uid, filepath):
|
|
|
|
data = json.loads(db.get(uid))
|
|
|
|
flavor = data.get("flavor", "compose")
|
|
|
|
return flask.Response(
|
|
|
|
render_flavor(flavor, filepath, data),
|
|
|
|
mimetype="application/text"
|
|
|
|
)
|
|
|
|
|
2018-12-19 17:15:45 +02:00
|
|
|
app.register_blueprint(prefix_bp, url_prefix="/{}".format(version))
|
|
|
|
app.register_blueprint(root_bp)
|
2018-03-10 16:25:52 +02:00
|
|
|
|
2018-03-10 15:05:49 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2018-03-10 16:25:52 +02:00
|
|
|
build_app("/tmp/mailutest")
|
2018-03-10 15:05:49 +02:00
|
|
|
app.run(debug=True)
|