From 568207e3d2531712cd5d893693e8978398a468bf Mon Sep 17 00:00:00 2001 From: Mr Chen Date: Mon, 17 Dec 2018 11:06:58 +0800 Subject: [PATCH] update --- plugins/openresty/index.py | 60 ++++++++++++++++++++----------- plugins/openresty/js/openresty.js | 9 ++--- 2 files changed, 45 insertions(+), 24 deletions(-) diff --git a/plugins/openresty/index.py b/plugins/openresty/index.py index e4f9e651a..ec79632a3 100755 --- a/plugins/openresty/index.py +++ b/plugins/openresty/index.py @@ -64,6 +64,17 @@ def getConf(): return path +def getOs(): + data = {} + data['os'] = public.getOs() + ng_exe_bin = getServerDir() + "/nginx/sbin/nginx" + if checkAuthEq(ng_exe_bin, 'root'): + data['auth'] = True + else: + data['auth'] = False + return public.getJson(data) + + def getInitDTpl(): path = getPluginDir() + "/init.d/nginx.tpl" return path @@ -81,6 +92,21 @@ def makeConf(): shutil.copyfile(source_vhost, dest_vhost) +def getFileOwner(filename): + import pwd + stat = os.lstat(filename) + uid = stat.st_uid + pw = pwd.getpwuid(uid) + return pw.pw_name + + +def checkAuthEq(file, owner='root'): + fowner = getFileOwner(file) + if (fowner == owner): + return True + return False + + def confReplace(): service_path = os.path.dirname(os.getcwd()) content = public.readFile(getConf()) @@ -88,8 +114,9 @@ def confReplace(): user = 'www' user_group = 'www' - # macosx do + if public.getOs() == 'darwin': + # macosx do user = public.execShell( "who | sed -n '2, 1p' |awk '{print $1}'")[0].strip() # user = 'root' @@ -105,22 +132,17 @@ def confReplace(): public.writeFile(getServerDir() + '/nginx/conf/nginx.conf', content) - # exe_bin = getServerDir() + "/bin/openresty" - # print 'chown -R ' + user + ':' + user_group + ' ' + exe_bin - # print public.execShell('chown -R ' + user + ':' + user_group + ' ' + exe_bin) - # public.execShell('chmod 755 ' + exe_bin) - # public.execShell('chmod u+s ' + exe_bin) - + # give nginx root permission ng_exe_bin = getServerDir() + "/nginx/sbin/nginx" - - # args = getArgs() - # print args['pwd'] - # sudoPassword = args['pwd'] - # command = 'chown -R ' + 'root:' + user_group + ' ' + ng_exe_bin - # print os.system('echo %s|sudo -S %s' % (args['pwd'], command)) - # print os.system('echo %s|sudo -S %s' % (sudoPassword, 'chmod 755 ' + ng_exe_bin)) - # print os.system('echo %s|sudo -S %s' % (sudoPassword, 'chmod u+s ' + - # ng_exe_bin)) + if not checkAuthEq(ng_exe_bin, 'root'): + args = getArgs() + sudoPwd = args['pwd'] + cmd_own = 'chown -R ' + 'root:' + user_group + ' ' + ng_exe_bin + os.system('echo %s|sudo -S %s' % (sudoPwd, cmd_own)) + cmd_mod = 'chmod 755 ' + ng_exe_bin + os.system('echo %s|sudo -S %s' % (sudoPwd, cmd_mod)) + cmd_s = 'chmod u+s ' + ng_exe_bin + os.system('echo %s|sudo -S %s' % (sudoPwd, cmd_s)) def initDreplace(): @@ -150,7 +172,7 @@ def initDreplace(): def status(): data = public.execShell( - "ps -ef|grep openresty |grep -v grep | grep -v python | awk '{print $2}'") + "ps -ef|grep nginx |grep -v grep | grep -v python | awk '{print $2}'") if data[0] == '': return 'stop' return 'start' @@ -159,7 +181,6 @@ def status(): def start(): file = initDreplace() data = public.execShell(file + ' start') - print data if data[1] == '': return 'ok' return 'fail' @@ -168,7 +189,6 @@ def start(): def stop(): file = initDreplace() data = public.execShell(file + ' stop') - print data clearTemp() if data[1] == '': return 'ok' @@ -269,7 +289,7 @@ if __name__ == "__main__": elif func == 'conf': print getConf() elif func == 'get_os': - print public.getOs() + print getOs() elif func == 'run_info': print runInfo() elif func == 'error_log': diff --git a/plugins/openresty/js/openresty.js b/plugins/openresty/js/openresty.js index 729349f87..7c39df125 100755 --- a/plugins/openresty/js/openresty.js +++ b/plugins/openresty/js/openresty.js @@ -63,22 +63,23 @@ function orPluginOpService(a, b, v) { } layer.confirm( msgTpl('您真的要{1}{2}{3}服务吗?', [d,a,v]), {icon:3,closeBtn: 2}, function() { orPost('get_os',{},function(data){ - if (data.data == 'darwin'){ + var rdata = $.parseJSON(data.data); + if (rdata['os'] == 'darwin' && !rdata['auth']){ layer.prompt({title: '检查到权限不足,需要输入密码!', formType: 1},function(pwd, index){ layer.close(index); var data = {'pwd':pwd}; c += '&args='+JSON.stringify(data); - orPluginOpServiceOp(a,c,d,a,v); + orPluginOpServiceOp(a,b,c,d,a,v); }); } else { - orPluginOpServiceOp(a,c,d,a,v); + orPluginOpServiceOp(a,b,c,d,a,v); } }); }) } -function orPluginOpServiceOp(a,c,d,a,v){ +function orPluginOpServiceOp(a,b,c,d,a,v){ var e = layer.msg(msgTpl('正在{1}{2}{3}服务,请稍候...',[d,a,v]), {icon: 16,time: 0}); $.post("/plugins/run", c, function(g) { layer.close(e);