diff --git a/plugins/gitea/hook/commit.tpl b/plugins/gitea/hook/commit.tpl index 2f26a67e3..727a2b9cc 100755 --- a/plugins/gitea/hook/commit.tpl +++ b/plugins/gitea/hook/commit.tpl @@ -30,7 +30,8 @@ unset GIT_DIR -cd $GIT_PROJECT_DIR && git pull +# cd $GIT_PROJECT_DIR && git pull +cd $GIT_PROJECT_DIR && sudo git pull # func 2 # cd $GIT_PROJECT_DIR && env -i git pull origin master diff --git a/plugins/webhook/index.html b/plugins/webhook/index.html index b50d38710..0de594878 100755 --- a/plugins/webhook/index.html +++ b/plugins/webhook/index.html @@ -14,11 +14,11 @@ 名称 - 添加时间 - 近期调用 - 调用次数 - 密钥 - 操作 + 添加时间 + 近期调用 + 调用次数 + 密钥 + 操作 diff --git a/plugins/webhook/index.py b/plugins/webhook/index.py index 15189f727..9d92ee009 100755 --- a/plugins/webhook/index.py +++ b/plugins/webhook/index.py @@ -97,7 +97,30 @@ def addHook(): hook = {} hook['title'] = args['title'] - hook['access_key'] = mw.getRandomString(48) + if hook['title'] == '': + return mw.returnJson(False, '名称不能为空!') + + hook['access_key'] = mw.md5(hook['title']) + hook['count'] = 0 + hook['addtime'] = int(time.time()) + hook['uptime'] = 0 + + script_dir = getServerDir() + "/scripts" + if not os.path.exists(script_dir): + os.mkdir(script_dir) + + addCfg(hook) + shellFile = script_dir + '/' + hook['access_key'] + mw.writeFile(shellFile, args['shell']) + return mw.returnJson(True, '添加成功!') + +def addHookShell(args): + if args['title'] == '': + return mw.returnJson(False, '名称不能为空!') + + hook = {} + hook['title'] = args['title'] + hook['access_key'] = mw.md5(hook['title']) hook['count'] = 0 hook['addtime'] = int(time.time()) hook['uptime'] = 0 @@ -129,7 +152,7 @@ def getLog(): logPath = args['path'] - content = mw.getLastLine(logPath, 16) + content = mw.getLastLine(logPath, 100) return mw.returnJson(True, 'ok', content) @@ -155,6 +178,21 @@ def runShellArgs(args): return mw.returnJson(True, '运行成功!') return mw.returnJson(False, '指定Hook不存在!') +def getRunShellCmd(): + args = getArgs() + check_arg = checkArgs(args, ['access_key']) + if not check_arg[0]: + return check_arg[1] + + script_dir = getServerDir() + "/scripts" + shellFile = script_dir + '/' + args['access_key'] + param = '' + if 'params' in args: + param = args['params'] + param = re.sub("\"", '', param) + cmd = "bash {} {}".format(shellFile, param) + return mw.returnJson(True, 'ok', cmd) + def runShell(): args = getArgs() @@ -191,6 +229,39 @@ def delHook(): mw.writeFile(jsonFile, json.dumps(newdata)) return mw.returnJson(True, '删除成功!') + +def contentReplace(content): + service_path = mw.getServerDir() + content = content.replace('{$ROOT_PATH}', mw.getFatherDir()) + return content + + +def configTpl(): + path = getPluginDir() + '/tpl' + pathFile = os.listdir(path) + tmp = [] + for one in pathFile: + file = path + '/' + one + tmp.append(file) + return mw.getJson(tmp) + +def readConfigTpl(): + args = getArgs() + data = checkArgs(args, ['file', 'title']) + if not data[0]: + return data[1] + + if args['title'] == '': + return mw.returnJson(False, '名称不能为空!') + + content = mw.readFile(args['file']) + content = contentReplace(content) + + content = content.replace('{$REPO}', args['title']) + content = content.replace('{$REPO_NAME}', mw.md5(args['title'])) + + return mw.returnJson(True, 'ok', content) + if __name__ == "__main__": func = sys.argv[1] if func == 'status': @@ -201,9 +272,15 @@ if __name__ == "__main__": print(getList()) elif func == "run_shell": print(runShell()) + elif func == 'run_shell_cmd': + print(getRunShellCmd()) elif func == 'del_hook': print(delHook()) elif func == 'get_log': print(getLog()) + elif func == 'config_tpl': + print(configTpl()) + elif func == 'read_config_tpl': + print(readConfigTpl()) else: print('error') diff --git a/plugins/webhook/js/webhook.js b/plugins/webhook/js/webhook.js index 231219989..72111a09f 100755 --- a/plugins/webhook/js/webhook.js +++ b/plugins/webhook/js/webhook.js @@ -26,7 +26,6 @@ function whPost(method, args,callback){ },'json'); } - function whPostNoMessage(method, args,callback){ var req_data = {}; @@ -79,29 +78,43 @@ function whPostCallbak(method, version, args,callback){ },'json'); } +function getFileName(file){ + var list = file.split('/'); + var f = list[list.length-1]; + return f; +} + var whEditor = null; //添加 function addHook(){ layer.open({ type: 1, - area: '600px', + area: '650px', title: '添加Hook', - closeBtn: 1, + closeBtn: 2, shift: 5, shadeClose: false, btn:['提交','关闭'], content: "
\
\ - 名称\ -
\ + 名称\ +
\
\
\ - 执行脚本\ -
\ - \ + 执行脚本\ +
\ + \ +
\ +
\ +
\ + 模板选择\ +
\ + \ +
\
\ -
\
", success:function(){ @@ -121,6 +134,51 @@ function addHook(){ $(".CodeMirror-scroll").css({"height":"300px","margin":0,"padding":0}); whEditor.focus(); + + + whPostNoMessage('config_tpl','', function(data){ + var rdata = $.parseJSON(data.data); + for (var i = 0; i < rdata.length; i++) { + $('#hook_tpl').append(''); + } + + $('#hook_tpl').change(function(){ + var selected = $(this).val(); + if (selected == '0') { + return; + } + + var title = $('#hook_title').val(); + var loadT = layer.msg('配置模版获取中...',{icon:16,time:0,shade: [0.3, '#000']}); + whPostNoMessage('read_config_tpl', {file:selected,title:title}, function(data){ + layer.close(loadT); + var rdata = $.parseJSON(data.data); + if (!rdata.status){ + layer.msg(rdata.msg,{icon:16,time:2000,shade: [5, '#000']}); + return; + } + + $("#hook_shell").empty().text(rdata.data); + $(".CodeMirror").remove(); + whEditor = CodeMirror.fromTextArea(document.getElementById("hook_shell"), { + extraKeys: { + "Ctrl-Space": "autocomplete", + "Ctrl-F": "findPersistent", + "Ctrl-H": "replaceAll", + "Ctrl-S": function() {} + }, + lineNumbers: true, + matchBrackets:true, + mode:"sh", + }); + + $(".CodeMirror-scroll").css({"height":"300px","margin":0,"padding":0}); + whEditor.focus(); + + }); + }); + }); + }, yes:function(indexs){ var loadT = layer.msg("提交中...",{icon:16,time:0}); @@ -128,7 +186,19 @@ function addHook(){ title: $("#hook_title").val(), shell: whEditor.getValue(), } - whPost('add_hook', data, function(rdata){ + // whPost('add_hook', data, function(rdata){ + // var rdata = $.parseJSON(rdata.data); + // if (!rdata.status){ + // layer.msg(rdata.msg,{icon:2}); + // return; + // } + // layer.close(indexs); + // showMsg(rdata.msg, function(){ + // getHookList(); + // }, {icon:1}, 2000); + // }); + + whPostCallbak('addHookShell', '', data, function(rdata){ var rdata = $.parseJSON(rdata.data); if (!rdata.status){ layer.msg(rdata.msg,{icon:2}); @@ -157,6 +227,7 @@ function getHookList(){ +''+mlist[i].count+'' +'查看密钥' +'测试 | ' + +'命令 | ' +'编辑 | ' +'日志 | ' +'删除' @@ -246,6 +317,36 @@ function runHook(key){ },{icon:1},2000); }); } + + +function getRunHookCmd(key) { + whPost('run_shell_cmd', {"access_key":key}, function(rdata){ + var rdata = $.parseJSON(rdata.data); + if (!rdata.status){ + layer.msg(rdata.msg,{icon:2}); + } + layer.open({ + title: "手动执行命令CMD", + area: ['600px', '180px'], + type:1, + closeBtn: 1, + shadeClose: false, + btn:["复制","取消"], + content: '
\ +
\ +
'+rdata.data+'
\ +
\ +
', + success:function(){ + copyText(rdata.data); + }, + yes:function(){ + copyText(rdata.data); + } + }); + }); +} + //删除 function deleteHook(key, title){ layer.confirm('删除Hook-['+ title +']',{ diff --git a/plugins/webhook/tpl/git.tpl b/plugins/webhook/tpl/git.tpl new file mode 100644 index 000000000..e7f6776d0 --- /dev/null +++ b/plugins/webhook/tpl/git.tpl @@ -0,0 +1,28 @@ +echo "" > {$ROOT_PATH}/server/webhook/scripts/{$REPO_NAME}.log + +export HOME=/tmp +date "+%Y-%m-%d %H:%M:%S" + +git config --global credential.helper store +git config --global pull.rebase false + + +if [ ! -d {$ROOT_PATH}/gitcode ];then + mkdir -p {$ROOT_PATH}/gitcode +fi + +if [ -d {$ROOT_PATH}/gitcode/{$REPO} ];then + cd {$ROOT_PATH}/gitcode/{$REPO} && sudo git pull +else + cd {$ROOT_PATH}/gitcode && git clone http://0.0.0.0:6660/xx/{$REPO} +fi + +if [ ! -d {$ROOT_PATH}/wwwroot/{$REPO} ];then + mkdir -p {$ROOT_PATH}/wwwroot/{$REPO} +fi + +rsync -vauP '--exclude=.*' {$ROOT_PATH}/gitcode/{$REPO}/ {$ROOT_PATH}/wwwroot/{$REPO} +chown -R www:www {$ROOT_PATH}/wwwroot/{$REPO} + + +