def pre_start(self): logger.run.info("==============start==============") port = config.safe_get_int('network', 'listen-port') model = config.safe_get('model', 'path') if model[len(model) - 1] == "/": model = model[:len(model) - 2] pos = model.rfind('/') model_path = model[:pos] model_name = model[pos + 1:] sys.path.append(model_path) if config.safe_get('system', 'module').lower() == "mds": init_dbservice() # 在import的时候,会执行每个服务的init.py的INIT过程 __import__(model_name) reactor.listenTCP(port, MyServerFactory()) reactor.callWhenRunning(loop_machine) reactor.suggestThreadPoolSize(50)
def support_license_time(self): if self.libc.pbdata_license_disable():#license不启用 PASSWD = config.safe_get('model-config', 'lic-pass') if self.libc.get_license_passwd() == int(PASSWD): return 0 else: return -1 if self.support_smartmgr(): return -1 sys = self.__get_sys_info() if not sys: return -1 tm = time.time() if sys["lic_mode"] == PBLIC_MODE_FOREVER: return 0 if tm < sys["authorize_time"]: if not os.path.exists(Time): if not os.path.exists(os.path.dirname(Time)): os.makedirs(os.path.dirname(Time), 0755) try: f = open(Time,"w") f.close() except IOError,e: return -2 else: try: f = open(Time,"w") f.close() except IOError,e: return -2
def main(): options, args = parse_args() if options.action == None: print "Miss action params" sys.exit(-1) if options.filename == None: print "Miss config file params" sys.exit(-1) filename = options.filename front = options.front action = options.action if not os.path.exists(filename): print "Config file '%s' not exists" % filename sys.exit(-1) init_config(filename) check_netconfig(config.safe_get('network', 'listen-ip')) pidfile = "/var/run/smartmgr-api.pid" stdlog = os.path.join(config.safe_get('log', 'path'), "%s.log" % config.safe_get("model-config","api-log-prefix")) daemon = APIDaemon(pidfile=pidfile, stdout=stdlog, stderr=stdlog, front=front) if 'start' == action: daemon.start() elif 'stop' == action: daemon.stop() elif 'restart' == action: daemon.restart() elif 'status' == action: if daemon.status(): sys.exit(0) else: sys.exit(1) else: print 'Unknown command' sys.exit(2) sys.exit(0)
def init_dbservice(): db_file = config.safe_get('model-config', 'db_file') if not os.path.exists(db_file): try: f = open(db_file, 'w', 0) f.write(json.dumps({"version": CURR_DB_VERSION}, indent=4)) f.close() except Exception as e: logger.run.error("Init db file '%s' failed : %s" % (db_file, e)) assert (0) dbservice.check_version(db_file) dbservice.init(db_file)
def main(): options, args = parse_args() if options.action == None: print "Miss action params" sys.exit(-1) if options.filename == None: print "Miss config file params" sys.exit(-1) filename = options.filename front = options.front action = options.action if not os.path.exists(filename): print "Config file '%s' not exists" % filename sys.exit(-1) init_config(filename) init_logger() # 检查kernel标记 check_kernel_flag() module = config.safe_get("system", "module") pidfile = "/var/run/smartmgr-%s.pid" % module stdlog = "/var/log/smartmgr/.%s.stdlog" % module daemon = S2SFrameDaemon(pidfile=pidfile, stdout=stdlog, stderr=stdlog, front=front) if 'start' == action: daemon.start() elif 'stop' == action: daemon.stop() elif 'restart' == action: daemon.restart() elif 'status' == action: if daemon.status(): sys.exit(0) else: sys.exit(1) else: print 'Unknown command' sys.exit(2) sys.exit(0)
def init(self): log_level = config.safe_get('log', 'level') log_path = config.safe_get('log', 'path') log_maxsize = int(config.safe_get('log', 'maxsize')) log_maxcount = int(config.safe_get('log', 'maxcount')) log_prefix = config.safe_get('log', 'prefix') if log_level == "debug": log_level = logging.DEBUG elif log_level == "info": log_level = logging.INFO elif log_level == "warning": log_level = logging.WARNING elif log_level == "error": log_level = logging.ERROR elif log_level == "critical": log_level = logging.CRITICAL else: log_level = logging.DEBUG if not os.path.exists(log_path): os.makedirs(log_path) logfile = lambda file_name: os.path.join(log_path, file_name) run_log_file = logfile("%s.log" % config.safe_get("log", "prefix")) # 运行日志 self.run = logging.getLogger('run') hdlr = logging.handlers.RotatingFileHandler(run_log_file, maxBytes=log_maxsize, backupCount=log_maxcount) formatter = logging.Formatter( '[%(asctime)s] [%(levelname)s] %(message)s') hdlr.setFormatter(formatter) self.run.setLevel(log_level) # 终端日志 console = logging.StreamHandler() formatter = logging.Formatter( '[%(asctime)s] [%(levelname)s] %(message)s') console.setFormatter(formatter) console.setLevel(logging.INFO) self.run.addHandler(hdlr) self.run.addHandler(console)
def main(): if "--help" in sys.argv: print "Usage: smartmgrcli [option]" sys.exit(0) cli = CLI() mds_config = "/opt/smartmgr/conf/service.mds.ini" if "--config" in sys.argv: if len(sys.argv) == sys.argv.index("--config") + 1: print "Miss config file" sys.exit(1) mds_config = sys.argv[sys.argv.index("--config") + 1] if not os.path.exists(mds_config): print "Please cheack your config file path" sys.exit(1) del sys.argv[sys.argv.index('--config') + 1] del sys.argv[sys.argv.index('--config')] init_config(mds_config) platform = load_platform() if platform.has_key('sys_mode') and platform.has_key('merge_mode'): cli.srv['role'] = platform['merge_mode'] else: cli.srv['role'] = platform['sys_mode'] cli.srv['platform'] = platform['platform'] cli.srv['ip'] = config.safe_get('network', 'listen-ip') cli.srv['port'] = config.safe_get_int('network', 'listen-port') check_netconfig(cli.srv['ip']) cli_config = {'debug': False, 'json': False, 'detail': False} if "--debug" in sys.argv: del sys.argv[sys.argv.index('--debug')] cli_config['debug'] = True if "--detail" in sys.argv: del sys.argv[sys.argv.index('--detail')] cli_config['detail'] = True if "--json" in sys.argv: del sys.argv[sys.argv.index('--json')] cli_config['json'] = True cli.pre_init(cli_config) cli.post_init() # shell 命令行方式调用 if len(sys.argv) > 1: try: cli._save_hist(' '.join(sys.argv[1:])) cli.onecmd(' '.join(sys.argv[1:])) except KeyboardInterrupt: print "Stopped.." cli.do_quit('') else: try: cli.cmdloop() except KeyboardInterrupt: print "Stopped.." cli.do_quit('')
def run(self): listen_port = int(config.safe_get('model-config', 'api-listen-port')) if self.front: app.run(debug=True, host="0.0.0.0", port=listen_port) else: app.run(host="0.0.0.0", port=listen_port)