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

alpha渠道打包

parent 6c65e5d9
import hashlib
from django.conf import settings
from gm_types.ascle import ERROR
from helios.rpc.exceptions import RPCFaultException
from utils.base import APIView
from utils.channel_package_tools import ChannelPackage
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):
......@@ -32,24 +42,41 @@ class ChannelList(APIView):
:return:
"""
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', ""))
try:
channel_list = self.rpc['endpoint'](
start=(page - 1) * offset, offset=offset, filters=filters
channel_list = self.rpc['sun/channel_build/channel/list'](
offset=page, limit=limit, filters=filters
).unwrap()
except Exception as e:
error_logger.error(u'获取渠道列表失败:%s', e)
raise e
data = [
{
'name': data.get('name'),
'url_name': data.get('url_name'),
'pack': data.get('pack'),
'download_url': data.get(), # TODO 拼接下载链接 最近的一次发布
}
for data in channel_list]
data = {'total': channel_list.get('total', 0)}
if filters.get('channel_id', ''):
url = settings.APK_DOMAIN + settings.APK_BUILD_DIR + "/{version}/{name}/alpha_{name}.apk'"
channels = [
{
'version': data.get('version'),
'name': data.get('name'),
'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)
......@@ -60,22 +87,25 @@ class Channel(APIView):
:param request:
:return:
"""
channel_id = request.GET.get('channel_id', 0)
channel_id = request.GET.get('channel_id')
if not channel_id:
error_logger.error(u'参数不完整')
return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整')
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:
error_logger.error(u'获取渠道详情失败:%s', e)
raise e
channel = {
'channel_id': channel_data.get('channel_id', 0),
'channel_id': channel_data.get('id'),
'channel_name': channel_data.get('name'),
'url_name': channel_data.get('url_name'),
'auto_pack': channel_data.get('pack'),
'channel_vendor': channel_data.get('vendor'),
'channel_type': channel_data.get('type'),
'released_version': channel_data.get('released_version'),
}
return self.write_success(message='获取渠道详情成功', data=channel)
......@@ -91,55 +121,59 @@ class Channel(APIView):
auto_pack = request.POST.get('pack', True)
channel_vendor = request.POST.get('vendor', 0)
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'参数不完整')
return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整')
if channel_id: # 编辑渠道信息
try:
self.rpc['endpoint'](
channel_id=channel_id, name=channel_name, pack=auto_pack,
vendor=channel_vendor, type=channel_type
).unwrap()
except RPCFaultException as e:
raise e
return self.write_success(message='操作成功', data={})
else: # 创建渠道信息
try:
self.rpc['endpoint'](
name=channel_name, pack=auto_pack, vendor=channel_vendor, type=channel_type
).unwrap()
except RPCFaultException as e:
raise e
return self.write_success(message='操作成功', data={})
data = dict()
if channel_name:
data.update({'name': channel_name})
if url_name:
data.update({'url_name': url_name})
if auto_pack:
data.update({'pack': auto_pack})
if channel_vendor:
data.update({'vendor': channel_vendor})
if channel_type:
data.update({'type': channel_type})
try:
self.rpc['sun/channel_build/channel/edit'](
id=channel_id, data=data
).unwrap()
except RPCFaultException as e:
raise e
return self.write_success(message='操作成功', data={})
class ChannelPacked(APIView):
def get(self, request):
"""
当前渠道已经打包的所有版本
:param request:
:return:
"""
channel_id = request.GET.get('channel_id')
if not channel_id:
error_logger.error(u'参数不完整')
return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整')
try:
channel_data = self.rpc['endpoint'](channel_id=channel_id).unwrap()
except Exception as e:
error_logger.error(u'获取渠道打包列表失败:%s', e)
raise e
channel = [
{
'version': data.get('version', 0),
'channel_name': data.get('name'),
'packed_time': data.get('packed_time'),
'release_time': data.get('release_time'),
'is_released': data.get('is_released'),
'download_url': '', # TODO 拼接下载链接
}
for data in channel_data]
return self.write_success(message='获取渠道打包列表成功', data=channel)
# def get(self, request):
# """
# 当前渠道已经打包的所有版本
# :param request:
# :return:
# """
# channel_id = request.GET.get('channel_id')
# if not channel_id:
# error_logger.error(u'参数不完整')
# return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整')
# try:
# channel_data = self.rpc['endpoint'](
# channel_id=channel_id
# ).unwrap()
# except Exception as e:
# error_logger.error(u'获取渠道打包列表失败:%s', e)
# raise e
# url = settings.APK_DOMAIN + settings.APK_BUILD_DIR + "/{version}/{name}/alpha_{name}.apk'"
# channel = [
# {
# 'version': data.get('version'),
# 'channel_name': data.get('name'),
# '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_data]
# return self.write_success(message='获取渠道打包列表成功', data=channel)
def post(self, request):
"""
......@@ -147,55 +181,63 @@ class ChannelPacked(APIView):
:param request:
:return:
"""
version2channel_id = request.POST.get('id')
if not version2channel_id:
version_id = request.POST.get('version_id')
channel_id = request.POST.get('channel_id')
if not all([channel_id, version_id]):
error_logger.error(u'参数不完整')
return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整')
# get version-channel object
version2channel = self.rpc['endpoint'](id=version2channel_id).unwrap()
version = version2channel['version']
channel = version2channel['channel']
ChannelPackage.publish_apk(channel, version)
channel = self.rpc['sun/channel_build/channel/get'](
id=channel_id
).unwrap()
version = self.rpc['sun/channel_build/version/get'](
id=version_id
).unwrap()
ChannelPackage.publish_apk(channel.get('url_name'), version.get('version'))
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:
error_logger.error(u'发布失败:%s', e)
return self.write_fail(code=-1, message='发布失败!')
return self.write_success(data={}, message='发布成功!')
class VersionPackedChannel(APIView):
def get(self, request):
"""
当前版本所有已打包渠道列表
:param request:
:return:
"""
version_id = request.GET.get('version__id', '')
page = int(request.GET.get('page', 1))
offset = int(request.GET.get('offset', 10))
if version_id:
version = self.rpc['endpoint'](version_id=version_id).unwrap()
# 母包地址 由七牛上传返回
base_apk_url = ChannelPackage.apk_url(channel='', version=version['version'])
else:
base_apk_url = ''
version = {}
version2channels = self.rpc['endpoint'](
version_id=version, start=(page - 1) * offset, offset=offset).unwrap()
data = {
'version': [{
'version': item.get('version'),
'name': item.get('name'),
'packed_time': item.get('packed_time'),
'download_url': '',
'release_time': item.get('release_time'),
'is_released': item.get('is_released'),
} for item in version2channels],
'base_apk_url': base_apk_url,
}
return self.write_success(data=data)
# class VersionPackedChannel(APIView):
# def get(self, request):
# """
# 当前版本所有已打包渠道列表
# :param request:
# :return:
# """
# version_id = request.GET.get('version_id', '')
# page = int(request.GET.get('page', 1))
# offset = int(request.GET.get('offset', 10))
# if version_id:
# version = self.rpc['sun/channel_build/version/get'](
# id=version_id
# ).unwrap()
# # 母包地址 由七牛上传返回
# base_apk_url = ChannelPackage.apk_url(channel='', version=version.get('version'))
# else:
# base_apk_url = ''
# version = {}
# version2channels = self.rpc['endpoint'](
# version_id=version, start=(page - 1) * offset, offset=offset).unwrap()
# url = settings.APK_DOMAIN + settings.APK_BUILD_DIR + "/{version}/{name}/alpha_{name}.apk'"
# data = {
# '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 version2channels],
# 'base_apk_url': base_apk_url,
# }
# return self.write_success(data=data)
class VersionList(APIView):
......@@ -209,19 +251,39 @@ class VersionList(APIView):
offset = int(request.GET.get('offset', 10))
filters = self.handle_filter(request.GET.get('filter', ""))
try:
version_list = self.rpc['endpoint'](
start=(page - 1) * offset, offset=offset, filters=filters
version_list = self.rpc['sun/channel_build/version/list'](
offset=page, limit=offset, filters=filters
).unwrap()
except Exception as e:
error_logger.error(u'获取版本列表失败:%s', e)
raise e
data = [
{
'version': item.get('version'),
'MD5': item.get('MD5'),
'release_time': item.get('release_time'),
} for item in version_list
]
data = {'total': version_list.get('total', 0)}
if filters.get('version_id', ''):
version = self.rpc['sun/channel_build/version/get'](
id=filters.get('version_id')
).unwrap()
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)
......@@ -236,12 +298,14 @@ class VersionDetailUpdate(APIView):
if not version_id:
error_logger.error(u'参数不完整')
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 = {
'version': version_info['version'],
'release_time': version_info['release_time'],
'md5': version_info['md5'],
'description': version_info['description'],
'version': version_info.get('version'),
'release_time': version_info.get('release_time'),
'md5': version_info.get('md5'),
'description': version_info.get('description'),
}
return self.write_success(data=data, message='操作成功')
......@@ -252,12 +316,26 @@ class VersionDetailUpdate(APIView):
:return:
"""
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')
if not version_id:
error_logger.error(u'参数不完整')
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:
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:
error_logger.error(e)
return self.write_fail(code=-1, message='操作失败')
......@@ -276,9 +354,55 @@ class BatchPublishVersion(APIView):
error_logger.error(u'参数不完整')
return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整')
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:
error_logger.error(u'获取渠道失败%s', e)
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='操作成功')
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
git+ssh://git@git.wanmeizhensuo.com/alpha/alpha-types.git@master
filetype==1.0.2
Pillow==5.4.1
celery==4.2.1
\ No newline at end of file
celery==4.2.1
requests==2.21.0
\ No newline at end of file
......@@ -158,4 +158,9 @@ OPERATOR_PASSWORD = 123456
# 图片下载的存储路径
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 django.conf import settings
import requests
from gm_upload.utils.qiniu_tool import QiniuTool
from middleware.rpc import rpc_invoker
from utils.channel_package_tools import ChannelPackage
from utils.logger import log_error
from utils.logger import log_error, info_logger
@shared_task
def version_release_task(packed_channels): # 一键发布,发布完成邮件通知
def version_release_task(packed_channels, version_id=None, version=None): # 一键发布,发布完成邮件通知
rpc = rpc_invoker
channels = []
for vtc in packed_channels:
version = vtc['version']
channel = vtc['channel']
vtc_id = vtc['id']
version = version
channel_ids = list()
for version2channel in packed_channels:
channel = version2channel['url_name']
channel_ids.append(version2channel.get('id'))
try:
ChannelPackage.publish_apk(channel, version)
rpc['endpoint'](version_id=vtc_id).unwrap()
channels.append(channel)
except:
log_error()
raise
# send email
rpc.origin['hera/channelbuild/version_release_notify'](
version=version,
channels=channels,
raise Exception('发布失败')
# 发布
rpc['sun/channel_build/version/release'](
version_id=version_id, channel_ids=channel_ids
).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