Files
web_management/flaskapp/__init__.py
Andreas Stephanides c4ab1f9fdd move flaskapp
2017-07-25 23:42:24 +02:00

70 lines
2.0 KiB
Python

# -*- coding: utf-8 -*-
from flask import Flask,jsonify,send_from_directory, render_template
import subprocess
import os
from config import Config
import yaml
app = Flask(__name__, template_folder="templates", static_folder="static")
package_directory = os.path.dirname(os.path.abspath(__file__))
cfg = Config(file(os.path.join(package_directory, 'config.cfg')))
if not "vers" in cfg:
cfg.vers=1
global cmds
if "vers" in cfg and cfg.vers == 2:
f=open(os.path.join(package_directory, 'cmds.cfg'),"r")
cmds=yaml.safe_load(f)
f.close()
keys=cmds.keys()
else:
cmds = Config(file(os.path.join(package_directory, 'cmds.cfg')))
keys=cmds.keys()
def render_index(out,code=200):
return render_template("index.html",
out=out,keys=keys,
title=cfg.title.decode("utf8"),
cmds=cmds,
vers=cfg.vers), code
def run_cmd(cmd):
p = subprocess.Popen(cmd, stdout = subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE)
out,err = p.communicate()
return render_index(out+"\nErrorLog\n"+err)
@app.route("/")
def hello():
return render_index(str(cmds))
@app.route("/reload")
def reload():
global cmds
if "vers" in cfg and cfg.vers == 2:
f=open(os.path.join(package_directory, 'cmds.cfg'),"r")
cmds=yaml.safe_load(f)
f.close()
keys=cmds.keys()
else:
cmds = Config(file(os.path.join(package_directory, 'cmds.cfg')))
keys=cmds.keys()
return render_index("Commands in cmds.cfg reloaded")
@app.route("/<string:cmd>")
def cm(cmd):
if cmd in keys:
if "vers" in cfg and cfg.vers == 2:
c=cmds[cmd].cmd
else:
c=cmds[cmd]
return run_cmd(c)
else:
return render_template("index.html", out="Command not found", cmds=cmds, keys=keys, title=cfg.title.decode("utf8"), vers=cfg.vers), 404