diff --git a/antgo/main.py b/antgo/main.py index 8c4a226..6825bd6 100755 --- a/antgo/main.py +++ b/antgo/main.py @@ -26,6 +26,7 @@ from filelock import FileLock from aligo import Aligo import antvis.client.mlogger as mlogger +from antvis.client.httprpc import * import tempfile @@ -158,7 +159,31 @@ def main(): args.ssh = True ######################################### 配置文件操作 ################################################ - # 检查配置文件是否存在 + # 创建配置文件 + if action_name == 'config': + config_data = {'FACTORY': '', 'USER_TOKEN': ''} + # 读取现有数值 + config_xml = os.path.join(os.environ['HOME'], '.config', 'antgo', 'config.xml') + config.AntConfig.parse_xml(config_xml) + config_data['FACTORY'] = getattr(config.AntConfig, 'factory', '') + config_data['USER_TOKEN'] = getattr(config.AntConfig, 'token', '') + + if args.root is not None: + config_data['FACTORY'] = args.root + if args.token is not None: + config_data['USER_TOKEN'] = args.token + + env = Environment(loader=FileSystemLoader('/'.join(os.path.realpath(__file__).split('/')[0:-1]))) + config_template = env.get_template('config.xml') + config_content = config_template.render(**config_data) + + with open(os.path.join(os.environ['HOME'], '.config', 'antgo', 'config.xml'), 'w') as fp: + fp.write(config_content) + + logging.info('Update config file.') + return + + # 配置文件不存在则创建默认 if not os.path.exists(os.path.join(os.environ['HOME'], '.config', 'antgo', 'config.xml')): # 使用指定配置文件更新 if not os.path.exists(os.path.join(os.environ['HOME'], '.config', 'antgo')): @@ -177,19 +202,33 @@ def main(): fp.write(config_content) logging.warn('Using default config file.') - # 配置操作 - if action_name == 'config': - config_data = {'FACTORY': '', 'USER_TOKEN': ''} - # 读取现有数值 - config_xml = os.path.join(os.environ['HOME'], '.config', 'antgo', 'config.xml') - config.AntConfig.parse_xml(config_xml) - config_data['FACTORY'] = getattr(config.AntConfig, 'factory', '') - config_data['USER_TOKEN'] = getattr(config.AntConfig, 'token', '') + ######################################### 检查token ################################################# + # 解析配置文件 + config_xml = os.path.join(os.environ['HOME'], '.config', 'antgo', 'config.xml') + config.AntConfig.parse_xml(config_xml) - if args.root is not None: - config_data['FACTORY'] = args.root - if args.token is not None: - config_data['USER_TOKEN'] = args.token + if not os.path.exists(config.AntConfig.factory): + os.makedirs(config.AntConfig.factory) + if not os.path.exists(config.AntConfig.data_factory): + os.makedirs(config.AntConfig.data_factory) + if not os.path.exists(config.AntConfig.task_factory): + os.makedirs(config.AntConfig.task_factory) + + # 检查token是否存在,否则重新生成 + token = None + if os.path.exists('./.token'): + with open('./.token', 'r') as fp: + token = fp.readline() + else: + token = getattr(config.AntConfig, 'server_user_token', '') + + if token is None or token == '': + logging.info("generate experiment token") + token = mlogger.create_token() + config_data = { + 'FACTORY': getattr(config.AntConfig, 'factory'), + 'USER_TOKEN': token + } env = Environment(loader=FileSystemLoader('/'.join(os.path.realpath(__file__).split('/')[0:-1]))) config_template = env.get_template('config.xml') @@ -197,9 +236,10 @@ def main(): with open(os.path.join(os.environ['HOME'], '.config', 'antgo', 'config.xml'), 'w') as fp: fp.write(config_content) + logging.warn(f'update config file (token: {config_data["USER_TOKEN"]}, factory: {config_data["FACTORY"]}).') - logging.info('Update config file.') - return + with open('./.token', 'w') as fp: + fp.write(token) # web服务 if action_name == 'web': @@ -417,7 +457,8 @@ def main(): # step 3: 更新平台记录(name, logo, description, address) # 仅内部团队测试使用 - + rpc = HttpRpc("v1", 'antvis', 'experiment.vibstring.com', 80, token=token) + rpc.research.create.post(research_url=f'http://{args.ip}:{args.port}', research_name=server_info["name"], research_description='') return # 查看运行设备(本地/远程) @@ -685,45 +726,6 @@ def main(): print('Using default root address ali:///exp') args.root = "ali:///exp" - ######################################### 检查token ################################################# - # 解析配置文件 - config_xml = os.path.join(os.environ['HOME'], '.config', 'antgo', 'config.xml') - config.AntConfig.parse_xml(config_xml) - - if not os.path.exists(config.AntConfig.factory): - os.makedirs(config.AntConfig.factory) - if not os.path.exists(config.AntConfig.data_factory): - os.makedirs(config.AntConfig.data_factory) - if not os.path.exists(config.AntConfig.task_factory): - os.makedirs(config.AntConfig.task_factory) - - # 检查token是否存在,否则重新生成 - token = None - if os.path.exists('./.token'): - with open('./.token', 'r') as fp: - token = fp.readline() - else: - token = getattr(config.AntConfig, 'server_user_token', '') - - if token is None or token == '': - logging.info("generate experiment token") - token = mlogger.create_token() - config_data = { - 'FACTORY': getattr(config.AntConfig, 'factory'), - 'USER_TOKEN': token - } - - env = Environment(loader=FileSystemLoader('/'.join(os.path.realpath(__file__).split('/')[0:-1]))) - config_template = env.get_template('config.xml') - config_content = config_template.render(**config_data) - - with open(os.path.join(os.environ['HOME'], '.config', 'antgo', 'config.xml'), 'w') as fp: - fp.write(config_content) - logging.warn(f'update config file (token: {config_data["USER_TOKEN"]}, factory: {config_data["FACTORY"]}).') - - with open('./.token', 'w') as fp: - fp.write(token) - ######################################### 后台监控服务 ################################################ if action_name == 'server': os.system(f'nohup python3 {os.path.join(os.path.dirname(__file__), "ant", "client.py")} --port={args.port} --root={args.root} --ext-module={args.ext_module} > /tmp/antgo.server.log 2>&1 &')