Files
Tools-H5/python/switch_bundle_common.py

197 lines
5.5 KiB
Python

"""Shared Vault authorization and bundle publishing for both environments."""
import os
import sys
import subprocess
import hashlib
from vault_options import parse_vault_options, read_switch_bundle
REGION_CONFIG = {
'dev': 'ap-east-1',
'prod': 'eu-west-3',
}
OSS_PATH_CONFIG = {
'dev': {
'waje': 's3://web-h5game-test-waje/internal/',
'wtan': 's3://web-h5game-test-wtan/internal/',
},
'prod': {
'waje': 's3://web-h5game-prod/internal/',
'wtan': ''
},
}
PROFILE_CONFIG = {
'dev': {
'waje': 'h5-new',
'wtan': 'h5-new',
},
'prod': {
'waje': 'default',
'wtan': 'h5-new',
}
}
VAULT_ADDR = 'https://keys.i-see--you.icu'
VAULT_TOKEN = 'hvs.CAESIJrOb65T9ylAMyzlqQOJjI-yutFmTysRbvYbN3IpGra5Gh4KHGh2cy5GeXU4N3FuVEhIQ3A5YmpIM1BETFdYMDc'
BUNDLE_CONFIG = {
'6001': 'whot',
'6002': 'whot',
'6003': 'whot',
'6005': 'BRWhot',
'2001': 'roulette',
'2002': 'wajeSpin',
'2003': 'bottleSpin',
'2004': 'blackjack',
'2006': 'betterjack',
'6007': 'BRWhot',
'2008': 'slots',
'2005': 'christmas',
}
UNLINK_ARRAY = [
'cocos2d-js.109f3.js',
'cocos2d-js-min.13181.js',
'physics.9d2de.js',
'physics-min.ce5ee.js',
'style-desktop.dfd76.css',
'style-mobile.6e9cd.css',
'splash.85cfd.png',
]
def create_vault_client(**kwargs):
"""Create the default Vault client only when a real publish is run."""
import hvac
return hvac.Client(**kwargs)
def remove_folder(path):
print('remove path: ', path)
if os.path.exists(path):
if os.path.isfile(path) or os.path.islink(path):
os.remove(path)
else:
for filename in os.listdir(path):
remove_folder(os.path.join(path, filename))
os.rmdir(path)
def run_publish(proj_dir, subgame_path, region, profile,
run_command=subprocess.run):
"""Upload shared resources and bundles, then publish each game's shell."""
build_dir = proj_dir + '\\build\\web-mobile\\'
src_dir = build_dir + '\\src\\'
src_dst = subgame_path + 'lib/src/'
remove_result = run_command(
['cmd', '/c', 'aws s3 rm ' + src_dst + ' --recursive' + ' --profile ' + profile + ' --region ' + region],
capture_output=True,
text=True,
)
print('remove dir src result: ', remove_result)
command = (
'call aws s3 cp ' + src_dir + ' ' + src_dst
+ ' --recursive'
+ ' --profile ' + profile
+ ' --region ' + region
)
result = run_command(
['cmd', '/c', command],
capture_output=True,
text=True,
)
print('upload dir src result: ', result)
remove_folder(src_dir)
for filename in os.listdir(build_dir):
file_path = os.path.join(build_dir, filename)
if filename in UNLINK_ARRAY:
os.remove(file_path)
for gameid in BUNDLE_CONFIG:
aws_path = (
subgame_path + gameid + '/assets/'
+ BUNDLE_CONFIG[gameid] + '/'
)
remove_result = run_command(
['cmd', '/c', 'aws s3 rm ' + aws_path + ' --recursive' + ' --profile ' + profile + ' --region ' + region],
capture_output=True,
text=True,
)
print('remove game gameid: ', gameid, ' result: ', remove_result)
subgame_dir = build_dir + 'assets\\' + BUNDLE_CONFIG[gameid] + '\\'
command = (
'call aws s3 cp ' + subgame_dir + ' ' + aws_path
+ ' --recursive'
+ ' --profile ' + profile
+ ' --region ' + region
)
result = run_command(
['cmd', '/c', command],
capture_output=True,
text=True,
)
print(result)
for gameid in BUNDLE_CONFIG:
subgame_dir = build_dir + 'assets\\' + BUNDLE_CONFIG[gameid]
remove_folder(subgame_dir)
for gameid in BUNDLE_CONFIG:
aws_path = subgame_path + gameid + '/'
command = (
'call aws s3 cp ' + build_dir + ' ' + aws_path
+ ' --recursive'
+ ' --profile ' + profile
+ ' --region ' + region
)
result = run_command(
['cmd', '/c', command],
capture_output=True,
text=True,
)
print('upload game gameid: ', gameid, ' result: ', result)
def main(argv, client_factory=None, publish=None):
"""Parse the legacy CLI arguments and publish when Vault permits it."""
proj_dir, pwd, env, cat, vault_path, vault_mount_point = parse_vault_options(argv)
if len(argv) < 5:
print('argv error!')
sys.exit(-1)
md5_obj = hashlib.md5()
md5_obj.update((env + cat).encode('utf-8'))
hash_value = md5_obj.hexdigest()
print('hash = ' + hash_value)
if hash_value != pwd:
print('argv verify failed!')
sys.exit(-2)
if client_factory is None:
client_factory = create_vault_client
if publish is None:
publish = run_publish
client = client_factory(url=VAULT_ADDR, token=VAULT_TOKEN)
if client.is_authenticated():
secret = read_switch_bundle(client, vault_path, vault_mount_point)
switch_bundle = secret['data']['data']['enable']
print(switch_bundle)
if '1' != switch_bundle:
print('script exit, switchBundle status is ' + switch_bundle)
sys.exit(-999)
else:
print('running script failed!')
sys.exit(-1000)
print('start running script...')
publish(proj_dir, OSS_PATH_CONFIG[env][cat], REGION_CONFIG[env], PROFILE_CONFIG[env][cat])