예제 #1
0
def upload_logo(uid, priv):
    def allowed_file(filename):
        return '.' in filename and filename.rsplit(
            '.', 1)[1] in ['png', 'jpeg', 'jpg']

    rtn = returnModel("string")
    gc = GlobalConfig.getInstance()

    try:
        if 'file' not in request.files:
            return rtn.error(500)

        # get file object
        file = request.files['file']

        if file.filename == "":
            return rtn.error(500)
        if file and allowed_file(file.filename):
            filename = md5(file.filename.encode() + os.urandom(8)).hexdigest()
            file.save(os.path.join(gc.get("uploads_dir"), filename))
            return rtn.success(filename)

        return rtn.error(500)
    except Exception as e:
        logger.error(traceback.format_exc())
        return rtn.error(500)
예제 #2
0
    def decorated_function(*args, **kwargs):
        rtn = returnModel("string")
        # read session (remember me == false)
        session_token = session.get("session_token")

        if session_token == None or session_token == '':
            # read token
            session_token = request.cookies.get("session_token")

        if session_token == None:
            return rtn.error(403)
        else:
            # query login user via token
            user = db.session.query(UserToken).join(Users).filter(
                UserToken.token == session_token).first()
            if user is None:
                return rtn.error(403)
            else:
                priv = user.ob_user.privilege
                uid = user.uid

                if priv == PRIVILEGES.ROOT_USER:
                    return fn(uid, priv, *args, **kwargs)
                else:
                    return rtn.error(403)
예제 #3
0
def test_mysql_connection():
    rtn = returnModel(type="string")
    try:
        db_env = DatabaseEnv()
        F = request.json

        mysql_username = F.get("mysql_username")
        mysql_password = F.get("mysql_password")

        return rtn.success(db_env.testMySQLdb(mysql_username, mysql_password))
    except:
        return rtn.error(500)
예제 #4
0
def server_logo_source(uid, priv, inst_id):
    rtn = returnModel("string")
    user_inst_obj = db.session.query(ServerInstance).filter(
        ServerInstance.inst_id == inst_id).first()
    if user_inst_obj == None:
        # inst id not belong to this user
        abort(403)
    elif user_inst_obj.owner_id != uid:
        abort(403)
    else:
        inst_dir = user_inst_obj.inst_dir
        logo_file_name = os.path.join(inst_dir, "server-icon.png")
        if os.path.exists(logo_file_name):
            return send_file(logo_file_name)
        else:
            abort(404)
예제 #5
0
def detect_java_environment():
    rtn = returnModel("string")
    gc = GlobalConfig.getInstance()

    try:
        env = JavaEnv()
        java_envs = {"system": [], "user": []}
        __dir, __ver = env.findSystemJavaInfo()
        if __dir != None:
            _model = {"name": "java", "dir": "(%s)" % __dir}
            java_envs.get("system").append(_model)

        _arr = env.findUserJavaInfo()
        for java_ver in _arr:
            _model = {
                "name": "JDK %s" % java_ver['version'],
                "dir": "(%s)" % java_ver["dir"]
            }

            java_envs.get("user").append(_model)
        return rtn.success(java_envs)
    except:
        return rtn.error(500)
    pass
예제 #6
0
__author__ = "Nigshoxiz"

from flask import render_template, abort, request, make_response, redirect
from jinja2 import TemplateNotFound

from app import db, proxy, app
from app.model import JavaBinary
from app.utils import returnModel
from app.tools.mc_downloader import sourceJAVA
from app.tools.mq_proxy import WS_TAG

from . import super_admin_page, logger, version
from .check_login import super_admin_only, ajax_super_admin_only

rtn = returnModel("string")


# some dirty but useful functions' collection
class _utils:

    WAIT = 1
    DOWNLOADING = 2
    EXTRACTING = 3
    FINISH = 4
    FAIL = 5
    EXTRACT_FAIL = 6


# render page
@super_admin_page.route('/java_binary', methods=['GET'])
@super_admin_only
예제 #7
0
def submit_new_inst(uid, priv):
    def _inst_directory(inst_id):
        '''
        In order to create a new instance, we have to create an individual space to
        store files first.
        :return:
        '''
        gc = GlobalConfig.getInstance()
        servers_dir = gc.get("servers_dir")

        owner = db.session.query(Users).filter(Users.id == uid).first()

        owner_name = owner.username
        dir_name = "%s_%s" % (owner_name, inst_id)

        logger.debug("[user_inst] dir_name = %s" % dir_name)
        return os.path.join(servers_dir, dir_name)
        pass

    rtn = returnModel("string")
    gc = GlobalConfig.getInstance()
    try:
        F = request.json

        inst_name = F.get("inst_name")
        core_file_id = F.get("core_file_id")
        java_bin_id = F.get("java_bin_id")
        listening_port = F.get("listening_port")
        auto_port_dispatch = F.get("auto_port_dispatch")

        # unit: GiB
        max_RAM = F.get("max_RAM")
        max_user = F.get("max_user")

        # json format
        server_properties = F.get("server_properties")

        # logo url
        logo_url = F.get("logo_url")

        # set encoded motd content
        motd = F.get("motd")

        # FTP account
        FTP_account_name = F.get("ftp_account")
        FTP_default_password = (F.get("ftp_default_password") == True)
        FTP_password = F.get("ftp_password")

        i = UserInstance(uid)

        try:
            if auto_port_dispatch:
                i.set_listening_port()
            else:
                i.set_listening_port(listening_port)

            i.set_instance_name(inst_name)
            i.set_java_bin(java_bin_id)
            i.set_allocate_RAM(int(max_RAM) * 1024)
            i.set_server_core(core_file_id)
            i.set_max_user(max_user)

            properties_json = json.loads(server_properties)
            properties_json["motd"] = motd

            # 'true' -> True , 'false' -> False
            for key in properties_json:
                if properties_json.get(key) == 'true':
                    properties_json[key] = True
                elif properties_json.get(key) == 'false':
                    properties_json[key] = False

            i.set_instance_properties(properties_json)

            inst_id = i.create_inst()
            # move logo
            if logo_url != None and logo_url != "":
                logo_file_name = os.path.join(gc.get("uploads_dir"), logo_url)
                if os.path.exists(logo_file_name):
                    shutil.move(
                        logo_file_name,
                        os.path.join(_inst_directory(inst_id),
                                     "server-icon.png"))
            # create FTP accountx
            ftp_controller = FTPController()

            if not FTP_default_password:
                _ftp_password = FTP_password
            else:
                _ftp_password = None
            ftp_controller.create_account(uid,
                                          FTP_account_name,
                                          inst_id,
                                          ftp_password=_ftp_password)

            return rtn.success(inst_id)
            # return redirect("/server_inst/dashboard/%s" % inst_id)
        except:
            logger.error(traceback.format_exc())
            return rtn.error(500)
    except Exception as e:
        logger.error(traceback.format_exc())
        return rtn.error(500)