Just learn python recently, do not want to do something ....
Made an automated deployment of the script, shortcoming.
The following hypothetical database is used together with
The process is as follows:
Local maven Package
upload to Server
Server database library backup
server shutdown Tomcat
Server application backup
Server purge application, work
server boot tomcat
/utils/sshconnection.py, remote Operations Tool class
#!
/usr/bin/python Import OS Import Paramiko class sshconnection: __hostname = ' __port = __username = ' __password = ' __ssh = ' def __init__ (self, hostname, port, username, password): Self.__hostname = Hostname Self.__port = Port Self.__username = Username Self.__password = Password def sshcli ENT (self): print (' ssh%s@%s ... '% (Self.__username, self.__hostname)) Try:self.__ssh = P Aramiko. Sshclient () Self.__ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) self.__ssh.connect (hostname = self.__hostname, username = self.__username, port = Self.__por T, Password = self.__password) print (' SSH%s@%s success!!! '% (self.__username, self.__hostname)) E Xcept Exception as E:print (' SSH%s@%s:%s '% (Self.__username, Self.__hostname, E)) os._exit (0 def exec_command (self, command): Print (' Command: ', command) stdin, stdout, stderr = Self.__ssh.exec_command (command) err_list = Stderr.readlin ES () If Len (err_list) > 0:print (' SSH exec remote command [%s] Error:%s '% (command, err_list[0 ]) print (Stdout.read (). Decode (' Utf-8 ')) def upload (self, src, DST): Try:sftp = self._
_ssh.open_sftp () except Exception as E:print (' Open sftp failed: ', e) os._exit (0) Try:print (' Uploading file:%s-->%s '% (src, DST)) Sftp.put (SRC, DST) print (' Uploaded file:%s-->%s '% (src, DST)) Sftp.close () except Exception as E:print (' UPL oading file failed: ', e) os._exit (0) def close (self): Self.__ssh.close ()
deploy.py, publishing process implementation class
#! /usr/bin/python Import Paramiko import OS import time import sys import configparser import urllib import urllib.reques
T from utils import sshconnection class Deploy: __config_file = ' def __init__ (self, config_file): Self.__config_file = Config_file def deploy (self): start = Int (round (time.time () * 1000)) Config_ Sections_global = ' GLOBAL ' config_sections_local = ' local ' config_sections_remote = ' REMOTE ' now = Time.strftime ('%y%m%d_%h%m%s ') print (' Loading config file: ', self.__config_file) config = Configparser .
Configparser () config.read (self.__config_file) print (' loading config file success! ') # Global project_name = config.get (Config_sections_global, ' project_name ') ENV = Config.get (config_section S_global, ' env ') # local local_project_dir = Config.get (config_sections_local, ' Project_dir ') SRC = Local_project_dir + '/target/root.war ' # Remote Remote_hostname = Config.get (config_sections_remote, ' HOSTNAME ') remote_port
= Config.getint (config_sections_remote, ' port ') Remote_username = Config.get (config_sections_remote, ' USERNAME ') Remote_password = Config.get (config_sections_remote, ' PASSWORD ') Remote_db_username = Config.get (CONFIG_SE Ctions_remote, ' db_username ') Remote_db_password = Config.get (config_sections_remote, ' Db_password ') REMOT E_db_port = Config.get (config_sections_remote, ' db_port ') Remote_db_name = Config.get (Config_sections_remote, ' db_ Name ') Tmp_dir = Config.get (config_sections_remote, ' tmp_dir ') Bak_dir = Config.get (Config_sections_remot E, ' bak_dir ') Bak_db_dir = Bak_dir + '/db ' Bak_app_dir = bak_dir + '/app ' tomcat_home = config.ge
T (config_sections_remote, ' tomcat_home ') App_test_url = Config.get (config_sections_remote, ' APP_TEST_URL ') Key_maven_home = ' MaveN_home ' maven_home = os.getenv (key_maven_home) if (Maven_home = none): Print (' no environment variable configured [' + Key_maven_home + '] ' os._exit (0) # Local Pack cmd = maven_home + '/bin/mvn-f ' + local_project_dir + '/pom.xml package-denv= ' + ENV + '-dmaven.test.skip=true-q ' Print (' Running local command: ', cmd) OS . System (CMD) print (' Running Local command success, file path: ', SRC) # establish remote connection SSH = Sshconnectio N.sshconnection (Remote_hostname, Remote_port, Remote_username, Remote_password) ssh. Sshclient () # War package upload Ssh.upload (SRC, Tmp_dir + '/root.war ') # Remote database backup print (' Backup dat Abase ... ') Ssh.exec_command (' mysqldump-u ' + remote_db_username + '-P ' + Remote_db_password + ' + '-P ' + REMO
Te_db_port + ' + remote_db_name + ' > ' + bak_db_dir + '/' + Now + '. sql ') print (' Backup database Success ') # Remote shutdown tomcat print (' Stop tomcat ... ') Ssh.exec_command (tomcat_home + '/bin/shutdown.sh ') print (' Stop TOMCAT Success ') print (' Kill P Rocess ... ') ssh.exec_command (' ps-ef | grep ' + tomcat_home + ' | grep-v grep | awk \ ' {print $2}\ ' | Xargs kill -15 ') print (' Kill process Success ') # Remote backup applies print (' Backup webapp ... ') Ssh.exec_comman D (' cp-r ' + tomcat_home + '/webapps/root ' + bak_app_dir + '/' + Now ') print (' Backup WebApp success ') Remote Delete project print (' Remove Project ... ') Ssh.exec_command (' rm-rf ' + tomcat_home + '/webapps/root* ') p Rint (' Remove Project success ') # Remote flush cache print (' Remove work ... ') Ssh.exec_command (' RM-RF ' + T
Omcat_home + '/work ') print (' Remove work Success ') # Remote Mobile War to Tomcat under Print (' mv war ... ')
src = tmp_dir + '/root.war ' DST = tomcat_home + '/webapps/' Ssh.exec_command (' mv%s%s '% (SRC, DST)) Print (' MV War suCcess:%s-->%s '% (SRC, DST) # Remote boot tomcat print (' Start tomcat ... ') Ssh.exec_command (TOMCA
T_home + '/bin/startup.sh ') print (' Start Tomcat success ') # Close Connection ssh.close () # Detect success Print (' connectionning ', App_test_url, ' ... ') response = Urllib.request.urlopen (App_test_url) prin T (' Connection ', App_test_url, ' HTTP code: ', Response.getcode ()) if (response.getcode () = =): Print
(' success! ')
Else:print (' Fail!!! ') end = Int (round (time.time () * 1000)) print (' Deploy%s use time%dms. '% (project_name, (end-start))) if __nam e__ = = ' __main__ ': Deploy = Deploy ((sys.argv[1)) Deploy.deploy ()
Deploy.bat Batch Call deploy.py
@echo off
python deploy.py config\config-pc-qa.ini
Pause
/config/config.ini, configuration file,
[Global]
Project_name=project
Env=qa
[local]
project_dir=d:/workspace/project
[remote]
hostname= 192.168.1.80
port=22
username=root
password=123456
db_username=root
db_password=123456
db_port=3306
db_name=project
tmp_dir=/tmp
bak_dir=/bak
tomcat_home=/home/tomcat7
app_test_url=http://192.168.1.80:8080
Source: Https://github.com/xiaojianhx/deploy.git