forked from lichengdev/wilddog-doc
/
deploy.py
executable file
·77 lines (61 loc) · 1.63 KB
/
deploy.py
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
#!/usr/bin/env python
import pexpect
import sys
import os
import datetime
import shutil
def scpWithbackUp(localDir,remoteDir,backupDir,hosts):
time=datetime.datetime.now().strftime('%Y%m%d%H%M%S')
sp=remoteDir.split('/')
name=sp[len(sp)-1]
tgz = name+".tar.gz"
res=pexpect.run('npm prune --production',cwd='dist')
res=pexpect.run('tar -zcvf %s %s '%(tgz,localDir),withexitstatus=1,cwd='.')
for host in hosts:
scp=pexpect.spawn('scp %s %s:%s'%(tgz,host,'~'),timeout=3000)
scp.logfile = sys.stdout
scp.expect(pexpect.EOF)
#ssh
ssh = pexpect.spawn('ssh',[host])
ssh.logfile = sys.stdout
ssh.expect(']#')
#backupfile
ssh.sendline("mv %s %s"%(remoteDir,backupDir+"/"+name+"."+time))
ssh.expect(']#')
#unzip
ssh.sendline('tar -xvf %s'%(tgz))
ssh.expect(']#')
#cleanup
ssh.sendline('rm -f %s'%(tgz))
ssh.expect(']#')
#replece
ssh.sendline('mv %s %s'%(localDir,remoteDir))
ssh.expect(']#')
def restartService(hosts,dir_,stop,start):
for host in hosts:
#ssh
ssh = pexpect.spawn('ssh',[host])
ssh.logfile = sys.stdout
ssh.expect(']#')
#cd to dir
ssh.sendline("cd %s"%(dir_))
ssh.expect(']#')
#stop
ssh.sendline(stop)
ssh.expect(']#')
#start
ssh.sendline(start)
ssh.expect(']#')
if __name__=="__main__":
env=sys.argv[1]
hosts=['test']
if(env=='pre'):
hosts=['1-006']
if(env=='product'):
hosts=['101','106']
local= 'dist'
remoteDir='/data/www/z.wilddog.com'
backupDir='/data/www/backup'
for host in hosts:
scpWithbackUp(local,remoteDir,backupDir,[host]);
restartService([host],remoteDir,'forever list','NODE_ENV=production forever restart /data/www/z.wilddog.com/bin/www')