The company Testing Services update more frequently, in order to simplify the process, to achieve the following simple automatic deployment
The deployment process is probably the following figure
after many twists and pains finally found the fabric under the tips of colleagues, but not familiar with Python, after some toss finally realized the above function, the general steps are as follows: install Python, I use the Ubuntu environment, this step omitted, if necessary please own Baidu ... (current fabric appears to support only python2.x) installation fabric
The official Guidance document installation method is as follows
Pip Install Fabric
However, there may be a compilation error during the installation process, please find out the reason for yourself according to the actual situation
I encountered the following two problems in this process
1 python.h missing, using Apt-get install Python-dev can be solved
2 forgot ... It's also a. h file is missing, the solution is to write code
#-*-Coding:utf-8-*-' Created on May 24, 2017 @author: mqy parameter Description: Python auto.py provides 5 run parameter Parameters 1 #秘钥位置 parameter 2 #服务器地址 parameter 3 #tomcat on the server's path parameter 4 #项目名称 parameter 5 #打包好的项目位置 ' ' Import datetime import time import OS import sys Fabric.context_manag ERS import CD from fabric.operations import A, put, sudo from fabric.state import env #tomcat The path to the server Default_tomcat _path = '/usr/local/tomcat ' #服务器地址 default_server_host = ' 127.0.0.1 ' #秘钥位置 default_key_path = ' ~/key.pem ' #项目名称 default_pr Oject_name = ' project ' #打包好的项目位置 default_project_path = ' ~/project.war ' def copy_to_server (): Local ("Scp-i {Key_path} {Product_path} ubuntu@{host}:/tmp/{target_name} '. Format (Key_path=default_key_path, Product_path=default_proje Ct_path, host=default_server_host,target_name=default_project_name+ ". War") def Kill_service (): Set_env (default_ Server_host) sudo ("Kill-9 $ (ps-ef|grep-i%s|grep-v ' grep ' |awk ' {print $} ')"% Default_tomcat_path) def Bak_serv Ice (): Set_enV (default_server_host) with CD ("{Baseservicehome}/webapps". Format (Baseservicehome=default_tomcat_path)): sudo ( ' CP./{baseservice}. /bak/{time}.war.bak '. Format (Baseservice=default_project_name, Time=time.mktime (Datetime.datetime.now (). Timetuple ())) def remove_old_service (): Set_env (Default_server_host) with CD ("{Baseservicehome}/webapps". Format (
Baseservicehome=default_tomcat_path): sudo (' Rm-rf./{service_name} '. Format (service_name=default_project_name)) sudo (' rm./{service_name} '. Format (service_name=default_project_name+ ". War")) def deploy_new_service (): set_env (Default_server_host) with CD ("{Base_service_home}/webapps". Format (Base_service_home=default_tomcat_path)): su Do ("Mv/tmp/{service_file}./". Format (service_file=default_project_name+ ". War")) def start_service (): Set_env ( Default_server_host) with CD ("{base_service_home}/bin". Format (Base_service_home=default_tomcat_path)): sudo ("s Et-m;. /startup.sh ") def SET_env (host): env.host_string = host Env.user = "Ubuntu" Env.key_filename = Default_key_path env.warn_only = True def deploy (): Copy_to_server () Kill_service () Bak_service () Remove_old_service () deploy_new_s
Ervice () Start_service () if __name__ = = ' __main__ ': For I in range (len (SYS.ARGV)): If i==0: Pass if I==1:DEFAULT_KEY_PATH=SYS.ARGV[1] if i==2:default_server_host=sys.argv[ 2] If i==3:default_tomcat_path=sys.argv[3] if i==4:default_project_name=sys.argv [4] If i==5:default_project_path=sys.argv[5] print default_key_path+ = = = "+default_server_host+" = = = = "+default_tomcat_path+" "=====" +default_project_name+ "= =" +default_project_path Deploy ()
Because the first time I wrote Python code, the above process is a bit bizarre, but fortunately the function has been initially implemented. It's not a waste of my day.