# !/d/ymq/py3/bin/python import os import time import sh from appPublic.folderUtils import listFile, _mkdir from appPublic.timeUtils import getCurrentTimeStamp, TimeStamp home = os.getenv('HOME') kgadget_build = False class Node: def __init__(self, ip, mode): self.host = ip self.user = 'kboss' + mode self.target = f'{self.user}@{self.host}' def scp_l2r(self, lf, rf): sh.scp(lf, f'{self.target}:{rf}') def exe(self, cmd): sh.ssh(self.target, cmd) def buildModule(path): global kgadget_build os.chdir(path) txt = sh.git('pull') if txt.startswith('Already'): print(f'{path} {txt}') return try: x = sh.pip('install', '-r', 'requirements.txt') except: pass x = sh.pip('install', '.') kgadget_build = True print(x) def get_deploy_timestamp(): deploy_file = f'{kboss_path}/.deploy/deploy.time' _mkdir(f'{kboss_path}/.deploy') if not os.path.exists(deploy_file): set_deploy_timestamp() with open(deploy_file, 'r') as f: b = f.read() return b return '0001-01-01' def set_deploy_timestamp(): deploy_file = f'{kboss_path}/.deploy/deploy.time' _mkdir(f'{kboss_path}/.deploy') with open(deploy_file, 'w') as f: f.write(getCurrentTimeStamp()) def new_modified(path, ts): # ts = get_deploy_timestamp() for f in listFile(path, rescursive=True): t = time.localtime(os.path.getmtime(f)) fts = TimeStamp(t) if fts > ts: return True return False def build_kgadget(mode, ts): global kgadget_build x = new_modified(f'{kboss_path}/kgadget', ts) if x: kgadget_build = True if not kgadget_build: print('kgadget is up to date') # return osname = 'windows' if sys.platform == 'linux': osname = 'ubuntu' elif sys.platform == 'darwin': osname = 'macos' # add crontab os.chdir(f'{kboss_path}/crontab') sh.crontab(f'crontab.txt') print('定时器任务更新成功') os.chdir(f'{kboss_path}/kgadget/src') sh.nohup(sh.python3("baidu_sms_kafka_consumer.py"), _bg=True) print('执行百度kafka接收任务成功') os.chdir(f'{kboss_path}/kgadget') try: x = sh.pip('install', '-r', 'requirements.txt') except: pass # os.chdir(f'{home}/py/mirotalkc2c') # sh.bash("mirtalk.sh") print('视频会话执行成功') # os.chdir(f'{home}/py/sqlor/dataloader/{osname}') # sh.sh('./build.sh') # sh.cp('dist/dataloader', f'{kboss_path}/{mode}/bin') os.chdir(f'{kboss_path}/kgadget/{osname}') sh.sh('./build.sh') sh.cp('dist/kgadget', f'{kboss_path}/{mode}/bin') sh.cp(f'{home}/build/web', f'{kboss_path}/{mode}/bin') try: sh.cp('-a', f'{kboss_path}/alipay', f'{kboss_path}/{mode}/www/') sh.cp('-a', f'{kboss_path}/weixinpay', f'{kboss_path}/{mode}/www/') except: pass def build_frontend(mode, ts): x = new_modified(f'{kboss_path}/f/web-kboss', ts) if not x: print('frontend is up to date') # return os.chdir(f'{kboss_path}/f/web-kboss') sh.rm("-f", "package-lock.json") sh.npm('i', '--legacy-peer-deps') print('前端依赖包安装完成') sh.npm('run', 'build:prod') front_path = f'{kboss_path}/f/web-kboss/dist' os.chdir(front_path) sh.tar('cf', f'{home}/tmp/kboss.{mode}.f.tar', '.') os.chdir(f'{kboss_path}/{mode}/www/wwwroot') sh.tar('xf', f'{home}/tmp/kboss.{mode}.f.tar') def copy_backend(mode, ts): backend_path = f'{kboss_path}/b' x = new_modified(backend_path, ts) if not x: print('backend is up to date') # return os.chdir(backend_path) sh.tar('cf', f'{home}/tmp/kboss.{mode}.b.tar', '.') os.chdir(f'{kboss_path}/{mode}/www/wwwroot') sh.tar('xf', f'{home}/tmp/kboss.{mode}.b.tar') sh.cp('-a', f'{kboss_path}/plugins', f'{kboss_path}/{mode}/www') def change_repo_mode(mode): sh.git('pull') _mkdir(f'{kboss_path}/{mode}/bin') _mkdir(f'{kboss_path}/{mode}/initdata') _mkdir(f'{kboss_path}/{mode}/www/conf') _mkdir(f'{kboss_path}/{mode}/www/files') _mkdir(f'{kboss_path}/{mode}/www/logs') _mkdir(f'{kboss_path}/{mode}/www/i18n') _mkdir(f'{kboss_path}/{mode}/www/wwwroot') sh.cp(f'{kboss_path}/conf/config.{mode}.json', f'{kboss_path}/{mode}/www/conf/config.json') sh.cp(f'{kboss_path}/models/mysql.ddl.sql', f'{kboss_path}/{mode}/initdata') sh.cp(f'{kboss_path}/initdata/kboss_init.sql', f'{kboss_path}/{mode}/initdata') def packing(mode): os.chdir(f'{kboss_path}/{mode}') sh.tar('-cf', f'/{kboss_path}/kboss.{mode}.tar', '.') def build(mode): for p in [f'{home}/py/apppublic', f'{home}/py/sqlor', f'{home}/py/ahserver', f'{home}/py/dataui', f'{home}/py/mirotalkc2c']: buildModule(p) os.chdir(kboss_path) print(f'change repo mode:{kboss_path}') change_repo_mode(mode) ts = get_deploy_timestamp() build_kgadget(mode, ts) build_frontend(mode, ts) copy_backend(mode, ts) packing(mode) if __name__ == '__main__': import sys mode = 'dev' if len(sys.argv) > 1: mode = sys.argv[1] if mode not in ['dev', 'test', 'prod', 'ncmatch']: print(f'{mode} not defined') kboss_path = f'{home}/kboss_{mode}' print(f'build for {mode} in {kboss_path}') build(mode)