-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvs_generator.py
180 lines (158 loc) · 6.46 KB
/
envs_generator.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
import argparse
import secrets
import string
import os
# Generate a random password of length len
# if must_be_alphanumeric is TRUE then password will be alphanumeric,
# otherwise it will be alphabetical
def generate_password(len, must_be_alphanumeric):
choices = string.ascii_letters
if must_be_alphanumeric:
choices += string.digits
return ''.join(secrets.choice(choices) for i in range(len))
# generate .django file in .envs/.production directory
def generate_django_env_file(hostname):
django_file = "# General\n"
django_file += "# ---------------------------------------------------------------------------\n"
django_file += "# DJANGO_READ_DOT_ENV_FILE=True\n"
django_file += "DJANGO_SETTINGS_MODULE=config.settings.production\n"
django_file += "DJANGO_SECRET_KEY="
django_file += generate_password(64, True) + "\n"
django_file += "DJANGO_ADMIN_URL="
django_file += generate_password(32, True) + "/\n"
django_file += "DJANGO_ALLOWED_HOSTS="
django_file += hostname + "\n\n"
django_file += "# Security\n"
django_file += "# ---------------------------------------------------------------------------\n"
django_file += "# TIP: better off using DNS, however, redirect is OK too\n"
django_file += "DJANGO_SECURE_SSL_REDIRECT=False\n\n"
django_file += "# Email\n"
django_file += "# ---------------------------------------------------------------------------\n"
django_file += "MAILGUN_API_KEY=\n"
django_file += "DJANGO_SERVER_EMAIL=\n"
django_file += "MAILGUN_DOMAIN=\n\n"
django_file += "# AWS\n"
django_file += "# ---------------------------------------------------------------------------\n"
django_file += "DJANGO_AWS_ACCESS_KEY_ID=\n"
django_file += "DJANGO_AWS_SECRET_ACCESS_KEY=\n"
django_file += "DJANGO_AWS_STORAGE_BUCKET_NAME=\n\n"
django_file += "# django-allauth\n"
django_file += "# ---------------------------------------------------------------------------\n"
django_file += "DJANGO_ACCOUNT_ALLOW_REGISTRATION=True\n\n"
django_file += "# Gunicorn\n"
django_file += "# ---------------------------------------------------------------------------\n"
django_file += "WEB_CONCURRENCY=4\n\n"
django_file += "# Redis\n"
django_file += "# ---------------------------------------------------------------------------\n"
django_file += "REDIS_URL=redis://redis:6379/0\n"
os.makedirs(".envs/.production", exist_ok=True)
f = open(".envs/.production/.django", "w")
f.write(django_file)
f.close()
# generate .postgres file in .envs/.production directory
def genereate_postgres_env_file():
postgres_file = "# PostgreSQL\n"
postgres_file += "# -------------------------------------------------------------------------\n"
postgres_file += "POSTGRES_HOST=postgres\n"
postgres_file += "POSTGRES_PORT=5432\n"
postgres_file += "POSTGRES_DB=static_page_proxy\n"
postgres_file += "POSTGRES_USER="
postgres_file += generate_password(32, False) + "\n"
postgres_file += "POSTGRES_PASSWORD="
postgres_file += generate_password(64, True) + "\n"
f = open(".envs/.production/.postgres", "w")
f.write(postgres_file)
f.close()
return
# return line with only leading whitespaces
def whitespace_template(line):
num_leading_spaces = len(line) - len(line.lstrip(" "))
line = ""
for i in range(0, num_leading_spaces):
line += " "
return line
# generate new traefik.toml with updated hostname and voyant_hostname
def get_new_traefik_toml(lines, hostname, voyant_hostname):
new_traefik_toml = ""
rules_left = 3
for line in lines:
copy = line.strip()
if copy.startswith("main"):
line = whitespace_template(line)
line += "main = \""
line += hostname
line += "\"\n"
elif copy.startswith("sans"):
line = whitespace_template(line)
line += "sans = [\""
line += voyant_hostname
line += "\"]\n"
elif copy.startswith("rule"):
line = whitespace_template(line)
line += "rule = \"Host:"
if rules_left == 3:
line += voyant_hostname
line += "\"\n"
elif rules_left == 2:
line += voyant_hostname
line += ";PathPrefix:/corpora\"\n"
else:
line += hostname
line += "\"\n"
rules_left -= 1
new_traefik_toml += line
return new_traefik_toml
# update hosts in traefik.toml
def update_traefik_toml(hostname, voyant_hostname, traefik_toml_path):
f = open(traefik_toml_path)
lines = f.readlines()
f.close()
new_traefik_toml = get_new_traefik_toml(lines, hostname, voyant_hostname)
f = open(traefik_toml_path, "w")
f.write(new_traefik_toml)
f.close()
return
# generate new voyant_gen.py with upated voyant_hostname
def get_new_voyant_gen_code(lines, voyant_hostname):
new_python_code = ""
for line in lines:
copy = line.strip()
if copy.startswith("url_template"):
line = whitespace_template(line)
line += "url_template = \'https://"
line += voyant_hostname
line += "/?input=https://"
line += voyant_hostname
line += "/corpora/{}\'\n"
new_python_code += line
return new_python_code
# update voyant-host in voyant_gen.py
def update_voyant_gen(voyant_hostname, voyant_gen_py_path):
f = open(voyant_gen_py_path)
lines = f.readlines()
f.close()
new_python_code = get_new_voyant_gen_code(lines, voyant_hostname)
f = open(voyant_gen_py_path, "w")
f.write(new_python_code)
f.close()
return
# get hostname from command-line
# get voyant_hostname (inferred from hostname)
def get_hostnames():
parser = argparse.ArgumentParser(
description="Generate environment files for django and postgre")
parser.add_argument("hostname")
args = parser.parse_args()
hostname = vars(args)["hostname"]
voyant_hostname = hostname.split(".")[0] + "-voyant.pennds.org"
return hostname, voyant_hostname
def main():
voyant_gen_py_path = "voyant_gen/voyant_gen.py"
traefik_toml_path = "compose/traefik/traefik.toml"
hostname, voyant_hostname = get_hostnames()
update_voyant_gen(voyant_hostname, voyant_gen_py_path)
update_traefik_toml(hostname, voyant_hostname, traefik_toml_path)
generate_django_env_file(hostname)
genereate_postgres_env_file()
if __name__ == "__main__":
main()