def createserver(): form = ServerForm() if request.method == "POST": if form.validate() == False: return render_template("server.html", form=form) else: arch = Arch.query.filter_by(id=form.arch.data).first() os = Os.query.filter_by(id=form.os.data).first() server = Server( hostname=form.hostname.data, ipv4=form.ipv4.data, port=form.port.data, arch=arch, os=os, cores=form.cores.data, ram=form.ram.data, hds=form.hds.data, cores_free=form.cores.data, ram_free=form.ram.data, hds_free=form.hds.data, ) db.session.add(server) db.session.commit() list = [(s.hostname, s.ipv4, s.port, s.cores, s.ram, s.hds) for s in Server.query.order_by("hostname")] return render_template("servers.html", servers=list) # return render_template('server.html', form=ServerForm(formdata=None),servers=list) elif request.method == "GET": return render_template("server.html", form=form)
def server_create(): if not can_create(current_user): abort(401) form = ServerForm(request.form) if request.method == 'POST' and form.validate(): db_session = DBSession() server = get_server(db_session, form.hostname.data) if server is not None: return render_template('server/edit.html', form=form, duplicate_error=True) create_or_update_server_repository( db_session=db_session, hostname=form.hostname.data, server_type=form.server_type.data, server_url=form.server_url.data, username=form.username.data, password=form.password.data, vrf=form.vrf.data if form.server_type.data == ServerType.TFTP_SERVER or form.server_type.data == ServerType.FTP_SERVER else '', server_directory=form.server_directory.data, destination_on_host=form.destination_on_host.data, created_by=current_user.username) return redirect(url_for('home')) return render_template('server/edit.html', form=form)
def getServer(*args, **kwargs): """ 获取服务器信息 """ form = ServerForm(data=kwargs) if not form.validate(): return dictify(test={"code": 1}, success=False, msg=u"表单参数有误") project_id = form.project_id.data cache = form.cache.data try: project = Project.objects.values("name").get(id=project_id) except: return dictify(test={"code": 2}, success=False, msg=u"业务不存在") service = SealService() try: servers = service.getServer( productName=project.get("name").encode("utf-8"), cache=cache) except: return dictify(test={"code": 3}, success=False, msg=u"海豹系统异常") data = [] for i in servers: if i.get("wanIpTd"): host = i.get("wanIpTd").split(";")[0] elif i.get("cloudWanIp"): host = i.get("cloudWanIp").split(";")[0] elif i.get("lanIpTd"): host = i.get("lanIpTd").split(";")[0] elif i.get("cloudLanIp"): host = i.get("cloudLanIp").split(";")[0] else: continue if i.has_key("comment"): desc = i.get("comment", "") else: desc = i.get("yewuComment", "") server = { "host": host, "zone": i.get("zoneName", ""), "channel": i.get("channel", ""), "desc": desc, "function": i.get("function", "") } data.append(server) return dictify(test={"code": 4}, success=True, data=data)
def server_edit(hostname): db_session = DBSession() server = get_server(db_session, hostname) if server is None: abort(404) form = ServerForm(request.form) if request.method == 'POST' and form.validate(): if not can_edit(current_user): abort(401) if hostname != form.hostname.data and get_server( db_session, form.hostname.data) is not None: return render_template('server/edit.html', form=form, duplicate_error=True) create_or_update_server_repository( db_session=db_session, hostname=form.hostname.data, server_type=form.server_type.data, server_url=form.server_url.data, username=form.username.data, password=form.password.data if len(form.password.data) > 0 else server.password, vrf=form.vrf.data if form.server_type.data == ServerType.TFTP_SERVER or form.server_type.data == ServerType.FTP_SERVER else '', server_directory=form.server_directory.data, destination_on_host=form.destination_on_host.data, created_by=current_user.username, server=server) return redirect(url_for('home')) else: # Assign the values to form fields form.hostname.data = server.hostname form.server_type.data = server.server_type form.server_url.data = server.server_url form.username.data = server.username form.vrf.data = server.vrf form.server_directory.data = server.server_directory form.destination_on_host.data = server.destination_on_host if not is_empty(server.password): form.password_placeholder = 'Use Password on File' else: form.password_placeholder = 'No Password Specified' return render_template('server/edit.html', form=form)
def getServer(*args, **kwargs): """ 获取服务器信息 """ form = ServerForm(data = kwargs) if not form.validate(): return dictify(test = {"code": 1}, success = False, msg = u"表单参数有误") project_id = form.project_id.data cache = form.cache.data try: project = Project.objects.values("name").get(id = project_id) except: return dictify(test = {"code": 2}, success = False, msg = u"业务不存在") service = SealService() try: servers = service.getServer(productName = project.get("name").encode("utf-8"), cache = cache) except: return dictify(test = {"code": 3}, success = False, msg = u"海豹系统异常") data = [] for i in servers: if i.get("wanIpTd"): host = i.get("wanIpTd").split(";")[0] elif i.get("cloudWanIp"): host = i.get("cloudWanIp").split(";")[0] elif i.get("lanIpTd"): host = i.get("lanIpTd").split(";")[0] elif i.get("cloudLanIp"): host = i.get("cloudLanIp").split(";")[0] else: continue if i.has_key("comment"): desc = i.get("comment", "") else: desc = i.get("yewuComment", "") server = { "host": host, "zone": i.get("zoneName", ""), "channel": i.get("channel", ""), "desc": desc, "function": i.get("function", "") } data.append(server) return dictify(test = {"code": 4}, success = True, data = data)
def add(account): if request.method == 'POST': form = ServerForm(request.form) if form.validate(): server = Server() form.populate_obj(server) server.put() flash(u'Server added!') return redirect(url_for('server.show', server_key=server.key.urlsafe())) else: form = ServerForm() return render_template('server/form.html', form=form, account=account)
def edit(account, server_key): server = get_server(server_key) if server is None: return abort(404) if request.method == 'POST': form = ServerForm(request.form) if form.validate(): form.populate_obj(server) server.put() return redirect(url_for('server.show', server_key=server.key.urlsafe())) else: form = ServerForm(**server._to_dict()) return render_template('/server/form.html', form=form, server_key=server.key.urlsafe(), account=account)