Commit 43b47139 authored by 张永's avatar 张永

alpha渠道打包

parent 6c65e5d9
import hashlib
from django.conf import settings
from gm_types.ascle import ERROR from gm_types.ascle import ERROR
from helios.rpc.exceptions import RPCFaultException from helios.rpc.exceptions import RPCFaultException
from utils.base import APIView from utils.base import APIView
from utils.channel_package_tools import ChannelPackage from utils.channel_package_tools import ChannelPackage
from utils.logger import error_logger from utils.logger import error_logger
from utils.channel_task import version_release_task from utils.channel_task import version_release_task, channel_build
def get_md5_value(src):
md5 = hashlib.md5()
md5.update(src)
md5_digest = md5.hexdigest()
return md5_digest
class Vendor(APIView): class Vendor(APIView):
...@@ -32,24 +42,41 @@ class ChannelList(APIView): ...@@ -32,24 +42,41 @@ class ChannelList(APIView):
:return: :return:
""" """
page = int(request.GET.get('page', 1)) page = int(request.GET.get('page', 1))
offset = int(request.GET.get('limit', 10)) limit = int(request.GET.get('limit', 10))
filters = self.handle_filter(request.GET.get('filter', "")) filters = self.handle_filter(request.GET.get('filter', ""))
try: try:
channel_list = self.rpc['endpoint']( channel_list = self.rpc['sun/channel_build/channel/list'](
start=(page - 1) * offset, offset=offset, filters=filters offset=page, limit=limit, filters=filters
).unwrap() ).unwrap()
except Exception as e: except Exception as e:
error_logger.error(u'获取渠道列表失败:%s', e) error_logger.error(u'获取渠道列表失败:%s', e)
raise e raise e
data = [ data = {'total': channel_list.get('total', 0)}
{ if filters.get('channel_id', ''):
'name': data.get('name'), url = settings.APK_DOMAIN + settings.APK_BUILD_DIR + "/{version}/{name}/alpha_{name}.apk'"
'url_name': data.get('url_name'), channels = [
'pack': data.get('pack'), {
'download_url': data.get(), # TODO 拼接下载链接 最近的一次发布 'version': data.get('version'),
} 'name': data.get('name'),
for data in channel_list] 'packed_time': data.get('packed_time'),
'release_time': data.get('release_time'),
'is_released': data.get('is_released'),
'download_url': url.format(version=data.get('version'), name=data.get('name')),
}
for data in channel_list.get('data', [])]
else:
url = settings.APK_DOMAIN + settings.APK_RELEASE_DIR + "/{name}/alpha_{name}.apk'"
channels = [
{
'id': data.get('id'),
'name': data.get('name'),
'url_name': data.get('url_name'),
'pack': data.get('pack'),
'released_version': data.get('released_version'),
'download_url': url.format(name=data.get('name')),
}
for data in channel_list.get('data', [])]
data.update({'channel': channels})
return self.write_success(message='获取渠道列表成功', data=data) return self.write_success(message='获取渠道列表成功', data=data)
...@@ -60,22 +87,25 @@ class Channel(APIView): ...@@ -60,22 +87,25 @@ class Channel(APIView):
:param request: :param request:
:return: :return:
""" """
channel_id = request.GET.get('channel_id', 0) channel_id = request.GET.get('channel_id')
if not channel_id: if not channel_id:
error_logger.error(u'参数不完整') error_logger.error(u'参数不完整')
return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整') return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整')
try: try:
channel_data = self.rpc['endpoint'](channel_id=channel_id).unwrap() channel_data = self.rpc['sun/channel_build/channel/get'](
id=channel_id
).unwrap()
except Exception as e: except Exception as e:
error_logger.error(u'获取渠道详情失败:%s', e) error_logger.error(u'获取渠道详情失败:%s', e)
raise e raise e
channel = { channel = {
'channel_id': channel_data.get('channel_id', 0), 'channel_id': channel_data.get('id'),
'channel_name': channel_data.get('name'), 'channel_name': channel_data.get('name'),
'url_name': channel_data.get('url_name'), 'url_name': channel_data.get('url_name'),
'auto_pack': channel_data.get('pack'), 'auto_pack': channel_data.get('pack'),
'channel_vendor': channel_data.get('vendor'), 'channel_vendor': channel_data.get('vendor'),
'channel_type': channel_data.get('type'), 'channel_type': channel_data.get('type'),
'released_version': channel_data.get('released_version'),
} }
return self.write_success(message='获取渠道详情成功', data=channel) return self.write_success(message='获取渠道详情成功', data=channel)
...@@ -91,55 +121,59 @@ class Channel(APIView): ...@@ -91,55 +121,59 @@ class Channel(APIView):
auto_pack = request.POST.get('pack', True) auto_pack = request.POST.get('pack', True)
channel_vendor = request.POST.get('vendor', 0) channel_vendor = request.POST.get('vendor', 0)
channel_type = request.POST.get('type', 2) channel_type = request.POST.get('type', 2)
if not all([channel_name, url_name, channel_vendor]): if not all([channel_id, channel_name, url_name, channel_vendor]):
error_logger.error(u'参数不完整') error_logger.error(u'参数不完整')
return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整') return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整')
if channel_id: # 编辑渠道信息 data = dict()
try: if channel_name:
self.rpc['endpoint']( data.update({'name': channel_name})
channel_id=channel_id, name=channel_name, pack=auto_pack, if url_name:
vendor=channel_vendor, type=channel_type data.update({'url_name': url_name})
).unwrap() if auto_pack:
except RPCFaultException as e: data.update({'pack': auto_pack})
raise e if channel_vendor:
return self.write_success(message='操作成功', data={}) data.update({'vendor': channel_vendor})
else: # 创建渠道信息 if channel_type:
try: data.update({'type': channel_type})
self.rpc['endpoint']( try:
name=channel_name, pack=auto_pack, vendor=channel_vendor, type=channel_type self.rpc['sun/channel_build/channel/edit'](
).unwrap() id=channel_id, data=data
except RPCFaultException as e: ).unwrap()
raise e except RPCFaultException as e:
return self.write_success(message='操作成功', data={}) raise e
return self.write_success(message='操作成功', data={})
class ChannelPacked(APIView): class ChannelPacked(APIView):
def get(self, request): # def get(self, request):
""" # """
当前渠道已经打包的所有版本 # 当前渠道已经打包的所有版本
:param request: # :param request:
:return: # :return:
""" # """
channel_id = request.GET.get('channel_id') # channel_id = request.GET.get('channel_id')
if not channel_id: # if not channel_id:
error_logger.error(u'参数不完整') # error_logger.error(u'参数不完整')
return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整') # return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整')
try: # try:
channel_data = self.rpc['endpoint'](channel_id=channel_id).unwrap() # channel_data = self.rpc['endpoint'](
except Exception as e: # channel_id=channel_id
error_logger.error(u'获取渠道打包列表失败:%s', e) # ).unwrap()
raise e # except Exception as e:
channel = [ # error_logger.error(u'获取渠道打包列表失败:%s', e)
{ # raise e
'version': data.get('version', 0), # url = settings.APK_DOMAIN + settings.APK_BUILD_DIR + "/{version}/{name}/alpha_{name}.apk'"
'channel_name': data.get('name'), # channel = [
'packed_time': data.get('packed_time'), # {
'release_time': data.get('release_time'), # 'version': data.get('version'),
'is_released': data.get('is_released'), # 'channel_name': data.get('name'),
'download_url': '', # TODO 拼接下载链接 # 'packed_time': data.get('packed_time'),
} # 'release_time': data.get('release_time'),
for data in channel_data] # 'is_released': data.get('is_released'),
return self.write_success(message='获取渠道打包列表成功', data=channel) # 'download_url': url.format(version=data.get('version'), name=data.get('name')),
# }
# for data in channel_data]
# return self.write_success(message='获取渠道打包列表成功', data=channel)
def post(self, request): def post(self, request):
""" """
...@@ -147,55 +181,63 @@ class ChannelPacked(APIView): ...@@ -147,55 +181,63 @@ class ChannelPacked(APIView):
:param request: :param request:
:return: :return:
""" """
version2channel_id = request.POST.get('id') version_id = request.POST.get('version_id')
if not version2channel_id: channel_id = request.POST.get('channel_id')
if not all([channel_id, version_id]):
error_logger.error(u'参数不完整') error_logger.error(u'参数不完整')
return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整') return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整')
# get version-channel object channel = self.rpc['sun/channel_build/channel/get'](
version2channel = self.rpc['endpoint'](id=version2channel_id).unwrap() id=channel_id
version = version2channel['version'] ).unwrap()
channel = version2channel['channel'] version = self.rpc['sun/channel_build/version/get'](
ChannelPackage.publish_apk(channel, version) id=version_id
).unwrap()
ChannelPackage.publish_apk(channel.get('url_name'), version.get('version'))
try: try:
self.rpc['endpoint'](id=version2channel_id).unwrap() #发布完成 修改状态 self.rpc['sun/channel_build/version/release'](
version_id=version_id, channel_ids=channel_id
).unwrap() #发布
except RPCFaultException as e: except RPCFaultException as e:
error_logger.error(u'发布失败:%s', e) error_logger.error(u'发布失败:%s', e)
return self.write_fail(code=-1, message='发布失败!') return self.write_fail(code=-1, message='发布失败!')
return self.write_success(data={}, message='发布成功!') return self.write_success(data={}, message='发布成功!')
class VersionPackedChannel(APIView): # class VersionPackedChannel(APIView):
def get(self, request): # def get(self, request):
""" # """
当前版本所有已打包渠道列表 # 当前版本所有已打包渠道列表
:param request: # :param request:
:return: # :return:
""" # """
version_id = request.GET.get('version__id', '') # version_id = request.GET.get('version_id', '')
page = int(request.GET.get('page', 1)) # page = int(request.GET.get('page', 1))
offset = int(request.GET.get('offset', 10)) # offset = int(request.GET.get('offset', 10))
if version_id: # if version_id:
version = self.rpc['endpoint'](version_id=version_id).unwrap() # version = self.rpc['sun/channel_build/version/get'](
# 母包地址 由七牛上传返回 # id=version_id
base_apk_url = ChannelPackage.apk_url(channel='', version=version['version']) # ).unwrap()
else: # # 母包地址 由七牛上传返回
base_apk_url = '' # base_apk_url = ChannelPackage.apk_url(channel='', version=version.get('version'))
version = {} # else:
version2channels = self.rpc['endpoint']( # base_apk_url = ''
version_id=version, start=(page - 1) * offset, offset=offset).unwrap() # version = {}
data = { # version2channels = self.rpc['endpoint'](
'version': [{ # version_id=version, start=(page - 1) * offset, offset=offset).unwrap()
'version': item.get('version'), # url = settings.APK_DOMAIN + settings.APK_BUILD_DIR + "/{version}/{name}/alpha_{name}.apk'"
'name': item.get('name'), # data = {
'packed_time': item.get('packed_time'), # 'version': [{
'download_url': '', # 'version': item.get('version'),
'release_time': item.get('release_time'), # 'name': item.get('name'),
'is_released': item.get('is_released'), # 'packed_time': item.get('packed_time'),
} for item in version2channels], # 'download_url': url.format(version=item.get(version), name=item.get('name')),
'base_apk_url': base_apk_url, # 'release_time': item.get('release_time'),
} # 'is_released': item.get('is_released'),
return self.write_success(data=data) # } for item in version2channels],
# 'base_apk_url': base_apk_url,
# }
# return self.write_success(data=data)
class VersionList(APIView): class VersionList(APIView):
...@@ -209,19 +251,39 @@ class VersionList(APIView): ...@@ -209,19 +251,39 @@ class VersionList(APIView):
offset = int(request.GET.get('offset', 10)) offset = int(request.GET.get('offset', 10))
filters = self.handle_filter(request.GET.get('filter', "")) filters = self.handle_filter(request.GET.get('filter', ""))
try: try:
version_list = self.rpc['endpoint']( version_list = self.rpc['sun/channel_build/version/list'](
start=(page - 1) * offset, offset=offset, filters=filters offset=page, limit=offset, filters=filters
).unwrap() ).unwrap()
except Exception as e: except Exception as e:
error_logger.error(u'获取版本列表失败:%s', e) error_logger.error(u'获取版本列表失败:%s', e)
raise e raise e
data = [ data = {'total': version_list.get('total', 0)}
{ if filters.get('version_id', ''):
'version': item.get('version'), version = self.rpc['sun/channel_build/version/get'](
'MD5': item.get('MD5'), id=filters.get('version_id')
'release_time': item.get('release_time'), ).unwrap()
} for item in version_list base_apk_url = ChannelPackage.apk_url(channel='', version=version.get('version'))
] url = settings.APK_DOMAIN + settings.APK_BUILD_DIR + "/{version}/{name}/alpha_{name}.apk'"
versions = {
'version': [{
'version': item.get('version'),
'name': item.get('name'),
'packed_time': item.get('packed_time'),
'download_url': url.format(version=item.get(version), name=item.get('name')),
'release_time': item.get('release_time'),
'is_released': item.get('is_released'),
} for item in version_list.get('data', [])],
'base_apk_url': base_apk_url,
}
else:
versions = [
{
'version': item.get('version'),
'MD5': item.get('md5'),
'release_time': item.get('release_time'),
} for item in version_list.get('data', [])
]
data.update({'data': versions})
return self.write_success(data=data) return self.write_success(data=data)
...@@ -236,12 +298,14 @@ class VersionDetailUpdate(APIView): ...@@ -236,12 +298,14 @@ class VersionDetailUpdate(APIView):
if not version_id: if not version_id:
error_logger.error(u'参数不完整') error_logger.error(u'参数不完整')
return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整') return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整')
version_info = self.rpc['endpoint'](version_id=version_id).unwrap() version_info = self.rpc['sun/channel_build/version/get'](
id=version_id
).unwrap()
data = { data = {
'version': version_info['version'], 'version': version_info.get('version'),
'release_time': version_info['release_time'], 'release_time': version_info.get('release_time'),
'md5': version_info['md5'], 'md5': version_info.get('md5'),
'description': version_info['description'], 'description': version_info.get('description'),
} }
return self.write_success(data=data, message='操作成功') return self.write_success(data=data, message='操作成功')
...@@ -252,12 +316,26 @@ class VersionDetailUpdate(APIView): ...@@ -252,12 +316,26 @@ class VersionDetailUpdate(APIView):
:return: :return:
""" """
version_id = request.POST.get('version_id') version_id = request.POST.get('version_id')
version = request.POST.get('version')
md5 = request.POST.get('md5')
release_time = request.POST.get('release_time')
desc = request.POST.get('desc') desc = request.POST.get('desc')
if not version_id: if not version_id:
error_logger.error(u'参数不完整') error_logger.error(u'参数不完整')
return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整') return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整')
data = dict()
if version:
data.update({'version': version})
if release_time:
data.update({'release_time': release_time})
if desc:
data.update({'description': desc})
if md5:
data.update({'md5': md5})
try: try:
self.rpc['endpoint'](version_id=version_id, description=desc) self.rpc['sun/channel_build/version/edit'](
id=version_id, data=data
)
except RPCFaultException as e: except RPCFaultException as e:
error_logger.error(e) error_logger.error(e)
return self.write_fail(code=-1, message='操作失败') return self.write_fail(code=-1, message='操作失败')
...@@ -276,9 +354,55 @@ class BatchPublishVersion(APIView): ...@@ -276,9 +354,55 @@ class BatchPublishVersion(APIView):
error_logger.error(u'参数不完整') error_logger.error(u'参数不完整')
return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整') return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整')
try: try:
packed_channels = self.rpc['endpoint'](version_id=version_id).unwrap() # 获取当前版本所有已打包渠道
channel_info = self.rpc['sun/channel_build/version/get_channel_info'](
version_id=version_id
).unwrap()
except RPCFaultException as e: except RPCFaultException as e:
error_logger.error(u'获取渠道失败%s', e) error_logger.error(u'获取渠道失败%s', e)
return self.write_fail(code=-1, message='获取渠道失败!') return self.write_fail(code=-1, message='获取渠道失败!')
version_release_task.delay(packed_channels) packed_channels = list()
for item in channel_info.get('data', []):
if item.get('pack', False):
packed_channels.append(item)
version_release_task.delay(
packed_channels, version_id=version_id, version=channel_info.get('version')
)
return self.write_success(data={}, message='操作成功') return self.write_success(data={}, message='操作成功')
class VersionCreate(APIView):
def post(self, request):
"""
版本创建 供脚本使用
:param request:
:return:
"""
apk = request.FILES.get('file').read()
file_md5 = get_md5_value(apk)
version = request.POST.get('version')
md5 = request.POST.get('md5')
if md5 != file_md5:
raise Exception('md5校验失败, 文件md5{}'.format(file_md5))
data = dict()
if version:
data.update({'version': version})
if md5:
data.update({'md5': md5})
try:
version_id = self.rpc['sun/channel_build/version/edit'](
data=data
).unwrap()
except RPCFaultException as e:
error_logger.error('创建版本失败:%s', e)
raise Exception('创建版本失败!')
channel_info = self.rpc['sun/channel_build/version/get_channel_info'](
version_id=version_id
).unwrap() # 所有渠道的url_name: list()
url_names = []
for item in channel_info.get('data', []):
if item.get('pack', False):
url_names.append(item.get('url_name'))
channel_build.delay(version, url_names, apk)
return {'success': True}
...@@ -15,4 +15,5 @@ git+ssh://git@git.wanmeizhensuo.com/system/gm-tracer.git@v0.1.2 ...@@ -15,4 +15,5 @@ git+ssh://git@git.wanmeizhensuo.com/system/gm-tracer.git@v0.1.2
git+ssh://git@git.wanmeizhensuo.com/alpha/alpha-types.git@master git+ssh://git@git.wanmeizhensuo.com/alpha/alpha-types.git@master
filetype==1.0.2 filetype==1.0.2
Pillow==5.4.1 Pillow==5.4.1
celery==4.2.1 celery==4.2.1
\ No newline at end of file requests==2.21.0
\ No newline at end of file
...@@ -158,4 +158,9 @@ OPERATOR_PASSWORD = 123456 ...@@ -158,4 +158,9 @@ OPERATOR_PASSWORD = 123456
# 图片下载的存储路径 # 图片下载的存储路径
DOWNLOAD_IMAGE_PATH = u'/data/header-images/' DOWNLOAD_IMAGE_PATH = u'/data/header-images/'
# # apk 七牛上传空间
APK_SCOPE = 'download'
APK_DOMAIN = 'http://dl.igengmei.com/'
DEFAULT_CHANNEL = 'benzhan'
APK_RELEASE_DIR = 'sun'
APK_BUILD_DIR = 'sun_build'
from celery import shared_task from celery import shared_task
from django.conf import settings
import requests
from gm_upload.utils.qiniu_tool import QiniuTool
from middleware.rpc import rpc_invoker from middleware.rpc import rpc_invoker
from utils.channel_package_tools import ChannelPackage from utils.channel_package_tools import ChannelPackage
from utils.logger import log_error from utils.logger import log_error, info_logger
@shared_task @shared_task
def version_release_task(packed_channels): # 一键发布,发布完成邮件通知 def version_release_task(packed_channels, version_id=None, version=None): # 一键发布,发布完成邮件通知
rpc = rpc_invoker rpc = rpc_invoker
channels = [] version = version
for vtc in packed_channels: channel_ids = list()
version = vtc['version'] for version2channel in packed_channels:
channel = vtc['channel'] channel = version2channel['url_name']
vtc_id = vtc['id'] channel_ids.append(version2channel.get('id'))
try: try:
ChannelPackage.publish_apk(channel, version) ChannelPackage.publish_apk(channel, version)
rpc['endpoint'](version_id=vtc_id).unwrap()
channels.append(channel)
except: except:
log_error() log_error()
raise raise Exception('发布失败')
# send email # 发布
rpc.origin['hera/channelbuild/version_release_notify']( rpc['sun/channel_build/version/release'](
version=version, version_id=version_id, channel_ids=channel_ids
channels=channels,
).unwrap() ).unwrap()
# email notify
# rpc.origin['endpoint'](version=version, channels=channels).unwrap()
@shared_task
def channel_build_one(version=None, channel=None, data=None):
url = ChannelPackage.apk_url('', version)
r = requests.get(url)
chn_pkg = ChannelPackage(r.content, version)
build_one_channel(chn_pkg, channel)
@shared_task
def channel_build(version=None, channels=None, data=None):
url = ChannelPackage.apk_key('', version)
QiniuTool.delete(url, settings.APK_SCOPE)
QiniuTool.upload(data, url, settings.APK_SCOPE)
if channels is None:
channels = [settings.DEFAULT_CHANNEL]
elif settings.DEFAULT_CHANNEL not in channels:
channels.append(settings.DEFAULT_CHANNEL)
info_logger.debug(channels)
chn_pkg = ChannelPackage(data, version)
# 遍历渠道号并创建对应渠道号的apk文件
for channel in channels:
build_one_channel(chn_pkg, channel)
def build_one_channel(pkg_obj, channel):
info_logger.debug(channel)
if not channel:
return
channel = channel.strip()
ret = pkg_obj.upload_apk(channel)
info_logger.debug(ret)
if 'file' in ret:
# TODO 打包
rpc_invoker['endpoint'](
version=pkg_obj.version, channel=channel
).unwrap()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment