-
Notifications
You must be signed in to change notification settings - Fork 14
/
create_config.py
153 lines (141 loc) · 5.11 KB
/
create_config.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
import argparse
import os
import random
import string
import uuid
from jinja2 import Environment, FileSystemLoader
def random_password(size):
"""Generate a random password"""
random_source = string.ascii_letters + string.digits
password = random.choice(string.ascii_lowercase)
password += random.choice(string.ascii_uppercase)
password += random.choice(string.digits)
for i in range(size):
password += random.choice(random_source)
password_list = list(password)
random.SystemRandom().shuffle(password_list)
password = "".join(password_list)
return password
def main():
parser = argparse.ArgumentParser()
parser.add_argument("ini_path", help="Path to ini file to create")
parser.add_argument("--mysql_host", required=True, help="MySQL host server to use")
parser.add_argument(
"--forwarded_allow_ip",
required=True,
help="IP of the proxy server calling FormShare",
)
parser.add_argument(
"--pid_file",
required=True,
help="File that will store the FormShare process ID",
)
parser.add_argument(
"--error_log_file",
required=True,
help="File that will store the FormShare logs",
)
parser.add_argument(
"-d",
"--daemon",
action="store_true",
help="Start as FormShare in detached mode",
)
parser.add_argument(
"-c",
"--capture_output",
action="store_true",
help="Start as FormShare in detached mode",
)
parser.add_argument(
"-o", "--overwrite", action="store_true", help="Overwrite if exists"
)
parser.add_argument(
"--repository_path", required=True, help="Path to the FormShare repository"
)
parser.add_argument("--odktools_path", required=True, help="Path to ODK Tools")
parser.add_argument(
"--mysql_port", default=3306, help="MySQL port to use. Default to 3306"
)
parser.add_argument(
"--mysql_schema",
default="formshare",
help="MySQL schema to use. Default to 'formshare'",
)
parser.add_argument(
"--mysql_user_name",
required=True,
help="MySQL user name to use to create the schema",
)
parser.add_argument(
"--mysql_user_password", required=True, help="MySQL user name password"
)
parser.add_argument(
"--elastic_search_host", required=True, help="ElasticSearch host name"
)
parser.add_argument(
"--elastic_search_port", required=True, help="ElasticSearch host name"
)
parser.add_argument(
"--formshare_host", required=True, help="Host name for FormShare"
)
parser.add_argument("--formshare_port", required=True, help="Port for FormShare")
parser.add_argument(
"--elastic_search_ssl", action="store_true", help="ElasticSearch use SSL"
)
args = parser.parse_args()
formshare_path = "."
main_secret = random_password(14).replace("%", "~")
redis_sessions_secret = random_password(14).replace("%", "~")
assistant_secret = random_password(14).replace("%", "!")
auth_secret = random_password(14).replace("%", "#")
auth_secret2 = random_password(14).replace("%", "#")
aes_key = random_password(29).replace("%", "#")
auth_opaque = uuid.uuid4().hex
template_environment = Environment(
autoescape=False,
loader=FileSystemLoader(os.path.join(formshare_path, "templates")),
trim_blocks=False,
)
context = {
"mysql_host": args.mysql_host,
"mysql_port": args.mysql_port,
"mysql_schema": args.mysql_schema,
"mysql_user_name": args.mysql_user_name,
"mysql_user_password": args.mysql_user_password,
"main_secret": main_secret,
"assistant_secret": assistant_secret,
"auth_secret": auth_secret,
"auth_secret2": auth_secret2,
"aes_key": aes_key,
"auth_opaque": auth_opaque,
"repository_path": args.repository_path,
"odktools_path": args.odktools_path,
"elastic_search_host": args.elastic_search_host,
"elastic_search_port": args.elastic_search_port,
"elastic_search_ssl": args.elastic_search_ssl,
"formshare_host": args.formshare_host,
"formshare_port": args.formshare_port,
"capture_output": args.capture_output,
"daemon": args.daemon,
"pid_file": args.pid_file,
"error_log_file": args.error_log_file,
"forwarded_allow_ip": args.forwarded_allow_ip,
"redis_sessions_secret": redis_sessions_secret,
}
rendered_template = template_environment.get_template("formshare.jinja2").render(
context
)
if not os.path.exists(args.ini_path):
with open(args.ini_path, "w") as f:
f.write(rendered_template)
print("FormShare INI file created at {}".format(args.ini_path))
else:
if args.overwrite:
with open(args.ini_path, "w") as f:
f.write(rendered_template)
print("FormShare INI file created at {}".format(args.ini_path))
else:
print("INI file {} already exists".format(args.ini_path))
if __name__ == "__main__":
main()