예제 #1
0
def batch_hosts():
    data_text = request.form["text"].strip()
    for num, line in enumerate(data_text.split('\n')):
        line_split = line.split()
        if len(line_split) < 2:
            return jsonify({"code": 1, "msg": "%d line, field numbers < 2" % (num + 1)})
        if utils.is_valid_address(line_split[1]) is False:
            return jsonify({"code": 1, "msg": "%d line, ip address is incorrect" % (num + 1)})

    for line in data_text.split('\n'):
        line_split = line.split()
        # 已存在的域名忽略导入
        query = HostsModel.query.filter(HostsModel.status.notin_([-1]))
        if query.filter_by(domain=line_split[0]).count() > 0:
            continue
        data = {
            "domain": line_split[0],
            "ip": line_split[1],
            "status": 0,
            "user_id": current_user.id,
        }
        if len(line_split) < 3:
            data.update({"note": ""})
        else:
            data.update({"note": line_split[2]})
        HostsModel.add_host(data)
    return jsonify({"code": 0, "msg": "success"})
예제 #2
0
def hosts():
    if request.method == 'GET':
        page = request.args.get('page', 1, type=int)
        per_page = request.args.get('per_page', 10, type=int)
        kw = request.values.get('kw', '')
        (data, total) = HostsModel.list(page=page, per_page=per_page, kw=kw)
        page_data = {
            "limit": per_page,
            "page": page,
            "data": data,
            "total": total,
            "code": 0,
            "message": ""
        }
        return jsonify(page_data)

    if request.method == 'POST':
        form = AddHostForm(csrf_enabled=False)
        if form.validate_on_submit():
            data_dict = form.data
            data_dict.update({"user_id": current_user.id})
            print(data_dict, type(data_dict))
            HostsModel.add_host(data_dict)
            return jsonify({"code": 0, "msg": "success"})
        return jsonify({
            "code": 1,
            "msg": form.errors[form.errors.keys()[0]]
        })
예제 #3
0
 def __init__(self, user, password, ip):
     self.ip = ip
     self.password = password
     self.user = user
     # self.connect = Co
     self.session = sessions.connect(user, password, ip)
     self.api = self.session.xenapi
     self.hosts_model = HostsModel.get_instance()
예제 #4
0
def hosts():
    if 'get' in request.args.keys():
        try:
            hosts_model = HostsModel.get_instance()
            hosts = hosts_model.get_hosts()
            return response(result=hosts, resp_type='success')
        except BaseException as e:
            return response(result=str(e), resp_type='error')
    return render_template('hosts.html')
예제 #5
0
def vms():
    backup_sr_model = BackupStorageModel.get_instance()
    srs = backup_sr_model.get_backup_sr_wo_login()

    if 'get' in request.args.keys():
        try:
            hosts_model = HostsModel.get_instance()
            vm = hosts_model.get_vm()
            return response(result=vm, resp_type='success')
        except BaseException as e:
            return response(result=str(e), resp_type='error')
    return render_template('vms.html', srs=srs)
예제 #6
0
def backups():
    hosts_model = HostsModel.get_instance()
    hosts = hosts_model.get_hosts()

    if 'get' in request.args.keys():
        try:
            backup_model = BackupModel.get_instance()
            backups = backup_model.get_backups()
            return response(result=backups, resp_type='success')
        except BaseException as e:
            return response(result=str(e), resp_type='error')

    return render_template('backups.html', hosts=hosts)
예제 #7
0
def storage():
    print(request.args)
    if 'get' in request.args.keys():
        try:
            sr = []
            if request.args['get'] == 'host_sr':
                pool = request.args['pool']
                hosts_model = HostsModel.get_instance()
                sr = hosts_model.get_sr(pool=pool)
            elif request.args['get'] == 'backup_sr':
                sr = BackupStorageModel()
                sr.get_backup_sr_wo_login()

            return response(result=sr, resp_type='success')
        except BaseException as e:
            return response(result=str(e), resp_type='error')

    return render_template('storage.html')
예제 #8
0
    def rm_host(hosts=None):
        result = 'Remove pool success'
        res_type = 'success'

        if hosts is not None:
            for host in hosts:
                try:
                    hosts_model = HostsModel.get_instance()
                    r = hosts_model.rm_pool(host)
                    if r['ok'] == 0:
                        result = 'Remove pool failed'
                        res_type = 'error'
                except errors:
                    result = 'Remove pool failed'
                    res_type = 'error'
        else:
            result = 'No hosts selected'
            res_type = 'error'

        return result, res_type