-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfabric_commands.py
172 lines (125 loc) · 4.85 KB
/
fabric_commands.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
"""
A useful set of tools for your gae application.
Just add a file 'fabfile.py' to your application root, install
fabric of course::
from gae_django.fabric_commands import *
Available commands::
deploy Deploy an application::
local_shell Open a local shell for this application
remote_shell Open a remote shell for this application
"""
import sys
import os
from fabric.api import env, task, local
def setup_paths():
"""Setup sys.path with everything we need to run."""
import google
DIR_PATH = os.path.abspath(os.path.dirname(os.path.dirname(google.__file__)))
EXTRA_PATHS = [
os.getcwd(),
DIR_PATH,
os.path.join(DIR_PATH, 'lib', 'antlr3'),
os.path.join(DIR_PATH, 'lib', 'django_1_4'),
os.path.join(DIR_PATH, 'lib', 'fancy_urllib'),
os.path.join(DIR_PATH, 'lib', 'ipaddr'),
os.path.join(DIR_PATH, 'lib', 'jinja2'),
os.path.join(DIR_PATH, 'lib', 'protorpc'),
os.path.join(DIR_PATH, 'lib', 'markupsafe'),
os.path.join(DIR_PATH, 'lib', 'webob_0_9'),
os.path.join(DIR_PATH, 'lib', 'webapp2'),
os.path.join(DIR_PATH, 'lib', 'yaml', 'lib'),
os.path.join(DIR_PATH, 'lib', 'simplejson'),
os.path.join(DIR_PATH, 'lib', 'google.appengine._internal.graphy'),
]
sys.path = EXTRA_PATHS + sys.path
def read_app_config():
"""Load the app.yaml file and add some things to env."""
setup_paths()
from google.appengine.tools.dev_appserver import ReadAppConfig
appinfo = ReadAppConfig('app.yaml')
return appinfo
@task
def deploy(version='', appid='.'):
"""
Deploy an application::
Standard Deploy
$ fab deploy
Deploy a different version than in the app.yaml
$ fab deploy:version=fabulous
Deploy to a different appid
$ fab deploy:appid=someotherapp
"""
yaml_file = os.path.join(appid, 'app.yaml')
assert os.path.isfile(yaml_file), "Could not find app.yaml file"
version_str = '-V %s' % version if version else ''
cmd = 'appcfg.py update %s %s' % (version_str, appid)
local(cmd)
def prep_shell(prefix, appid=None, server=None):
"""Setup the remote shell for either remote or local.
* prefix: either 's~' or 'dev~'
* appid: Use a different application id
* server: Point to a different server
"""
appinfo = read_app_config()
# TODO: assert 'remote_api' in appinfo.builtins
if hasattr(appinfo, 'env_variables'):
os.environ.update(appinfo.env_variables)
if appid is not None:
appinfo.application = appid
if server is None:
server = '%s.appspot.com' % appinfo.application
application = '%s%s' % (prefix, appinfo.application)
return application, server
@task
def remote_shell(appid=None, server=None):
"""
Open a remote shell for this application
The builtin 'remote_api' must be set to 'on' in your app.yaml file.
$ fab remote_shell
Use a different application id:
$ fab remote_shell:different-id
Point to a different server
$ fab remote_shell:server=other-app.appspot.com
"""
application, server = prep_shell('s~', appid, server)
from google.appengine.tools import remote_api_shell
from google.appengine.tools import appengine_rpc
remote_api_shell.remote_api_shell(server, application,
remote_api_shell.DEFAULT_PATH, True, appengine_rpc.HttpRpcServer)
@task
def local_shell(appid=None, server=None):
"""
Open a local shell for this application
The builtin 'remote_api' must be set to 'on' in your app.yaml file.
The default will use the remote api against your local dev server,
which is at 'localhost:8080'
$ fab local_shell
Use a different application id:
$ fab local_shell:different-id
Point to a different server
$ fab local_shell:server=localhost:8081
"""
server = server or 'localhost:8080'
application, server = prep_shell('dev~', appid, server)
from google.appengine.tools import remote_api_shell
from google.appengine.tools import appengine_rpc
remote_api_shell.remote_api_shell(server, application,
remote_api_shell.DEFAULT_PATH, False, appengine_rpc.HttpRpcServer)
@task
def runserver(use_sqlite='True', port=8080, clear_datastore=False):
"""
Run the development server.
Helper command to run dev_appserver::
$ fab runserver
Clear the datastore
$ fab runserver:clear_datastore=1
Use a different port.
$ fab runserver:port=8089
"""
cmd = 'dev_appserver.py -p %s ' % port
if use_sqlite.lower() not in ['0', 'false', 'f']:
cmd += '--use_sqlite '
if clear_datastore:
cmd += '-c '
cmd += '.'
local(cmd)