def __make_redis_clear(params, server, date_time): fpath = os.path.join(params['template_dir'], 'redis-cache-clear.sh') shell_clear_cache = File.read_file(fpath) cache_redis = server['redis']['cache'] s = shell_clear_cache % (date_time, params['service_file'], params['bin_dir'], cache_redis['host'], cache_redis['port'], cache_redis['db']) File.write_file(params['shell_dir'], 'redis-cache-clear.sh', s) params['clear_script'] = os.path.join(params['shell_dir'], 'redis-cache-clear.sh')
def action_log_conf(params): """生成process对应log配置文件""" File.make_dirs(params['log_dir']) server = params['server'] for process in server['process']: log_key = process['log_key'] bi_log = os.path.join(params['log_dir'], 'bi-%s.log' % log_key) common_log = os.path.join(params['log_dir'], '%s.log' % log_key) process['bi_log_file'] = bi_log process['log_file'] = common_log
def __make_process_stop_script(params, server, date_time): # 生成停止脚本 fpath = os.path.join(params['template_dir'], 'stop.sh') shell_stop_template = File.read_file(fpath) for process in server['process']: if process['type'] in server['startup']: s = shell_stop_template % (date_time, params['service_file'], process['proc_key'], params['bin_dir']) fname = "kill-" + process['shell_key'] File.write_file(params['shell_dir'], fname, s) params['shell'].append(os.path.join(params['shell_dir'], fname))
def __make_stop_all(params, date_time, redis_key, service_type): if params['shell']: fpath = os.path.join(params['template_dir'], 'stop.sh') shell_stop_template = File.read_file(fpath) # 生成停止全部脚本 s = shell_stop_template % (date_time, params['service_file'], redis_key, params['bin_dir']) fname = 'kill-%s-all.sh' % service_type File.write_file(params['shell_dir'], fname, s) params['shell'].append(os.path.join(params['shell_dir'], fname)) params['kill_script'] = os.path.join(params['shell_dir'], fname)
def __make_process_start_script(params, server, date_time, redis_key, bin_dir): # 生成启动脚本 fpath_py = os.path.join(params['template_dir'], 'start.sh') shell_start_template_py = File.read_file(fpath_py) for process in server['process']: if process['type'] in server['startup']: listen_port = process['port'] process['proc_key'] = "%s:%s:%s" % (redis_key, listen_port, process['id']) svrd = 'lemon/framework/service.py' s = shell_start_template_py % (date_time, params['service_file'], bin_dir, svrd, process['proc_key'], process['log_file']) fname = "start-" + process['shell_key'] File.write_file(params['shell_dir'], fname, s) params['shell'].append(os.path.join(params['shell_dir'], fname))
def action_copy_file(params, *dirs): cmds = [] for d in dirs: from_dir = os.path.join(params['src_dir'], d) up_dir = os.path.dirname(from_dir) File.make_dirs(up_dir) cmd = 'cd %s; tar -cf - %s --exclude=*.pyc --exclude=*.c | tar -xf - -C %s' % (up_dir, d, params['bin_dir']) cmds.append(cmd) File.make_dirs(params['bin_dir']) for cmd in cmds: Log.log(cmd) status, output = commands.getstatusoutput(cmd) if status: Log.log(output) return status
def __make_start_all(params, date_time, service_type): if params['shell']: shell_template = ''' #!/usr/bin/env bash # -*- coding=utf-8 -*- # Author: likebeta <*****@*****.**> # Create: %s export service_file=%s sh %s ''' % (date_time, params['service_file'], '\nsh '.join(params['shell'])) fname = 'start-%s-all.sh' % service_type File.write_file(params['shell_dir'], fname, shell_template) params['shell'].append(os.path.join(params['shell_dir'], fname)) params['start_script'] = os.path.join(params['shell_dir'], fname)
def action_load_server(params): """加载服务器配置文件""" if 'service_file' not in params: raise Exception('miss service_file') data = File.read_file(params['service_file']) params['server'] = json.loads(data) mode = params['server']['mode'] Global.run_mode = mode Global.http_game = params['server']['http.game'] server = params['server'] for k in ('host', 'port', 'db'): v = os.environ.get('redis_%s' % k, None) if v: server['redis.%s' % k] = v if k == 'host' else int(v) if 'redis.%s' % k in server: server['redis']['config'][k] = server['redis.%s' % k] http_port = server.get('http.port', 8080) http_port = os.environ.get('http_port', http_port) server['http.port'] = int(http_port) tcp_port = os.environ.get('tcp_port', None) if tcp_port: server['tcp.port'] = int(tcp_port) type_set = set() tcp_port = server['tcp.port'] http_port = server['http.port'] for process in server['process']: if process['type'] not in type_set: type_set.add(process['type']) process['first'] = 1 if process['type'] == 'sdk': process['port'] = http_port http_port += 2 else: process['port'] = tcp_port tcp_port += 2 process['shell_key'] = '%s-%s-%05d.sh' % ( process['type'], server['name'], process['id']) process['log_key'] = '%s-%s-%05d' % (process['type'], server['name'], process['id']) redis_info = params['server']['redis'] redis_key = '%s:%s:%s' % (redis_info['config']['host'], redis_info['config']['port'], redis_info['config']['db']) params['redis_key'] = redis_key add_global_config('redis.config', redis_info)
def action_push_config(params): """推送配置""" data = File.read_file(params['redis.output']) j = Context.json_loads(data) for item in j: Context.RedisConfig.execute(*item)