forked from openplans/shareabouts-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postinstall
192 lines (165 loc) · 5.49 KB
/
postinstall
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
#!/bin/sh
shout() { echo "$0: $*" >&2; } # Just echo an error and the program name
barf() { shout "$*"; exit 111; }
safe() { "$@" || barf "cannot $*"; }
# =============================================================================
# Post-install hook file for configuring dotcloud server
mkdir ../data
echo "Writing the local settings file..."
cat >> src/project/local_settings.py <<EOF
import json
with open('/home/dotcloud/environment.json') as f:
env = json.load(f)
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'shareabouts',
'USER': env['DOTCLOUD_DB_SQL_LOGIN'],
'PASSWORD': env['DOTCLOUD_DB_SQL_PASSWORD'],
'HOST': env['DOTCLOUD_DB_SQL_HOST'],
'PORT': int(env['DOTCLOUD_DB_SQL_PORT']),
}
}
CACHES = {
"default": {
"BACKEND": "redis_cache.cache.RedisCache",
"LOCATION": "%s:%s:1" % (env['DOTCLOUD_CACHE_REDIS_HOST'],
env['DOTCLOUD_CACHE_REDIS_PORT']),
"OPTIONS": {
"CLIENT_CLASS": "redis_cache.client.DefaultClient",
"PASSWORD": env['DOTCLOUD_CACHE_REDIS_PASSWORD'],
}
}
}
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
STATIC_ROOT = '/home/dotcloud/current/static/'
# Debug is False by default, true if set in the environment.
DEBUG = (env.get('DEBUG', 'False') in ['true', 'True'])
TEMPLATE_DEBUG = DEBUG
SHOW_DEBUG_TOOLBAR = DEBUG
SOCIAL_AUTH_RAISE_EXCEPTIONS = DEBUG
ATTACHMENT_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
AWS_ACCESS_KEY_ID = env['SHAREABOUTS_AWS_KEY']
AWS_SECRET_ACCESS_KEY = env['SHAREABOUTS_AWS_SECRET']
AWS_STORAGE_BUCKET_NAME = env['SHAREABOUTS_AWS_BUCKET']
AWS_QUERYSTRING_AUTH = False
# Set these explicitly here to override whatever may have come from settings
DEFAULT_FILE_STORAGE = 'django.core.files.storage.FileSystemStorage'
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'
STATIC_URL = '/static/'
SOCIAL_AUTH_TWITTER_KEY = env['SHAREABOUTS_TWITTER_KEY']
SOCIAL_AUTH_TWITTER_SECRET = env['SHAREABOUTS_TWITTER_SECRET']
SOCIAL_AUTH_FACEBOOK_KEY = env['SHAREABOUTS_FACEBOOK_KEY']
SOCIAL_AUTH_FACEBOOK_SECRET = env['SHAREABOUTS_FACEBOOK_SECRET']
LAUNCHROCK_KEY = env.get('SHAREABOUTS_LAUNCHROCK_KEY')
ADMINS = (
('Shareabouts API Admin', env.get('SHAREABOUTS_ADMIN_EMAIL')),
)
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse'
}
},
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(name)s: %(message)s %(process)d %(thread)d'
},
'moderate': {
'format': '%(levelname)s %(asctime)s %(name)s: %(message)s'
},
'simple': {
'format': '%(levelname)s %(message)s'
},
},
'handlers': {
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
},
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'moderate'
},
'debug_file': {
'level': 'DEBUG',
'class': 'logging.handlers.TimedRotatingFileHandler',
'formatter': 'verbose',
'filename': '/home/dotcloud/data/debug.log',
'backupCount': 7,
'when': 'midnight',
},
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
'django.db.backends': {
'handlers': ['debug_file'],
'level': 'DEBUG',
'propagate': True,
},
'utils.request_timer': {
'handlers': ['debug_file'],
'level': 'DEBUG',
'propagate': True,
},
'utils.cookies_logger': {
'handlers': ['debug_file'],
'level': 'DEBUG',
'propagate': True,
},
'storages': {
'handlers': ['debug_file'],
'level': 'DEBUG',
'propagate': True,
},
'redis_cache': {
'handlers': ['debug_file'],
'level': 'DEBUG',
'propagate': True,
},
}
}
EOF
# Need to be able to import settings to run the createdbs script...
echo VIRTUAL_ENV is "$VIRTUAL_ENV"
echo PYTHONPATH is "$PYTHONPATH"
export PYTHONPATH=$PYTHONPATH:$PWD/src/
export DJANGO_SETTINGS_MODULE=project.settings
echo "Creating database if needed..."
safe python src/scripts/dotcloud_createdbs.py
echo "Syncing the database..."
safe src/manage.py syncdb --migrate --noinput
echo "Setting up static file service..."
safe src/manage.py collectstatic --noinput
echo "Configuring nginx to serve static files..."
cat >> nginx.conf <<EOF
location /static/ {
root /home/dotcloud/current ;
expires max;
}
gzip on;
gzip_types
text/plain text/html text/css text/csv
application/json application/javascript;
error_page 411 = @cors;
location @cors {
if (\$request_method = OPTIONS) {
add_header Access-Control-Allow-Origin \$http_origin;
add_header Access-Control-Allow-Methods 'GET, HEAD, OPTIONS, TRACE, PUT, POST, DELETE, PATCH, CONNECT';
add_header Access-Control-Allow-Headers 'content-type, *';
add_header Access-Control-Allow-Credentials 'true';
add_header Content-Length 0;
add_header Content-Type text/plain;
return 200;
}
return 411;
}
EOF