From 6d15ede0d043f8906b2fb2b278b9ed7b0ec7d76a Mon Sep 17 00:00:00 2001 From: gongfuxiang Date: Thu, 17 Aug 2017 16:28:04 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E6=93=8D=E4=BD=9C=E5=9C=B0?= =?UTF-8?q?=E5=9D=80=E4=B8=8E=E4=B8=8A=E7=BA=BF=E5=9C=B0=E5=9D=80=E5=88=86?= =?UTF-8?q?=E5=BC=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + mogui/common/function.py | 12 +++++ mogui/controller/project.py | 20 +++++-- mogui/controller/release.py | 90 ++++++++++++++++++++++---------- mogui/controller/welcome.py | 3 +- mogui/view/v1/project/index.html | 2 +- mogui/view/v1/release/index.html | 2 +- mogui/view/v1/welcome/index.html | 2 +- 8 files changed, 96 insertions(+), 36 deletions(-) diff --git a/.gitignore b/.gitignore index 7a102a1..849d19e 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ Logs logs *.lock *.pyc +temp_project \ No newline at end of file diff --git a/mogui/common/function.py b/mogui/common/function.py index beaaee3..64bfa90 100644 --- a/mogui/common/function.py +++ b/mogui/common/function.py @@ -9,6 +9,7 @@ # ============================================================ from django.http import HttpResponse +from django.conf import settings import json # 特殊字符处理(超出ascii处理的范围) @@ -16,6 +17,17 @@ import sys reload(sys) sys.setdefaultencoding('utf8') + +# 获取项目临时操作地址 +# @author Devil +# @version 0.0.1 +# @blog http://gong.gg/ +# @date 2017-08-04 +# @return [string] [项目临时操作地址] +def get_project_handle_temp_dir() : + return settings.BASE_DIR+'/temp_project' + + # 获取项目名称 # @author Devil # @version 0.0.1 diff --git a/mogui/controller/project.py b/mogui/controller/project.py index 54b7377..a441988 100644 --- a/mogui/controller/project.py +++ b/mogui/controller/project.py @@ -94,17 +94,29 @@ def save(request) : if os.path.exists(git_dir_address) == True : shutil.rmtree(git_dir_address) - # 目录不存在则创建 + # 项目实际地址-目录不存在则创建 if os.path.exists(request.POST['dir_address']) == False : os.mkdir(request.POST['dir_address']) - # 创建分支 + # 项目实际地址-克隆代码 if os.path.exists(git_dir_address) == False : (status, output) = commands.getstatusoutput('cd '+request.POST['dir_address']+';git clone '+request.POST['git_ssh_address']) + if status != 0 : + return function.ajax_return_exit('git克隆失败', -10, [], output) + + # 临时操作地址 + project_temp_dir = function.get_project_handle_temp_dir() + project_git_name = function.get_git_ssh_name(request.POST['git_ssh_address']) + + # 临时操作地址-目录不存在则创建 + if os.path.exists(project_temp_dir) == False : + os.mkdir(project_temp_dir) - # 项目是否拉取成功 + # 临时操作地址-克隆代码 + if os.path.exists(project_temp_dir+'/'+project_git_name) == False : + (status, output) = commands.getstatusoutput('cd '+project_temp_dir+';git clone '+request.POST['git_ssh_address']) if status != 0 : - return function.ajax_return_exit('git克隆失败', -1, [], output) + return function.ajax_return_exit('git克隆失败', -10, [], output) # 等于0则添加 if project_id == '0' : diff --git a/mogui/controller/release.py b/mogui/controller/release.py index 0982481..ec6ff52 100644 --- a/mogui/controller/release.py +++ b/mogui/controller/release.py @@ -90,7 +90,8 @@ def get_branch_list(request) : data = Project.objects.filter(project_id=project_id).first() if data != None : # 获取项目名称 - git_dir_address = function.get_git_address(data.dir_address, data.git_ssh_address) + git_dir_address = function.get_git_address(function.get_project_handle_temp_dir(), data.git_ssh_address) + if os.path.exists(git_dir_address) == False: return function.ajax_return_exit('项目路径地址不存在', -2) @@ -126,7 +127,7 @@ def get_version_list(request) : data = Project.objects.filter(project_id=project_id).first() if data != None : # 获取项目名称 - git_dir_address = function.get_git_address(data.dir_address, data.git_ssh_address) + git_dir_address = function.get_git_address(function.get_project_handle_temp_dir(), data.git_ssh_address) if os.path.exists(git_dir_address) == False: return function.ajax_return_exit('项目路径地址不存在', -1) @@ -145,6 +146,11 @@ def get_version_list(request) : if status != 0 : return function.ajax_return_exit('git分支切换失败', -12, [], output) + # 拉取分支最新代码到本地分支 + (status, output) = commands.getstatusoutput('cd '+git_dir_address+';git pull origin '+branch) + if status != 0 : + return function.ajax_return_exit('git拉取分支代码失败', -13, [], output) + # 获取版本列表 (status, output) = commands.getstatusoutput('cd '+git_dir_address+';git log --pretty=format:"%h{|}%s{|}[%cd]{|}<%an>" --date=format:"%Y-%m-%d %H:%M:%S" -30') if status == 0 : @@ -232,42 +238,70 @@ def handle_release(request) : if status != 0 : return function.ajax_return_exit('git清除本地改动项失败', -10, [], output) - # 备份当前代码 - backup_name = 'backup_'+time.strftime('%Y%m%d%H%M%S', time.localtime()) - (status, output) = commands.getstatusoutput('cd '+git_dir_address+';git branch '+backup_name+';git push origin '+backup_name) - if status != 0 : - return function.ajax_return_exit('备份失败', -11, [], output) + # 上线操作 + if handle_type == 1 : + # 备份当前代码 + backup_name = 'backup_'+time.strftime('%Y%m%d%H%M%S', time.localtime()) + (status, output) = commands.getstatusoutput('cd '+git_dir_address+';git branch '+backup_name) + if status != 0 : + return function.ajax_return_exit('git创建备份失败', -11, [], output) - # 拉取远程分支最新代码 - (status, output) = commands.getstatusoutput('cd '+git_dir_address+';git fetch origin '+release.branch) - if status != 0 : - return function.ajax_return_exit('git拉取远程分支失败', -12, [], output) + # 备份分支推送到远程仓库 + (status, output) = commands.getstatusoutput('cd '+git_dir_address+';git push origin '+backup_name) + if status != 0 : + return function.ajax_return_exit('git备份到远程仓库失败', -12, [], output) - # 分支切换 - (status, output) = commands.getstatusoutput('cd '+git_dir_address+';git checkout '+release.branch) - if status != 0 : - return function.ajax_return_exit('git分支切换失败', -13, [], output) + # 拉取远程分支最新代码 + (status, output) = commands.getstatusoutput('cd '+git_dir_address+';git fetch origin '+release.branch) + if status != 0 : + return function.ajax_return_exit('git拉取远程分支失败', -13, [], output) + + # 分支切换 + (status, output) = commands.getstatusoutput('cd '+git_dir_address+';git checkout '+release.branch) + if status != 0 : + return function.ajax_return_exit('git分支切换失败', -14, [], output) + + # 拉取分支最新代码到本地分支 + (status, output) = commands.getstatusoutput('cd '+git_dir_address+';git pull origin '+release.branch) + if status != 0 : + return function.ajax_return_exit('git拉取分支代码失败', -15, [], output) - if handle_type == 1 : # git更新到工单指定分支与版本 (status, output) = commands.getstatusoutput('cd '+git_dir_address+';git reset --hard '+release.version) if status != 0 : return function.ajax_return_exit('上线失败', -100, [], output) + + # 更新工单数据 + Release.objects.filter(release_id=release_id).update( + status=1, + backup_name=backup_name + ) + return function.ajax_return_exit('上线成功') + + # 回滚操作 elif handle_type == 2 : + # 拉取远程分支最新代码 + (status, output) = commands.getstatusoutput('cd '+git_dir_address+';git fetch origin '+release.backup_name) + if status != 0 : + return function.ajax_return_exit('git远程备份分支拉取失败', -13, [], output) + + # 分支切换 (status, output) = commands.getstatusoutput('cd '+git_dir_address+';git checkout '+release.backup_name) if status != 0 : - return function.ajax_return_exit('上线失败', -100, [], output) + return function.ajax_return_exit('git分支切换失败', -14, [], output) + + # 拉取分支最新代码到本地分支 + (status, output) = commands.getstatusoutput('cd '+git_dir_address+';git pull origin '+release.backup_name) + if status != 0 : + return function.ajax_return_exit('回滚失败', -100, [], output) + + # 更新工单数据 + Release.objects.filter(release_id=release_id).update(status=2) + return function.ajax_return_exit('回滚成功') + + # 操作类型有误 else : return function.ajax_return_exit('上线工单类型错误', -99) - # 更新工单数据 - if handle_type == 1 : - Release.objects.filter(release_id=release_id).update( - status=1, - backup_name=backup_name - ) - msg = '上线成功' - else : - Release.objects.filter(release_id=release_id).update(status=2) - msg = '回滚成功' - return function.ajax_return_exit(msg) + # 默认失败返回 + return function.ajax_return_exit('操作失败', -1000) diff --git a/mogui/controller/welcome.py b/mogui/controller/welcome.py index 65c3816..8ab79f8 100644 --- a/mogui/controller/welcome.py +++ b/mogui/controller/welcome.py @@ -2,8 +2,9 @@ # 引入模板视图类库 from django.shortcuts import render +from django.conf import settings def index(request) : context = {} - context['hello'] = 'Hello World!' + context['hello'] = 'Hello World!'+settings.BASE_DIR return render(request, 'welcome/index.html', context) \ No newline at end of file diff --git a/mogui/view/v1/project/index.html b/mogui/view/v1/project/index.html index 4bdb6bc..8c0b0e1 100644 --- a/mogui/view/v1/project/index.html +++ b/mogui/view/v1/project/index.html @@ -1,6 +1,6 @@ {% include "../base/header.html" %} -
+
diff --git a/mogui/view/v1/release/index.html b/mogui/view/v1/release/index.html index c6f58c0..59601d0 100644 --- a/mogui/view/v1/release/index.html +++ b/mogui/view/v1/release/index.html @@ -1,6 +1,6 @@ {% include "../base/header.html" %} -
+
diff --git a/mogui/view/v1/welcome/index.html b/mogui/view/v1/welcome/index.html index b6cbc2d..59caffb 100644 --- a/mogui/view/v1/welcome/index.html +++ b/mogui/view/v1/welcome/index.html @@ -1 +1 @@ -welcome \ No newline at end of file +{{ hello }} \ No newline at end of file -- GitLab