def _execute(command): LogUtil.info("程序检测中") mac = MachineUtil.get_mac_address() param = ["{\"mac\":\"" + mac + "\"}"] s1 = AosServerService.route_service("ThriftApiService", "getMonitoringData", param) d1 = json.loads(s1) for k1 in d1: if k1["serverType"] == "5001": os.popen("sh " + sys.path[0] + "/api/extend/6000/mysql.sh " + k1["serverUserName"] + ' ' + k1["serverPassword"] + ' ' + k1["projectDir"] + ' ' + k1["projectPort"]).read() if k1["serverType"] == "5002": os.popen("su - " + k1["user"] + " -s /bin/bash " + sys.path[0] + "/api/extend/6000/redis.sh " + k1["serverUserName"] + ' ' + k1["serverPassword"] + ' ' + k1["projectDir"] + ' ' + k1["projectPort"]).read() if k1["serverType"] == "5003": os.popen("su - " + k1["user"] + " -s /bin/bash " + sys.path[0] + "/api/extend/6000/mongodb.sh " + k1["serverUserName"] + ' ' + k1["serverPassword"] + ' ' + k1["projectDir"] + ' ' + k1["projectPort"]).read() if k1["serverType"] == "5004": os.popen("su - " + k1["user"] + " -s /bin/bash " + sys.path[0] + "/api/extend/6000/flume.sh " + k1["serverUserName"] + ' ' + k1["serverPassword"] + ' ' + k1["projectDir"] + ' ' + k1["projectPort"]).read() if k1["serverType"] == "5005": LogUtil.info("检测rabbitMQ") os.popen("sh " + sys.path[0] + "/api/extend/6000/rabbitmq.sh " + k1["serverUserName"] + ' ' + k1["serverPassword"] + ' ' + k1["projectDir"] + ' ' + k1["projectPort"]).read() if k1["serverType"] == "5006": os.popen("su - " + k1["user"] + " -s /bin/bash " + sys.path[0] + "/api/extend/6000/zookeeper.sh " + k1["serverUserName"] + ' ' + k1["serverPassword"] + ' ' + k1["projectDir"] + ' ' + k1["projectPort"]).read() if k1["serverType"] == "5007": os.popen("su - " + k1["user"] + " -s /bin/bash " + sys.path[0] + "/api/extend/6000/jstorm.sh " + k1["serverUserName"] + ' ' + k1["serverPassword"] + ' ' + k1["projectDir"] + ' ' + k1["projectPort"]).read()
def _regist_node(self): mac = MachineUtil.get_mac_address() ip = NodeLifeTime.config.get_prop("aos-config", "client_ip") docker = NodeLifeTime.config.get_prop("docker-config", "docker") docker_status = "0" docker_images = "[]" docker_containers = "[]" if docker == "1": docker_status = DockerUtil._docker_status(NodeLifeTime.api) if docker_status == "2": docker_status = DockerUtil._docker_restart(mac, NodeLifeTime.api) if docker_status == "1": docker_images = base64.encodestring( DockerUtil._docker_images(NodeLifeTime.api)) docker_containers = base64.encodestring( DockerUtil._docker_containers()) param = [ "{\"mac\":\"" + mac + "\",\"ip\":\"" + ip + "\",\"dockerStatus\":\"" + docker_status + "\",\"dockerImages\":\"" + docker_images + "\",\"containers\":\"" + docker_containers + "\"}" ] LogUtil.info("注册节点信息:%s" % (json.dumps(param))) return AosServerService.route_service("ThriftApiService", "add", param)
def monitoring(self): while True: LogUtil.info("开始定时任务检测生命周期") ApiUtil.execute_api("6000",[]) LogUtil.info("结束定时任务检测生命周期") time.sleep(5)
def _execute(command): config = FileUtils.get_instance('config.ini') nginx_conf_dir = config.get_prop("nginx-config", "nginx_conf_dir") nginx_conf_dir_bak = config.get_prop("nginx-config", "nginx_conf_dir_bak") os.system('mkdir -p ' + nginx_conf_dir) os.system('cp -r ' + nginx_conf_dir + nginx_conf_dir_bak) os.system('rm -rf ' + nginx_conf_dir + '*') state = '0' try: for k1 in command: upstreamName = k1["upstreamName"] upstreamNode = json.loads(k1["upstreamNode"]) fileName = nginx_conf_dir + upstreamName + '.conf' os.system('touch ' + fileName) c = nginx.Conf() LogUtil.info("Node信息:%s" % (upstreamNode)) t = '' for k2 in range(len(upstreamNode)): if (k2 == 0): t = 'server' + ' ' + upstreamNode[k2][ "ipAdress"] + ' ' + upstreamNode[k2]["strategy"] + ';\n' if (0 < k2 < len(upstreamNode) - 1): t = t + ' ' + 'server' + ' ' + upstreamNode[k2][ "ipAdress"] + ' ' + upstreamNode[k2]["strategy"] + ';\n' if (k2 == len(upstreamNode) - 1): t = t + ' ' + 'server' + ' ' + upstreamNode[k2][ "ipAdress"] + ' ' + upstreamNode[k2]["strategy"] ipHash = k1["ipHash"] if (ipHash == '1'): u = nginx.Upstream(upstreamName, nginx.Key('', t), nginx.Key('', 'ip_hash')) c.add(u) if (ipHash == '0'): u = nginx.Upstream(upstreamName, nginx.Key('', t)) c.add(u) s = nginx.Server() s.add( nginx.Key('listen', k1["nginxListenPort"]), nginx.Key('server_name', k1["nginxServerName"]), nginx.Key('access_log', k1["accessLog"]), nginx.Location( '= /', nginx.Key('proxy_pass', 'http://' + k1["upstreamName"]), nginx.Key('proxy_redirect', 'off'), nginx.Key('proxy_set_header', 'Host $host'), nginx.Key('proxy_set_header', 'X-Real-IP $remote_addr'), nginx.Key('proxy_set_header', 'X-Forwarded-For $proxy_add_x_forwarded_for'))) c.add(s) nginx.dumpf(c, fileName) LogUtil.info("完成Nginx配置") output = os.popen('service nginx restart').read() state = '1' param = [ "{\"mac\":\"" + MachineUtil.get_mac_address() + "\",\"state\":\"" + state + "\",\"msg\":\"" + output + "\"}" ] os.system('rm -rf ' + nginx_conf_dir_bak) AosServerService.route_service("ThriftApiService", "nginxLog", param) except Exception, ex: LogUtil.error(ex) os.system('rm -rf ' + nginx_conf_dir) os.system('mv ' + nginx_conf_dir_bak + nginx_conf_dir) os.system('service nginx restart') output = ex.message state = '2' param = [ "{\"mac\":\"" + MachineUtil.get_mac_address() + "\",\"state\":\"" + state + "\",\"msg\":\"" + output + "\"}" ] AosServerService.route_service("ThriftApiService", "nginxLog", param)
def dispatch(self): while True: LogUtil.info("开始生命周期") self.run() LogUtil.info("结束生命周期") time.sleep(5)
def _do_config(self, server_response_config): if len(server_response_config) == 0: return for config in server_response_config: LogUtil.info("执行配置项:%s" % (json.dumps(config))) ApiUtil.execute_api(config["commandId"], config)
def _do_action(self, server_response_command): if len(server_response_command) == 0: return for action in server_response_command: LogUtil.info("执行指令:%s" % (json.dumps(action))) ApiUtil.execute_api(action["commandId"], action)
def nginx_dispatch(self): while True: LogUtil.info("开始拉取Nginx配置信息") self.nginx_run() LogUtil.info("结束Nginx配置") time.sleep(1800)
def _do_action(self, server_response_command): LogUtil.info("执行指令:%s" % (server_response_command)) ApiUtil.execute_api("2111", server_response_command)
def _nginx_conf(self): mac = MachineUtil.get_mac_address() param = ["{\"mac\":\"" + mac + "\"}"] LogUtil.info("拉取Nginx配置信息:%s" % (json.dumps(param))) return AosServerService.route_service("ThriftApiService", "getNginxConf", param)