diff --git a/apps/cicd/views.py b/apps/cicd/views.py index 422106d9..597edfab 100644 --- a/apps/cicd/views.py +++ b/apps/cicd/views.py @@ -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) @@ -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':"
 " + result[1].replace('<br>','\n') + "
"}) diff --git a/dao/cicd.py b/dao/cicd.py index 1d42de78..4947f9cd 100644 --- a/dao/cicd.py +++ b/dao/cicd.py @@ -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): @@ -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') @@ -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) #查询项目部署成员 @@ -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): @@ -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: diff --git a/static/js/cicd/apps.js b/static/js/cicd/apps.js index 7f1ad8f0..e1436a7a 100644 --- a/static/js/cicd/apps.js +++ b/static/js/cicd/apps.js @@ -421,15 +421,15 @@ $(document).ready(function() { } else { var run = '' } - if (row.project_repertory=='git' && row.project_model == 'branch'){ +/* if (row.project_repertory=='git' && row.project_model == 'branch'){ var branch = '' } else{ var branch = '' - } + }*/ return '
' + run + '' + - branch + +// branch + '' + '
'; }, diff --git a/utils/deploy/git.py b/utils/deploy/git.py index 54355d9e..465a4afa 100644 --- a/utils/deploy/git.py +++ b/utils/deploy/git.py @@ -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) @@ -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)