khuedoan-homelab/scripts/configure

100 lines
2.4 KiB
Python
Executable File

#!/usr/bin/python
# WIP
# TODO clean this up
"""
Basic configure script for new users
"""
import fileinput
import subprocess
import sys
from rich.prompt import Confirm, Prompt
default_seed_repo = "https://github.com/khuedoan/homelab"
default_domain = "khuedoan.com"
default_timezone = "Asia/Ho_Chi_Minh"
default_terraform_workspace = "khuedoan"
def check_python_version(required_version: str) -> None:
if sys.version_info < tuple(map(int, required_version.split('.'))):
raise Exception(f"Must be using Python >= {required_version}")
def find_and_replace(pattern: str, replacement: str, paths: list[str]) -> None:
files_with_matches = subprocess.run(
["git", "grep", "--files-with-matches", pattern, "--"] + paths,
capture_output=True,
text=True
).stdout.splitlines()
for file_with_maches in files_with_matches:
with fileinput.FileInput(file_with_maches, inplace=True) as file:
for line in file:
print(line.replace(pattern, replacement), end='')
def main() -> None:
check_python_version(
required_version='3.10.0'
)
editor = Prompt.ask("Select text editor", default='nvim')
domain = Prompt.ask("Enter your domain", default=default_domain)
seed_repo = Prompt.ask("Enter seed repo", default=default_seed_repo)
timezone = Prompt.ask("Enter time zone", default=default_timezone)
find_and_replace(
pattern=default_domain,
replacement=domain,
paths=[
".tekton",
"apps",
"bootstrap",
"platform",
"system",
"external"
]
)
find_and_replace(
pattern=default_seed_repo,
replacement=seed_repo,
paths=[
"bootstrap",
"platform"
]
)
find_and_replace(
pattern=default_timezone,
replacement=timezone,
paths=[
"apps",
"system",
"metal"
]
)
if Confirm.ask("Do you want to use managed services?"):
terraform_workspace = Prompt.ask("Enter Terraform Workspace", default=default_terraform_workspace)
find_and_replace(
pattern=default_terraform_workspace,
replacement=terraform_workspace,
paths=[
"external/versions.tf"
]
)
subprocess.run(
[editor, 'metal/inventories/prod.yml']
)
if __name__ == '__main__':
main()