def nginx_config_init(): content = '' with open('%s/buildin/nginx.template' % (config.const_client_root()), 'r') as template_file: content = template_file.read() content = content.replace('##CONTENT_ROOT##', config.const_client_root()) with open('%s/buildin/nginx.conf' % (config.const_client_root()), 'w') as template_file: content = template_file.write(content)
def update_product(self): device_info = self.get_device_info() if util.get_cached_version('product') is not None: if device_info.get('need_update_content') is None: return if device_info.get('need_update_content') == False: return product_info_url = '%s/%s/Product/GetAll' % (util.util_remote_service( config.const_api_name_product), config.const_api_name_product) print(product_info_url) product_info_json = requests.get(product_info_url).json() product_info_text = json.dumps(product_info_json, ensure_ascii=False) #print(product_info_json) # download image to target folder for image in product_info_json['downloadList']: try: util.download_file_to_target( image, '%s/Content/ProductResources/' % (config.const_client_root()), True) except Exception as err: print(err) # save data file to target folder data_js = '%s/Content/ProductResources/data.js' % ( config.const_client_root()) with open(data_js, 'w+', encoding='utf-8') as data_file: data_file.write(product_info_text) pro_info = { 'BasicPath': '%s/Content/ProductResources/' % (config.const_client_root()), 'DataJsonPath': data_js, 'HostBasicPath': '%s/Content/ProductResources' % (config.const_client_web_server_root), 'HostDataJsonPath': '%s/Content/ProductResources/data.js' % (config.const_client_web_server_root), 'DataJson': product_info_text } util.set_cached_version('product_info', json.dumps(pro_info, ensure_ascii=False)) # tell server product has been updated product_update_done_url = '%s/%s/Device/StopUpdateContent?deviceId=%s' % ( util.util_remote_service(config.const_api_name_resouce), config.const_api_name_resouce, config.const_service_id) feedback = requests.post(product_update_done_url, json={}) util.set_cached_version('product', 'inited')
def download_extract_target_cut(download_uri, target_dir, is_overwirte): tmp_file = '%s/buildin/tmp/%s' % (config.const_client_root(), str(uuid.uuid1())) subprocess.call(['curl', '-o', tmp_file, download_uri]) subprocess.call(['rm -rf ' + target_dir+'/*'],shell=True) subprocess.call(['unzip', '-o', tmp_file, '-d', target_dir]) subprocess.call(['mv ' + target_dir + '/*/*' + ' '+target_dir],shell=True) subprocess.call(['rm', '-f', tmp_file])
def get_cached_version(resource): ver = '%s/buildin/vers/%s.ver' % (config.const_client_root(), resource) if os.path.exists(ver): with open(ver, 'r') as content_file: content = content_file.read() return content else: return None
def contentInfos(): util.log_info('web_api_server', 'recieve call for contentInfos') applist = [] dr = '%s/buildin/vers' % (config.const_client_root()) for root, dirs, files in os.walk(dr, topdown=False): for name in files: if name.startswith('rv_'): ver_name = name.replace('.ver', '') applist.append(json.loads(util.get_cached_version(ver_name))) return jsonify(applist)
def download_extract_target(download_uri, target_dir, is_overwirte): tmp_file = '%s/buildin/tmp/%s' % (config.const_client_root(), str(uuid.uuid1())) if is_overwirte == True: path = target_dir + '/*' subprocess.call(['rm', '-f',path]) #subprocess.call(['mkdir',tmp_file]) subprocess.call(['curl', '-o', tmp_file, download_uri]) subprocess.call(['unzip', '-o', tmp_file, '-d', target_dir]) subprocess.call(['rm', '-f', tmp_file])
def __worker_thread(self): while True: try: dr = '%s/buildin/logs'%(config.const_client_root()) current_log_prefix = 'service_log_' + util.current_log_file for root, dirs, files in os.walk(dr, topdown=False): for name in files: if name.startswith(current_log_prefix) == False: self.upload_file(name) time.sleep(60) except Exception as err: util.log_error('downloader',err) time.sleep(60)
def qrByUnique(unique): util.log_info('web_api_server', 'recieve call for qrByUnique') ret_obj = { 'Id': str(uuid.uuid1()), 'FileName': unique, 'FileUrl': '%s/Content/QrCodeResources/%s' % (config.const_client_root(), unique), 'HostUrl': '%s/Content/QrCodeResources/%s' % (config.const_client_web_server_root, unique) } return json.dumps(ret_obj)
def get_default_start(self): try: file_path = '%s/Content/AppContents/app_info.json' % ( config.const_client_root()) if os.path.exists(file_path): with open(file_path, 'r', encoding='utf-8') as data_file: str_content = data_file.read() app_list = json.loads(str_content) for app in app_list: if app['isStart'] == True: f = util.find_file( app['startPath'], '%s/Content/AppContents/%s' % (config.const_client_root(), str( app['appId']))) finnal = f.replace(config.const_client_root(), '') config.current_app_id = app['appId'] return app['appId'], '%s%s' % ( config.const_client_web_server_root, finnal) return None else: return None except Exception as err: print(err)
def qrInfos(): util.log_info('web_api_server', 'recieve call for qrInfos') ret = [] for root, dirs, files in os.walk('%s/content/QrCodeResources/'): for name in files: item = { 'Id': str(uuid.uuid1()), 'FileName': name, 'FileUrl': '%s/Content/QrCodeResources/%s' % (config.const_client_root(), name), 'HostUrl': '%s/Content/QrCodeResources/%s' % (config.const_client_web_server_root, name) } ret.append(item) return json.dumps(ret)
def update_qr_code(self): if config.const_service_id is None: return # Qrcode device_info = self.get_device_info() print(device_info) # QR version device_info_version = int( re.findall(r'\b\d.*\d\b', device_info['wechat_qrcode_zip_url'])[0]) print(device_info_version) cached_ver = -1 if util.get_cached_version('QR') is not None: cached_ver = int(util.get_cached_version('QR')) # download to update if device_info_version > cached_ver: util.download_extract_target( device_info['wechat_qrcode_zip_url'], '%s/Content/QrCodeResources' % (config.const_client_root()), True) util.set_cached_version('QR', device_info_version) print(device_info)
def nginx_reload_start(): subprocess.call(['sudo','nginx', '-s', 'stop']) subprocess.call(['sudo','nginx', '-c', '%s/buildin/nginx.conf' % (config.const_client_root())])
def remove_cached_version(resource): ver = '%s/buildin/vers/%s.ver' % (config.const_client_root(), resource) if os.path.exists(ver): os.remove(ver) return True
def set_cached_version(resource, value): ver = '%s/buildin/vers/%s.ver' % (config.const_client_root(), resource) if os.path.exists(ver): os.remove(ver) with open(ver, 'w+') as content_file: content_file.write(str(value))
def upload_file(self,file_name): full_log_path = "%s/buildin/logs/%s"%(config.const_client_root(),file_name) target_file_name = "%s_%s"%(config.const_service_id,file_name) cmd = "azcopy --source " + full_log_path + " --destination https://rsdisprd.blob.core.chinacloudapi.cn/dis-client-log/" + target_file_name + " --dest-key 'cPluFpf91Uw/C+8QtNNN8Y669tZrFDiAr3NQTEbe6aWdHvq7LgRXUThxjdbAyPr4C2IKbxr4WYd0/lsmnB751g==' --quiet" subprocess.call([cmd],shell=True) subprocess.call(['rm -f ' + full_log_path ],shell=True)
def update_apps(self): #get app info url app_url_info = '%s/%s/AppContent/getNewestVersionOfZipBy?appKey=%s' % ( util.util_remote_service(config.const_api_name_webcontent), config.const_api_name_webcontent, config.const_service_id) #get app entitys util.log_info('update_apps', app_url_info) apps_info = requests.get(app_url_info).json() #check app is any version different isNeedReloadChrome = False for app in apps_info: local_ver = util.get_cached_version('app_' + str(app['appId'])) if local_ver is None or local_ver != app['version']: isNeedReloadChrome = True target_dir = '%s/Content/AppContents/' % ( config.const_client_root()) target_dir = target_dir + str(app['appId']) subprocess.call(['mkdir', target_dir]) # perform update util.download_extract_target_cut(app['zipPath'], target_dir, True) util.set_cached_version('app_' + str(app['appId']), str(app['version'])) rv = { 'AppId': app['appId'], #'AppName' = appInfo.AppName, 'AppName': app['appNameCN'], 'StartPath': '%s/Content/AppContents/%s/%s' % (config.const_client_web_server_root, str( app['appId']), app['startPath']), #'Type' = appInfo.Type, 'Version': app['version'], 'BasicDirectory': '%s/Content/AppContents/%s' % (config.const_client_web_server_root, str(app['appId'])), 'IsStart': app['isStart'], # gck 'Icon': app['icon'].replace( "https://rsdisuat.blob.core.chinacloudapi.cn/image/", "") } util.set_cached_version('rv_' + str(app['appId']), json.dumps(rv, ensure_ascii=False)) #remove data file dr = '%s/buildin/vers' % (config.const_client_root()) for root, dirs, files in os.walk(dr, topdown=False): for name in files: if name.startswith('app_'): verId = name.replace('.ver', '') verId = int(verId.replace('app_', '')) isRemoveThisVer = True for app in apps_info: if app["appId"] == verId: isRemoveThisVer = False if isRemoveThisVer == True: util.remove_cached_version('app_' + str(verId)) util.remove_cached_version('rv_' + str(verId)) isNeedReloadChrome = True curr_default_app, curr_start_path = self.get_default_start() #save data file with open('%s/Content/AppContents/app_info.json' % (config.const_client_root()), 'w+', encoding='utf-8') as data_file: data_file.write(json.dumps(apps_info, ensure_ascii=False)) #reload chrome default_app, start_path = self.get_default_start() if curr_default_app != default_app and isNeedReloadChrome == False: isNeedReloadChrome = True util.log_info("downloader", 'get default apps') if default_app is not None and isNeedReloadChrome == True: msg = {'MessageType': 0, 'MessageData': start_path} web_socket.instance.send(json.dumps(msg))