Skip to content

Commit

Permalink
up: add host get hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
deploy committed Apr 29, 2019
1 parent 274c71d commit 0417fe3
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 8 deletions.
6 changes: 6 additions & 0 deletions app/main/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ def writefile(path, content):
f.close()


def getHostname(host):
shell_cmd = '''ssh -o StrictHostKeyChecking=no -o ConnectTimeout=2 %s@%s "hostname" ''' %(exec_user, host)
Result = shellcmd(shell_cmd)
return Result


def hostInit(project, host, Type):
if Type == 'java':
shell_cmd = '''ssh -o StrictHostKeyChecking=no -o ConnectTimeout=2 %s@%s "cp -a %s/tomcat8_install_template %s/%s " ''' %(
Expand Down
57 changes: 56 additions & 1 deletion app/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,37 @@ def clean_git_cache():

return json.dumps([status])

@main.route("/add_group", methods=["GET", "POST"])
@login_required
def add_group():
R = {'status':'ok', 'log':'', 'data':''}
try:
addgroupname = request.form.get("addgroupname","null").strip().split(' ')[0]
if addgroupname == '' or addgroupname == 'null':
raise Exception('ERROR: add groupname null')
u = servicegroup(servicegroup=addgroupname)
db.session.add(u)
db.session.commit()
except Exception as err:
R['log'] = str(err)
R['status'] = 'fail'
return json.dumps(R)


@main.route("/del_group", methods=["POST"])
@login_required
def del_group():
R = {'status':'ok', 'log':'', 'data':''}
try:
groupname = request.form.get('selectgroup', 'null')
if groupname == 'null':
raise Exception('ERROR: servicegroup null')
servicegroup.query.filter(servicegroup.servicegroup == groupname).delete()
except Exception as err:
R['log'] = str(err)
R['status'] = 'fail'
return json.dumps(R)


@main.route("/adduserservicegroup", methods=["GET", "POST"])
@login_required
Expand Down Expand Up @@ -264,6 +295,23 @@ def group_list():
grouplist = sorted(list(set(grouplist)))
return json.dumps(grouplist)

@main.route("/group_list_all", methods=["GET", "POST"])
@login_required
def group_list_all():
user = current_user.username
if user in adminuser:
try:
ones = servicegroup.query.all()
except:
return json.dumps(['sql error or no authority'])

grouplist = []
for i in ones:
if i.servicegroup != 'null':
grouplist.append(i.servicegroup)
grouplist = sorted(list(set(grouplist)))
return json.dumps(grouplist)


@main.route("/group_list_user", methods=["GET", "POST"])
def group_list_user():
Expand Down Expand Up @@ -582,13 +630,20 @@ def add_host():
host = request.form.get('host', "null").strip()
pnum = request.form.get('pnum', "1" ).strip()
env = request.form.get('env', "" ).strip()
if project == "null" or hostname == "null" or host == "null" or project == "" or hostname == "" or host == "":
if project == "null" or host == "null" or project == "" or host == "":
raise Exception('ERROR: parameter error')
ones1 = serverinfo.query.filter(serverinfo.project_name == project, serverinfo.ip == host).all()
if len(ones1) != 0:
raise Exception('ERROR: host exist.')
if pnum == '':
pnum = '1'

if hostname == '' or hostname == 'null':
Result = getHostname(host)
if Result['status'] == 'ok':
hostname = Result['log'].strip().split('.')[0]
else:
raise Exception('ERROR: get hostname error')

newserver = serverinfo(project, hostname, host, pnum, env, 'checkstatus', 'checktime', 'commitid', 'updatestatus', 'updatetime')
db.session.add(newserver)
Expand Down
7 changes: 7 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ def __init__(self, username, servicegroup, permissions):
self.servicegroup = servicegroup
self.permissions = permissions

class servicegroup(db.Model):
__tablename__ = 'servicegroup'
id = db.Column(db.Integer, primary_key=True)
servicegroup = db.Column(db.String(64))
def __init__(self, servicegroup):
self.servicegroup = servicegroup

class projectinfo(db.Model):
__tablename__ = 'projectinfo'
project_name = db.Column(db.String(64), primary_key=True)
Expand Down
57 changes: 52 additions & 5 deletions app/static/ops.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,34 @@ $("body").on('click', '#add_host', function(){
});


$("body").on('click', '#add_group', function(){
if (confirm('确认提交?')) {
var addgroupname = $('#addgroupname').val()
var param = {
addgroupname: addgroupname,
}
$.post('/add_group', param, function(data){
group_list_all()
alert(data.status+" "+data.log);
}, 'json');
};
});


$("body").on('click', '#del_group', function(){
if (confirm('确认删除服务组?')) {
var selectgroup = $('#selectgroup').val()
var param = {
selectgroup: selectgroup,
}
$.post('/del_group', param, function(data){
group_list_all()
alert(data.status+" "+data.log);
}, 'json');
};
});


$("body").on('click', '#add_servergroup', function(){
if (confirm('确认提交?')) {
var username = $('#current_user').val()
Expand Down Expand Up @@ -619,6 +647,16 @@ function group_list(){
})
};

function group_list_all(){
$.getJSON('/group_list_all', function(data){
var htm=['<select class="form-control" id="selectgroup">'];
for(var i=0,len=data.length; i<len; i++){
htm.push('<option value="'+data[i]+'">'+data[i]+'</option>');
}
htm.push('</select>');
$('#groupnamediv').html(htm.join(''));
})
};

function user_list(){
$.getJSON('/user_list', function(data){
Expand All @@ -636,24 +674,23 @@ function user_list(){
$("body").on('click', '.userlist', function(){
var username = $(this).attr('username')
userservicegrouplist(username)
servergroup_list_all()
});


function userservicegrouplist(username){

var param = {
user: username
}

$.getJSON('/userservicegrouplist', param, function(data){

var htm=['<table class="table table-hover">'];
htm.push('<thead><tr><th>username</th><th>server group</th><th>permissions</th><th>operation</th></thead>');
htm.push('<tr>');
htm.push('<td>'+'<input type="text" readonly="true" id="current_user" class="form-control" value="'+username+'">'+'</td>');
htm.push('<td>'+'<input type="text" id="add_server_group" class="form-control" value="">'+'</td>');
htm.push('<td>'+'<div id="usergroupnamediv" class="sidebar-menu"></div>'+'</td>');
htm.push('<td>'+'<select class="form-control" id="add_permissions"><option value="developer" selected="selected">developer</option><option value="config">config</option><option value="online">online</option></select>'+'</td>');
htm.push('<td>'+'<button id="add_servergroup" class="btn btn-small btn-success">添加组</button>'+'</td>');
htm.push('<td>'+'<button id="add_servergroup" class="btn btn-small btn-success">添加组权限</button>'+'</td>');
htm.push('</tr>');
htm.push('<tr>');
htm.push('<td></td>');
Expand All @@ -666,7 +703,7 @@ function userservicegrouplist(username){
htm.push('<td>'+'<input type="text" readonly="true" class="form-control" value="'+username+'">'+'</td>');
htm.push('<td>'+'<input type="text" readonly="true" class="form-control" value="'+data[i][0]+'">'+'</td>');
htm.push('<td>'+'<input type="text" readonly="true" class="form-control" value="'+data[i][1]+'">'+'</td>');
htm.push('<td>'+'<button id="delete_servergroup" username="'+username+'" servergroup="'+data[i][0]+'" class="btn btn-small btn-danger">删除组</button>'+'</td>');
htm.push('<td>'+'<button id="delete_servergroup" username="'+username+'" servergroup="'+data[i][0]+'" class="btn btn-small btn-danger">删除组权限</button>'+'</td>');
htm.push('</tr>');
}

Expand All @@ -680,6 +717,16 @@ function userservicegrouplist(username){
}


function servergroup_list_all(){
$.getJSON('/group_list_all', function(data){
var htm=['<select class="form-control" id="add_server_group">'];
for(var i=0,len=data.length; i<len; i++){
htm.push('<option value="'+data[i]+'">'+data[i]+'</option>');
}
htm.push('</select>');
$('#usergroupnamediv').html(htm.join(''));
})
};

function project_info(p){
var status = $('#project_div').attr('status')
Expand Down
14 changes: 12 additions & 2 deletions app/templates/useradmin.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

{% block loading %}
<script type="text/javascript"> user_list(); </script>
<script type="text/javascript"> group_list_all(); </script>
{% endblock %}


Expand All @@ -23,11 +24,20 @@
<tr>
<td><input type="text" id="addusername" class="form-control" value=""></td>
<td><input type="text" id="addpassword" class="form-control" value=""></td>
<td><button id="add_user" class="btn btn-small btn-success">添用户</button></td>
<td><button id="add_user" class="btn btn-small btn-success">添加用户</button></td>
</tr>
</table>
</div>
<div id="add_servergroup" class="sidebar-menu">
<div class="sidebar-menu">
<table class="table table-hover">
<thead><tr><th>addgroupname</th><th>添加服务组</th><th>delgroupname</th><th>删除服务组</th></thead>
<tr>
<td><input type="text" id="addgroupname" class="form-control" value=""></td>
<td><button id="add_group" class="btn btn-small btn-success">添服务加组</button></td>
<td><div id="groupnamediv" class="sidebar-menu"><input type="text" id="selectgroup" class="form-control" value=""></div></td>
<td><button id="del_group" class="btn btn-small btn-danger">删服务除组</button></td>
</tr>
</table>
</div>
<div id="servergroup_list" class="sidebar-menu">
</div>
Expand Down

0 comments on commit 0417fe3

Please sign in to comment.