copied from internal repo
This commit is contained in:
124
file_generator/generate_bind9.py
Normal file
124
file_generator/generate_bind9.py
Normal file
@@ -0,0 +1,124 @@
|
||||
import os
|
||||
import shutil
|
||||
from datetime import datetime
|
||||
|
||||
from conpeek_setup import util
|
||||
from jinja2 import Template
|
||||
|
||||
|
||||
def get_serial():
|
||||
current_date = datetime.now()
|
||||
bind_serial_number = int(current_date.timestamp())
|
||||
return bind_serial_number
|
||||
|
||||
|
||||
def prepare_access_zone_config(external_ip, app_installation_domain, output_directory, template_file_dir, script_dir, internal_ip, developer_mode, kamailio_nat):
|
||||
util.print_black_light(f"Preparing access zone (External IP:{external_ip}) (Core IP: {internal_ip})")
|
||||
template_file_path = os.path.join(script_dir, template_file_dir)
|
||||
|
||||
file = open(template_file_path, 'r')
|
||||
template_content = file.read()
|
||||
template = Template(template_content)
|
||||
|
||||
data = {
|
||||
"app_installation_domain": app_installation_domain,
|
||||
"access_ip": external_ip,
|
||||
"serial_number": get_serial(),
|
||||
"core_ip": internal_ip,
|
||||
"developer_mode": developer_mode,
|
||||
"kamailio_nat": kamailio_nat == '1'
|
||||
}
|
||||
|
||||
output_text = template.render(data)
|
||||
output_file = output_directory + "/db." + app_installation_domain
|
||||
output_file_path = os.path.join(script_dir, output_file)
|
||||
|
||||
with open(output_file_path, "w") as file:
|
||||
file.write(output_text + "\n")
|
||||
|
||||
|
||||
def prepare_system_zone_config(core_ip, output_directory, template_file_dir, script_dir, system_app_installation_domain):
|
||||
util.print_black_light(f"Preparing system zone (Core IP:{core_ip})")
|
||||
template_file_path = os.path.join(script_dir, template_file_dir)
|
||||
|
||||
file = open(template_file_path, 'r')
|
||||
template_content = file.read()
|
||||
template = Template(template_content)
|
||||
|
||||
data = {
|
||||
"system_app_installation_domain": system_app_installation_domain,
|
||||
"core_ip": core_ip,
|
||||
"serial_number": get_serial()
|
||||
}
|
||||
|
||||
output_text = template.render(data)
|
||||
output_file = output_directory + "/db." + system_app_installation_domain
|
||||
output_file_path = os.path.join(script_dir, output_file)
|
||||
|
||||
with open(output_file_path, "w") as file:
|
||||
file.write(output_text + "\n")
|
||||
|
||||
|
||||
def create_config(data, template_path, output_path, script_dir):
|
||||
template_file_path = os.path.join(script_dir, template_path)
|
||||
|
||||
file = open(template_file_path, 'r')
|
||||
template_content = file.read()
|
||||
template = Template(template_content)
|
||||
|
||||
output_text = template.render(data)
|
||||
|
||||
output_file_path = os.path.join(script_dir, output_path)
|
||||
|
||||
with open(output_file_path, "w") as file:
|
||||
file.write(output_text + "\n")
|
||||
|
||||
|
||||
def run(config, developer_mode=False):
|
||||
util.print_black_light("Preparing bind configs")
|
||||
|
||||
internal_ip = config["new_machine_network"]["internal_ip"]
|
||||
external_ip = config["new_machine_network"]["external_ip"]
|
||||
app_installation_domain = config["new_machine_network"]["app_installation_domain"]
|
||||
system_app_installation_domain = config["new_machine_network"]["system_app_installation_domain"]
|
||||
|
||||
var_bind_directory = os.path.join(util.get_output_path(), "var", "cache", "bind")
|
||||
shutil.rmtree(var_bind_directory, ignore_errors=True)
|
||||
os.makedirs(var_bind_directory, exist_ok=True)
|
||||
|
||||
etc_bind_directory = os.path.join(util.get_output_path(), "etc", "bind")
|
||||
shutil.rmtree(etc_bind_directory, ignore_errors=True)
|
||||
os.makedirs(etc_bind_directory, exist_ok=True)
|
||||
|
||||
template_directory = os.path.join(util.get_templates_path(), "bind9")
|
||||
|
||||
kamailio_nat = '1'
|
||||
if config.has_option('kamailio', 'nat'):
|
||||
kamailio_nat = config["kamailio"]["nat"]
|
||||
|
||||
# /var/cache configs
|
||||
prepare_access_zone_config(external_ip, app_installation_domain, var_bind_directory, "access_zone.tmpl", template_directory, internal_ip, developer_mode, kamailio_nat)
|
||||
prepare_system_zone_config(internal_ip, var_bind_directory, "system_zone.tmpl", template_directory, system_app_installation_domain)
|
||||
|
||||
forwarders = config["bind9"]["forwarders"]
|
||||
if not forwarders:
|
||||
forwarders = "1.1.1.1;"
|
||||
|
||||
if ";" not in forwarders:
|
||||
util.print_yellow("; not in bind9 forwarders... dynamically add to end")
|
||||
forwarders += ";"
|
||||
|
||||
# /etc/configs
|
||||
data = {"forward_dns": forwarders}
|
||||
create_config(data, "named.conf.options.tmpl", os.path.join(etc_bind_directory, "named.conf.options"), template_directory)
|
||||
data = {"NETWORK_DOMAIN": app_installation_domain}
|
||||
create_config(data, "named.conf.access_network.tmpl", os.path.join(etc_bind_directory, "named.conf.access_network"), template_directory)
|
||||
data = {"NETWORK_DOMAIN": system_app_installation_domain}
|
||||
create_config(data, "named.conf.system_network.tmpl", os.path.join(etc_bind_directory, "named.conf.system_network"), template_directory)
|
||||
|
||||
files_to_copy = [
|
||||
"named.conf"
|
||||
]
|
||||
|
||||
for file in files_to_copy:
|
||||
util.copy_file(os.path.join(template_directory, file), etc_bind_directory)
|
||||
Reference in New Issue
Block a user