forked from ProjectSidewalk/SidewalkWebpage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.py
225 lines (180 loc) · 7.94 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
'''
Script to create a binary of a new version of Sidewalk on the server
and deploys it to port 9000
Usage: change into the git:SidewalkWebpage directory
run python deploy.py
If only the application needs to run, use
python deploy.py run
'''
import os
import shutil
import inspect
import subprocess
import sys
import glob
import errno
import datetime
import re
import time
sidewalk_home_directory = "/var/www/html/sidewalk"
sidewalk_app_directory = sidewalk_home_directory + "/sidewalk-webpage"
# Helper function
def run_shell_command(command):
subprocess.call(command.split())
# Functions to manage distribution files
def stop_existing_application():
# rm_pid_cmd = "rm " + sidewalk_app_directory + "/RUNNING_PID"
# subprocess.call(rm_pid_cmd.split())
# Identify the running Play PID. If there is one, kill.
play_pid_command="netstat -tulpn"
p1 = subprocess.Popen(play_pid_command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p2 = subprocess.Popen(["grep", "9000"], stdin=p1.stdout, stdout=subprocess.PIPE)
p3 = subprocess.Popen(["awk", "{print $7}"], stdin=p2.stdout, stdout=subprocess.PIPE)
p4 = subprocess.Popen(["cut", "-d", "/", "-f", "1"], stdin=p3.stdout, stdout=subprocess.PIPE)
stdout, stderr = p4.communicate()
play_pid_9000 = stdout.strip('\n')
p1.stdout.close()
p2.stdout.close()
p3.stdout.close()
p4.stdout.close()
if play_pid_9000 != '':
print "Running process has pid: " + play_pid_9000
subprocess.call(["kill", play_pid_9000])
print "Killed older application process"
else:
print "No running application process to kill"
def move_existing_application():
"""Check if the sidewalk-webpage directory exists already. If so, change the name of the directory"""
print "Checking if the directory `sidewalk-webpage` already exists"
command = "ls %s" % sidewalk_home_directory
p = subprocess.Popen(command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
ls_output = stdout.split("\n")
if "sidewalk-webpage" in ls_output:
print "Changing the directory name from `sidewalk-webpage` to `_sidewalk-webpage`"
command = "mv %s %s" % (sidewalk_app_directory,
sidewalk_home_directory + "/_sidewalk-webpage")
run_shell_command(command)
else:
# Directory doesn't exist create one
print "Directory `sidewalk-webpage` does not exist"
# Create a new sidewalk-webpage folder
command = "mkdir " + sidewalk_app_directory
run_shell_command(command)
print "Directory `sidewalk-webpage` created"
def unzip_file(zip_file_path):
"""Unzip and run the application"""
print "Unzipping the files"
# command = "bsdtar -xf %s -s'|[^/]*/||' -C %s" % (zip_file_path, sidewalk_app_directory)
command = "unzip %s -d %s" % (zip_file_path, sidewalk_app_directory)
p = subprocess.Popen(command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
if stdout not in ['', '\n']:
print stdout
zip_file_name = (zip_file_path.split('/')[-1]).replace(".zip", "")
print "Zip Filename: " + zip_file_name
unzipped_folder = os.path.join(sidewalk_app_directory, zip_file_name)
print unzipped_folder
for filename in os.listdir(unzipped_folder):
file_to_move = os.path.join(unzipped_folder, filename)
print "File to move:" + file_to_move
try:
shutil.copytree(file_to_move, os.path.join(sidewalk_app_directory, filename))
except OSError as e:
# If the error was caused because the source wasn't a directory
if e.errno == errno.ENOTDIR:
shutil.copy(file_to_move, sidewalk_app_directory)
else:
print('\tDirectory not copied. Error: %s' % e)
shutil.rmtree(unzipped_folder)
else:
print "Error while unzipping:"
print stderr
print "Finished unzipping the files"
# change_permission_command = "chmod g+w " + sidewalk_app_directory + "/*"
# subprocess.call(change_permission_command.split())
def run_application():
"""Run the application"""
print "Starting the application"
# command = "%s/sidewalk_runner.sh >/dev/null 2>&1 &" % sidewalk_home_directory
command = "nohup %s/bin/sidewalk-webpage -Dhttp.port=9000 -mem 3072 -J-server > %s/nohup.out &" % (sidewalk_app_directory,
sidewalk_app_directory)
subprocess.call(command, shell=True)
print "Started running the application"
def remove_previous_application():
"""Remove the application that was previously here"""
print "Checking if the directory `_sidewalk-webpage` exists"
command = "ls %s" % sidewalk_home_directory
p = subprocess.Popen(command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
ls_output = stdout.split("\n")
if "_sidewalk-webpage" in ls_output:
print "Removing `_sidewalk-webpage` directory"
command = "rm -r %s" % sidewalk_home_directory + "/_sidewalk-webpage"
run_shell_command(command)
else:
print "Directory `_sidewalk-webpage` does not exist"
# Functions to add timestamp to the deployed version
def add_version_to_the_footer():
p1 = subprocess.Popen(["grep", "-e", "version :=", "build.sbt"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p2 = subprocess.Popen(["cut", "-d", "\"", "-f", "2"], stdin=p1.stdout, stdout=subprocess.PIPE)
stdout, stderr = p2.communicate()
version = stdout.strip('\n')
p1.stdout.close()
p2.stdout.close()
timestamp = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d')
version_string = "Version %s | Last Updated: %s" % (version, timestamp)
with file("./app/views/main.scala.html", "r+") as f:
file_contents = f.read()
new_file_contents = re.sub(r"""<span id="application-version">.*</span>""",
"""<span id="application-version">""" + version_string + """</span>""",
file_contents)
f.seek(0)
f.write(new_file_contents)
f.truncate()
def remove_version_from_the_footer():
with file("./app/views/main.scala.html", "r+") as f:
file_contents = f.read()
new_file_contents = re.sub(r"""<span id="application-version">.*</span>""",
"""<span id="application-version"></span>""",
file_contents)
f.seek(0)
f.write(new_file_contents)
f.truncate()
# Functions to create a binary distribution
def call_activator_dist():
command = 'activator dist'
run_shell_command(command)
def create_distribution():
print "Adding timestamp to the footer"
add_version_to_the_footer()
print "Started zipping up files"
call_activator_dist()
remove_version_from_the_footer()
def prepare_local_repo():
# Update repo
run_shell_command("git pull")
# Run grunt
run_shell_command("grunt")
# Main Script
if __name__ == '__main__':
if len(sys.argv) > 1:
if sys.argv[1] == 'dist':
create_distribution()
elif sys.argv[1] == 'run':
run_application()
else:
prepare_local_repo()
create_distribution()
# Get created distribution file
current_file_path = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
zip_file_path = os.path.join(current_file_path, "target/universal/")
file_list = glob.glob(zip_file_path + "sidewalk-webpage-*.zip")
file_list = sorted(file_list)
zip_file_path = file_list[-1]
print "File to be deployed: " + zip_file_path
stop_existing_application()
move_existing_application()
unzip_file(zip_file_path)
run_application()
remove_previous_application()