优化存储代码

pull/5/head
youngS 4 years ago
parent d1ea31cdba
commit 4d856bf5c0
  1. 7
      fir_client/src/components/FirApps.vue
  2. 2
      fir_ser/api/utils/app/iossignapi.py
  3. 129
      fir_ser/api/utils/app/supersignutils.py
  4. 18
      fir_ser/api/utils/utils.py
  5. 12
      fir_ser/api/views/apps.py

@ -701,8 +701,15 @@
return false; return false;
}, },
delApp() { delApp() {
let loadingobj = this.$loading({
lock: true,
text: '操作中,请耐心等待',
spinner: 'el-icon-loading',
// background: 'rgba(0, 0, 0, 0.7)'
});
this.willDeleteApp = false; this.willDeleteApp = false;
apputils(data => { apputils(data => {
loadingobj.close();
if (data.code === 1000) { if (data.code === 1000) {
for (let i = 0; i < this.applists.length; i++) { for (let i = 0; i < this.applists.length; i++) {
if (this.delapp.app_id === this.applists[i].app_id) { if (this.delapp.app_id === this.applists[i].app_id) {

@ -267,9 +267,9 @@ class AppDeveloperApiV2(object):
return False, result return False, result
def set_device_status(self, status, device_udid): def set_device_status(self, status, device_udid):
apple_obj = AppStoreConnectApi(self.issuer_id, self.private_key_id, self.p8key)
result = {} result = {}
try: try:
apple_obj = AppStoreConnectApi(self.issuer_id, self.private_key_id, self.p8key)
if status == "enable": if status == "enable":
device_obj = apple_obj.enabled_device(device_udid) device_obj = apple_obj.enabled_device(device_udid)
else: else:

@ -4,7 +4,7 @@
# author: liuyu # author: liuyu
# date: 2020/3/6 # date: 2020/3/6
import uuid, xmltodict, os, re, logging, time import uuid, xmltodict, os, re, logging, time, requests
from fir_ser.settings import SUPER_SIGN_ROOT, MEDIA_ROOT, SERVER_DOMAIN, MOBILECONFIG_SIGN_SSL from fir_ser.settings import SUPER_SIGN_ROOT, MEDIA_ROOT, SERVER_DOMAIN, MOBILECONFIG_SIGN_SSL
from api.utils.app.iossignapi import AppDeveloperApi, ResignApp, AppDeveloperApiV2 from api.utils.app.iossignapi import AppDeveloperApi, ResignApp, AppDeveloperApiV2
from api.models import APPSuperSignUsedInfo, AppUDID, AppIOSDeveloperInfo, AppReleaseInfo, Apps, APPToDeveloper, \ from api.models import APPSuperSignUsedInfo, AppUDID, AppIOSDeveloperInfo, AppReleaseInfo, Apps, APPToDeveloper, \
@ -14,11 +14,36 @@ from api.utils.serializer import get_developer_udided
from api.utils.storage.localApi import LocalStorage from api.utils.storage.localApi import LocalStorage
from api.utils.storage.caches import del_cache_response_by_short, send_msg_over_limit from api.utils.storage.caches import del_cache_response_by_short, send_msg_over_limit
from api.utils.utils import file_format_path, delete_app_to_dev_and_file, delete_app_profile_file, \ from api.utils.utils import file_format_path, delete_app_to_dev_and_file, delete_app_profile_file, \
send_ios_developer_active_status, get_profile_full_path send_ios_developer_active_status, get_profile_full_path, delete_local_files
from api.utils.storage.storage import Storage
logger = logging.getLogger(__file__) logger = logging.getLogger(__file__)
def check_org_file(user_obj, org_file):
if not os.path.isdir(os.path.dirname(org_file)):
os.makedirs(os.path.dirname(org_file))
if os.path.isfile(org_file):
return
storage_obj = Storage(user_obj)
download_url = storage_obj.get_download_url(os.path.basename(org_file), 600, key='check_org_file', force_new=True)
req = requests.get(download_url)
logger.info("download user %s file %s success" % (user_obj, org_file))
with open(org_file + ".check.tmp", "wb") as f:
f.write(req.content)
try:
if os.path.isfile(org_file):
os.remove(org_file)
os.rename(os.path.join(org_file + ".check.tmp"), org_file)
return True
except Exception as e:
logger.error("check org file and mv file %s failed Exception %s" % (org_file, e))
return False
def resign_by_app_obj(app_obj, need_download_profile=True): def resign_by_app_obj(app_obj, need_download_profile=True):
user_obj = app_obj.user_id user_obj = app_obj.user_id
if app_obj.issupersign and user_obj.supersign_active: if app_obj.issupersign and user_obj.supersign_active:
@ -327,6 +352,7 @@ class IosUtils(object):
auth, developer_app_id, device_id_lists) auth, developer_app_id, device_id_lists)
if add_did_flag: if add_did_flag:
result['did'] = sync_device_obj.serial result['did'] = sync_device_obj.serial
result['did_exists'] = True
if not status: if not status:
sign_try_attempts -= 1 sign_try_attempts -= 1
logger.warning("app %s developer %s sign failed %s .try again " % (app_obj, developer_obj, result)) logger.warning("app %s developer %s sign failed %s .try again " % (app_obj, developer_obj, result))
@ -355,6 +381,7 @@ class IosUtils(object):
app_dev_pem = file_format_path_name + ".pem" app_dev_pem = file_format_path_name + ".pem"
resign_app_obj = ResignApp(my_local_key, app_dev_pem) resign_app_obj = ResignApp(my_local_key, app_dev_pem)
org_file = os.path.join(MEDIA_ROOT, release_obj.release_id + ".ipa") org_file = os.path.join(MEDIA_ROOT, release_obj.release_id + ".ipa")
check_org_file(user_obj, org_file)
new_file = os.path.join(MEDIA_ROOT, random_file_name + ".ipa") new_file = os.path.join(MEDIA_ROOT, random_file_name + ".ipa")
properties_info = {} properties_info = {}
if app_obj.new_bundle_id: if app_obj.new_bundle_id:
@ -370,20 +397,22 @@ class IosUtils(object):
return False return False
@staticmethod @staticmethod
def update_sign_file_name(app_obj, developer_obj, release_obj, random_file_name): def update_sign_file_name(user_obj, app_obj, developer_obj, release_obj, random_file_name):
apptodev_obj = APPToDeveloper.objects.filter(developerid=developer_obj, app_id=app_obj).first() apptodev_obj = APPToDeveloper.objects.filter(developerid=developer_obj, app_id=app_obj).first()
storage_obj = Storage(user_obj)
if apptodev_obj: if apptodev_obj:
storage = LocalStorage("localhost", False) delete_local_files(apptodev_obj.binary_file + ".ipa")
storage.del_file(apptodev_obj.binary_file + ".ipa") storage_obj.delete_file(apptodev_obj.binary_file + ".ipa")
apptodev_obj.binary_file = random_file_name apptodev_obj.binary_file = random_file_name
apptodev_obj.release_file = release_obj.release_id apptodev_obj.release_file = release_obj.release_id
apptodev_obj.save() apptodev_obj.save()
else: else:
APPToDeveloper.objects.create(developerid=developer_obj, app_id=app_obj, APPToDeveloper.objects.create(developerid=developer_obj, app_id=app_obj,
binary_file=random_file_name, release_file=release_obj.release_id) binary_file=random_file_name, release_file=release_obj.release_id)
def update_sign_data(self, random_file_name, release_obj, result): storage_obj.upload_file(os.path.join(MEDIA_ROOT, random_file_name + ".ipa"))
def update_sign_data(self, random_file_name, release_obj):
newdata = { newdata = {
"is_signed": True, "is_signed": True,
"binary_file": random_file_name "binary_file": random_file_name
@ -405,30 +434,11 @@ class IosUtils(object):
developerid=self.developer_obj, developerid=self.developer_obj,
udid=AppUDID.objects.filter(app_id=self.app_obj, udid=AppUDID.objects.filter(app_id=self.app_obj,
udid=self.udid_info.get('udid')).first()) udid=self.udid_info.get('udid')).first())
# 更新新签名的ipa包
IosUtils.update_sign_file_name(self.user_obj, self.app_obj, self.developer_obj, release_obj, random_file_name)
del_cache_response_by_short(self.app_obj.app_id, udid=self.udid_info.get('udid')) del_cache_response_by_short(self.app_obj.app_id, udid=self.udid_info.get('udid'))
app_udid_obj = UDIDsyncDeveloper.objects.filter(developerid=self.developer_obj,
udid=self.udid_info.get('udid')).first()
if not app_udid_obj:
appudid_obj = AppUDID.objects.filter(app_id=self.app_obj, udid=self.udid_info.get('udid'))
device = appudid_obj.values("serial",
'product',
'udid',
'version').first()
if result.get("did", None):
device['serial'] = result["did"]
app_udid_obj = UDIDsyncDeveloper.objects.create(developerid=self.developer_obj, **device)
DeveloperDevicesID.objects.create(did=result["did"], udid=app_udid_obj, developerid=self.developer_obj,
app_id=self.app_obj)
else:
if result.get("did", None):
DeveloperDevicesID.objects.create(did=result["did"], udid=app_udid_obj, developerid=self.developer_obj,
app_id=self.app_obj)
# 更新新签名的ipa包
IosUtils.update_sign_file_name(self.app_obj, self.developer_obj, release_obj, random_file_name)
@staticmethod @staticmethod
def run_sign(user_obj, app_obj, developer_obj, download_flag, obj, d_time, result, resign=False): def run_sign(user_obj, app_obj, developer_obj, download_flag, obj, d_time, result, resign=False):
d_result = {'code': 0, 'msg': 'success'} d_result = {'code': 0, 'msg': 'success'}
@ -441,9 +451,9 @@ class IosUtils(object):
s_time1 = time.time() s_time1 = time.time()
logger.info("app_id %s exec sign ipa success. time:%s" % (app_obj, s_time1 - start_time)) logger.info("app_id %s exec sign ipa success. time:%s" % (app_obj, s_time1 - start_time))
if resign: if resign:
IosUtils.update_sign_file_name(app_obj, developer_obj, release_obj, random_file_name) IosUtils.update_sign_file_name(user_obj, app_obj, developer_obj, release_obj, random_file_name)
else: else:
obj.update_sign_data(random_file_name, release_obj, result) obj.update_sign_data(random_file_name, release_obj)
else: else:
msg = "app_id %s download profile failed. %s time:%s" % (app_obj, result, time.time() - start_time) msg = "app_id %s download profile failed. %s time:%s" % (app_obj, result, time.time() - start_time)
d_result['code'] = 1002 d_result['code'] = 1002
@ -504,6 +514,27 @@ class IosUtils(object):
if download_flag: if download_flag:
AppUDID.objects.update_or_create(app_id=self.app_obj, udid=self.udid_info.get('udid'), AppUDID.objects.update_or_create(app_id=self.app_obj, udid=self.udid_info.get('udid'),
defaults=self.udid_info) defaults=self.udid_info)
if not result.get("did_exists", None):
app_udid_obj = UDIDsyncDeveloper.objects.filter(developerid=self.developer_obj,
udid=self.udid_info.get('udid')).first()
if not app_udid_obj:
appudid_obj = AppUDID.objects.filter(app_id=self.app_obj, udid=self.udid_info.get('udid'))
device = appudid_obj.values("serial",
'product',
'udid',
'version').first()
if result.get("did", None):
device['serial'] = result["did"]
app_udid_obj = UDIDsyncDeveloper.objects.create(developerid=self.developer_obj, **device)
DeveloperDevicesID.objects.create(did=result["did"], udid=app_udid_obj,
developerid=self.developer_obj,
app_id=self.app_obj)
else:
if result.get("did", None):
DeveloperDevicesID.objects.create(did=result["did"], udid=app_udid_obj,
developerid=self.developer_obj,
app_id=self.app_obj)
return IosUtils.run_sign(self.user_obj, self.app_obj, self.developer_obj, download_flag, self, start_time, return IosUtils.run_sign(self.user_obj, self.app_obj, self.developer_obj, download_flag, self, start_time,
result) result)
@ -517,17 +548,7 @@ class IosUtils(object):
# 需要判断该设备在同一个账户下 的多个应用,若存在,则不操作 # 需要判断该设备在同一个账户下 的多个应用,若存在,则不操作
udid_lists = list(APPSuperSignUsedInfo.objects.values_list("udid__udid").filter(developerid=developer_obj)) udid_lists = list(APPSuperSignUsedInfo.objects.values_list("udid__udid").filter(developerid=developer_obj))
if udid_lists.count((udid_obj.udid,)) == 1: IosUtils.do_disable_device(developer_obj, udid_lists, udid_obj, auth)
app_api_obj = get_api_obj(auth)
app_api_obj.set_device_status("disable", udid_obj.udid)
UDIDsyncDeveloper.objects.filter(udid=udid_obj.udid, developerid=developer_obj).delete()
if developer_obj.use_number > 0:
developer_obj.use_number = developer_obj.use_number - 1
developer_obj.save()
usedeviceobj.delete()
# 通过开发者id判断 app_id 是否多个,否则删除profile 文件 # 通过开发者id判断 app_id 是否多个,否则删除profile 文件
if APPSuperSignUsedInfo.objects.filter(developerid=developer_obj, app_id_id=app_id).count() == 0: if APPSuperSignUsedInfo.objects.filter(developerid=developer_obj, app_id_id=app_id).count() == 0:
app_api_obj = get_api_obj(auth) app_api_obj = get_api_obj(auth)
@ -543,6 +564,19 @@ class IosUtils(object):
udid=udid_obj.udid).first() udid=udid_obj.udid).first()
DeveloperDevicesID.objects.filter(udid=app_udid_obj, developerid=developer_obj, app_id_id=app_id).delete() DeveloperDevicesID.objects.filter(udid=app_udid_obj, developerid=developer_obj, app_id_id=app_id).delete()
@staticmethod
def do_disable_device(developer_obj, udid_lists, udid_obj, auth):
if udid_lists.count((udid_obj.udid,)) == 1:
app_api_obj = get_api_obj(auth)
app_api_obj.set_device_status("disable", udid_obj.udid)
UDIDsyncDeveloper.objects.filter(udid=udid_obj.udid, developerid=developer_obj).delete()
if developer_obj.use_number > 0:
developer_obj.use_number = developer_obj.use_number - 1
developer_obj.save()
udid_obj.delete()
@staticmethod @staticmethod
def clean_udid_by_app_obj(app_obj, developer_obj): def clean_udid_by_app_obj(app_obj, developer_obj):
@ -553,18 +587,7 @@ class IosUtils(object):
for SuperSignUsed_obj in APPSuperSignUsedInfo.objects.filter(app_id=app_obj, developerid=developer_obj): for SuperSignUsed_obj in APPSuperSignUsedInfo.objects.filter(app_id=app_obj, developerid=developer_obj):
udid_obj = SuperSignUsed_obj.udid udid_obj = SuperSignUsed_obj.udid
IosUtils.do_disable_device(developer_obj, udid_lists, udid_obj, auth)
if udid_lists.count((udid_obj.udid,)) == 1:
app_api_obj = get_api_obj(auth)
app_api_obj.set_device_status("disable", udid_obj.udid)
UDIDsyncDeveloper.objects.filter(udid=udid_obj.udid, developerid=developer_obj).delete()
if developer_obj.use_number > 0:
developer_obj.use_number = developer_obj.use_number - 1
developer_obj.save()
udid_obj.delete()
SuperSignUsed_obj.delete() SuperSignUsed_obj.delete()
@staticmethod @staticmethod

@ -52,8 +52,9 @@ def delete_app_to_dev_and_file(developer_obj, app_id):
APPToDeveloper_obj = APPToDeveloper.objects.filter(developerid=developer_obj, app_id_id=app_id) APPToDeveloper_obj = APPToDeveloper.objects.filter(developerid=developer_obj, app_id_id=app_id)
if APPToDeveloper_obj: if APPToDeveloper_obj:
binary_file = APPToDeveloper_obj.first().binary_file + ".ipa" binary_file = APPToDeveloper_obj.first().binary_file + ".ipa"
lsobj = LocalStorage("localhost", False) delete_local_files(binary_file)
lsobj.del_file(binary_file) storage = Storage(developer_obj.user_id)
storage.delete_file(binary_file)
APPToDeveloper_obj.delete() APPToDeveloper_obj.delete()
@ -203,3 +204,16 @@ def send_ios_developer_active_status(user_info, msg):
get_sender_email_token(act, email, 'msg', msg) get_sender_email_token(act, email, 'msg', msg)
else: else:
logger.warning("user %s has no email. so %s can't send!" % (user_info, msg)) logger.warning("user %s has no email. so %s can't send!" % (user_info, msg))
def delete_local_files(filename, apptype=None):
storage = LocalStorage("localhost", False)
if apptype is not None:
if apptype == 0:
filename = filename + '.apk'
else:
filename = filename + '.ipa'
try:
return storage.del_file(filename)
except Exception as e:
logger.error("delete file %s failed Exception %s" % (filename, e))

@ -18,7 +18,7 @@ from rest_framework.pagination import PageNumberPagination
import logging import logging
from fir_ser.settings import SERVER_DOMAIN from fir_ser.settings import SERVER_DOMAIN
from api.utils.storage.caches import set_default_app_wx_easy from api.utils.storage.caches import set_default_app_wx_easy
from api.utils.utils import is_valid_domain from api.utils.utils import is_valid_domain, delete_local_files
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -120,7 +120,8 @@ class AppInfoView(APIView):
if app_id: if app_id:
apps_obj = Apps.objects.filter(user_id=request.user, app_id=app_id).first() apps_obj = Apps.objects.filter(user_id=request.user, app_id=app_id).first()
if apps_obj: if apps_obj:
if apps_obj.issupersign: count = APPToDeveloper.objects.filter(app_id=apps_obj).count()
if apps_obj.issupersign or count > 0:
logger.info("app_id:%s is supersign ,delete this app need clean IOS developer" % (app_id)) logger.info("app_id:%s is supersign ,delete this app need clean IOS developer" % (app_id))
IosUtils.clean_app_by_user_obj(apps_obj, request.user) IosUtils.clean_app_by_user_obj(apps_obj, request.user)
@ -137,6 +138,7 @@ class AppInfoView(APIView):
logger.info("delete app_id:%s need clean all release,release_id:%s" % ( logger.info("delete app_id:%s need clean all release,release_id:%s" % (
app_id, appreleaseobj.release_id)) app_id, appreleaseobj.release_id))
storage.delete_file(appreleaseobj.release_id, appreleaseobj.release_type) storage.delete_file(appreleaseobj.release_id, appreleaseobj.release_type)
delete_local_files(appreleaseobj.release_id, appreleaseobj.release_type)
storage.delete_file(appreleaseobj.icon_url) storage.delete_file(appreleaseobj.icon_url)
appreleaseobj.delete() appreleaseobj.delete()
apps_obj.delete() apps_obj.delete()
@ -327,17 +329,19 @@ class AppReleaseinfoView(APIView):
if not appreleaseobj.is_master: if not appreleaseobj.is_master:
logger.info("delete app release %s" % (appreleaseobj)) logger.info("delete app release %s" % (appreleaseobj))
storage.delete_file(appreleaseobj.release_id, appreleaseobj.release_type) storage.delete_file(appreleaseobj.release_id, appreleaseobj.release_type)
delete_local_files(appreleaseobj.release_id, appreleaseobj.release_type)
storage.delete_file(appreleaseobj.icon_url) storage.delete_file(appreleaseobj.icon_url)
appreleaseobj.delete() appreleaseobj.delete()
elif appreleaseobj.is_master and apprelease_count < 2: elif appreleaseobj.is_master and apprelease_count < 2:
logger.info("delete app master release %s and clean app %s " % (appreleaseobj, apps_obj)) logger.info("delete app master release %s and clean app %s " % (appreleaseobj, apps_obj))
count = APPToDeveloper.objects.filter(app_id=apps_obj).count()
if apps_obj.issupersign: if apps_obj.issupersign or count > 0:
logger.info("app_id:%s is supersign ,delete this app need clean IOS developer" % (app_id)) logger.info("app_id:%s is supersign ,delete this app need clean IOS developer" % (app_id))
IosUtils.clean_app_by_user_obj(apps_obj, request.user) IosUtils.clean_app_by_user_obj(apps_obj, request.user)
storage.delete_file(appreleaseobj.release_id, appreleaseobj.release_type) storage.delete_file(appreleaseobj.release_id, appreleaseobj.release_type)
delete_local_files(appreleaseobj.release_id, appreleaseobj.release_type)
storage.delete_file(appreleaseobj.icon_url) storage.delete_file(appreleaseobj.icon_url)
del_cache_by_delete_app(apps_obj.app_id) del_cache_by_delete_app(apps_obj.app_id)

Loading…
Cancel
Save