python starts server related services

Posted by phpeanuts on Thu, 31 Oct 2019 19:19:19 +0100

Because the server room has no cooling facilities, it often needs to be shut down after work in summer, which makes it very troublesome to restart the application frequently. It is convenient to write a service restart script in python.

Environment dependency: Readme.txt

Environmental dependence:
	python3
	pip3 install paramiko
	pip3 install --upgrade cryptography

Enter the current directory and execute the python script directly:
	python pythonStarter.py

If you want to add a new startup instruction, you only need to set the conditions in the command under the corresponding host Directory:
	[host]
	command={"":""}
	Remarks:
		You need to add as a key value pair:
				key: query process instruction.
				value: to execute the start instruction, multiple instructions are separated by

 

Startup file: pythonStarter.py

#-*- coding:utf-8 -*-
'''
    python One click Start server--All deployed projects
'''

import paramiko
import configparser
import time
import ast
import threadpool

##Set log print level
#logging.basicConfig(format='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s',level=logging.DEBUG)

#Profile read
cf = configparser.ConfigParser()
cf.read(".\config.ini")  # Read the configuration file. If you write the absolute path of the file, you can not use the os module.

missHosts=["data4","name2","name4"]#todo exclude host

pool = threadpool.ThreadPool(4)

'''
    //Execution operation
'''
def operation():
    secs = cf.sections()  # Get all sections in the file (there can be multiple configurations in a configuration file, such as database related configuration and mailbox related configuration. Each section is wrapped by [], i.e., [section]), and return in the form of a list.
    secs=[sec for sec in secs if sec not in missHosts]

    print("{}==>Start initializing all database services...".format(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())))
    db_success = {}
    #Initialize database operation first
    for sec in secs:
        host = cf.get(sec, "host")
        port = cf.get(sec, "port")
        user = cf.get(sec, "user")
        pwd = cf.get(sec, "pwd")
        ssh = getConnection(host,port,user,pwd)

        if ssh:
            db_status = initDatabse(sec,ssh)
            db_success.setdefault(sec,db_status)
        else:
            print("[error]{}Host connection failed!".format(sec))
        closeConnection(ssh)

    #If all databases are initialized successfully, start execution of start instruction
    if False not in db_success.values():
        print("{}==>All database services are initialized!".format(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
        for sec in secs:
            host = cf.get(sec, "host")
            port = cf.get(sec, "port")
            user = cf.get(sec, "user")
            pwd = cf.get(sec, "pwd")
            ssh = getConnection(host, port, user, pwd)
            if ssh:
                startProject(sec,ssh)
            else:
                print("[error]{}Host connection failed!".format(sec))
            closeConnection(ssh)
    else:
        print("{}==>Database not initialized successfully,Please check after trying!",format("|".join([k for k,v in db_success.items() if v==False])))


#Startup project
def startProject(secname,ssh):
    flag = {}
    if assertKey(secname,"command"):
        print("Start project action:[{}]".format(secname))
        dbs = ast.literal_eval(cf.get(secname,"command"))
        for k,v in dbs.items():
            success = assertServiceStatus(ssh,k)
            if success:
                print("=====[{}]Project service started,instructions=[{}]".format(secname,k))
                flag.setdefault(k,True)
            else:
                print("=====[{}]Project services starting,instructions=[{}]".format(secname, v))
                executeService(ssh,v)
        #Verify again that everything is started
        time.sleep(1)
        for k,v in dbs.items():
            success = assertServiceStatus(ssh,k)
            if success:
                flag.setdefault(k,True)
            else:
                print("=====[{}]Host computer[{}]Start failed!".format(secname, v))
                #Try to restart once, return True or False, and the execution information will be printed.
                flag.setdefault(k,restartProject(ssh,k,v))

        if False not in flag.values():
            print("=====[{}]All projects completed!".format(secname))
            return True
        else:
            print("[error]=====[{}]Project start failed!".format(secname))
            return False


#Initialize all databases
def initDatabse(secname,ssh):
    flag = {}
    if assertKey(secname,"database"):
        print("Database operation:[{}]:".format(secname))
        dbs = ast.literal_eval(cf.get(secname,"database"))
        for k,v in dbs.items():
            success = assertServiceStatus(ssh,k)
            if success:
                print("=====[{}]Database service started,instructions=[{}]".format(secname,k))
                flag.setdefault(k,True)
            else:
                print("=====[{}]Database service starting,instructions=[{}]".format(secname, v))
                executeService(ssh,v)
        #Verify again that the programs are all started
        time.sleep(2)
        for k,v in dbs.items():
            success = assertServiceStatus(ssh,k)
            if success:
                flag.setdefault(k,True)
            else:
                print("=====[{}]Host computer[{}]Database service initialization failed!".format(secname, v))
                flag.setdefault(k,False)

        if False not in flag.values():
            print("=====[{}]Initialization of database completed!".format(secname))
            return True
        else:
            print("[error]=====[{}]Failed to initialize database!".format(secname))
            return False


#tomcat failed to start for the program that failed to start, but there will be process for ps query.
def restartProject(ssh,k,v):
    pids=[]
    stdin, stdout, stderr = ssh.exec_command(k,get_pty=True)#Query process
    lines = stdout.readlines()
    for line in lines:
        pid = line[9:14]
        pids.append(pid)
        print("process=[{}],info=[{}]".format(pid,line))

    if pids:
        ssh.exec_command("kill -9 {}".format(" ".join(pids)))
    #Try to restart after killing the process
    stdin, stdout, stderr = ssh.exec_command(v,get_pty=True)
    for out in stdout:
        print("Execution return information:[{}]".format(out))
    for err in stderr:
        print("Execution error message:[{}]".format(err))

    time.sleep(3)
    success = assertServiceStatus(ssh,k)
    return success


#Judge whether the service is started
def assertServiceStatus(ssh,command):
    stdin, stdout, stderr = ssh.exec_command(command,get_pty=True)
    res = [line for line in stdout]
    if len(res) > 0:
        return True
    else:
        return False

#Judge whether the service is started
def executeService(ssh,command):
    try:
        ssh.exec_command("{}".format(command),timeout=2)#Do not wait for execution results
    except:
        print("{}==>Execution complete!")

def assertKey(secname,key):
    has =cf.has_option(secname,key)
    return has

def getConnection(host,port,user,pwd):
    '''
    //Get the link again
    :param host:
    :param port:
    :param user:
    :param pwd:
    :return:
    '''
    ssh = paramiko.SSHClient()  # Create SSH object
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())  # Allow connections to hosts that are not in the know? Hosts file
    try:
        ssh.connect(hostname=host, port=port, username=user, password=pwd,timeout=5)  # Connect to server
        return ssh
    except:
        print("[error]{}connection failed".format(host))
        return None

def closeConnection(ssh):
    '''
    //Close links
    :param ssh:
    :return:
    '''
    try:
        ssh.close()
    except:
        print("[error]{}:Link close exception".format(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))


if __name__ == '__main__':
    start = time.time()
    operation()
    end = time.time()
    print("All projects are started and completed, taking a total of time{}second".format((end-start)))
    # sec="data2"
    # host = cf.get(sec, "host")
    # port = cf.get(sec, "port")
    # user = cf.get(sec, "user")
    # pwd = cf.get(sec, "pwd")
    # ssh = getConnection(host, port, user, pwd)
    # k = "service mysqld status"
    # v = "cd /var/neo4j-community-3.5.1/bin;./neo4j start"
    # #restartProject(ssh,k,v)
    # stdin, stdout, stderr = ssh.exec_command(k, get_pty=True)  # Query process
    #
    # if str(stdout.readlines()[-1]).__contains__("Started MySQL Server."):
    #     print("service started! ""

Configuration file: config.ini

[data1]
host=xxx.xxx.0.101
user=root
pwd=xxxxx
port=22
command={}
[data2]
host=xxx.xxx.0.102
user=root
pwd=xxxxx
port=22
command={
        "ps -ef|grep tomcat-8082|grep -v grep|grep -v PPID":"cd /opt/tomcat-8082/bin;./startup.sh",
        "ps -ef|grep tomcat-8083|grep -v grep|grep -v PPID":"cd /opt/tomcat-8083/bin;./startup.sh",
        "ps -ef|grep tomcat-8087|grep -v grep|grep -v PPID":"cd /opt/tomcat-8087/bin;./startup.sh",
        "ps -ef|grep tomcat-8090|grep -v grep|grep -v PPID":"cd /opt/tomcat-8090/bin;./startup.sh",
        "ps -ef|grep qy-ys.jar|grep -v grep|grep -v PPID":"cd /deploy/qy-ys;./start.sh",
        "ps -ef|grep tomcat-8092|grep -v grep|grep -v PPID":"cd /opt/tomcat-8092/bin;./startup.sh",
        "ps -ef|grep tomcat-8093|grep -v grep|grep -v PPID":"cd /opt/tomcat-8093/bin;./startup.sh",
        "ps -ef|grep tomcat-8094|grep -v grep|grep -v PPID":"cd /opt/tomcat-8094/bin;./startup.sh",
        "ps -ef|grep dims-monitor-1.0.jar|grep -v grep|grep -v PPID":"cd /deploy/dims-monitor;./start.sh"
        }
database={"ps -ef|grep 3306|grep -v grep|grep -v PPID":"service mysqld start","ps -ef|grep elasticsearch|grep -v grep|grep -v PPID":"cd /opt/elasticsearch-1.7.2/bin;./start.sh"}
[data3]
host=xxx.xxx.0.103
user=root
pwd=xxxxx
port=22
command={}
database={"ps -ef|grep neo4j-community-3.5.1|grep -v grep|grep -v PPID":"cd /var/neo4j-community-3.5.1/bin;./neo4j start"}
[name1]
host=xxx.xxx.0.201
user=root
pwd=xxxxx
port=22
command={"ps -ef|grep svn|grep -v grep|grep -v PPID":"svnserve -d -r /home/svn/cetc"}
[name3]
host=xxx.xxx.0.203
user=root
pwd=xxxxx
port=22
command={"ps -ef|grep neo4j.jar|grep -v grep|grep -v PPID":"cd /opt/knowledge-neo4j;nohup java -jar neo4j.jar --spring.config.local=application.yml &",
        "ps -ef|grep 9090|grep -v grep|grep -v PPID":"cd /opt/knowledgegraph-service/com/fdrx/django;rm -rf nohup.out;nohup python3 manage.py runserver 0.0.0.0:9090 &"
        }
database={"ps -ef|grep neo4j-community-3.5.1|grep -v grep|grep -v PPID":"cd /opt/neo4j-community-3.5.1/bin;./neo4j start"}
[name5]
host=xxx.xxx.0.205
user=root
pwd=xxxxx
port=22
command={}
[name6]
host=xxx.xxx.0.206
user=root
pwd=rxxxxxot
port=22
command={}
[name7]
host=xxx.xxx.0.207
user=root
pwd=xxxxx
port=22
command={}
[name8]
host=xxx.xxx.0.208
user=root
pwd=xxxxx
port=22
command={}

Topics: Programming ssh Database Tomcat Python