def post_assets(request): response = BaseResponse() release_id = request.POST.get('audit_id', None) user = request.session['username'] models.ReleaseTask.objects.filter(id=release_id).update( release_status=6) audit_log(release_id, '[ %s ] DB审核通过' % user) audit_log(release_id, '[ 系统 ] 等待SA审核..') response.status = True return response
def post_assets(request): response = BaseResponse() release_id = request.POST.get('audit_id', None) user = request.session['username'] have_db_script = models.ReleaseTask.objects.filter(id=release_id).first().release_db if have_db_script: models.ReleaseTask.objects.filter(id=release_id).update(release_status=5) audit_log(release_id, '[ %s ] 项目经理审核通过' % user) audit_log(release_id, '[ 系统 ] 等待DBA审核..') else: models.ReleaseTask.objects.filter(id=release_id).update(release_status=6) audit_log(release_id, '[ %s ] 项目经理审核通过' % user) audit_log(release_id, '[ 系统 ] 未检测到数据库执行脚本') audit_log(release_id, '[ 系统 ] 等待SA审核..') response.status = True return response
def delete_assets(request): response = BaseResponse() try: delete_dict = QueryDict(request.body, encoding='utf-8') id_list = delete_dict.getlist('id_list') status = models.ReleaseTask.objects.filter( id=id_list[0]).first().release_status # if status != 4: # response.status = False # response.message = '非待审核状态不能删除' # else: # 不删除 标记为取消状态 # models.ReleaseTask.objects.filter(id__in=id_list).delete() models.ReleaseTask.objects.filter(id__in=id_list).update( release_status=8) user = request.session['username'] audit_log(id_list, '[ %s ] 撤销发布申请' % user) response.message = '删除成功' except Exception as e: response.status = True # response.message = str(e) return response
def post_assets(self, request): response = BaseResponse() # 运维审核完成后-开始发布 release_id = request.POST.get('release_id', None) if release_id: status = models.ReleaseTask.objects.filter( id=release_id).first().release_status # 发布任务状态不是 待发布 不允许进行发布 if status not in [3, 7]: response.status = False response.message = '待审任务或完成状态不允许执行发布..' return response # 开始创建发布任务 else: models.ReleaseTask.objects.filter(id=release_id).update( release_status=1) self.post_task(release_id) audit_log(release_id, '[ %s ] 开始执行发布' % request.session['username']) response.status = True return response # 运维审核流程 audit_id = request.POST.get('audit_id', None) if audit_id: user = request.session['username'] status = models.ReleaseTask.objects.filter( id=audit_id).first().release_status if status != 6: response.status = False response.message = '审核失败..' return response models.ReleaseTask.objects.filter(id=audit_id).update( release_status=7) audit_log(audit_id, '[ %s ] SA审核通过' % user) audit_log(audit_id, '[ 系统 ] 等待发布..') response.status = True return response # 前端页面请求任务状态 task_id = request.POST.getlist('task_id', None) if task_id: task_id_list = task_id con_q = Q() con_q.connector = 'OR' for item in task_id_list: con_q.children.append(('id', item)) obj_list = models.ReleaseTask.objects.filter(con_q).values( 'id', 'release_status') response.data = {'data_list': list(obj_list)} response.status = True return response
def jenkins_task(self, pkg_name, release_git_url, release_branch, task_id, release_name, release_env, pack_cmd, release_type, jdk_version, static_type, release_user, port): jdk = { '1': '/usr/local/jdk8', '2': '/usr/local/jdk7', '3': '/usr/java/jdk1.6.0_32' } static = {'1': '覆盖式', '2': '迭代式'} release_types = {'1': 'Tomcat', '2': 'Static'} self.log(task_id, '------------------- 开始创建发布任务 -------------------') self.log(task_id, '项目名称: %s' % release_name) self.log(task_id, '发布类型: %s' % release_types[str(release_type)]) if release_type == 3: self.log(task_id, '项目监听端口: %s' % port) self.log( task_id, '发布环境: %s 发布分支: %s 发布用户: %s' % (release_env, release_branch, release_user)) self.log( task_id, 'Java版本: %s 静态资源类型: %s' % (jdk[str(jdk_version)], static[str(static_type)])) # self.log(task_id, 'git地址:%s' % release_git_url) self.log( task_id, '资源地址: %s' % pkg_name.replace('/data/packages', jenkins_config.pkgUrl)) self.log( task_id, '-----------------------------------------------------------') self.log(task_id, '尝试连接Jenkins...') # time.sleep(10) # models.ReleaseTask.objects.filter(id=task_id).update(release_status=2) # return True # 将发布脚本发送到目标机器 cmd = "/usr/bin/scp -r %s root@%s:/opt/" % ( jenkins_config.source_script_path, jenkins_config.host) os.system(cmd) # 将配置文件发送到目标机器 cmd = '/usr/bin/scp -r %s root@%s:/opt/' % (jenkins_config.config_path, jenkins_config.host) os.system(cmd) pack_cmd = '"' + pack_cmd + '"' cmd = "python2.6 {0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10}".format( *[ jenkins_config.script_path, pkg_name, task_id, release_git_url, release_branch, release_name, release_env, pack_cmd, jdk_version, release_type, static_type ]) cmd = "ssh [email protected] '%s'" % cmd print(cmd) os.system(cmd) # ssh = paramiko.SSHClient() # ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # ssh.connect(jenkins_config.host, port=22, username='******', password='******', timeout=3) # stdin, stdout, stderr = ssh.exec_command(cmd) # result = stdout.read() obj = models.ReleaseTask.objects.filter(id=task_id).first() md5 = obj.release_md5 print(md5) # 发布类型为静态资源 if release_type == 2: if release_env != 'prod': nginx_ip_list = jenkins_config.nginx_test_ip_list else: nginx_ip_list = jenkins_config.nginx_prod_ip_list if nginx_ip_list: self.log(task_id, '------ 开始发布静态资源 ------') num = 1 self.log(task_id, nginx_ip_list) for ip in nginx_ip_list: self.log(task_id, '当前发布第%s台Nginx服务器%s...' % (num, ip)) ret = self.nginx_task(ip, release_name, pkg_name, task_id, release_env, static_type, release_branch, port) if not ret: self.log(task_id, '发布第%s台Nginx服务器%s...【失败】' % (num, ip)) self.log(task_id, '终止发布...') models.ReleaseTask.objects.filter(id=task_id).update( release_status=3) return False if release_name == 'apk': self.log(task_id, 'apk项目仅需发布一台nginx服务器...') self.log(task_id, '正在刷新cdn..') cmd = 'cd %s && python2.6 cdn.py Action=RefreshObjectCaches ObjectType=File ObjectPath=%s' % \ (os.path.dirname(jenkins_config.source_script_path), jenkins_config.cdn_url_1) self.log(task_id, cmd) ret = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, preexec_fn=os.setsid) out, err = ret.communicate() url = str(out, encoding='utf-8') cmd = 'curl %s' % url ret = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, preexec_fn=os.setsid) out, err = ret.communicate() cmd = 'cd %s && python2.6 cdn.py Action=RefreshObjectCaches ObjectType=File ObjectPath=%s' % \ (os.path.dirname(jenkins_config.source_script_path), jenkins_config.cdn_url_2) self.log(task_id, cmd) ret = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, preexec_fn=os.setsid) out, err = ret.communicate() url = str(out, encoding='utf-8') cmd = 'curl -I %s' % url ret = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, preexec_fn=os.setsid) out, err = ret.communicate() break num += 1 models.ReleaseTask.objects.filter(id=task_id).update( release_status=2) self.log(task_id, '发布成功结束!') return True else: models.ReleaseTask.objects.filter(id=task_id).update( release_status=3) self.log(task_id, '未配置nginx服务器地址..') return False if md5: self.log(task_id, '生成资源md5...检查是否需要发布静态资源') # 打包成功后查找业务线节点机器 环境 + 业务线 release_obj = models.ReleaseTask.objects.filter(id=task_id).first() business_1 = release_obj.release_env business_2 = release_obj.release_name md5sum = release_obj.release_md5 release_type = release_obj.release_type count = models.Asset.objects.filter(business_1=business_1, business_2=business_2).count() values = models.Asset.objects.filter(business_1=business_1, business_2=business_2).only( 'id', 'host_ip') # 发布 front和 webapp 的静态资源 name = str(release_name) if name in jenkins_config.static_nginx_dict: self.log(task_id, '----- 向Nginx发布static静态资源 -----') num = 1 if release_env != 'prod': nginx_ip_list = jenkins_config.nginx_test_ip_list else: nginx_ip_list = jenkins_config.nginx_prod_ip_list result = True if nginx_ip_list: for ip in nginx_ip_list: self.log(task_id, '当前发布第%s台Nginx服务器%s...' % (num, ip)) ret = self.nginx_task(ip, release_name, pkg_name, task_id, release_env, static_type, release_branch, port) if not ret: self.log(task_id, '发布第%s台Nginx服务器%s...【失败】' % (num, ip)) self.log(task_id, '终止发布...') models.ReleaseTask.objects.filter( id=task_id).update(release_status=3) result = False break num += 1 else: self.log(task_id, '未配置nginx服务器地址..') if not result: self.log(task_id, '发布失败!') return False self.log(task_id, '----- 共需发布【%s】台节点服务器 -------' % count) num = 1 result = True for item in values: print(item.host_ip) self.log(task_id, '当前发布第%s台%s...' % (num, item.host_ip)) # 目标机开始执行发布脚本 ret = self.shell_task(item.host_ip, pkg_name, md5sum, task_id, release_type, business_2, port) if not ret: self.log(task_id, '当前发布第%s台%s...【失败】' % (num, item.host_ip)) self.log(task_id, '终止发布...') models.ReleaseTask.objects.filter(id=task_id).update( release_status=3) result = False break num += 1 if result: if release_type == 1: self.log(task_id, 'Java start success...') self.log(task_id, '发布成功结束!') audit_log(task_id, '[ 系统 ] 发布成功结束!') models.ReleaseTask.objects.filter(id=task_id).update( release_status=2) else: self.log(task_id, '发布失败!') models.ReleaseTask.objects.filter(id=task_id).update( release_status=3) else: # ret = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, # preexec_fn=os.setsid) # out, err = ret.communicate() # err = str(err, encoding='utf-8') # self.log(task_id, err) # self.log(task_id, '......拉取代码失败') self.log(task_id, '发布终止!') models.ReleaseTask.objects.filter(id=task_id).update( release_status=3)
def post_task(self, request): response = BaseResponse() release_id = request.POST.get('id') release_env = request.POST.get('release_env') release_branch = request.POST.get('release_branch') release_reason = request.POST.get('release_reason') release_db = request.POST.get('release_db') t = datetime.datetime.now() + datetime.timedelta(hours=8) release_time = t.strftime('%Y-%m-%d %H:%M') # release_user = request.POST.get('user_name') release_user = request.session['username'] print(release_id, release_env, release_branch, release_reason, release_db, release_time, release_user) obj = models.ProjectTask.objects.filter(id=release_id).first() release_name = obj.business_2 pack_cmd = obj.pack_cmd static_type = obj.static_type obj_env = models.BusinessOne.objects.filter(id=release_env).first() release_env_name = obj_env.name release_git_url = obj.git_url release_jdk_version = obj.jdk_version release_type = obj.project_type_id release_obj = models.ReleaseTask( release_name=release_name, release_env_id=release_env, release_time=release_time, apply_user=release_user, apply_time=release_time, release_git_branch=release_branch, release_id=release_id, release_user=release_user, release_git_url=release_git_url, release_jdk_version=release_jdk_version, release_type_id=release_type, release_reason=release_reason, release_db=release_db) release_obj.save() models.ProjectTask.objects.filter(id=release_id).update( release_last_id=release_obj.id, release_last_time=release_time) models.ReleaseTask.objects.filter(id=release_obj.id).update( release_status=4) # 项目审核日志 # models.AuditLog.objects.create(audit_id=release_obj.id, audit_msg='[ %s ] 上线申请提交..' % release_user) # models.AuditLog.objects.create(audit_id=release_obj.id, audit_msg='[ %s ] 上线说明: %s' % (release_user, release_reason)) # models.AuditLog.objects.create(audit_id=release_obj.id, audit_msg='[ 系统 ] 等待项目经理审核..') audit_log(release_obj.id, '[ %s ] 上线申请提交..' % release_user) audit_log(release_obj.id, '[ %s ] 上线说明: %s' % (release_user, release_reason)) audit_log(release_obj.id, '[ 系统 ] 等待项目经理审核..') response.status = True return response obj = models.ProjectTask.objects.filter(id=release_id).first() release_name = obj.business_2 pack_cmd = obj.pack_cmd static_type = obj.static_type obj_env = models.BusinessOne.objects.filter(id=release_env).first() release_env_name = obj_env.name release_git_url = obj.git_url release_jdk_version = obj.jdk_version release_type = obj.project_type_id try: release_obj = models.ReleaseTask( release_name=release_name, release_env_id=release_env, release_time=release_time, release_git_branch=release_branch, release_id=release_id, release_user=release_user, release_git_url=release_git_url, release_jdk_version=release_jdk_version, release_type_id=release_type, release_status=4) release_obj.save() models.ProjectTask.objects.filter(id=release_id).update( release_last_id=release_obj.id, release_last_time=release_time) response.data = {'id': release_obj.id, 'time': release_time} release_business_1 = release_obj.release_env release_business_2 = release_obj.release_name task_id = release_obj.id # 返回给页面新的发布ID和时间 release_name = str(release_name) if release_type == 2: pkg_name = "/data/packages/%s/%s/%s/%s.zip" % ( release_business_1, release_business_2, release_obj.id, jenkins_config.static_pkg_name[release_name]) else: pkg_name = "/data/packages/%s/%s/%s/%s.war" % ( release_business_1, release_business_2, release_obj.id, release_business_2) # 多进程执行连接Jenkins执行 # p = Process(target=self.JenkinsTask, args=(pkg_name, release_git_url, release_branch, task_id, obj)) # p.start() # 多线程 t = threading.Thread( target=self.jenkins_task, args=(pkg_name, release_git_url, release_branch, task_id, release_name, release_env_name, pack_cmd, release_type, release_jdk_version, static_type, release_user)) t.start() except Exception as e: print(e) response.status = True return response
def post_task(self, request): response = BaseResponse() release_id = request.POST.get('id') release_env = request.POST.get('release_env') release_branch = request.POST.get('release_branch') release_reason = request.POST.get('release_reason') release_db = request.POST.get('release_db') # t = datetime.datetime.now() + datetime.timedelta(hours=8) t = datetime.datetime.now() release_time = t.strftime('%Y-%m-%d %H:%M') # release_user = request.POST.get('user_name') release_user = request.session['username'] release_last_id = request.POST.get('release_last_id', None) if release_last_id: try: last_status = models.ReleaseTask.objects.filter( id=release_last_id).first().release_status if last_status not in [2, 3, 8]: response.status = False response.message = '当前项目有未执行完的上线流程、请等待上线流程完成后再申请..' return response except Exception as e: pass print(release_id, release_env, release_branch, release_reason, release_db, release_time, release_user) obj = models.ProjectTask.objects.filter(id=release_id).first() release_name = obj.business_2 pack_cmd = obj.pack_cmd static_type = obj.static_type obj_env = models.BusinessOne.objects.filter(id=release_env).first() release_env_name = obj_env.name release_git_url = obj.git_url release_jdk_version = obj.jdk_version release_type = obj.project_type_id release_obj = models.ReleaseTask( release_name=release_name, release_env_id=release_env, release_time=release_time, apply_user=release_user, apply_time=release_time, release_git_branch=release_branch, release_id=release_id, release_user=release_user, release_git_url=release_git_url, release_jdk_version=release_jdk_version, release_type_id=release_type, release_reason=release_reason, release_db=release_db) release_obj.save() models.ProjectTask.objects.filter(id=release_id).update( release_last_id=release_obj.id, release_last_time=release_time) models.ReleaseTask.objects.filter(id=release_obj.id).update( release_status=4) # 项目审核日志 audit_log(release_obj.id, '[ %s ] 上线申请提交..' % release_user) audit_log(release_obj.id, '[ %s ] 上线说明: %s' % (release_user, release_reason)) audit_log(release_obj.id, '[ 系统 ] 等待项目经理审核..') response.status = True return response
def post_task(self, request): response = BaseResponse() release_id = request.POST.get('id') release_env = request.POST.get('release_env') release_branch = request.POST.get('release_branch') release_time = time.strftime('%Y-%m-%d %H:%M') print('-----------------------') print(release_time) release_user = request.POST.get('user_name') obj = models.ProjectTask.objects.filter(id=release_id).first() release_name = obj.business_2 pack_cmd = obj.pack_cmd static_type = obj.static_type port = obj.git_branch if not port: port = "None" print('========', port) obj_env = models.BusinessOne.objects.filter(id=release_env).first() release_env_name = obj_env.name release_git_url = obj.git_url release_jdk_version = obj.jdk_version release_type = obj.project_type_id try: release_obj = models.ReleaseTask( release_name=release_name, release_env_id=release_env, release_time=release_time, release_git_branch=release_branch, release_id=release_id, apply_user=release_user, release_git_url=release_git_url, release_jdk_version=release_jdk_version, release_type_id=release_type, apply_time='-') release_obj.save() models.ProjectTask.objects.filter(id=release_id).update( release_last_id=release_obj.id, release_last_time=release_time) response.data = {'id': release_obj.id, 'time': release_time} release_business_1 = release_obj.release_env release_business_2 = release_obj.release_name task_id = release_obj.id audit_log(release_obj.id, '[ %s ] 上线紧急发布任务 ' % request.session['username']) audit_log(release_obj.id, '[ 系统 ] 自动完成审核任务 ') audit_log(release_obj.id, '[ %s ] 开始发布..' % request.session['username']) # 返回给页面新的发布ID和时间 release_name = str(release_name) if release_type == 2: pkg_name = "/data/packages/%s/%s/%s/%s.zip" % ( release_business_1, release_business_2, release_obj.id, jenkins_config.static_pkg_name[release_name]) elif release_type == 3: pkg_name = "/data/packages/%s/%s/%s/%s.tar.gz" % ( release_business_1, release_business_2, release_obj.id, release_business_2) else: pkg_name = "/data/packages/%s/%s/%s/%s.war" % ( release_business_1, release_business_2, release_obj.id, release_business_2) # 多进程执行连接Jenkins执行 # p = Process(target=self.JenkinsTask, args=(pkg_name, release_git_url, release_branch, task_id, obj)) # p.start() # 多线程 t = threading.Thread( target=self.jenkins_task, args=(pkg_name, release_git_url, release_branch, task_id, release_name, release_env_name, pack_cmd, release_type, release_jdk_version, static_type, release_user, port)) t.start() except Exception as e: print(e) response.status = True return response