首页 > 代码库 > python 之自动发布版本

python 之自动发布版本

#!/usr/local/bin/python3
# coding:utf-8

# ====================================================
# Author: chang - EMail:changbo@hmg100.com
# Last modified: 2017-5-17
# Filename: updateversion.py
# Description: file upload ,base paramiko, sys, time
# blog:http://www.cnblogs.com/changbo
# ====================================================

"""
1,close tomcat 
2,bakup website
3,delete old website
4,start tomcat
"""

import paramiko
import time
import sys

websitelist = [api]
iplist = [10.0.0.2, 10.0.0.149, 10.0.0.122]
accpwd = {iplist[0]: abc-123, iplist[1]: abc-123, iplist[2]: abc-123}
basefile = /usr/local/tomcat
localpath = 123.pdf
targetpath = %s/webapps/%s % (basefile, websitelist[0])


class SSHSession:
    def __init__(self, host, pwd, username=root, port=22):
        self.host = host
        self.port = port
        self.username = username
        self.pwd = pwd

    def connect(self):
        transport = paramiko.Transport((self.host, self.port))
        transport.connect(username=self.username, password=self.pwd)
        self.__transport = transport

    def close(self):
        self.__transport.close()

    def shutdown(self):
        ssh = paramiko.SSHClient()
        ssh._transport = self.__transport
        print("[*] tomcat will be shutdown ....")
        stdin, stdout, stderr = ssh.exec_command(basefile + "/bin/shutdown.sh")
        time.sleep(2)
        stdin, stdout, stderr = ssh.exec_command("ps -axu|grep tomcat |grep -v grep|awk ‘{print $2}‘")
        tomcatpid = stdout.read().decode()

        if tomcatpid != 0:
            print(>>> Tomcat already shutdown!)
        else:
            print(>>> Tomcat not shutdown complete! will kill the tomcat pid)
            ssh.exec_command("kill -9 " + tomcatpid)
            print(>>> The tomcat pid already killed!)

    def startapp(self):
         ssh = paramiko.SSHClient()
         ssh._transport = self.__transport
         print("[*] Tomcat will be start, please check tomcat log....")
         stdin, stdout, stderr = ssh.exec_command(basefile + "/bin/start.sh")
         print(stdout.read().decode())

    def backup(self):
        ssh = paramiko.SSHClient()
        ssh._transport = self.__transport
        print([*] Bakup old site now ,It will take a few minutes ..... )
        ssh.exec_command("Time=`date +%m%d%H%M` 
                         && cd /usr/local/tomcat/webapps 
                         && tar czf /home/websitebakup/webapps$Time.tar.gz   + websitelist[0])
        print(>>> The web site backup complete!)

    def progress_bar(self, transferred, toBeTransferred, suffix=‘‘):
        bar_len = 60
        filled_len = int(round(bar_len * transferred / float(toBeTransferred)))
        percents = round(100.0 * transferred / float(toBeTransferred), 1)
        bar = = * filled_len + - * (bar_len - filled_len)
        sys.stdout.write(upload [%s] %s%s ...%s\n % (bar, percents, %, suffix))
        sys.stdout.flush()

    def uploadfile(self, local_path, target_path):
        ssh = paramiko.SSHClient()
        ssh._transport = self.__transport
        print([*] Delete old site now ,It will take a few minutes ..... )
        ssh.exec_command(mv -f /usr/local/tomcat/webapps/ + websitelist[0] + /tmp)
        print(>>> The web site delete complete!\n)

        print([*] Clean tomcat web cache  now ....  )
        ssh.exec_command("mv -f " + basefile + "/temp/*  /tmp && mv -f " + basefile + "work/Catalina/localhost/*  /tmp")
        print(>>> Tomcat web cache already cleaned!)

        print("[*] Upload website file to remote server...")
        sftp = paramiko.SFTPClient.from_transport(self.__transport)
        sftp.put(local_path, target_path, callback=self.progress_bar)


if __name__ == __main__:
    i = iplist[0]
    try:
        ssh = SSHSession(i, accpwd[i])
        ssh.connect()
        ssh.uploadfile(localpath, targetpath)
        ssh.close()
    except Exception as e:
        print(e)

 

python 之自动发布版本