2013-10-25 19:35:24 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# This tools generates local_settings_generated.py using the template
|
|
|
|
|
|
|
|
import sys, os, os.path
|
|
|
|
|
2013-10-25 23:34:44 +02:00
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))
|
2013-10-25 19:35:24 +02:00
|
|
|
os.environ['DJANGO_SETTINGS_MODULE'] = 'zproject.settings'
|
|
|
|
|
|
|
|
from django.utils.crypto import get_random_string
|
|
|
|
from zerver.lib.utils import generate_random_token
|
|
|
|
|
2013-11-14 14:59:33 +01:00
|
|
|
os.chdir(os.path.join(os.path.dirname(__file__), '..', '..'))
|
|
|
|
|
2013-10-25 19:35:24 +02:00
|
|
|
SETTINGS_FILENAME = "zproject/local_settings_template.py"
|
|
|
|
OUTPUT_SETTINGS_FILENAME = "zproject/local_settings_generated.py"
|
2013-11-10 16:04:24 +01:00
|
|
|
CAMO_CONFIG_FILENAME = '/etc/default/camo'
|
2013-10-25 19:35:24 +02:00
|
|
|
|
|
|
|
if not os.path.exists(SETTINGS_FILENAME):
|
|
|
|
print "Unable to find settings file at %s" % (SETTINGS_FILENAME,)
|
|
|
|
|
2015-08-19 00:27:39 +02:00
|
|
|
AUTOGENERATED_SETTINGS = ['SHARED_SECRET', 'AVATAR_SALT', 'RABBITMQ_PASSWORD']
|
2013-10-25 19:35:24 +02:00
|
|
|
|
|
|
|
def generate_camo_config_file(camo_key):
|
|
|
|
camo_config = """ENABLED=yes
|
|
|
|
PORT=9292
|
|
|
|
CAMO_KEY=%s
|
|
|
|
""" % (camo_key,)
|
|
|
|
with open(CAMO_CONFIG_FILENAME, 'w') as camo_file:
|
|
|
|
camo_file.write(camo_config)
|
|
|
|
print "Generated Camo config file %s" % (CAMO_CONFIG_FILENAME,)
|
|
|
|
|
|
|
|
def generate_django_secretkey():
|
|
|
|
# Secret key generation taken from Django's startproject.py
|
|
|
|
chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
|
|
|
|
return get_random_string(50, chars)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
f = open(SETTINGS_FILENAME, 'r')
|
|
|
|
lines = f.readlines()
|
|
|
|
|
|
|
|
for idx, line in enumerate(lines):
|
|
|
|
parts = [part.strip() for part in line.split('=')]
|
|
|
|
if len(parts) != 2:
|
|
|
|
continue
|
|
|
|
|
|
|
|
def config_line(var, value):
|
|
|
|
return "%s = '%s'\n" % (var, value)
|
|
|
|
|
|
|
|
if parts[0] in AUTOGENERATED_SETTINGS:
|
|
|
|
lines[idx] = config_line(parts[0], generate_random_token(64))
|
|
|
|
elif parts[0] == 'SECRET_KEY':
|
|
|
|
lines[idx] = config_line("SECRET_KEY", generate_django_secretkey())
|
|
|
|
elif parts[0] == 'CAMO_KEY':
|
|
|
|
camo_key = get_random_string(64)
|
|
|
|
lines[idx] = config_line(parts[0], camo_key)
|
|
|
|
# Write the Camo config file directly
|
|
|
|
generate_camo_config_file(camo_key)
|
|
|
|
|
|
|
|
out = open(OUTPUT_SETTINGS_FILENAME, 'w')
|
|
|
|
out.write("".join(lines))
|
|
|
|
out.close()
|
|
|
|
|
|
|
|
print "Generated %s with auto-generated secrets!" % (OUTPUT_SETTINGS_FILENAME,)
|