Skip to content

Commit

Permalink
修复代码部署模块功能,初始化只有master分支的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
welliamcao committed Apr 18, 2020
1 parent 94d20d8 commit 13ac6bf
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 133 deletions.
136 changes: 52 additions & 84 deletions apps/cicd/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,85 +95,72 @@ def viewLogs(self,request):
result = []
project = self.get_apps(request)
logPaths = request.POST.get('log_path')
if project:
logPathsList = []
for path in project.project_logpath.split(";"):
if path not in logPathsList:logPathsList.append(path)
sList,resource = self.idSource(request.POST.get('server'))
if logPaths and sList and logPaths in logPathsList:
if request.POST.get('keywords'):module_args="""grep "{keywords}" {log_path}| tail -n 1000 """.format(keywords=request.POST.get('keywords'),log_path=logPaths)
else:module_args="""tail -n 1000 {log_path}""".format(log_path=logPaths)
ANS = ANSRunner(resource)
ANS.run_model(host_list=sList, module_name='raw', module_args=module_args)
result = ANS.handle_model_data(ANS.get_model_result(), 'raw', module_args)
else:
return JsonResponse({'msg':"日志文件与数据库记录不一致","code":500,'data':[]})
else:
return JsonResponse({'msg':"项目不存在","code":500,'data':[]})
logPathsList = []
for path in project.project_logpath.split(";"):
if path not in logPathsList:logPathsList.append(path)
sList,resource = self.idSource(request.POST.get('server'))
if logPaths and sList and logPaths in logPathsList:
if request.POST.get('keywords'):module_args="""grep "{keywords}" {log_path}| tail -n 1000 """.format(keywords=request.POST.get('keywords'),log_path=logPaths)
else:module_args="""tail -n 1000 {log_path}""".format(log_path=logPaths)
ANS = ANSRunner(resource)
ANS.run_model(host_list=sList, module_name='raw', module_args=module_args)
result = ANS.handle_model_data(ANS.get_model_result(), 'raw', module_args)
else:
return JsonResponse({'msg':"日志文件与数据库记录不一致","code":500,'data':[]})
return JsonResponse({'msg':"日志文件与数据库记录不一致","code":200,'data':result})

def status(self,request, *args, **kwagrs):
project = self.get_apps(request)
if project:
if project.project_status == 0:
logger.error(msg="项目未初始化:{id}".format(id=project.id))
return HttpResponseRedirect('/apps/list/')
if project.project_status == 0:
logger.error(msg="项目未初始化:{id}".format(id=project.id))
return HttpResponseRedirect('/apps/list/')

#获取最新版本
apps = DeployRunner(apps_id=project.id)
return render(request, 'cicd/cicd_status.html',{"user":request.user,'project':project,
'project_data':{
'type':project.project_model,
'bList':apps.list_branch(),
'tList': apps.list_tag(),
'number':self.get_apps_number(project),
}})

#获取最新版本
apps = DeployRunner(apps_id=project.id)
return render(request, 'cicd/cicd_status.html',{"user":request.user,'project':project,
'project_data':{
'type':project.project_model,
'bList':apps.list_branch(),
'tList': apps.list_tag(),
'number':self.get_apps_number(project),
}})
else:
return HttpResponseRedirect('/404/')

def query_repo(self,request):
project = self.get_apps(request)
if project:
apps = DeployRunner(apps_id=project.id)
apps.init_apps()
if project.project_model == 'branch':
result = apps.list_commits(branch=request.GET.get('name'))
return JsonResponse({'msg':"操作成功","code":200,'data':result})
else:
result = apps.list_tag()
return JsonResponse({'msg':"操作成功","code":200,'data':result})
apps = DeployRunner(apps_id=project.id)
apps.init_apps()
if project.project_model == 'branch':
result = apps.list_commits(branch=request.GET.get('name'))
return JsonResponse({'msg':"操作成功","code":200,'data':result})
else:
return JsonResponse({'msg':"项目不存在","code":500,'data':[]})
result = apps.list_tag()
return JsonResponse({'msg':"操作成功","code":200,'data':result})


def refresh_branch(self,request):
project = self.get_apps(request)
if project:
apps = DeployRunner(apps_id=project.id)
apps.init_apps()
result = apps.list_branch()
return JsonResponse({'msg':"操作成功","code":200,'data':result})
else:
return JsonResponse({'msg':"项目不存在","code":500,'data':[]})
project = self.get_apps(request)
apps = DeployRunner(apps_id=project.id)
apps.init_apps()
result = apps.list_branch()
return JsonResponse({'msg':"操作成功","code":200,'data':result})


def refresh_commit(self,request):
project = self.get_apps(request)
if project:
apps = DeployRunner(apps_id=project.id)
apps.init_apps()
result = apps.list_commits(branch=request.GET.get('name'))
return JsonResponse({'msg':"操作成功","code":200,'data':result})
else:
return JsonResponse({'msg':"项目不存在","code":500,'data':[]})
project = self.get_apps(request)
apps = DeployRunner(apps_id=project.id)
apps.init_apps()
result = apps.list_commits(branch=request.GET.get('name'))
return JsonResponse({'msg':"操作成功","code":200,'data':result})


def refresh_tag(self,request):
project = self.get_apps(request)
if project:
apps = DeployRunner(apps_id=project.id)
apps.init_apps()
result = apps.list_tag()
return JsonResponse({'msg':"操作成功","code":200,'data':result})
else:
return JsonResponse({'msg':"项目不存在","code":500,'data':[]})
project = self.get_apps(request)
apps = DeployRunner(apps_id=project.id)
apps.init_apps()
result = apps.list_tag()
return JsonResponse({'msg':"操作成功","code":200,'data':result})


def download_package(self,request):
project = self.get_apps(request)
Expand All @@ -187,27 +174,8 @@ def download_package(self,request):
return JsonResponse({'msg':"项目或者任务不存在","code":500,'data':[]})


@method_decorator_adaptor(permission_required, "cicd.project_change_project_config","/403/")
def create_branch(self,request):
version,project = self.apps_type(request)
if project.project_model == 'branch':
result = version.createBranch(path=project.project_repo_dir,branchName=request.POST.get('name'))
elif request.project_model == 'tag':
result = version.createTag(path=project.project_repo_dir,tagName=request.POST.get('name'))
if result[0] > 0:return JsonResponse({'msg':result[1],"code":500,'data':[]})
else:return JsonResponse({'msg':"操作成功","code":200,'data':[]})

@method_decorator_adaptor(permission_required, "cicd.project_delete_project_config","/403/")
def delete_branch(self,request):
version,project = self.apps_type(request)
if project.project_model == 'branch':result = version.delBranch(path=project.project_repo_dir,branchName=request.POST.get('name'))
elif project.project_model == 'tag':result = version.delTag(path=project.project_repo_dir,tagName=request.POST.get('name'))
if result[0] > 0:return JsonResponse({'msg':result[1],"code":500,'data':[]})
else:return JsonResponse({'msg':"操作成功","code":200,'data':[]})


def histroy(self,request):
version,project = self.apps_type(request)
version, project = self.apps_type(request)
result = version.show(path=project.project_repo_dir,branch=request.GET.get('project_branch'),cid=request.POST.get('project_version',None))
return JsonResponse({'msg':"操作成功","code":200,'data':"<pre> <xmp>" + result[1].replace('<br>','\n') + "</xmp></pre>"})

Expand Down
89 changes: 45 additions & 44 deletions dao/cicd.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from utils import base
import uuid,random,json
from cicd.service.deploy import DeployRunner
from django.http import Http404

class AppsCount:
def __init__(self):
Expand Down Expand Up @@ -127,7 +128,7 @@ def get_apps(self,request):
return deploy
except Exception as ex:
logger.warn(msg="获取部署项目失败: {ex}".format(ex=ex))
return False
raise Http404

def get_task(self,request):
if request.method == 'GET':cid = request.GET.get('tasks_id')
Expand All @@ -143,11 +144,11 @@ def get_task(self,request):

def apps_type(self,request):
project = self.get_apps(request)
if project:
if project.project_repertory == 'git':
return GitTools(),project
elif project.project_repertory == 'svn':
return SvnTools(),project
# if project:
if project.project_repertory == 'git':
return GitTools(),project
elif project.project_repertory == 'svn':
return SvnTools(),project
return (None,None)

#查询项目部署成员
Expand All @@ -172,26 +173,26 @@ def get_apps_number(self,project):
#项目信息
def info_apps(self,request):
project = self.get_apps(request)
if project:
data = self.convert_to_dict(project)
# if project:
data = self.convert_to_dict(project)
# data['project_id'] = Project_Assets.objects.get(id=data['project_id']).project_name
# data['service_name'] = Service_Assets.objects.get(id=data['project_service']).service_name
data['number'] = self.get_apps_number(project) #member
data['roles'] = self.get_role(project)
data["project_servers"] = json.loads(project.project_servers)
return data
return '项目不存在'
data['number'] = self.get_apps_number(project) #member
data['roles'] = self.get_role(project)
data["project_servers"] = json.loads(project.project_servers)
return data
# return '项目不存在'

#初始化项目
def init_apps(self,request):
project = self.get_apps(request)
if project:
if project.project_status == 1:return '项目已经初始化过'
DeployRunner(apps_id=project.id).init_apps()
project.project_status = 1
project.save()
return project
return '项目不存在'
# if project:
if project.project_status == 1:return '项目已经初始化过'
DeployRunner(apps_id=project.id).init_apps()
project.project_status = 1
project.save()
return project
# return '项目不存在'

#添加项目
def create_apps(self,request):
Expand Down Expand Up @@ -228,30 +229,30 @@ def create_apps(self,request):

def update_apps(self,request):
project = self.get_apps(request)
if project:
try:
project.project_env = request.POST.get('project_env')
project.project_type = request.POST.get('project_type')
project.project_repertory = request.POST.get('project_repertory')
project.project_address = request.POST.get('project_address')
project.project_remote_command = request.POST.get('project_remote_command')
project.project_pre_remote_command = request.POST.get('project_pre_remote_command')
project.project_local_command = request.POST.get('project_local_command')
project.project_model = request.POST.get('project_model')
project.project_dir = request.POST.get('project_dir')
project.project_user = request.POST.get('project_user')
project.project_is_include = request.POST.get('project_is_include')
project.project_exclude = request.POST.get('project_exclude')
project.project_repo_user = request.POST.get('project_repo_user')
project.project_repo_passwd = request.POST.get('project_repo_passwd')
project.project_servers = json.dumps(list(filter(None, request.POST.get('project_servers').split(','))))
project.project_target_root = request.POST.get('project_target_root')
project.project_logpath = request.POST.get('project_logpath')
project.save()
except Exception as ex:
logger.warn(msg="修改项目部署失败: {ex}".format(ex=ex))
return "修改项目部署失败: {ex}".format(ex=ex)
return project
# if project:
try:
project.project_env = request.POST.get('project_env')
project.project_type = request.POST.get('project_type')
project.project_repertory = request.POST.get('project_repertory')
project.project_address = request.POST.get('project_address')
project.project_remote_command = request.POST.get('project_remote_command')
project.project_pre_remote_command = request.POST.get('project_pre_remote_command')
project.project_local_command = request.POST.get('project_local_command')
project.project_model = request.POST.get('project_model')
project.project_dir = request.POST.get('project_dir')
project.project_user = request.POST.get('project_user')
project.project_is_include = request.POST.get('project_is_include')
project.project_exclude = request.POST.get('project_exclude')
project.project_repo_user = request.POST.get('project_repo_user')
project.project_repo_passwd = request.POST.get('project_repo_passwd')
project.project_servers = json.dumps(list(filter(None, request.POST.get('project_servers').split(','))))
project.project_target_root = request.POST.get('project_target_root')
project.project_logpath = request.POST.get('project_logpath')
project.save()
except Exception as ex:
logger.warn(msg="修改项目部署失败: {ex}".format(ex=ex))
return "修改项目部署失败: {ex}".format(ex=ex)
# return project

def create_apps_log(self,project,version,user,uuid,content,status='failed',type="deploy"):
try:
Expand Down
6 changes: 3 additions & 3 deletions static/js/cicd/apps.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,15 +421,15 @@ $(document).ready(function() {
} else {
var run = '<button type="button" name="btn-project-run" value="'+ row.id +'" class="btn btn-default" aria-label="Justify"><a href="/apps/manage/?type=status&id='+ row.id + '"><span class="fa fa-play" aria-hidden="true"></span></a>' + '</button>'
}
if (row.project_repertory=='git' && row.project_model == 'branch'){
/* if (row.project_repertory=='git' && row.project_model == 'branch'){
var branch = '<button type="button" name="btn-project-branch" value="'+ row.id +'" class="btn btn-default" aria-label="Justify"><span class="fa fa-github-alt" aria-hidden="true"></span></button>'
} else{
var branch = '<button type="button" name="btn-project-branch" value="'+ row.id +'" class="btn btn-default disabled" aria-label="Justify"><span class="fa fa-github-alt" aria-hidden="true"></span></button>'
}
}*/
return '<div class="btn-group btn-group-sm">' +
run +
'<button type="button" name="btn-project-edit" value="'+ row.id +'" class="btn btn-default" aria-label="Justify"><a href="/apps/config/?type=edit&id='+ row.id +'"><span class="fa fa-pencil-square-o" aria-hidden="true"></span></a>' + '</button>' +
branch +
// branch +
'<button type="button" name="btn-project-delete" value="'+ row.id +'" class="btn btn-default" aria-label="Justify"><span class="fa fa-trash" aria-hidden="true"></span>' + '</button>' +
'</div>';
},
Expand Down
4 changes: 2 additions & 2 deletions utils/deploy/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def is_git_dir(self):
return False

def clone(self, url):
return Repo.clone_from(url, self.path, depth=1)
return Repo.clone_from(url, self.path)

def pull(self):
repo = Repo(self.path)
Expand Down Expand Up @@ -82,4 +82,4 @@ def rsync_to_release_version(self, dest_path):
return {"status":"succeed","msg":result}

def mkdir(self,dir):
if os.path.exists(dir) is False:os.makedirs(dir)
if dir and os.path.exists(dir) is False:os.makedirs(dir)

0 comments on commit 13ac6bf

Please sign in to comment.