提交 6d15ede0 编写于 作者: G gongfuxiang

项目操作地址与上线地址分开

上级 6786d91d
...@@ -9,3 +9,4 @@ Logs ...@@ -9,3 +9,4 @@ Logs
logs logs
*.lock *.lock
*.pyc *.pyc
temp_project
\ No newline at end of file
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
# ============================================================ # ============================================================
from django.http import HttpResponse from django.http import HttpResponse
from django.conf import settings
import json import json
# 特殊字符处理(超出ascii处理的范围) # 特殊字符处理(超出ascii处理的范围)
...@@ -16,6 +17,17 @@ import sys ...@@ -16,6 +17,17 @@ import sys
reload(sys) reload(sys)
sys.setdefaultencoding('utf8') 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 # @author Devil
# @version 0.0.1 # @version 0.0.1
......
...@@ -94,17 +94,29 @@ def save(request) : ...@@ -94,17 +94,29 @@ def save(request) :
if os.path.exists(git_dir_address) == True : if os.path.exists(git_dir_address) == True :
shutil.rmtree(git_dir_address) shutil.rmtree(git_dir_address)
# 目录不存在则创建 # 项目实际地址-目录不存在则创建
if os.path.exists(request.POST['dir_address']) == False : if os.path.exists(request.POST['dir_address']) == False :
os.mkdir(request.POST['dir_address']) os.mkdir(request.POST['dir_address'])
# 创建分支 # 项目实际地址-克隆代码
if os.path.exists(git_dir_address) == False : if os.path.exists(git_dir_address) == False :
(status, output) = commands.getstatusoutput('cd '+request.POST['dir_address']+';git clone '+request.POST['git_ssh_address']) (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 : if status != 0 :
return function.ajax_return_exit('git克隆失败', -1, [], output) return function.ajax_return_exit('git克隆失败', -10, [], output)
# 等于0则添加 # 等于0则添加
if project_id == '0' : if project_id == '0' :
......
...@@ -90,7 +90,8 @@ def get_branch_list(request) : ...@@ -90,7 +90,8 @@ def get_branch_list(request) :
data = Project.objects.filter(project_id=project_id).first() data = Project.objects.filter(project_id=project_id).first()
if data != None : 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: if os.path.exists(git_dir_address) == False:
return function.ajax_return_exit('项目路径地址不存在', -2) return function.ajax_return_exit('项目路径地址不存在', -2)
...@@ -126,7 +127,7 @@ def get_version_list(request) : ...@@ -126,7 +127,7 @@ def get_version_list(request) :
data = Project.objects.filter(project_id=project_id).first() data = Project.objects.filter(project_id=project_id).first()
if data != None : 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: if os.path.exists(git_dir_address) == False:
return function.ajax_return_exit('项目路径地址不存在', -1) return function.ajax_return_exit('项目路径地址不存在', -1)
...@@ -145,6 +146,11 @@ def get_version_list(request) : ...@@ -145,6 +146,11 @@ def get_version_list(request) :
if status != 0 : if status != 0 :
return function.ajax_return_exit('git分支切换失败', -12, [], output) 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') (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 : if status == 0 :
...@@ -232,42 +238,70 @@ def handle_release(request) : ...@@ -232,42 +238,70 @@ def handle_release(request) :
if status != 0 : if status != 0 :
return function.ajax_return_exit('git清除本地改动项失败', -10, [], output) return function.ajax_return_exit('git清除本地改动项失败', -10, [], output)
# 备份当前代码 # 上线操作
backup_name = 'backup_'+time.strftime('%Y%m%d%H%M%S', time.localtime()) if handle_type == 1 :
(status, output) = commands.getstatusoutput('cd '+git_dir_address+';git branch '+backup_name+';git push origin '+backup_name) # 备份当前代码
if status != 0 : backup_name = 'backup_'+time.strftime('%Y%m%d%H%M%S', time.localtime())
return function.ajax_return_exit('备份失败', -11, [], output) (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) (status, output) = commands.getstatusoutput('cd '+git_dir_address+';git push origin '+backup_name)
if status != 0 : if status != 0 :
return function.ajax_return_exit('git拉取远程分支失败', -12, [], output) return function.ajax_return_exit('git备份到远程仓库失败', -12, [], output)
# 分支切换 # 拉取远程分支最新代码
(status, output) = commands.getstatusoutput('cd '+git_dir_address+';git checkout '+release.branch) (status, output) = commands.getstatusoutput('cd '+git_dir_address+';git fetch origin '+release.branch)
if status != 0 : if status != 0 :
return function.ajax_return_exit('git分支切换失败', -13, [], output) 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更新到工单指定分支与版本 # git更新到工单指定分支与版本
(status, output) = commands.getstatusoutput('cd '+git_dir_address+';git reset --hard '+release.version) (status, output) = commands.getstatusoutput('cd '+git_dir_address+';git reset --hard '+release.version)
if status != 0 : if status != 0 :
return function.ajax_return_exit('上线失败', -100, [], output) 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 : 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) (status, output) = commands.getstatusoutput('cd '+git_dir_address+';git checkout '+release.backup_name)
if status != 0 : 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 : else :
return function.ajax_return_exit('上线工单类型错误', -99) return function.ajax_return_exit('上线工单类型错误', -99)
# 更新工单数据 # 默认失败返回
if handle_type == 1 : return function.ajax_return_exit('操作失败', -1000)
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)
...@@ -2,8 +2,9 @@ ...@@ -2,8 +2,9 @@
# 引入模板视图类库 # 引入模板视图类库
from django.shortcuts import render from django.shortcuts import render
from django.conf import settings
def index(request) : def index(request) :
context = {} context = {}
context['hello'] = 'Hello World!' context['hello'] = 'Hello World!'+settings.BASE_DIR
return render(request, 'welcome/index.html', context) return render(request, 'welcome/index.html', context)
\ No newline at end of file
{% include "../base/header.html" %} {% include "../base/header.html" %}
<div id="app" v-loading="loading" element-loading-text="加载中..."> <div id="app" v-loading="loading" element-loading-text="处理中...">
<form class="search-form" action="" method="post"> <form class="search-form" action="" method="post">
<el-input placeholder="请输入搜索内容" v-model="search.keywords" class="keywords"> <el-input placeholder="请输入搜索内容" v-model="search.keywords" class="keywords">
<el-button slot="append" icon="search" @click="get_project_list"></el-button> <el-button slot="append" icon="search" @click="get_project_list"></el-button>
......
{% include "../base/header.html" %} {% include "../base/header.html" %}
<div id="app" v-loading="loading" element-loading-text="加载中..."> <div id="app" v-loading="loading" element-loading-text="处理中...">
<form class="search-form" action="" method="post"> <form class="search-form" action="" method="post">
<el-input placeholder="请输入搜索内容" v-model="search.keywords" class="keywords"> <el-input placeholder="请输入搜索内容" v-model="search.keywords" class="keywords">
<el-button slot="append" icon="search" @click="get_release_list"></el-button> <el-button slot="append" icon="search" @click="get_release_list"></el-button>
......
welcome {{ hello }}
\ No newline at end of file \ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册