# @Author : kane.zhu # @Time : 2020/9/8 21:43 # @Software: PyCharm from aliyunsdkcore.client import AcsClient from aliyunsdkcore.acs_exception.exceptions import ClientException from aliyunsdkcore.acs_exception.exceptions import ServerException from aliyunsdkresourcemanager.request.v20200331.ListResourceGroupsRequest import ListResourceGroupsRequest from cmdb import models as cmdbmodels from common import baseconfig from midplatform import settings import json, datetime accesskeyId = baseconfig.getconfig()['accessKey'] accessSecret = baseconfig.getconfig()['accessSecret'] # 罗列资产组 def listResourceGroup(): getdata = cmdbmodels.resourceGroup.objects.all().values() settings.RESULT['code'] = 2001 settings.RESULT['msg'] = 'success' settings.RESULT['data'] = list(getdata) settings.RESULT['count'] = getdata.count() return settings.RESULT #同步资产组 def syncResourceGroup(): client = AcsClient(accesskeyId, accessSecret, 'cn-hangzhou')
# @Author : kane.zhu # @Time : 2020/6/15 19:13 # @Software: PyCharm import redis from common import baseconfig redishost = baseconfig.getconfig()['redisAddr'] redisPort = int(baseconfig.getconfig()['redisPort']) redisPass = baseconfig.getconfig()['redisPass'] r = redis.Redis(host=redishost, port=redisPort, decode_responses=True, password=redisPass) def keyset(**tokenserials): r.set(tokenserials['k'], tokenserials['v'], tokenserials['expire']) def keyExists(key): """ :param key: 登陆后得到的KEY :return: int 0 不存在 1 存在 """ res = r.exists(key) return res def getNameByToken(key): res = r.get(key) resdata = res.split('|')[0] return resdata
def domainsync(request): """ 同步各个账户的域名列表接口 http://127.0.0.1:88/domains/domainsync/?account=adam.king """ from common import baseconfig api_url = baseconfig.getconfig()['nameDomainApi'] id = request.GET.get('id') registerType = list( models.Domainaccount.objects.filter(pk=id).values_list( 'register_website', flat=True))[0] if registerType == 'www.aliyun.com': if aliyundomainsync(id, request): settings.RESULT['code'] = 2001 settings.RESULT['msg'] = 'successs' settings.RESULT['data'] = settings.codeMsg[2001] return JsonResponse(settings.RESULT) account_token = list( models.Domainaccount.objects.filter(pk=id).values_list( 'username', 'token').first()) account = account_token[0] token = account_token[1] print(account, token) s.auth = (account, token) res = s.get(url=api_url) print('ddd' + str(res.status_code)) if res.status_code == 200: domainresult = res.content.decode('utf-8') if 'domains' in domainresult: data = json.loads(domainresult)['domains'] ###利用bulk_create进行批量插入 domain_obj_list = [] for i in range(len(data)): recordinfo(s, api_url, data[i]['domainName'], account) if 'autorenewEnabled' not in data[i]: # if data[i].has_key('autorenewEnabled'): ###python3 已经删除了has_key方法 data[i]['autorenewEnabled'] = False if 'locked' not in data[i]: data[i]['locked'] = False domain_obj_list.append( models.Domainlist( name_account=account, domainName=data[i]['domainName'], locked=data[i]['locked'], autorenewEnabled=data[i]['autorenewEnabled'], expireDate=data[i]['expireDate'].split('T')[0].split( 'T')[0], createDate=data[i]['createDate'].split('T')[0])) else: settings.RESULT['code'] = 2008 settings.RESULT['msg'] = 'fail' settings.RESULT['data'] = settings.codeMsg[2008] return JsonResponse(settings.RESULT) info = '域名账号' + account + '同步域名总共' + str(len(data)) get_log_insert.logrecord(1, request, {'msg': info}) models.Domainlist.objects.filter(name_account=account).delete() models.Domainlist.objects.bulk_create(domain_obj_list) settings.RESULT['code'] = 2001 settings.RESULT['msg'] = 'successs' settings.RESULT['data'] = settings.codeMsg[2001] else: settings.RESULT['code'] = 2007 settings.RESULT['msg'] = 'fail' settings.RESULT['data'] = settings.codeMsg[2007] return JsonResponse(settings.RESULT)
def dingtalkmsg(data, type): """ :param data: :param type: 0 - 是未处理 1 - 失败 2 - 成功 :return: https://moppowar.oss-ap-southeast-1.aliyuncs.com/midplatform/deploystatus/aduitok.png """ from project import models from common import baseconfig ossurl = baseconfig.getconfig()['baseFileUrl'] print(data) headers = {'Content-Type': 'application/json;charset=utf-8'} logo = ossurl + models.projectName.objects.values('projectLogo').filter( projectName=data['projectName']).first()['projectLogo'] projecthook = models.projectName.objects.values('projectHook').filter( projectName=data['projectName']).first()['projectHook'] # projecthook = '75e709c1a28e4d79d3ab6643ef5923d9409af252ca6cd3ed52dc4da40b1e98fc' api_url = "https://oapi.dingtalk.com/robot/send?access_token=" + projecthook # 获取该项目的项目经理用户ID from project import models as proownermol idList = proownermol.projectName.objects.filter( projectName=data['projectName']).values('projectOwnerId', 'opsOwnerId').first() from sysconf import models as sysmol dowithbaseurl = baseconfig.getconfig()['dowithurl'] print(data) url = dowithbaseurl + '?id=' + str(data['id']) # print(url) testnum = sysmol.sys_user.objects.filter( nickname=data['tester']).values('phone').first()['phone'] ownerNum = sysmol.sys_user.objects.filter( id=int(idList['projectOwnerId'])).values('phone').first()['phone'] opsNum = sysmol.sys_user.objects.filter( id=int(idList['opsOwnerId'])).values('phone').first()['phone'] deploystatus = { 0: [ "发布开始,请前去补充测试结论", 'https://moppowar.oss-accelerate.aliyuncs.com/midplatform/deploystatus/newstart.png', testnum ], 1: [ "测试未通过", 'https://moppowar.oss-accelerate.aliyuncs.com/midplatform/deploystatus/newtestfail.png' ], 2: [ "测试通过,请前去审核", 'https://moppowar.oss-accelerate.aliyuncs.com/midplatform/deploystatus/newtestok.png', ownerNum ], 3: [ "审核失败", 'https://moppowar.oss-accelerate.aliyuncs.com/midplatform/deploystatus/newaduitfail.png' ], 4: [ "审核通过,请前去发布", 'https://moppowar.oss-accelerate.aliyuncs.com/midplatform/deploystatus/newaduitok.png', opsNum ], 5: [ "部署失败", 'https://moppowar.oss-accelerate.aliyuncs.com/midplatform/deploystatus/newarrangefail.png' ], 6: [ "部署成功,请进行线上测试", 'https://moppowar.oss-accelerate.aliyuncs.com/midplatform/deploystatus/newarrangeok.png', testnum ], 7: [ "发布失败", 'https://moppowar.oss-accelerate.aliyuncs.com/midplatform/deploystatus/newdeployfail.png' ], 8: [ "发布成功,线上测试正常", 'https://moppowar.oss-accelerate.aliyuncs.com/midplatform/deploystatus/newdeployok.png' ], 9: [ "你有待处理的记录,请尽快处理", testnum, ownerNum, opsNum, 'https://moppowar.oss-accelerate.aliyuncs.com/midplatform/deploystatus/newmsgalarm.png' ] } if type == 0 or type == 2 or type == 4 or type == 6: # 这是开发人员填写完成 @测试人员 数据 if type == 0: data['testResult'] = '测试结论未填写' senddata = { "msgtype": "markdown", "markdown": { "title": "项目发布", "text": "### " + data['projectName'] + "***" + deploystatus[int(type)][0] + "***" + "\n" + "![screenshot](" + logo + ")\n" + "> #### 更新模块:\r\n" + data['modifyModel'] + "\n\r" + "> #### 更新内容:\r\n " + data['modifyContent'] + "\n\r" + "> #### 开发人员: \r\n" + data['publisher'] + "\n\r" + "> #### 测试人员: \r\n" + data['tester'] + "\n\r" + "@" + deploystatus[int(type)][2] + "\n\r" + "![screenshot](" + deploystatus[int(type)][1] + ")\n\r" + " [去处理](" + url + ")" + "\n" }, "at": { "atMobiles": [deploystatus[int(type)][2]], "isAtAll": False } } elif type == 1 or type == 3 or type == 5 or type == 7 or type == 8: # 这是任何一个阶段 失败的数据格式,默认@全体 # 此外包含最后一步线上测试成功 数据格式 也是@全体 senddata = { "msgtype": "markdown", "markdown": { "title": "项目发布", "text": "### " + data['projectName'] + "***" + deploystatus[int(type)][0] + "***" + "\n" + "![screenshot](" + logo + ")\n" + "> #### 更新模块:\r\n" + data['modifyModel'] + "\n\r" + "> #### 更新内容:\r\n " + data['modifyContent'] + "\n\r" + "> #### 开发人员: \r\n" + data['publisher'] + "\n\r" + "> #### 测试人员: \r\n" + data['tester'] + "\n\r" + "![screenshot](" + deploystatus[int(type)][1] + ")\n\r" + " [去处理](" + url + ")" + "\n" }, "at": { "isAtAll": True } } else: # type = 9 是 钉功能 senddata = { "msgtype": "markdown", "markdown": { "title": "项目发布", "text": "### 项目" + data['projectName'] + "\n" + "### @" + deploystatus[int(type)][data['dingstep']] + "\n" + "![screenshot](" + deploystatus[int(type)][4] + ")\n\r" + " [去处理](" + url + ")" + "\n" }, "at": { "atMobiles": [deploystatus[int(type)][data['dingstep']]], "isAtAll": False } } requests.post(url=api_url, data=json.dumps(senddata), headers=headers)
import oss2 import base64 from common import baseconfig auth = oss2.Auth(baseconfig.getconfig()['accessKey'], baseconfig.getconfig()['accessSecret']) bucket = oss2.Bucket(auth, baseconfig.getconfig()['endPoint'], baseconfig.getconfig()['bucketName']) base_file_url = baseconfig.getconfig()['baseFileUrl'] def percentage(consumed_bytes, total_bytes): if total_bytes: rate = int(100 * (float(consumed_bytes) / float(total_bytes))) print('\r{0}% '.format(rate), end='') def update_fil_file(envtype,project,filename,file): """ :param envtype: 环境类型 :param project: 项目名称 :param filename: 文件名称 :param file: 文件流 :return: """ print(envtype,type(envtype)) if envtype == 1: env = 'online' elif envtype == 0: env = 'offline' accessurl = 'midplatform/ApkDir/' + env +'/' + project + '/' + filename # 这个是阿里提供的SDK方法 bucket是调用的4.1中配置的变量名 res = bucket.put_object(accessurl,file,progress_callback=percentage) if res.status == 200:
# @Author : kane.zhu # @Time : 2020/6/13 19:42 # @Software: PyCharm from django.http import JsonResponse from common import baseconfig import uuid TIME_OUT = baseconfig.getconfig()['redisToeknExpire'].rstrip() import re TIME_OUT = int(re.sub("\D", "", TIME_OUT)) from common import checklogin from midplatform import settings def isLogin(func): def inner(request, *args, **kwargs): token = request.META.get('HTTP_AUTHORIZATION') if token is None or checklogin.keyExists(token) == 0: settings.finalData['code'] = 2005 settings.finalData['msg'] = settings.codeMsg[2005] return JsonResponse(settings.finalData) else: print(checklogin.keyExists(token)) return func(*args, **kwargs) return inner import itsdangerous