def run(): """运行 """ return_value = normal_code # exec使用get_pty, redis配置为后台运行, 但未启动完全时, 断开依然会停止, 故使用sleep 2让其完全启动 redis_start_command = f"cd {redis_dir} && bin/redis-server conf/redis.conf" log.logger.debug(f"redis启动: {redis_start_command=}") result, msg = common.exec_command(redis_start_command) if result: log.logger.debug(f"检测端口: {redis_port} ") if not common.port_exist([redis_port]): return_value = error_code else: log.logger.error(msg) return_value = error_code if sentinel_flag: #sentinel_port=sentinel_info.get("sentinel_port") sentinel_start_command = f"cd {redis_dir} && bin/redis-sentinel conf/sentinel.conf" log.logger.debug(f"sentinel启动: {sentinel_start_command=}") result, msg = common.exec_command(sentinel_start_command) if result: log.logger.debug(f"检测端口: {sentinel_port} ") if not common.port_exist([sentinel_port]): return_value = error_code else: log.logger.error(msg) return_value = error_code return return_value
def stop(): """停止 """ return_value=normal_code dch_stop_command=f"cd {dch_dir} && bin/dch-cli -a {dch_password} shutdown" log.logger.debug(f"dch停止: {dch_stop_command=}") result, msg=common.exec_command(dch_stop_command) if result: log.logger.debug(f"检测端口: {dch_port}") if not common.port_exist([dch_port], exist_or_not=False): return_value=error_code else: log.logger.error(msg) return_value=error_code if sentinel_flag: if sentinel_password_str == "": sentinel_stop_command=f"cd {dch_dir} && bin/dch-cli -p {sentinel_port} shutdown" else: sentinel_stop_command=f"cd {dch_dir} && bin/dch-cli -a {sentinel_password} -p {sentinel_port} shutdown" log.logger.debug(f"sentinel停止: {sentinel_stop_command=}") result, msg=common.exec_command(sentinel_stop_command) if result: log.logger.debug(f"检测端口: {sentinel_port}") if not common.port_exist([sentinel_port], exist_or_not=False): return_value=error_code else: log.logger.error(msg) return_value=error_code return return_value
def run(): """运行 """ return_value = normal_code redis_start_command = f"cd {redis_dir} && bin/redis-server conf/redis.conf" log.logger.debug(f"redis启动: {redis_start_command=}") result, msg = common.exec_command(redis_start_command) if result: log.logger.debug(f"检测端口: {redis_port} ") if not common.port_exist([redis_port]): return_value = error_code else: log.logger.error(msg) return_value = error_code if sentinel_flag: sentinel_start_command = f"cd {redis_dir} && bin/redis-sentinel conf/sentinel.conf" log.logger.debug(f"sentinel启动: {sentinel_start_command=}") result, msg = common.exec_command(sentinel_start_command) if result: log.logger.debug(f"检测端口: {sentinel_port} ") if not common.port_exist([sentinel_port]): return_value = error_code else: log.logger.error(msg) return_value = error_code return return_value
def stop(): """停止 """ return_value = normal_code redis_stop_command = f"cd {redis_dir} && bin/redis-cli -a {password} shutdown" log.logger.debug(f"redis停止: {redis_stop_command=}") result, msg = common.exec_command(redis_stop_command) if result: log.logger.debug(f"检测端口: {redis_port} ") if not common.port_exist([redis_port], exist_or_not=False): return_value = error_code else: log.logger.error(msg) return_value = error_code if sentinel_flag: sentinel_stop_command = f"cd {redis_dir} && bin/redis-cli -a {password} -p {sentinel_port} shutdown" log.logger.debug(f"sentinel停止: {sentinel_stop_command=}") result, msg = common.exec_command(sentinel_stop_command) if result: log.logger.debug(f"检测端口: {sentinel_port} ") if not common.port_exist([sentinel_port], exist_or_not=False): return_value = error_code else: log.logger.error(msg) return_value = error_code return return_value
def stop(): """停止 """ return_value = normal_code namesrv_command = f"bash -lc 'cd {rocketmq_dir} && bash bin/mqshutdown namesrv'" broker_command = f"bash -lc 'cd {rocketmq_dir} && bash bin/mqshutdown broker'" log.logger.debug(f"{namesrv_command=}") result, msg = common.exec_command(namesrv_command) if result: log.logger.debug(f"检测端口: {namesrv_port_list=}") if not common.port_exist(namesrv_port_list, exist_or_not=False): return_value = error_code else: log.logger.debug(f"{broker_command=}") result, msg = common.exec_command(broker_command) if result: log.logger.debug(f"检测端口: {broker_port_list=}") if not common.port_exist(broker_port_list, exist_or_not=False): return_value = error_code else: log.logger.error(msg) return_value = error_code else: log.logger.error(msg) return_value = error_code return return_value
def run(): """运行 """ return_value = normal_code namesrv_command = f"bash -lc 'cd {rocketmq_dir} && bash ./bin/start.sh namesrv'" broker_command = f"bash -lc 'cd {rocketmq_dir} && bash ./bin/start.sh broker'" log.logger.debug(f"{namesrv_command=}") result, msg = common.exec_command(namesrv_command) if result: log.logger.debug(f"检测端口: {namesrv_port_list=}") if not common.port_exist(namesrv_port_list): return_value = error_code else: log.logger.debug(f"{broker_command=}") result, msg = common.exec_command(broker_command) if result: log.logger.debug(f"检测端口: {broker_port_list=}") if not common.port_exist(broker_port_list): return_value = error_code else: log.logger.error(msg) return_value = error_code else: log.logger.error(msg) return_value = error_code return return_value
def volume_start(): """volume启动 """ log.logger.debug(f"检测端口: {volume_port}") if not common.port_exist([volume_port], seconds=2): start_volume_command = f"gluster volume start {glusterfs_volume_name}" log.logger.debug(f"启动volume: {start_volume_command=}") result, msg = common.exec_command(start_volume_command) if result: log.logger.debug(f"检测端口: {volume_port}") if not common.port_exist([volume_port]): return error_code else: return error_code return normal_code
def stop(): """关闭 """ if server_flag == 1: command = f"echo y | gluster volume stop {glusterfs_volume_name} force" log.logger.debug(f"关闭volume: {command=}") result, msg = common.exec_command(command) if result: log.logger.debug(f"检测端口: {volume_port}") if not common.port_exist([volume_port], exist_or_not=False): return error_code #else: # command="systemctl stop glusterd" # log.logger.debug(f"关闭glusterd: {command=}") # result, msg=common.exec_command(command) # if result: # log.logger.debug(f"检测端口: {glusterd_port}") # if not common.port_exist([glusterd_port], exist_or_not=False): # return error_code # else: # return error_code else: return error_code elif server_flag == 2: command = f"umount {mounted_dir}" log.logger.debug(f"执行卸载: {command}") result, msg = common.exec_command(command) if not result: log.logger.error(msg) return error_code return normal_code
def start(): """启动 """ log.logger.debug(f"{start_command=}") result, msg = common.exec_command(start_command, timeout=600) if result: log.logger.debug(f"检测端口: {port_list=}") if not common.port_exist(port_list): return error_code else: log.logger.error(msg) return error_code return normal_code
def stop(): """停止 """ stop_command = f"systemctl stop mysqld" log.logger.debug(f"{stop_command=}") result, msg = common.exec_command(stop_command, timeout=600) if result: log.logger.debug(f"检测端口: {port_list=}") if not common.port_exist(port_list, exist_or_not=False): return error_code else: log.logger.error(msg) return error_code return normal_code
def heapdump(): """jvm """ return_value=normal_code command=f"jmap -dump:format=b, file=heapdump.dump {pid}" log.logger.debug(f"{command=}") result, msg=common.exec_command(command) if result: log.logger.debug(f"检测端口: {port_list=}") if not common.port_exist(port_list, seconds=600): return_value=error_code else: log.logger.error(msg) return_value=error_code return return_value
def start(): """启动 """ return_value=normal_code start_command=f"bash -lc 'cd {program_dir} ; bash {program_sh_file} start'" log.logger.debug(f"{start_command=}") result, msg=common.exec_command(start_command) if result: log.logger.debug(f"检测端口: {port_list=}") if not common.port_exist(port_list, seconds=1200): return_value=error_code else: log.logger.error(msg) return_value=error_code return return_value
def stop(): """关闭 """ return_value = normal_code stop_command = f"bash -lc 'cd {nacos_dir} ; ./bin/shutdown.sh'" log.logger.debug(f"{stop_command=}") result, msg = common.exec_command(stop_command) if result: log.logger.debug(f"检测端口: {port_list=}") if not common.port_exist(port_list, exist_or_not=False): return_value = error_code else: log.logger.error(msg) return_value = error_code return return_value
def stop(): """停止 """ return_value=normal_code stop_command=f"bash -lc 'cd {program_dir} ; bash {program_sh_file} stop'" log.logger.debug(f"{stop_command=}") result, msg=common.exec_command(stop_command) if result: log.logger.debug(f"检测端口: {port_list=}") if not common.port_exist(port_list, exist_or_not=False): return_value=error_code else: log.logger.error(msg) return_value=error_code return return_value
def stop(): """关闭 """ return_value = normal_code stop_command = f"cd {dps_dir} ; ./sbin/dps-server -s stop" log.logger.debug(f"{stop_command=}") result, msg = common.exec_command(stop_command) if result: log.logger.debug(f"检测端口: {port_list=}") if not common.port_exist(port_list, exist_or_not=False): return_value = error_code else: log.logger.error(msg) return_value = error_code return return_value
def run(): """运行 """ return_value = normal_code start_command = f"cd {dps_dir} ; ./sbin/dps-server" log.logger.debug(f"{start_command=}") result, msg = common.exec_command(start_command) if result: log.logger.debug(f"检测端口: {port_list=}") if not common.port_exist(port_list): return_value = error_code else: log.logger.error(msg) return_value = error_code return return_value
def run(): """运行 """ return_value = normal_code #command=f"su elastic --session-command 'cd {es_dir} && ./bin/elasticsearch -d -p elasticsearch.pid &> /dev/null'" command = f"su elastic -lc 'cd {es_dir} && ./bin/elasticsearch -d -p elasticsearch.pid &> /dev/null'" log.logger.debug(f"{command=}") result, msg = common.exec_command(command, timeout=80) if result: log.logger.debug(f"检测端口: {port_list=}") if not common.port_exist(port_list): return_value = error_code else: log.logger.error(msg) return_value = error_code return return_value
def stop(): """停止 """ return_value = normal_code #command=f"su elastic --session-command 'cd {es_dir} && kill `cat elasticsearch.pid`'" command = f"su elastic -c 'cd {es_dir} && kill `cat elasticsearch.pid`'" log.logger.debug(f"{command=}") result, msg = common.exec_command(command) if result: log.logger.debug(f"检测端口: {port_list=}") if not common.port_exist(port_list, exist_or_not=False): return_value = error_code else: log.logger.error(msg) return_value = error_code return return_value
def glusterd_start(): """glusterd启动 """ if os.path.exists("/lib/systemd/system/glusterd.service"): command = "systemctl enable glusterd && systemctl start glusterd" elif os.path.exists("/etc/init.d/glusterfs-server"): command = "systemctl start glusterfs-server" else: log.logger.error(f"找不到glusterd启动文件") return error_code log.logger.debug(f"启动: {command=}") result, msg = common.exec_command(command) if result: log.logger.debug(f"检测端口: {glusterd_port}") if not common.port_exist([glusterd_port]): return error_code else: return error_code return normal_code
def run(): """运行 """ return_value = normal_code if cluster_flag: if nacos_info_dict["data_source"]["mode"] == "mysql": start_command = f"cd {nacos_dir} ; ./bin/startup.sh" else: start_command = f"cd {nacos_dir} ; ./bin/startup.sh -p embedded" else: start_command = f"bash -lc 'cd {nacos_dir} ; ./bin/startup.sh -m standalone'" log.logger.debug(f"{start_command=}") result, msg = common.exec_command(start_command, timeout=180) if result: log.logger.debug(f"检测端口: {port_list=}") if not common.port_exist(port_list): return_value = error_code else: log.logger.error(msg) return_value = error_code return return_value
def main(): softname, action, conf_json = sys.argv[1:] conf_dict = json.loads(conf_json) located = conf_dict.get("located") log = common.Logger({"remote": log_remote_level}, loggger_name="tomcat") tomcat_dir = f"{located}/{tomcat_dst}" tomcat_info_dict = conf_dict["tomcat_info"] http_port = tomcat_info_dict["port"].get("http_port") shutdown_port = tomcat_info_dict["port"].get("shutdown_port") #ajp_port=tomcat_info_dict["port"].get("ajp_port") ajp_port = 8009 port_list = [http_port, shutdown_port] flag = 0 # 安装 if action == "install": pkg_file = conf_dict["pkg_file"] value, msg = common.install(pkg_file, tomcat_src, tomcat_dst, tomcat_pkg_dir, located) if not value: log.logger.error(msg) flag = 1 sys.exit(flag) # 配置 try: # 删除tomcat原有程序目录 log.logger.debug("删除默认程序") webapps_dir = f"{tomcat_dir}/webapps" for i in os.listdir(webapps_dir): shutil.rmtree(f"{webapps_dir}/{i}") except Exception as e: log.logger.error(str(e)) jvm_mem = tomcat_info_dict.get("jvm_mem") min_threads, max_threads = tomcat_info_dict.get("threads") max_connections = tomcat_info_dict.get("max_connections") tomcat_sh_context = f"""\ export CATALINA_HOME={tomcat_dir} export PATH=$CATALINA_HOME/bin:$PATH """ server_xml_context = f"""\ <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- Note: A "Server" is not itself a "Container", so you may not define subcomponents such as "Valves" at this level. Documentation at /docs/config/server.html --> <Server port="{shutdown_port}" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> <!-- Security listener. Documentation at /docs/config/listeners.html <Listener className="org.apache.catalina.security.SecurityListener" /> --> <!--APR library loader. Documentation at /docs/apr.html --> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <!-- Prevent memory leaks due to use of particular java/javax APIs--> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <!-- Global JNDI resources Documentation at /docs/jndi-resources-howto.html --> <GlobalNamingResources> <!-- Editable user database that can also be used by UserDatabaseRealm to authenticate users --> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources> <!-- A "Service" is a collection of one or more "Connectors" that share a single "Container" Note: A "Service" is not itself a "Container", so you may not define subcomponents such as "Valves" at this level. Documentation at /docs/config/service.html --> <Service name="Catalina"> <!--The connectors can use a shared executor, you can define one or more named thread pools--> <!-- <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="150" minSpareThreads="4"/> --> <!-- A "Connector" represents an endpoint by which requests are received and responses are returned. Documentation at : Java HTTP Connector: /docs/config/http.html Java AJP Connector: /docs/config/ajp.html APR (HTTP/AJP) Connector: /docs/apr.html Define a non-SSL/TLS HTTP/1.1 Connector on port 8080 --> <Connector port="{http_port}" protocol="HTTP/1.1" maxHttpHeaderSize="8192" maxThreads="{max_threads}" minSpareThreads="{min_threads}" enableLookups="false" compression="on" compressionMinSize="2048" URIEncoding="utf-8" acceptCount="300" disableUploadTimeout="true" maxConnections="{max_connections}" connectionTimeout="20000" redirectPort="8443" /> <!-- A "Connector" using the shared thread pool--> <!-- <Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> --> <!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443 This connector uses the NIO implementation. The default SSLImplementation will depend on the presence of the APR/native library and the useOpenSSL attribute of the AprLifecycleListener. Either JSSE or OpenSSL style configuration may be used regardless of the SSLImplementation selected. JSSE style configuration is used below. --> <!-- <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol" maxThreads="150" SSLEnabled="true"> <SSLHostConfig> <Certificate certificateKeystoreFile="conf/localhost-rsa.jks" type="RSA" /> </SSLHostConfig> </Connector> --> <!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2 This connector uses the APR/native implementation which always uses OpenSSL for TLS. Either JSSE or OpenSSL style configuration may be used. OpenSSL style configuration is used below. --> <!-- <Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol" maxThreads="150" SSLEnabled="true" > <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" /> <SSLHostConfig> <Certificate certificateKeyFile="conf/localhost-rsa-key.pem" certificateFile="conf/localhost-rsa-cert.pem" certificateChainFile="conf/localhost-rsa-chain.pem" type="RSA" /> </SSLHostConfig> </Connector> --> <!-- Define an AJP 1.3 Connector on port 8009 --> <!-- <Connector protocol="AJP/1.3" address="::1" port="{ajp_port}" redirectPort="8443" /> --> <!-- An Engine represents the entry point (within Catalina) that processes every request. The Engine implementation for Tomcat stand alone analyzes the HTTP headers included with the request, and passes them on to the appropriate Host (virtual host). Documentation at /docs/config/engine.html --> <!-- You should set jvmRoute to support load-balancing via AJP ie : <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1"> --> <Engine name="Catalina" defaultHost="localhost"> <!--For clustering, please take a look at documentation at: /docs/cluster-howto.html (simple how to) /docs/config/cluster.html (reference documentation) --> <!-- <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> --> <!-- Use the LockOutRealm to prevent attempts to guess user passwords via a brute-force attack --> <Realm className="org.apache.catalina.realm.LockOutRealm"> <!-- This Realm uses the UserDatabase configured in the global JNDI resources under the key "UserDatabase". Any edits that are performed against this UserDatabase are immediately available for use by the Realm. --> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <!-- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> --> <!-- Access log processes all example. Documentation at: /docs/config/valve.html Note: The pattern used is equivalent to using pattern="common" --> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> </Engine> </Service> </Server> """ setevn_sh_context = f"""\ #!/bin/bash # sky JAVA_OPTS="-server -XX:+AggressiveOpts -XX:+UseBiasedLocking -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -Djava.security.egd=file:/dev/./urandom -Djava.awt.headless=true" JAVA_OPTS="$JAVA_OPTS -Xms{jvm_mem} -Xmx{jvm_mem} -Xss512k -XX:LargePageSizeInBytes=128M -XX:MaxTenuringThreshold=11 -XX:MetaspaceSize=200m -XX:MaxMetaspaceSize=256m -XX:MaxNewSize=256m" UMASK=0022 CATALINA_PID=$CATALINA_HOME/bin/catalina.pid """ config_dict = { "server_xml": { "config_file": f"{tomcat_dir}/conf/server.xml", "config_context": server_xml_context, "mode": "w" }, "setenv_sh": { "config_file": f"{tomcat_dir}/bin/setenv.sh", "config_context": setevn_sh_context, "mode": "w" }, "tomcat_sh": { "config_file": f"/etc/profile.d/tomcat.sh", "config_context": tomcat_sh_context, "mode": "w" } } log.logger.debug(f"写入配置文件: {json.dumps(config_dict)=}") result, msg = common.config(config_dict) if result: command = f"{tomcat_dir}/bin/catalina.sh configtest" log.logger.debug(f"配置文件检测: {command=}") status, result = common.exec_command(command) # 返回值32512为apr未安装报错, 忽略 if status: if result.returncode != 0 and result.returncode != 32512: log.logger.error(result.stderr) flag = 1 else: log.logger.error(msg) flag = 1 else: log.logger.error(msg) flag = 1 sys.exit(flag) elif action == "run": command = f"set -m ; {tomcat_dir}/bin/catalina.sh start" log.logger.debug(f"{command=}") status, result = common.exec_command(command) if status: if result.returncode != 0: log.logger.error(result.stderr) flag = 1 else: log.logger.debug(f"检测端口: {port_list=}") if not common.port_exist(port_list): flag = 2 else: log.logger.error(result) flag = 1 sys.exit(flag) elif action == "start": pass elif action == "stop": pass
def run(): """运行 """ return_value = normal_code #init_command=f"{mysql_dir}/bin/mysqld --initialize --user={mysql_user} --datadir={mysql_dir}/{my_data}" init_command = f"{mysql_dir}/bin/mysqld --initialize --user={mysql_user}" log.logger.debug(f"初始化中: {init_command=}") result, msg = common.exec_command(init_command, timeout=600) if result: try: log.logger.debug("获取随机密码") with open(f"{mysql_dir}/{my_logs}/mysqld.log", "r") as f: for i in f.readlines(): if "temporary password" in i: pass_line = i.split(" ") init_password = pass_line[-1].strip() log.logger.debug(f"{init_password=}") break else: log.logger.error(f"初始化失败, 请查看MySQL日志") except Exception as e: log.logger.error(str(e)) return error_code mysql_plugin_context = """\ [mysqld] # plugin plugin-load-add=connection_control.so connection_control_failed_connections_threshold=10 connection_control_min_connection_delay=1000 """ config_dict = { "my_plugin_cnf": { "config_file": my_plugin_cnf_file, "config_context": mysql_plugin_context, "mode": "w" } } log.logger.debug(f"写入配置文件: {json.dumps(config_dict)}") result, msg = common.config(config_dict) if result: start_command = f"systemctl start mysqld" log.logger.debug(f"{start_command=}") result, msg = common.exec_command(start_command, timeout=600) if result: log.logger.debug(f"检测端口: {port_list=}") if common.port_exist(port_list): return_value = init(db_info_dict, mysql_dir, init_password, cluster_info_dict, role, log) else: return_value = error_code else: log.logger.error(msg) return_value = error_code else: log.logger.error(msg) return_value = error_code else: log.logger.error(msg) return_value = error_code return return_value