Commit 8edffe7c authored by 张永's avatar 张永

和并提交

parent 43b47139
......@@ -7,7 +7,7 @@ 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, channel_build
from utils.channel_task import version_release_task, channel_build, channel_build_one
def get_md5_value(src):
......@@ -45,35 +45,58 @@ class ChannelList(APIView):
limit = int(request.GET.get('limit', 10))
filters = self.handle_filter(request.GET.get('filter', ""))
try:
channel_list = self.rpc['sun/channel_build/channel/list'](
offset=page, limit=limit, filters=filters
channel_list = self.rpc['venus/sun/channel_build/channel/list'](
offset=(page-1)*limit, limit=limit, filters=filters
).unwrap()
except Exception as e:
error_logger.error(u'获取渠道列表失败:%s', e)
raise e
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'"
url = settings.APK_DOMAIN + settings.APK_RELEASE_DIR + "/{name}/gm_alpha_{name}.apk"
channels = [
{
'version': data.get('version'),
'id': data.get('id'),
'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')),
'url_name': data.get('url_name'),
'pack': data.get('pack'),
'released_version': data.get('released_version'),
'download_url': url.format(name=data.get('url_name')),
}
for data in channel_list.get('data', [])]
else:
url = settings.APK_DOMAIN + settings.APK_RELEASE_DIR + "/{name}/alpha_{name}.apk'"
data.update({'channel': channels})
return self.write_success(message='获取渠道列表成功', data=data)
class ChannelVersionList(APIView):
def get(self, request):
"""
:param request:
:return:
"""
channel_id = request.GET.get('channel_id')
page = int(request.GET.get('page', 1))
limit = int(request.GET.get('limit', 10))
try:
channel_list = self.rpc['venus/sun/channel_build/channel_version/list'](
offset=(page-1)*limit, limit=limit, channel_id=channel_id
).unwrap()
except Exception as e:
error_logger.error(u'获取渠道列表失败:%s', e)
raise e
data = {'total': channel_list.get('total', 0)}
url = settings.APK_DOMAIN + settings.APK_BUILD_DIR + "/{version}/{name}/gm_alpha_{name}.apk"
channels = [
{
'id': data.get('id'),
'version_id': data.get('version_id'),
'channel_id': data.get('channel_id'),
'version': data.get('version'),
'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')),
'packed_time': data.get('packed_time'),
'packed': data.get('packed'),
'release_time': data.get('release_time'),
'is_released': data.get('is_released'),
'download_url': url.format(version=data.get('version'), name=data.get('url_name')),
}
for data in channel_list.get('data', [])]
data.update({'channel': channels})
......@@ -92,19 +115,18 @@ class Channel(APIView):
error_logger.error(u'参数不完整')
return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整')
try:
channel_data = self.rpc['sun/channel_build/channel/get'](
channel_data = self.rpc['venus/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('id'),
'channel_name': channel_data.get('name'),
'id': channel_data.get('id'),
'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'),
'pack': channel_data.get('pack'),
'type': channel_data.get('type'),
'released_version': channel_data.get('released_version'),
}
return self.write_success(message='获取渠道详情成功', data=channel)
......@@ -115,13 +137,12 @@ class Channel(APIView):
:param request:
:return:
"""
channel_id = request.POST.get('channel_id', '')
channel_id = request.POST.get('id', None)
channel_name = request.POST.get('name', '')
url_name = request.POST.get('url_name', '')
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_id, channel_name, url_name, channel_vendor]):
if not all([channel_name, url_name, auto_pack, channel_type]):
error_logger.error(u'参数不完整')
return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整')
data = dict()
......@@ -131,50 +152,22 @@ class Channel(APIView):
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'](
resp = self.rpc['venus/sun/channel_build/channel/edit'](
id=channel_id, data=data
).unwrap()
except RPCFaultException as e:
raise e
error_logger.error('操作失败%s', e)
return self.write_fail(message='操作失败', code=-1)
version = resp.get('version', '')
if version:
channel_build_one.delay(version, url_name)
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
# 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):
"""
当前版本的渠道包发布
......@@ -187,16 +180,16 @@ class ChannelPacked(APIView):
error_logger.error(u'参数不完整')
return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整')
channel = self.rpc['sun/channel_build/channel/get'](
channel = self.rpc['venus/sun/channel_build/channel/get'](
id=channel_id
).unwrap()
version = self.rpc['sun/channel_build/version/get'](
version = self.rpc['venus/sun/channel_build/version/get'](
id=version_id
).unwrap()
ChannelPackage.publish_apk(channel.get('url_name'), version.get('version'))
try:
self.rpc['sun/channel_build/version/release'](
version_id=version_id, channel_ids=channel_id
self.rpc['venus/sun/channel_build/version/release'](
version_id=version_id, channel_ids=[channel_id]
).unwrap() #发布
except RPCFaultException as e:
error_logger.error(u'发布失败:%s', e)
......@@ -204,42 +197,6 @@ class ChannelPacked(APIView):
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['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):
def get(self, request):
"""
......@@ -249,40 +206,54 @@ class VersionList(APIView):
"""
page = int(request.GET.get('page', 1))
offset = int(request.GET.get('offset', 10))
filters = self.handle_filter(request.GET.get('filter', ""))
try:
version_list = self.rpc['sun/channel_build/version/list'](
offset=page, limit=offset, filters=filters
version_list = self.rpc['venus/sun/channel_build/version/list'](
offset=(page-1)*offset, limit=offset
).unwrap()
except Exception as e:
error_logger.error(u'获取版本列表失败:%s', e)
raise e
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')
versions = [
{
'version_id': item.get('id'),
'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)
class VersionChannelList(APIView):
def get(self, request):
page = int(request.GET.get('page', 1))
offset = int(request.GET.get('offset', 10))
version_id = request.GET.get('version_id', 10)
try:
version_list = self.rpc['venus/sun/channel_build/version_channel/list'](
offset=(page-1)*offset, limit=offset, version_id=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'"
except Exception as e:
error_logger.error(u'获取版本列表失败:%s', e)
raise e
data = {'total': version_list.get('total', 0)}
url = settings.APK_DOMAIN + settings.APK_BUILD_DIR + "/{version}/{name}/gm_alpha_{name}.apk"
versions = {
'version': [{
'channel_id': item.get('channel_id'),
'version_id': item.get('version_id'),
'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')),
'packed': item.get('packed'),
'download_url': url.format(version=item.get('version'), name=item.get('url_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,
'base_apk_url': ChannelPackage.apk_url(channel='', version=version_list.get('version')),
}
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)
......@@ -298,10 +269,11 @@ class VersionDetailUpdate(APIView):
if not version_id:
error_logger.error(u'参数不完整')
return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整')
version_info = self.rpc['sun/channel_build/version/get'](
version_info = self.rpc['venus/sun/channel_build/version/get'](
id=version_id
).unwrap()
data = {
'id': version_info.get('id'),
'version': version_info.get('version'),
'release_time': version_info.get('release_time'),
'md5': version_info.get('md5'),
......@@ -315,11 +287,11 @@ class VersionDetailUpdate(APIView):
:param request:
:return:
"""
version_id = request.POST.get('version_id')
version_id = request.POST.get('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('description')
if not version_id:
error_logger.error(u'参数不完整')
return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整')
......@@ -333,10 +305,10 @@ class VersionDetailUpdate(APIView):
if md5:
data.update({'md5': md5})
try:
self.rpc['sun/channel_build/version/edit'](
self.rpc['venus/sun/channel_build/version/edit'](
id=version_id, data=data
)
except RPCFaultException as e:
).unwrap()
except Exception as e:
error_logger.error(e)
return self.write_fail(code=-1, message='操作失败')
return self.write_success(data={}, message='操作成功')
......@@ -355,7 +327,7 @@ class BatchPublishVersion(APIView):
return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整')
try:
# 获取当前版本所有已打包渠道
channel_info = self.rpc['sun/channel_build/version/get_channel_info'](
channel_info = self.rpc['venus/sun/channel_build/version/get_channel_info'](
version_id=version_id
).unwrap()
except RPCFaultException as e:
......@@ -389,15 +361,14 @@ class VersionCreate(APIView):
data.update({'version': version})
if md5:
data.update({'md5': md5})
try:
version_id = self.rpc['sun/channel_build/version/edit'](
version_id = self.rpc['venus/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'](
channel_info = self.rpc['venus/sun/channel_build/version/get_channel_info'](
version_id=version_id
).unwrap() # 所有渠道的url_name: list()
url_names = []
......
......@@ -108,12 +108,17 @@ urlpatterns = [
url(r'^advertise/create$', AdvertiseCreateView.as_view()),
# 渠道打包相关API
url(r'^channel/get', Channel.as_view()),
url(r'^channel/create', Channel.as_view()),
url(r'^channel/detail', Channel.as_view()),
url(r'^channel/vendor/edit', Vendor.as_view()),
url(r'^channel/packed/get', ChannelPacked.as_view()),
url(r'^channel/packed/create', ChannelPacked.as_view()),
url(r'^channel_build/channel/get', ChannelList.as_view()),
url(r'^channel_build/channel_version/get', ChannelVersionList.as_view()),
url(r'^channel_build/channel/detail', Channel.as_view()),
url(r'^channel_build/channel/edit', Channel.as_view()),
url(r'^channel_build/build', ChannelPacked.as_view()),
url(r'^channel_build/version/get', VersionList.as_view()),
url(r'^channel_build/version_channel/get', VersionChannelList.as_view()),
url(r'^channel_build/version/detail', VersionDetailUpdate.as_view()),
url(r'^channel_build/version/update', VersionDetailUpdate.as_view()),
url(r'^channel_build/batch/release', BatchPublishVersion.as_view()),
url(r'^channel_build/version/create', VersionCreate.as_view()),
]
search_urlpatterns = [
......
......@@ -162,5 +162,5 @@ DOWNLOAD_IMAGE_PATH = u'/data/header-images/'
APK_SCOPE = 'download'
APK_DOMAIN = 'http://dl.igengmei.com/'
DEFAULT_CHANNEL = 'benzhan'
APK_RELEASE_DIR = 'sun'
APK_BUILD_DIR = 'sun_build'
APK_RELEASE_DIR = 'test'
APK_BUILD_DIR = 'testbuild'
......@@ -4,52 +4,90 @@ import re
from django.conf import settings
from gm_upload.utils.qiniu_tool import QiniuTool
from .logger import log_error
from qiniu import put_file
from utils.logger import log_error
class QiniuFileTool(QiniuTool):
@classmethod
def upload_file(cls, file_path, save_name, bucket_name):
"""
上传文件到七牛
:param file_path: 文本路径
:param save_name: 上传的名字
:param bucket_name: 空间名
:return:
"""
token = cls.q.upload_token(bucket_name)
for _ in range(5): # 多次尝试
ret, response_info = put_file(token, save_name, file_path)
if ret and "key" in ret and response_info.status_code == 200:
# assert ret["key"] == save_name
# assert ret["hash"] == etag(file_path)
return {
"file": ret["key"]
}
raise Exception('upload filed')
class ChannelPackage(object):
# 渠道识别前缀
channel_prefix = 'gmchannel'
channel_prefix = 'gm_alpha'
def __init__(self, api_data, version):
self.apk_data = api_data
self.version = version
self.apk = None
@classmethod
def get_parent_path(cls, data):
path = os.getcwd() + '/' + cls.channel_prefix + '_parent.apk'
with open(path, 'wb')as fw:
fw.write(data)
return path
def build_channel_apk(self, channel_name):
cp = re.compile(r'[^{}]+')
self.apk = self.channel_prefix + '.apk'
walle_cli_path = os.getcwd() + '/walle-cli-all.jar'
with open(self.apk, 'wb') as fw:
fw.write(self.apk_data)
fw.write(self.apk_data) # 母包
os.system('java -jar {0} batch -c {1} {2}'.format(walle_cli_path, channel_name, self.apk))
with open(self.channel_prefix + '_' + channel_name + '.apk', 'rb') as fr:
data = fr.read()
# with open(self.channel_prefix + '_' + channel_name + '.apk', 'rb') as fr:
# data = fr.read()
targe_apk = self.channel_prefix + '_' + channel_name + '.apk'
cmd_res = os.popen('java -jar walle-cli-all.jar show %s' % targe_apk)
ret_res = cmd_res.readlines()[0]
target_channel_info = cp.findall(ret_res)[1]
if target_channel_info and channel_name == target_channel_info.split('=')[1]:
return data
target_path = self.channel_prefix + '_' + channel_name + '.apk'
os.remove(self.apk)
return target_path
else:
log_error()
os.remove(self.apk)
os.remove(self.channel_prefix + '_' + channel_name + '.apk')
def upload_apk(self, channel_name):
chn_apk_data = self.build_channel_apk(channel_name)
chn_apk_path = self.build_channel_apk(channel_name)
apk_uri = self.apk_key(channel_name, self.version)
QiniuTool.delete(apk_uri, settings.APK_SCOPE)
ret = QiniuTool.upload(chn_apk_data, apk_uri, settings.APK_SCOPE)
ret = QiniuFileTool.upload_file(chn_apk_path, apk_uri, settings.APK_SCOPE)
os.remove(chn_apk_path)
return ret
@classmethod
def apk_key(cls, channel, version):
build_dir = getattr(settings, 'APK_BUILD_DIR', '')
if channel:
uri = '{version}/{channel}/gengmei_{channel}.apk'.format(version=version, channel=channel)
uri = '{version}/{channel}/gm_alpha_{channel}.apk'.format(version=version, channel=channel)
else:
uri = '{version}/gengmei.apk'.format(version=version)
uri = '{version}/gm_alpha.apk'.format(version=version)
if build_dir:
uri = '{build_dir}/{uri}'.format(build_dir=build_dir, uri=uri)
return uri
......@@ -65,8 +103,10 @@ class ChannelPackage(object):
apk_scope = settings.APK_SCOPE
version_key = cls.apk_key(channel, version)
current_key = cls.apk_key(channel, settings.APK_RELEASE_DIR)
print('current_key:', current_key)
ret, info = QiniuTool.bucket.stat(apk_scope, current_key)
if ret:
print('delete')
QiniuTool.delete(current_key, apk_scope)
QiniuTool.copy(version_key, current_key, apk_scope, apk_scope)
if need_refresh:
......
import os
from celery import shared_task
from django.conf import settings
import requests
......@@ -5,7 +6,7 @@ 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.channel_package_tools import ChannelPackage, QiniuFileTool
from utils.logger import log_error, info_logger
......@@ -23,7 +24,7 @@ def version_release_task(packed_channels, version_id=None, version=None): # 一
log_error()
raise Exception('发布失败')
# 发布
rpc['sun/channel_build/version/release'](
rpc['venus/sun/channel_build/version/release'](
version_id=version_id, channel_ids=channel_ids
).unwrap()
# email notify
......@@ -39,20 +40,21 @@ def channel_build_one(version=None, channel=None, data=None):
@shared_task
def channel_build(version=None, channels=None, data=None):
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)
target_path = ChannelPackage.get_parent_path(data)
QiniuFileTool.upload_file(target_path, url, settings.APK_SCOPE) # 上传母包
if not channels:
# channels = [settings.DEFAULT_CHANNEL]
return
info_logger.debug(channels)
chn_pkg = ChannelPackage(data, version)
# 遍历渠道号并创建对应渠道号的apk文件
for channel in channels:
build_one_channel(chn_pkg, channel)
os.remove(target_path)
def build_one_channel(pkg_obj, channel):
......@@ -63,7 +65,7 @@ def build_one_channel(pkg_obj, channel):
ret = pkg_obj.upload_apk(channel)
info_logger.debug(ret)
if 'file' in ret:
# TODO 打包
rpc_invoker['endpoint'](
version=pkg_obj.version, channel=channel
# 打包
rpc_invoker['venus/sun/channel_build/channel/pack'](
version=pkg_obj.version, url_name=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