2022-03-10 14:26:22 +00:00
|
|
|
import json
|
2022-08-30 10:24:58 +01:00
|
|
|
import os
|
2024-12-06 18:02:59 +00:00
|
|
|
from collections.abc import Mapping, Sequence
|
|
|
|
from typing import Any, List
|
2022-03-10 14:26:22 +00:00
|
|
|
|
|
|
|
from app import app
|
2022-05-16 17:09:33 +01:00
|
|
|
from app.lists import lists
|
2022-09-26 14:51:11 +01:00
|
|
|
from app.models.base import MirrorList, Pool
|
2022-05-08 17:20:04 +01:00
|
|
|
from app.terraform.terraform import TerraformAutomation
|
2022-03-10 14:26:22 +00:00
|
|
|
|
|
|
|
|
2022-05-16 17:09:33 +01:00
|
|
|
def obfuscator(obj: Any) -> Any:
|
|
|
|
if isinstance(obj, str):
|
|
|
|
return "".join([f"!AAA!{hex(ord(c))[2:].zfill(4)}" for c in obj])
|
|
|
|
if isinstance(obj, Mapping):
|
|
|
|
return {obfuscator(k): obfuscator(v) for k, v in obj.items()}
|
|
|
|
if isinstance(obj, Sequence):
|
|
|
|
return [obfuscator(i) for i in obj]
|
|
|
|
return obj
|
|
|
|
|
|
|
|
|
|
|
|
def json_encode(obj: Any, obfuscate: bool) -> str:
|
|
|
|
if obfuscate:
|
|
|
|
obj = obfuscator(obj)
|
2022-06-23 13:42:45 +01:00
|
|
|
result = json.dumps(obj, sort_keys=True).replace("!AAA!", "\\u")
|
|
|
|
return result
|
2022-05-16 17:09:33 +01:00
|
|
|
return json.dumps(obj, indent=2, sort_keys=True)
|
|
|
|
|
|
|
|
|
|
|
|
def javascript_encode(obj: Any, obfuscate: bool) -> str:
|
2022-05-19 12:27:47 +01:00
|
|
|
return "var mirrors = " + json_encode(obj, obfuscate) + ";"
|
2022-05-16 17:09:33 +01:00
|
|
|
|
|
|
|
|
2022-05-08 17:20:04 +01:00
|
|
|
class ListAutomation(TerraformAutomation):
|
2022-05-16 11:44:03 +01:00
|
|
|
template: str
|
|
|
|
"""
|
|
|
|
Terraform configuration template using Jinja 2.
|
|
|
|
"""
|
|
|
|
|
|
|
|
template_parameters: List[str]
|
|
|
|
"""
|
|
|
|
List of parameters to be read from the application configuration for use
|
|
|
|
in the templating of the Terraform configuration.
|
|
|
|
"""
|
|
|
|
|
2023-02-26 12:52:08 +00:00
|
|
|
provider: str # type: ignore[assignment]
|
|
|
|
# TODO: remove temporary override
|
|
|
|
|
2022-11-28 18:55:10 +00:00
|
|
|
def tf_generate(self) -> None:
|
|
|
|
if not self.working_dir:
|
|
|
|
raise RuntimeError("No working directory specified.")
|
2022-05-08 17:20:04 +01:00
|
|
|
self.tf_write(
|
2022-03-10 14:26:22 +00:00
|
|
|
self.template,
|
|
|
|
lists=MirrorList.query.filter(
|
2022-05-16 13:29:48 +01:00
|
|
|
MirrorList.destroyed.is_(None),
|
2022-03-10 14:26:22 +00:00
|
|
|
MirrorList.provider == self.provider,
|
|
|
|
).all(),
|
|
|
|
global_namespace=app.config['GLOBAL_NAMESPACE'],
|
2022-08-30 10:05:12 +01:00
|
|
|
terraform_modules_path=os.path.join(*list(os.path.split(app.root_path))[:-1], 'terraform-modules'),
|
|
|
|
backend_config=f"""backend "http" {{
|
|
|
|
lock_address = "{app.config['TFSTATE_BACKEND']}/{self.short_name}"
|
|
|
|
unlock_address = "{app.config['TFSTATE_BACKEND']}/{self.short_name}"
|
|
|
|
address = "{app.config['TFSTATE_BACKEND']}/{self.short_name}"
|
|
|
|
}}""",
|
2022-03-10 14:26:22 +00:00
|
|
|
**{
|
|
|
|
k: app.config[k.upper()]
|
|
|
|
for k in self.template_parameters
|
|
|
|
}
|
|
|
|
)
|
2022-09-26 14:51:11 +01:00
|
|
|
for pool in Pool.query.filter(Pool.destroyed.is_(None)).all():
|
|
|
|
for key, formatter in lists.items():
|
2024-11-09 11:08:48 +00:00
|
|
|
formatted_pool = formatter(pool)
|
2022-09-26 14:51:11 +01:00
|
|
|
for obfuscate in [True, False]:
|
2023-01-21 15:15:07 +00:00
|
|
|
with open(os.path.join(
|
|
|
|
self.working_dir, f"{key}.{pool.pool_name}{'.jsno' if obfuscate else '.json'}"),
|
2023-01-21 15:18:13 +00:00
|
|
|
'w', encoding="utf-8") as out:
|
2024-11-09 11:08:48 +00:00
|
|
|
out.write(json_encode(formatted_pool, obfuscate))
|
2022-11-28 18:55:10 +00:00
|
|
|
with open(os.path.join(self.working_dir, f"{key}.{pool.pool_name}{'.jso' if obfuscate else '.js'}"),
|
2022-09-26 14:51:11 +01:00
|
|
|
'w', encoding="utf-8") as out:
|
2024-11-09 11:08:48 +00:00
|
|
|
out.write(javascript_encode(formatted_pool, obfuscate))
|