This repository has been archived by the owner on Apr 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
fabfile.py
201 lines (161 loc) · 5.9 KB
/
fabfile.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
import os.path
from StringIO import StringIO
from fabric.api import env, local, cd, run, put, settings, hide
from fabric.contrib.files import exists
osm_login = None
from local_fabfile import *
local_repo = os.path.dirname(__file__)
server_name, server_prefix = server.split(':')
server_repo = "%s/src/OsmPoint" % server_prefix
server_virtualenv = "%s/virtualenv" % server_prefix
server_var = "%s/var" % server_prefix
osmapi_url = ("http://svn.openstreetmap.org/applications/utils/python_lib/"
"OsmApi/OsmApi.py")
PRODUCTION_CONFIG = """\
import os
import logging
import yaml
workdir = os.path.dirname(__file__)
OPENID_FS_STORE_PATH = os.path.join(workdir, 'openid_store')
OSM_PASSWORD_PATH = os.path.join(workdir,'osm-login.txt')
SQLALCHEMY_DATABASE_URI = "sqlite:///" + os.path.join(workdir, 'db.sqlite3')
with open(os.path.join(workdir, 'secret'), 'rb') as f:
SECRET_KEY = f.read().strip()
OSM_API = "www.openstreetmap.org"
IMPORTED_POINTS_PATH = os.path.join(workdir, 'points.yaml')
REDIS_SOCKET_PATH = os.path.join(workdir, 'redis.sock')
REDIS_DATA_PATH = os.path.join(workdir, 'redis.db')
OSMPOINT_ADMINS = [
# Alex Morega:
'http://grep.ro/openid',
('https://www.google.com/accounts/o8/id?'
'id=AItOawlvc4WaevDOhwzbc2j3rM74GSF9Cy5gMbY'),
# Groza Camelia:
'http://camelia-groza.myopenid.com/',
('https://www.google.com/accounts/o8/id?'
'id=AItOawnOOhuS0-AQvEC1EYyAc2zKzcRl0LV5OBU'),
]
CLOUDMADE_API_KEY = "87d74b5d089842f98679496ee6aef22e"
GOOGLE_ANALYTICS_ID = "UA-25325838-1"
MAIL_SERVER = 'localhost'
MAIL_FROM = '[email protected]'
MAIL_ADMIN = '[email protected]'
logdir = os.path.join(workdir, 'log')
_log_format = '%(asctime)s %(levelname)s %(name)s %(message)s'
logging.basicConfig(filename=os.path.join(logdir, 'osmpoint.log'),
level=logging.INFO, format=_log_format)
logging.getLogger('osmpoint').setLevel(logging.INFO)
data_log_handler = logging.FileHandler(os.path.join(logdir, 'data.log'))
data_log_handler.setFormatter(logging.Formatter(_log_format))
logging.getLogger('osmpoint.database').addHandler(data_log_handler)
"""
PRODUCTION_REDIS_CONFIG = """\
daemonize yes
port 0
unixsocket %(server_var)s/redis.sock
dir %(server_var)s/redis.db
logfile %(server_var)s/redis.log
loglevel notice
appendonly yes
appendfsync always
""" % {'server_var': server_var}
def _push_code():
local("git push -f '%s:%s' HEAD:incoming" % (server_name, server_repo))
def configure():
run("mkdir -p '%s'" % server_var)
with cd(server_var):
if run("test -f secret || echo 'missing'"):
run("OSMPOINT_WORKDIR=. ../virtualenv/bin/osmpoint "
"generate_secret_key > secret")
put(StringIO(PRODUCTION_CONFIG), "config.py")
put(StringIO(PRODUCTION_REDIS_CONFIG), "redis.conf")
if osm_login is not None:
put(StringIO("%s:%s\n" % osm_login), 'osm-login.txt')
def install_server():
run("mkdir -p '%s'" % server_prefix)
run("mkdir -p '%s/www'" % server_prefix)
with cd('%s/www' % server_prefix):
run("test -e static || ln -s '%s/osmpoint/static'" % server_repo)
if not exists(server_repo):
run("mkdir -p '%s'" % server_repo)
with cd(server_repo):
run("git init")
_push_code()
with cd(server_repo):
run("git checkout incoming -b deploy")
_push_code()
if not exists(server_virtualenv):
run("virtualenv -p python2.7 --distribute '%s'" %
server_virtualenv)
with cd(server_virtualenv):
run("bin/pip install -e '%s'" % server_repo)
run("bin/pip install flup")
site_packages = run("ls -d lib/python*/site-packages")
with cd(server_virtualenv + '/' + site_packages):
osmapi_filename = osmapi_url.rsplit('/', 1)[-1]
if run("test -f '%s' || echo 'missing'" % osmapi_filename):
run("curl -O '%s'" % osmapi_url)
configure()
def push():
_push_code()
with cd(server_repo):
run("git reset incoming --hard")
with cd(server_virtualenv):
run("bin/pip install -e '%s'" % server_repo)
def migrate_to_redis():
with cd(server_var):
run("OSMPOINT_WORKDIR=. "
"../virtualenv/bin/osmpoint migrate_to_redis "
"--redis-server")
def start_redis():
with cd(server_var):
run("redis-server redis.conf")
def start():
with cd(server_var):
run("OSMPOINT_WORKDIR=. "
"../virtualenv/bin/osmpoint runfcgi "
"--socket run/fcgi.socket "
"--pidfile run/fcgi.pid "
"--daemonize")
run("chmod 777 run/fcgi.socket")
def stop():
with cd(server_var):
run("kill `cat run/fcgi.pid` && rm run/fcgi.pid && rm run/fcgi.socket")
def restart():
try:
stop()
except:
pass
start()
def deploy():
push()
configure()
restart()
def put_points(dump_path):
with cd(server_var):
with open(dump_path, 'rb') as f:
put(f, "points.yaml")
def parse_points(dump_path="."):
local("curl http://download.geofabrik.de/osm/europe/romania.osm.pbf > dump.pbf")
local("parser dump.pbf > %s/points.yaml" % dump_path)
def map_party():
rst = os.path.join(local_repo, 'mapping-party', 'index.rst')
html = os.path.join(local_repo, 'mapping-party', 'index.html')
img = os.path.join(local_repo, 'mapping-party', 'screenshot.png')
local("/usr/local/bin/rst2plainhtml < '%s' > '%s'" % (rst, html))
with open(html, 'rb') as f:
data = f.read()
css = ('<style>'
'body {font-size: 14pt} '
'a {color: #05B} '
'img {display:block; float:right; margin: 2em; border: 2px solid #888} '
'</style>')
data = data.replace('</head>', '%s</head>' % css)
with open(html, 'wb') as f:
f.write(data)
folder = '%s/www/mapping-party' % server_prefix
run("mkdir -p '%s'" % folder)
with cd(folder):
put(html, '.')
put(img, '.')
os.unlink(html)