Example #1
0
def get_vm_list():
    data = {}
    d_id = request.form.get("d_id")
    if d_id:
        username = session.get("user")
        u_id = dao.get_id_by_username(username)
        if u_id:
            permission_list = dao.list_permission_by_user(u_id)
            for permission in permission_list:
                if permission["d_id"] == int(d_id):
                    vm_list = dao.list_vm_by_department(int(d_id))
                    department_list = dao.list_department()
                    department_dict = {}
                    for department in department_list:
                        department_dict[department["id"]] = department["name"]
                    staff_list = dao.list_staff()
                    staff_list_temp = []
                    staff_dict = {}
                    for staff in staff_list:
                        staff_dict[staff["id"]] = staff["name"]
                        if staff["d_id"] == int(d_id):
                            staff_list_temp.append(staff)
                    data["vm_list"] = vm_list
                    data["department_dict"] = department_dict
                    data["staff_dict"] = staff_dict
                    data["staff_list"] = staff_list_temp
    return jsonify(data)
Example #2
0
def powermanage():
    username = session.get("user")
    if username:
        u_id = dao.get_id_by_username(username)
        if u_id:
            permission_list = dao.list_permission_by_user(u_id)
            permission_dict = {}
            for permission in permission_list:
                permission_dict[
                    permission["d_id"]] = permission["permission"].split(",")
        else:
            abort(403)
    else:
        abort(403)
    v_id = request.form.get("v_id")
    action = request.form.get("action")
    data = {}
    if v_id and action:
        v_id = v_id.strip()
        action = action.strip()
        try:
            int(v_id)
        except:
            data["result"] = "fail"
            data["errorinfo"] = "提交的虚拟机信息不正确"
            return jsonify(data)
        vm = dao.get_vm_by_id(int(v_id))
        if action in permission_dict[vm["d_id"]]:
            client_temp = MyClient(vm["host_ip"], 54545, vm["name"])
            if action == "on":
                if client_temp.poweron():
                    cache.set(int(v_id), "on")
                    data["result"] = "success"
                else:
                    data["result"] = "fail"
                    data["errorinfo"] = "执行开机操作失败"
            elif action == "off":
                if client_temp.poweroff():
                    cache.set(int(v_id), "off")
                    data["result"] = "success"
                else:
                    data["result"] = "fail"
                    data["errorinfo"] = "执行关机操作失败"
            else:
                if client_temp.poweroff():
                    if client_temp.poweron():
                        data["result"] = "success"
                    else:
                        data["result"] = "fail"
                        data["errorinfo"] = "执行重启操作失败,无法成功开机"
                else:
                    data["result"] = "fail"
                    data["errorinfo"] = "执行重启操作失败,无法成功关机"
        else:
            data["result"] = "fail"
            data["errorinfo"] = "没有权限"
    else:
        data["result"] = "fail"
        data["errorinfo"] = "没有提交需要操作的虚拟机信息"
    return jsonify(data)
Example #3
0
def add_user():
    data = request.get_json()
    username = data.get("username")
    password = data.get("password")
    permission_dict = data.get("permission_dict")
    data = {}
    if username and password and permission_dict:
        current_user = session.get("user")
        if current_user != "root":
            data["result"] = "fail"
            data["errorinfo"] = "没有权限"
        else:
            password_md5_str = utils.get_md5(password)
            result = dao.save_user(username, password_md5_str)
            if result == 1:
                u_id = dao.get_id_by_username(username)
                if u_id:
                    total_num = len(permission_dict)
                    for k, v in permission_dict.items():
                        total_num -= dao.sava_permission(u_id, int(k), v)
                    if total_num != 0:
                        data["result"] = "fail"
                        data["errorinfo"] = "添加用户成功,但在添加权限时失败,请在修改权限页面重新添加权限"
                    else:
                        data["result"] = "success"
                else:
                    abort(500)
            else:
                data["result"] = "fail"
                data["errorinfo"] = "写入数据库时失败"
    else:
        data["result"] = "fail"
        data["errorinfo"] = "提交的信息不完整"
    return jsonify(data)
Example #4
0
def get_staff_list():
    data = {}
    d_id = request.form.get("d_id")
    if d_id:
        username = session.get("user")
        u_id = dao.get_id_by_username(username)
        if u_id:
            permission_list = dao.list_permission_by_user(u_id)
            for permission in permission_list:
                if permission["d_id"] == int(d_id):
                    staff_list = dao.list_staff_by_department(int(d_id))
                    data["staff_list"] = staff_list
    return jsonify(data)
Example #5
0
def check_username():
    username = request.form.get("username")
    data = {}
    if not username or username == "":
        data["result"] = "fail"
    else:
        if dao.get_id_by_username(username) == None:
            data["result"] = "success"
            data["exist"] = "no"
        else:
            data["result"] = "success"
            data["exist"] = "yes"
    return jsonify(data)
Example #6
0
def index():
    username = session.get("user")
    u_id = dao.get_id_by_username(username)
    if u_id:
        permission_list = dao.list_permission_by_user(u_id)
        department_list = []
        vm_list = []
        permission_dict = {}
        power_state_dict = {}

        for permission in permission_list:
            department = dao.get_department_by_id(permission["d_id"])
            department_list.append(department)
            permission_dict[department["id"]] = permission["permission"].split(
                ",")
            vm_list_temp = dao.list_vm_by_department(department["id"])
            vm_list = vm_list + list(vm_list_temp)

        thread_list = []
        for vm in vm_list:
            cache_key = vm["id"]
            power_state = cache.get(cache_key)
            if power_state:
                power_state_dict[cache_key] = power_state
            else:
                client_temp = MyClient(vm["host_ip"], 54545, vm["name"])
                thread_temp = utils.GetPowerStateThread(
                    client_temp, power_state_dict, cache, vm)
                thread_temp.start()
                thread_list.append(thread_temp)
        for thread_temp in thread_list:
            thread_temp.join()

        staff_dict = {}
        for department in department_list:
            staff_list = dao.list_staff_by_department(department["id"])
            staff_dict[department["name"]] = staff_list

        return render_template("index.html",
                               active="vm_manage",
                               title="虚拟机管理页面",
                               department_list=department_list,
                               staff_dict=staff_dict,
                               permission_dict=permission_dict,
                               vm_list=vm_list,
                               power_state_dict=power_state_dict,
                               username=username)
    else:
        abort(500)
Example #7
0
def getvmlist():
    username = session.get("user")
    if username:
        u_id = dao.get_id_by_username(username)
        if u_id:
            permission_list = dao.list_permission_by_user(u_id)
            permission_dict = {}
            for permission in permission_list:
                permission_dict[
                    permission["d_id"]] = permission["permission"].split(",")
        else:
            abort(403)
    else:
        abort(403)
    s_id = request.form.get("s_id")
    d_id = request.form.get("d_id")
    data = {}
    vm_list = []
    power_state_dict = {}
    data["permission_dict"] = permission_dict
    if s_id:
        vm_list = dao.list_vm_by_staff(int(s_id))
        data["vm_list"] = vm_list
    elif d_id:
        vm_list = dao.list_vm_by_department(int(d_id))
        data["vm_list"] = vm_list
    thread_list = []
    for vm in vm_list:
        cache_key = vm["id"]
        power_state = cache.get(cache_key)
        if power_state:
            power_state_dict[cache_key] = power_state
        else:
            client_temp = MyClient(vm["host_ip"], 54545, vm["name"])
            thread_temp = utils.GetPowerStateThread(client_temp,
                                                    power_state_dict, cache,
                                                    vm)
            thread_temp.start()
            thread_list.append(thread_temp)
    for thread_temp in thread_list:
        thread_temp.join()
    data["power_state_dict"] = power_state_dict
    return jsonify(data)
Example #8
0
def add_department():
    username = session.get("user")
    data = {}
    if username != "root":
        data["result"] = "fail"
        data["errorinfo"] = "权限不足"
    else:
        department_name = request.form.get("department_name")
        if department_name:
            if dao.save_department(department_name) == None:
                data["result"] = "fail"
                data["errorinfo"] = "写入数据库时失败"
            else:
                data["result"] = "success"
                d_id = dao.get_id_by_department(department_name)
                u_id = dao.get_id_by_username("root")
                if d_id and u_id:
                    dao.sava_permission(u_id, d_id, "reboot,on,off,")
        else:
            data["result"] = "fail"
            data["errorinfo"] = "提交的信息不完整"
    return jsonify(data)
Example #9
0
def batch_powermanage():
    username = session.get("user")
    if username:
        u_id = dao.get_id_by_username(username)
        if u_id:
            permission_list = dao.list_permission_by_user(u_id)
            permission_dict = {}
            for permission in permission_list:
                permission_dict[
                    permission["d_id"]] = permission["permission"].split(",")
        else:
            abort(403)
    else:
        abort(403)

    selected_id = request.form.get("selected_id")
    action = request.form.get("action")
    data = {}
    if action and selected_id:
        selected_id = selected_id.strip()
        action = action.strip()
        id_list = selected_id.split(",")

        power_state_dict = {}
        success_list = []
        fail_list = []
        for v_id in id_list:
            if v_id == "":
                continue
            try:
                int(v_id)
            except:
                continue
            vm = dao.get_vm_by_id(int(v_id))
            if action in permission_dict[vm["d_id"]]:
                client_temp = MyClient(vm["host_ip"], 54545, vm["name"])
                if action == "on":
                    if client_temp.poweron():
                        cache.set(int(v_id), "on")
                        success_list.append(vm["name"])
                        power_state_dict[v_id] = "on"
                    else:
                        fail_list.append(vm["name"])
                elif action == "off":
                    if client_temp.poweroff():
                        cache.set(int(v_id), "off")
                        power_state_dict[v_id] = "off"
                        success_list.append(vm["name"])
                    else:
                        fail_list.append(vm["name"])
                else:
                    if client_temp.poweroff():
                        if not client_temp.poweron():
                            power_state_dict[v_id] = "off"
                            fail_list.append(vm["name"])
                        else:
                            success_list.append(vm["name"])
                    else:
                        fail_list.append(vm["name"])
            else:
                fail_list.append(vm["name"])
        data["power_state_dict"] = power_state_dict
        data["success_list"] = success_list
        data["fail_list"] = fail_list
    return jsonify(data)