def webhook_gitlab(request, pid, env_id): logger.info('request path:' + request.get_full_path()) logger.info('request header:') logger.info(pformat(request.META)) logger.info('request body:') try: gitlabService = WebhookRequestBodyOfGitlabService(request) request_body = gitlabService.get_body() logger.info(pformat(request_body)) except RuntimeError as e: logger.error(str(e)) logger.error(pformat(request.body)) return HttpResponse(str(e), status=500) except: logger.error('未知问题') return HttpResponse('未知问题', status=500) try: project = Project.objects.get(id=pid) except: return HttpResponseNotFound('项目没找到') if project.repository_url not in githubService.get_urls(): return HttpResponseNotFound('项目Url不匹配') try: env = Env.objects.get(id=env_id) except: return HttpResponseNotFound('环境没找到') try: projectEnvConfig = ProjectEnvConfig.objects.get(project=project, env=env) except: return HttpResponseNotFound('项目环境配置未匹配') events = ['push', 'merge_request'] if request_body['object_kind'] not in events: return HttpResponseNotFound('只支持push及merge_request事件') logger.info('branch_name:' + gitlabService.get_branch_name()) if projectEnvConfig.branch != gitlabService.get_branch_name(): logger.error('项目环境配置中分支名不匹配') return HttpResponseNotFound('项目环境配置中分支名不匹配') comment = gitlabService.get_comment() try: projectService = ProjectService(pid) creater = User.objects.get(username='******') task = projectService.create_task(env_id, creater, comment) task_release.delay(task.id) return HttpResponse('创建成功, url:' + reverse('deploy:detail', args=[task.id])) except (RuntimeError, ObjectDoesNotExist) as e: return HttpResponse(str(e), status=500) except: return HttpResponse('创建失败,未知异常', status=500)
def release(request): if request.method == 'POST': f = ReleaseForm(request.POST) if f.is_valid(): cleaned_data = f.cleaned_data pid = cleaned_data['project'] env_id = cleaned_data['env'] comment = cleaned_data['comment'] scope = cleaned_data['scope'] files_list = cleaned_data['files_list'] try: projectService = ProjectService(pid) task = projectService.create_task(env_id, request.user, comment, scope, files_list) queue = task_release.delay(task.id) task.celery_task_id = queue.id task.save() messages.info(request, '任务提交成功,准备发布...') return redirect('deploy:progress', task.id) except RuntimeError as e: messages.error(request, str(e)) return redirect('deploy:homepage') else: messages.error(request, '任务申请校验失败, 请重新提交!') print(f.errors) envService = EnvService() envs = envService.get_all() projectService = ProjectService() projects = projectService.get_valid_all() return TemplateResponse(request, 'deploy/index.html', { 'form': f, 'envs': envs, 'projects': projects, }) else: return redirect('deploy:homepage')
def test(request): hosts_list = ['192.168.1.111', '192.168.1.112'] queue = task_release.delay(141, rollback=False, hosts_list=hosts_list) return HttpResponse('hello world')
def rollback(request, id): task_release.delay(id, rollback=True) return redirect(reverse('deploy:progress', args=[id]) + '?rollback=1')