|
# -*- coding: utf-8 -*-
#
!/usr/bin/python
#
########################################################################
#
File Name: Jenkins_Project_Deploy.py
#
Program function: ueed for deployment code in CI server.
#
Author:Jeson
#
mail:jeson@imoocc.com
#
Created Time: 一 6/20 19:24:33 2016
#
========================================================================
import ConfigParser
import sys
from sys import argv
import paramiko
def Gennerate_cmd(Project_Name,CI_IP,Project_Code_File,Project_Exclude_File,Project_Backup_Dir,Project_Deploy_Path,Project_Start_Command,Project_Log_Path):
cmd = []
#生成备份命令
if Project_Backup_Dir:
#生成备份原有文件
cmd.append("mkdir -p /opt/")
cmd.append("rsync -avzP %s %s && touch /tmp/CI_%s_backuptrue"%(Project_Deploy_Path,Project_Backup_Dir,Project_Name))
#生成关闭原有进程命令
cmd.append('test -e /tmp/CI_%s_backuptrue && ps -auxgrep "name:%s"grep -v grepawk \'{print $2}\'xargs kill'%(Project_Name,Project_Name))
#生成命令-确认原来服务已经顺利关闭
cmd.append('ps -auxgrep "name:%s"grep -v grepawk \'{print $2}\'&& touch /tmp/CI_killSucc ps -auxgrep "name:%s"grep -v grepawk \'{print $2}\'xargs kill -9'%(Project_Name,Project_Name))
#生成命令-用于同步新的代码文件
if Project_Code_File == "":
cmd.append('test -e "/tmp/CI_%s_killSucc" && rsync -avzP --delete-after --exclude "{%s}" %s::%s %s'%(Project_Name,Project_Exclude_File,CI_IP,Project_Name,Project_Deploy_Path))
else:
cmd.append('test -e "/tmp/CI_%s_killSucc" && rsync -avzP --delete-after --exclude "{%s}" %s::%s/%s %s'%(Project_Name,Project_Exclude_File,CI_IP,Project_Name,Project_Code_File,Project_Deploy_Path))
#生成命令-用于启动服务
cmd.append(Project_Start_Command)
#生成命令-查看服务启动后的日志
cmd.append('tail -n 200 -f %s'%Project_Log_Path)
#生成命令-清理此次生成的临时文件
# cmd.append('rm -f /tmp/\*%s\*'%Project_Name)
return cmd
def Login_Execute_Command(Project_IP,Project_Login_Port,Project_Login_User,Login_ISA_Key,cmds):
Project_Port = int(Project_Login_Port) if Project_Login_Port else 22
if Project_IP and Project_Login_User and Login_ISA_Key:
key = paramiko.RSAKey.from_private_key_file(Login_ISA_Key)
s = paramiko.SSHClient()
s.load_system_host_keys()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(hostname=Project_IP,port=Project_Port,username=Project_Login_User,pkey=key)
for cmd_item in cmds:
stdin,stdout,stderr=s.exec_command(cmd_item)
print stdout.read()
return True
else:
return false
def main():
if len(argv) > 2:
Config_File = argv[1] if argv[1] else "./Jenkins_Project_Deploy.conf "
Project_Name = argv[2] if argv[2] else ""
else:
print "Error 0:Input argv format error! ./Jenkins_Project_Deploy.py ./test.conf testname"
sys.exit()
try:
cf = ConfigParser.ConfigParser()
cf.read(Config_File)
CI_IP = cf.get(Project_Name, "CI_IP")
Project_Code_File = cf.get(Project_Name, "Project_Code_File")
Project_Exclude_File = cf.get(Project_Name, "Project_Exclude_File")
Project_Backup_Dir = cf.get(Project_Name, "Project_Backup_Dir")
Project_Deploy_Path = cf.get(Project_Name, "Project_Deploy_Path")
Project_Start_Command = cf.get(Project_Name, "Project_Start_Command")
Project_Log_Path = cf.get(Project_Name, "Project_Log_Path")
execute_command = Gennerate_cmd(Project_Name,CI_IP,Project_Code_File,Project_Exclude_File,Project_Backup_Dir,Project_Deploy_Path,Project_Start_Command,Project_Log_Path)
print "{Step 1: Command collected over!}",execute_command
Project_IP = cf.get(Project_Name, "Project_IP")
Project_Login_Port = cf.get(Project_Name, "Project_Login_Port")
Project_Login_User = cf.get(Project_Name, "Project_Login_User")
Login_ISA_Key = cf.get(Project_Name, "Login_ISA_Key")
except Exception as e:
print "Error 1: %s file open or read error!"%Config_File
sys.exit()
print "{Step 2:Prepare to loging...}",Project_IP
result = Login_Execute_Command(Project_IP,Project_Login_Port,Project_Login_User,Login_ISA_Key,cmds=execute_command)
if result is False:
print "Error 3:Execute command failed!"
if __name__ == '__main__':
main() |
|