forked from staticfloat/docker-nginx-certbot-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenConfFiles.py
187 lines (174 loc) · 6.46 KB
/
GenConfFiles.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
#------------------------------------------------------------------------------#
# Author : Nicklas Sindlev Andersen #
#------------------------------------------------------------------------------#
# File description: ...
#------------------------------------------------------------------------------#
# Import packages from the python standard library #
#------------------------------------------------------------------------------#
import os
import argparse
import json
import shutil
#------------------------------------------------------------------------------#
# Import thrid party packages #
#------------------------------------------------------------------------------#
from environs import Env
import jinja2
# Parse commadnline arguments
#----------------------------------------------------------------------#
def parse_commandline_args(args_list = None):
""" Setup, parse and validate given commandline arguments.
"""
# Create main parser
parser = argparse.ArgumentParser(description = "")
add_parser_arguments(parser)
# Parse commandline arguments
args = parser.parse_args(args_list)
return args
def add_required_parser_arguments(parser):
"""
"""
parser.add_argument("-env_file", "--env_file",
required = True,
default = ".env",
type = str,
help = "Specify the name of the environment variable file."
)
def add_parser_arguments(parser):
"""
"""
# Add required commandline arguments:
add_required_parser_arguments(parser)
# Parse file containing enviroment variables
#----------------------------------------------------------------------#
def parse_env_vars():
"""
"""
env = Env()
try:
env.read_env(args.env_file, recurse = False)
except IOError as e:
print("INFO : File ", args.env_file, " not accessible!")
print("Error: ", e)
return os.environ.copy()
# Load folder containing jinja templates and generate templates according to
# enviroment variables defined in the provided "settings.env" file
#------------------------------------------------------------------------------#
def load_jinja_templates(env_vars):
"""
"""
jinja_templates = None
try:
jinja_templates = jinja2.Environment(
autoescape = False,
loader = jinja2.FileSystemLoader(
searchpath = os.path.join(env_vars["TEMPLATE_DIR"]),
),
)
except KeyError as e:
print("INFO : Enviroment variable \"TEMPLATE_DIR\" not defined!")
print("Error: ", e)
return jinja_templates
def render_template(jinja_templates, template_vars, template_name, outfile):
"""
"""
# Generate configuration and other files according to the given templates
template = jinja_templates.select_template([template_name])
with open(outfile, "w") as file:
print(template.render(**template_vars), file = file)
def render_templates(jinja_templates, other_vars, env_vars, prefix = ""):
"""
"""
env_vars.update(other_vars)
try:
env_vars["NGINX_SERVER_NAMES"] = json.loads(env_vars["NGINX_SERVER_NAMES"])
except KeyError as e:
print("INFO : Enviroment variable \"NGINX_SERVER_NAMES\" not defined properly!")
print("Error: ", e)
try:
env_vars["NGINX_TRUSTED_ORIGINS"] = json.loads(env_vars["NGINX_TRUSTED_ORIGINS"])
except KeyError as e:
print("INFO : Enviroment variable \"NGINX_TRUSTED_ORIGINS\" not defined properly!")
print("Error: ", e)
# Render nginx-certbot dockerfile
render_template(
jinja_templates,
env_vars,
# The template file to use. Should not be changed:
"nginx_dockerfile.jinja",
# Specify the correct directory to place configuration file in
"nginx/Dockerfile",
)
# Render nginx configuration file
render_template(
jinja_templates,
env_vars,
# The template file to use. Should not be changed:
"nginx.conf.jinja",
# Specify the correct directory to place configuration file in:
"nginx/conf.d/nginx.conf"
)
# Render the main docker compose file for deploying the backend
render_template(
jinja_templates,
env_vars,
# The template file to use. Should not be changed:
"docker-compose.yml.jinja",
# Specify the correct directory to place configuration file in
"docker-compose.yml",
)
# Generate directories based on the defined enviroment variables
#------------------------------------------------------------------------------#
def generate_directories(args, other_vars, env_vars):
"""
"""
dir_list = [
"nginx/conf.d",
"nginx/dhparams",
"nginx/https",
"nginx/nginx_conf.d",
"nginx/scripts",
]
# Create required output directories if they're missing
for dir in dir_list:
os.makedirs(dir, exist_ok = True)
if not os.path.exists(dir):
pass
else:
print("NOTE: Directories already exists! Files are overwritten...")
# Copy in default directories
#------------------------------------------------------------------------------#
def copy_directories(args, other_vars, env_vars):
"""
"""
dir_list = {
"base/nginx_conf.d": "nginx/nginx_conf.d",
"base/scripts": "nginx/scripts",
}
for dir in dir_list:
shutil.copytree(dir, dir_list[dir], dirs_exist_ok = True)
if not os.path.exists(dir):
pass
else:
print("NOTE: Directories already exists! Files are overwritten...")
# Main function call...
#------------------------------------------------------------------------------#
def main(args, other_vars):
"""
"""
# Parse file containing environment variables to be defined and used
env_vars = parse_env_vars()
generate_directories(args, other_vars, env_vars)
copy_directories(args, other_vars, env_vars)
jinja_templates = load_jinja_templates(env_vars)
# render_templates(jinja_templates, other_vars, env_vars, prefix = "testing")
render_templates(jinja_templates, other_vars, env_vars, prefix = "")
#----------------------------------------------------------------------#
if __name__ == "__main__":
"""
Script entry point...
"""
args = parse_commandline_args()
# Other filesnames, configs, etc.
other_vars = {}
main(args, other_vars)