-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote.py
More file actions
298 lines (235 loc) · 11.3 KB
/
Copy pathremote.py
File metadata and controls
298 lines (235 loc) · 11.3 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
from __future__ import with_statement
from os.path import dirname, join, exists, basename, splitext, isdir, relpath
from os import listdir, mkdir, makedirs, remove, chdir
import os
import subprocess
from shutil import rmtree
import sys
import shutil
from jwl_make import JWLReader, gen, need_regen, merge_source_file, project_to_path, clean_path, resolve_import
import tornado.web, tornado.auth
#patching rmtree so it can deal with readonly files
def onerror(func, path, exc_info):
"""
Error handler for ``shutil.rmtree``.
If the error is due to an access error (read only file)
it attempts to add write permission and then retries.
If the error is for another reason it re-raises the error.
Usage : ``shutil.rmtree(path, onerror=onerror)``
"""
import stat
if not os.access(path, os.W_OK):
# Is the error an access error ?
os.chmod(path, stat.S_IWUSR)
func(path)
else:
raise
def sys_call(args,cwd=None, failokay=False):
ret = subprocess.call(args, cwd=cwd, shell=True)
if ret != 0:
if failokay:
print 'warning, call failed: ' + args
else:
raise Exception('call failed: ' + args)
def do_action(project, actionargs, deploypath, global_config, extra_env = {}):
target = actionargs[0]
branch = 'release' if len(actionargs) < 2 else actionargs[1]
deploypath = join(deploypath, target)
dependspath = join(deploypath, 'depends')
codepath = join(deploypath, 'code')
staticpath = join(deploypath, 'static')
htmlpath = join(deploypath, 'html')
reader = JWLReader(project)
#SWITCH TO RELEASE BRANCH
try:
if branch != 'CURRENT':
sys_call('git checkout ' + branch, reader.path)
reader.compile_coffee()
#SETUP DEPLOY CONFIG
envkey = target + '_'
dplines = ['from jwl import deployconfig']
config_data = {}
for section in reader._config.sections():
for key, value in reader._config.items(section):
if section.startswith(envkey) or section.find('_') == -1:
if section.startswith(envkey):
sectiontitle = 'env.' + section[len(envkey):]
else:
sectiontitle = section
rvalue = repr(value)
dplines.append("deployconfig.set2('%(sectiontitle)s.%(key)s', %(rvalue)s)"%locals())
dplines.append("print '%(sectiontitle)s.%(key)s', '=', %(rvalue)s"%locals())
config_data[sectiontitle + '.' + key] = value
print sectiontitle + '.' + key, '=', value
config_data['env'] = target
for key, value in extra_env.iteritems():
rvalue = repr(value)
dplines.append("deployconfig.set2('%(key)s', %(rvalue)s)"%locals())
dplines.append("print '%(key)s', '=', %(rvalue)s"%locals())
config_data[key] = value
#server_side paths
server_deploypath = config_data['env.basic.deploypath']
server_dependspath = server_deploypath + '/depends'
server_codepath = server_deploypath + '/code'
server_staticpath = server_deploypath + '/static'
server_htmlpath = server_deploypath + '/html'
rserver_dependspath = repr(server_dependspath)
rserver_codepath = repr(server_codepath)
rserver_staticpath = repr(server_staticpath)
rserver_htmlpath = repr(server_htmlpath)
deployrepo = config_data['env.basic.deployrepo']
if not exists(deploypath):
makedirs(deploypath)
sys_call('git clone ' + deployrepo + ' ' + deploypath)
sys_call('git checkout uploaded', deploypath)
else: #clean but don't remove the .git directory
sys_call('git pull origin uploaded', deploypath)
for p in (dependspath, codepath, htmlpath, staticpath):
if exists(p): rmtree(p, onerror=onerror)
for p in (dependspath, codepath, htmlpath, staticpath):
if not exists(p): makedirs(p)
print 'fetching dependencies'
#fetch the dependencies
depends = reader.config_items('depends')
for name, url in depends:
dpath = join(dependspath, name)
if url.startswith('local:'):
url = url[6:]
ls = url.split(';')
i = 0
try:
while not exists(ls[i]):
i += 1
except:
raise Exception('could not find path ' + url)
url = ls[i]
if exists(dpath):
rmtree(dpath)
shutil.copytree(url, dpath, ignore=shutil.ignore_patterns('*.git', '*.svn'))
else:
if not exists(dpath):
try:
makedirs(dpath)
sys_call('git init', dpath)
#sys_call('git remote add origin ' + url, dpath)
except:
rmtree(dpath)
raise
sys_call('git pull ' + url + ' master', dpath)
#run any custom compilation code
if exists(join(reader.path, 'compile.py')):
old_path = list(sys.path)
sys.path.append(reader.path)
sys.path.append(dependspath)
import compile
compile
compile.run(reader)
del sys.path[:]
sys.path.extend(old_path)
gen(join(codepath, 'deployconfig_init.py'), '\n'.join(dplines))
#legacy...
# config_data['facebook_app_id'] = config_data['env.facebook.facebook_app_id']
urlhandlers = []
#create the html pages
for sourcefile in reader.get_html(config_data):
stripped_name = basename(sourcefile.path).rsplit('.', 1)[0]
gen(join(htmlpath, stripped_name), merge_source_file(sourcefile))
urlhandlers.append('urlhandlers.append((r"/(%(stripped_name)s)", NoCacheStaticHandler, {"path": %(rserver_htmlpath)s}))'%locals())
urlhandlers.append('urlhandlers.append((r"/()", NoCacheStaticHandler, {"path": %(rserver_htmlpath)s, "default_filename": "index"}))'%locals())
#copy over resources
if exists(staticpath):
rmtree(staticpath)
for sourcefile in reader.get_resources(config_data):
relative_path = relpath(sourcefile.path, reader.resources)
if not exists(join(staticpath, dirname(relative_path))): makedirs(join(staticpath, dirname(relative_path)))
if sourcefile.binary:
shutil.copy(sourcefile.path, join(staticpath, relative_path))
else:
gen(join(staticpath, relative_path), merge_source_file(sourcefile))
rprefix = reader.resource_prefix
urlhandlers.append('urlhandlers.append((r"/%(rprefix)s/(.*)", NoCacheStaticHandler, {"path": %(rserver_staticpath)s}))'%locals())
#copy over any raw python files
for file in reader.list_python():
shutil.copy(file, join(codepath, basename(file)))
cookie_secret = repr(reader.config('basic', 'cookie_secret'))
# deal with in a non-legacy way at some point?
# google_consumer_key = reader.config('google', 'consumer_key')
# google_consumer_secret = reader.config('google', 'consumer_secret')
#build server_interface.js
server_interface_path = resolve_import('jwl_make/server_interface.js', None)
del sys.path[:]
sys.path.extend(clean_path)
sys.path.append(dependspath)
sys.path.append(codepath)
import index
from jwl.remote_method import make_dummy_handler
with open(join(htmlpath, 'server_interface.js'), 'w') as f:
f.write('var server = "%s";'%reader.server_prefix)
with open(server_interface_path, 'r') as f2:
f.write(f2.read())
f.write('\n')
f.write(make_dummy_handler(index.main).write_js_interface())
urlhandlers.append('urlhandlers.append((r"/(server_interface.js)", NoCacheStaticHandler, {"path": %(rserver_htmlpath)s}))'%locals())
urlhandlercode = '\n'.join(urlhandlers)
readerserverprefix = reader.server_prefix
is_debug = config_data['env.basic.debug']
server_port = config_data['env.basic.port']
#build the execution file
launch_server = r"""
import sys
sys.path.append(%(rserver_dependspath)s)
sys.path.append(%(rserver_codepath)s)
import deployconfig_init
from jwl import deployconfig
deployconfig.set(debug=%(is_debug)s)
import index
import tornado
from jwl.tornado_launch import launch
from jwl.remote_method import make_dummy_handler, NoCacheStaticHandler
urlhandlers = []
%(urlhandlercode)s
#GOOGLE LOGIN
# from jwl.googleauth import LoginController
# urlhandlers.append((r"/auth/(.*)", LoginController))
if __name__ == '__main__':
urlhandlers.append((r"/%(readerserverprefix)s.*", index.main))
print 'about to run startup code'
index.do_startup(urlhandlers)
application = tornado.web.Application(urlhandlers, cookie_secret=%(cookie_secret)s, gzip=True)#, google_consumer_key=google_consumer_key, google_consumer_secret=google_consumer_secret)
index.main._my_application = application
print 'startup code complete, starting server...'
launch(application, %(server_port)s)
"""%locals()
gen(join(codepath, 'launch_server.py'), launch_server)
print 'about to upload...'
#check in the local code to git
sys_call('git add --all', deploypath, failokay=True)
sys_call('git commit -a -m "automated..."', deploypath, failokay=True)
sys_call('git push origin uploaded', deploypath)
#Upload to server
host_string = config_data['env.basic.host']
if host_string == 'localhost':
execute = sys_call
else:
import fabric.api as fab
def execute(args, cwd, fail_okay):
with fab.settings(host_string=host_string,key_filename=keyfile,disable_known_hosts=True):
with fab.cd(cwd):
with fab.settings(warn_only=fail_okay):
fab.run(args)
keyfile = global_config.get('keys', config_data['env.basic.sshkey'])
try:
execute('git add --all', server_deploypath, True)
execute('git commit -a -m "saving any changes such as .pyc etc"', server_deploypath, True)
execute('git merge uploaded', server_deploypath, False)
execute(config_data['env.basic.startcommand'], server_deploypath, False)
finally:
if host_string != 'localhost':
from fabric.state import connections
for key in connections.keys():
connections[key].close()
del connections[key]
finally:
#SWITCH BACK TO MASTER BRANCH
if branch != 'CURRENT':
sys_call('git checkout master', reader.path)