Skip to content

Commit

Permalink
add config backup
Browse files Browse the repository at this point in the history
  • Loading branch information
v55448330 committed Aug 8, 2016
1 parent 75eed36 commit 6cd397c
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

项目起源于好哥们需要一个 7 层负载均衡器,无奈商业负载均衡器成本高昂,操作复杂。又没有特别喜欢(好看,好用)的开源产品,作为一名大 Ops 怎么能没有办法?正好最近在看 Django 框架,尝试自己给 Nginx 画皮,项目诞生!非专业开发,代码凑合看吧。

> * 项目基于 [Django](https://www.djangoproject.com/) + [AdminLTE](https://www.almsaeedstudio.com/) 构建,在 Ubuntu 14.04 上测试通过;
> * 项目基于 [Django](https://www.djangoproject.com/) + [AdminLTE](https://www.almsaeedstudio.com/) 构建,在 Ubuntu 14.04 上测试通过;为了保证良好的兼容性,请使用 Chrome 浏览器。
> * 因为增加了 iptables 自动控制,所以暂时不支持 docker 方式部署;需要本地测试的同学请使用 vagrant 方式
> * 因为使用了 [nginx_upstream_check_module](http://tengine.taobao.org/document/http_upstream_check.html) 模块,以及为了后续扩展方便,建议大家直接使用 [Tengine](http://tengine.taobao.org/) 替代 Nginx 服务
Expand All @@ -14,6 +14,7 @@
* 修复因前方有防火墙导致无法获取后端服务器状态
* 修复因主机头导致后端服务器探测失败
* 新增自定义管理员用户
* 新增配置通过文件备份和还原
* 更新 Vagrantfile
* 修复其他 Bug

Expand Down
Binary file added db.sqlite3.bak
Binary file not shown.
2 changes: 2 additions & 0 deletions resource/AdminLTE/plugins/FileSaver/FileSaver.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions settings/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
url(r'^password/$', views.modify_pass),
url(r'^admin/$', views.admin_reset),
url(r'^nic/$', views.select_nic),
url(r'^config/(?P<action>.*)/$', views.config_backup),
]
49 changes: 48 additions & 1 deletion settings/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
from django.template import RequestContext, loader
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.core import serializers
from django.http import HttpResponse
from proxy.models import *
from main.models import *
from lazy_balancer.views import is_auth
from nginx.ip import set_firewall
from .models import system_settings
Expand Down Expand Up @@ -64,7 +67,6 @@ def admin_reset(request):
@is_auth
def select_nic(request):
try:
content = "test"
post = json.loads(request.body)
internal_nic = post['select_nic']
if system_settings.objects.all().count() != 0:
Expand All @@ -76,3 +78,48 @@ def select_nic(request):
except Exception,e:
content = { "flag":"Error","content":str(e) }
return HttpResponse(json.dumps(content))

@is_auth
def config_backup(request,action):
main_config_qc = main_config.objects.all()
upstream_config_qc = upstream_config.objects.all()
proxy_config_qc = proxy_config.objects.all()
if action == "export":
try:
m_config = serializers.serialize('json', main_config_qc)
u_config = serializers.serialize('json', upstream_config_qc)
p_config = serializers.serialize('json', proxy_config_qc)

config = {
"main_config" : m_config,
"upstream_config" : u_config,
"proxy_config" : p_config,
}
content = { "flag":"Success", "content": config }
except Exception,e:
content = { "flag":"Error","content":str(e) }

elif action == "import":
try:
post = json.loads(request.body)

m_config = post['main_config']
u_config = post['upstream_config']
p_config = post['proxy_config']

main_config_qc.delete()
upstream_config_qc.delete()
proxy_config_qc.delete()

for obj in serializers.deserialize("json", m_config):
obj.save()
for obj in serializers.deserialize("json", u_config):
obj.save()
for obj in serializers.deserialize("json", p_config):
obj.save()

content = { "flag":"Success" }
except Exception,e:
content = { "flag":"Error","content":str(e) }

return HttpResponse(json.dumps(content))
1 change: 1 addition & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<script src="{% static "dist/js/app.min.js" %}"></script>
<!-- Inputmask -->
<script src="{% static "plugins/input-mask/inputmask.min.js" %}"></script>
<script src="{% static "plugins/FileSaver/FileSaver.min.js" %}"></script>
<script src="{% static "plugins/input-mask/jquery.inputmask.min.js" %}"></script>
<script src="{% static "plugins/input-mask/inputmask.extensions.min.js" %}"></script>
<script src="{% static "plugins/input-mask/inputmask.numeric.extensions.min.js" %}"></script>
Expand Down
76 changes: 76 additions & 0 deletions templates/settings/view.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ <h4 class="box-title">网络选择<small> 用于创建防火墙规则</small></h
</div>
</div>

<div class="row">
<div class="col-md-12">
<div class="box box-primary">
<div class="box-header with-border">
<h4 class="box-title">配置备份<small> 通过文件备份和还原负载均衡配置</small></h4>
</div>
<div class="box-body">
<button type="button" class="btn btn-primary" onclick="config_backup(1)">备份</button>
<button type="button" class="btn btn-primary" onclick="import_config.click()">还原</button>
<input type="file" style="display:none" name="import_config" id="import_config" onchange="config_backup(0)" />
</div>
</div>
</div>
</div>


<script>
//init page
Expand Down Expand Up @@ -133,6 +148,67 @@ <h4 class="box-title">网络选择<small> 用于创建防火墙规则</small></h
}
}

function config_backup(type) {
if (type==1){
if (confirm("此操作会将负载均衡器<全局配置>和<负载均衡配置>备份为文件以供还原,是否继续?")) {
jQuery.ajax({
type: 'post',
url: '/settings/config/export/',
dataType: 'json',
success: function(p) {
if (p.flag == "Success") {
var blob = new Blob([JSON.stringify(p.content)], {type: "text/plain;charset=utf-8"});
saveAs(blob, "LazyBalancer.bak");
alert("备份成功!")
top.location = '/settings/'
} else if (p.flag == "Error" && p.content == "AuthFailed") {
alert('认证失败!请重新登录!')
top.location = '/login/'
} else {
alert('修改错误!其他错误:' + p.content)
}
},
error: function(e) {
alert('请求失败!')
}
})
}
}else{
if (confirm("此操作会将负载均衡器<全局配置>和<负载均衡配置>通过您提供的配置文件覆盖,是否继续?")) {
var reader = new FileReader()
reader.readAsText($('#import_config')[0].files[0])
reader.onload = function(e){
$.ajax({
type: 'post',
url: '/settings/config/import/',
data: this.result,
dataType: 'json',
success: function(p) {
console.log(p)
if (p.flag == "Success") {
alert("还原完成!")
top.location = '/settings/'
} else if (p.flag == "Error" && p.content == "AuthFailed") {
alert('认证失败!请重新登录!')
top.location = '/login/'
} else {
$('#import_config').val('')
alert('还原失败!错误:' + p.content)
}
},
error: function(e) {
$('#import_config').val('')
alert('请求失败!')
}
})
}
}
else{
$('#import_config').val('')
}
}
}

function modify_pass(reset_admin) {
if (reset_admin == 1) {
if (confirm("确认要重置管理员?")) {
Expand Down

0 comments on commit 6cd397c

Please sign in to comment.