forked from mpoegel/SemNExT-Visualizations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy
144 lines (132 loc) · 5.08 KB
/
deploy
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python
import sys
import os
import subprocess
import glob
dirs = {
"dev": "/var/www/chem-dev",
"production": "/var/www/chem"
}
repo_url = 'https://github.com/mpoegel/SemNExT-Visualizations'
def pipeCommand(hostname, command, printResult=True):
ssh = subprocess.Popen(['ssh', '%s' % hostname, command],
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
result = ssh.stdout.readlines()
if (result == []):
error = ssh.stderr.readlines()
if (not (len(error) == 1 and '[sudo] password for' in error[0])):
print('>> ERROR')
for line in error:
line = line.strip()
print(line)
print('>> Aborting!')
sys.exit(1)
for line in result:
line = line.strip()
if printResult: print(line)
return result
def deploy(hostname, password, stategy, branch, config_file):
print('>> Stopping forever service...')
f_pid = pipeCommand(hostname,
'echo "{0}" | sudo -S forever list | grep {1}/src/server.js | awk {2}'
.format(password, dirs[strategy], "'{print $2}'"), False)
if (f_pid):
f_pid = f_pid[0].strip()
f_pid = f_pid.replace('[', '').replace(']', '')
pipeCommand(hostname, 'echo "{0}" | sudo -S forever stop {1}'
.format(password, f_pid))
print('\t stopped old forever process %s' % f_pid)
else:
print('\t no matching forever process found')
print('>> Cleaning up old files...')
pipeCommand(hostname, 'echo "{0}" | sudo -S rm -rf {1}'.format(password,
dirs[strategy]))
print('>> Pulling most recent commit on branch %s...' % branch)
pipeCommand(hostname,
'echo "{0}" | sudo -S git clone --depth=1 --branch={1} {2} {3}'
.format(password, branch, repo_url, dirs[strategy]))
print('>> Installing dependencies...')
pipeCommand(hostname,
'cd {0} && echo "{1}" | sudo -S npm install --production'
.format(dirs[strategy], password))
print('>> Downloading definitions...')
pipeCommand(hostname,
'cd {0} && echo "{1}" | sudo -S ./node_modules/tsd/build/cli.js install'
.format(dirs[strategy], password))
print('>> Compiling TypeScript...')
pipeCommand(hostname,
'cd {0} && echo "{1}" | sudo -S ./node_modules/typescript/bin/tsc -p ./src'
.format(dirs[strategy], password))
print('>> Bundling JavaScript for the client...')
pipeCommand(hostname,
'cd {0} && echo "{1}" | sudo -S mkdir -p ./src/public/script'
.format(dirs[strategy], password))
files = glob.glob('./src/client/script/*.js')
for file in files:
file = file.replace('\\', '/')
outfile = './src/public/script/' + file.split('/')[-1]
print('\t {0} -> {1}'.format(file, outfile))
pipeCommand(hostname,
('cd {0} && echo "{1}" | sudo -S ./node_modules/browserify/bin/cmd.js ' +
'{2} --outfile {3}')
.format(dirs[strategy], password, file, outfile))
print('>> Bundling CSS')
css_deps_file = open('./src/client/style/dependencies')
for dep in css_deps_file:
dep = dep.strip()
dest = '../src/public/style/';
orig = '/'.join([ d if '*' not in d else '' for d in dep.split('/')[1:] ])
if (dep != ''):
print('\t {0} -> {1}'.format(dep, dest))
pipeCommand(hostname,
'cd {0}/node_modules && echo "{1}" | sudo -S cp -r --parents {2} {3}'
.format(dirs[strategy], password, orig, dest))
css_deps_file.close()
print('>> Copying config file...')
scp = subprocess.Popen(['scp', config_file,
'{0}:~/tmp/config.json'.format(hostname, dirs[strategy])],
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
error = scp.stderr.readlines()
if (len(error) > 0):
print('>> ERROR')
for line in error:
line = line.strip()
print(line)
print('>> Aborting!')
sys.exit(1)
pipeCommand(hostname,
'echo "{0}" | sudo -S mv ~/tmp/config.json {1}/src && rm ~/tmp/config.json'
.format(password, dirs[strategy]))
print('>> Starting forever process as root...')
pipeCommand(hostname,
'echo "{0}" | sudo -S forever -e {1}/stderr.log start {1}/src/server.js'
.format(password, dirs[strategy]))
uptime = pipeCommand(hostname,
'echo "{0}" | sudo -S forever list | grep {1}/src/server.js | awk {2}'
.format(password, dirs[strategy], "'{print $9}'"), False)
if (not uptime or uptime[0].strip() == 'STOPPED'):
print('>> Failed to start process.')
pipeCommand(hostname, 'echo "{0}" | sudo -S cat {1}/stderr.log'
.format(password, dirs[strategy]))
print('>> Deployment failed.')
else:
print('>> Deployment successful.')
if (__name__ == '__main__'):
if (len(sys.argv) < 6):
print 'Usage: ',
print sys.argv[0] + " [hostname] [password] ['dev', 'production']" \
+ " [branch] [config file]"
sys.exit(1)
hostname = sys.argv[1]
password = sys.argv[2]
strategy = sys.argv[3]
branch = sys.argv[4]
config_file = sys.argv[5]
if (strategy not in dirs.keys()):
print 'Unrecognized deployment strategy: %s' %strategy
sys.exit(1)
deploy(hostname, password, strategy, branch, config_file)