释放双眼,带上耳机,听听看~!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89 1<pre name="code" class="python">#!/usr/bin/env python
2#--coding:utf8--
3import sys,subprocess,os,datetime,paramiko,re
4local_path='/home/pangpeng/'
5nginx_web_path='/var/www/html/'
6tomcat_webapps_path='/usr/local/tomcat7/webapps/'
7backup_nginx_path='/tmp/backup_nginx/'
8backup_tomcat_app='/tmp/backup_tomcat_app/'
9username='root'
10port=22 #ssh port
11
12def web(web_dir,update_ip):
13 #creat remote backup_path
14 creat_backup_dir_cmd = 'mkdir '+backup_nginx_path
15 exec_cmd(update_ip,creat_backup_dir_cmd)
16
17 current_date = datetime.datetime.now().strftime("%Y%m%d%H%M")
18 cmd = 'mv '+nginx_web_path+web_dir+' '+backup_nginx_path+web_dir+'_'+current_date
19 #backup
20 exec_cmd(update_ip,cmd)
21 #upload file
22 upload_cmd = 'scp -r '+local_path+web_dir+' '+update_ip+':'+nginx_web_path
23 print(upload_cmd)
24 subprocess.Popen(upload_cmd,shell=True,stdout=subprocess.PIPE)
25 #change owner
26 #chmod_cmd='chown -R jujusport.jujusport '+nginx_web_path+web_dir
27 #exec_cmd(update_ip,chmod_cmd)
28
29def tomcat(tomcat_ip,app):
30
31 root_dir = re.split('\.',app)[0]
32 creat_backup_tomcat_app_cmd = 'mkdir '+backup_tomcat_app
33 current_date = datetime.datetime.now().strftime("%Y%m%d%H%M")
34 tomcat_cmd='pkill -15 java;sleep 5;'+'mv '+tomcat_webapps_path+app+' '+backup_tomcat_app+app+'_'+current_date
35 tomcat_cmd2='mv '+tomcat_webapps_path+root_dir+' '+backup_tomcat_app+root_dir+'_'+current_date
36 upload_cmd='scp '+local_path+app+' '+tomcat_ip+':'+tomcat_webapps_path
37 cmd='sudo -u jujusport /usr/local/tomcat7/bin/catalina.sh start'
38
39 #create backup dir
40 exec_cmd(tomcat_ip,creat_backup_tomcat_app_cmd)
41 #backup PacketName.war PacketName
42 exec_cmd(tomcat_ip,tomcat_cmd)
43 exec_cmd(tomcat_ip,tomcat_cmd2)
44 #upload file
45 subprocess.Popen(upload_cmd,shell=True,stdout=subprocess.PIPE)
46 print "upload done:"+upload_cmd
47 #start tomcat
48 exec_cmd(tomcat_ip,cmd)
49
50
51def exec_cmd(server_ip, cmd):
52 paramiko.util.log_to_file('/tmp/paramiko.log')
53 pkey='/root/.ssh/id_rsa'
54 key=paramiko.RSAKey.from_private_key_file(pkey)
55 s=paramiko.SSHClient()
56 s.load_system_host_keys()
57 s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
58 s.connect(server_ip,port,username,pkey=key)
59 try:
60 print server_ip+':'+cmd
61 stdin,stdout,stderr=s.exec_command(cmd)
62 print 'stdout:'+stdout.read()
63 print 'stderr:'+stderr.read()
64 except Exception, e:
65 print e
66 finally:
67 s.close()
68
69def cleanstore(server_ip,back_up_dir):
70 command = "find %s -type d -mtime +30 |xargs rm -fr" %(back_up_dir)
71 exec_cmd(server_ip,command)
72if __name__ == "__main__":
73 print '''
74 example:deploy.py web v1
75 example:deploy.py tomcat 192.168.1.192 ROOT.war
76 '''
77 if (sys.argv[1] == 'web') and (sys.argv[2]=='v1'):
78 web("v1",'192.168.1.193')
79 cleanstore('192.168.1.193',backup_nginx_path)
80 elif (sys.argv[1] == 'tomcat') and (sys.argv[2]=='192.168.1.192'):
81 tomcat('192.168.1.192',sys.argv[3])
82 cleanstore('192.168.1.192',backup_tomcat_app)
83 elif (sys.argv[1] == 'tomcat') and(sys.argv[2]=='192.168.1.193'):
84 tomcat('192.168.1.193',sys.argv[3])
85 cleanstore('192.168.1.193',backup_tomcat_app)
86 else:
87 sys.exit(0)
88
89
sed -i 's/Defaults requiretty/#Defaults requiretty/' /etc/sudoers
#visudo 注释掉Defaults requiretty
不然无法执行:
1
2
3 1cmd='sudo -u jujusport /usr/local/tomcat7/bin/catalina.sh start'
2 exec_cmd(tomcat_ip,cmd)
3