From 368535f41c6a815a01c0563452b3e1bf14571328 Mon Sep 17 00:00:00 2001 From: Khue Doan Date: Sun, 20 Feb 2022 20:44:54 +0700 Subject: [PATCH] refactor: rewrite configure script in pure Python https://github.com/khuedoan/homelab/issues/30 --- configure.py | 111 +++++++++++++++++++++++------------- scripts/replace-domain | 7 --- scripts/replace-gitops-repo | 8 --- 3 files changed, 71 insertions(+), 55 deletions(-) delete mode 100755 scripts/replace-domain delete mode 100755 scripts/replace-gitops-repo diff --git a/configure.py b/configure.py index 918cf491..226c5192 100755 --- a/configure.py +++ b/configure.py @@ -7,52 +7,83 @@ Basic configure script for new users """ +import fileinput import os -import platform -import sys +import subprocess -editor = os.getenv('EDITOR') -seed_repo = "github.com/khuedoan/homelab" -domain = "khuedoan.com" -terraform_workspace = "khuedoan" +default_editor = os.getenv('EDITOR') +default_seed_repo = "https://github.com/khuedoan/homelab" +default_domain = "khuedoan.com" +default_timezone = "Asia/Ho_Chi_Minh" +default_terraform_workspace = "khuedoan" -if sys.version_info < (3, 10, 0): - raise Exception("Must be using Python >= 3.10.0") +editor = str(input(f"Text editor ({default_editor}): ") or default_editor) +domain = str(input(f"Enter your domain ({default_domain}): ") or default_domain) +seed_repo = str(input(f"Enter seed repo ({default_seed_repo}): ") or default_seed_repo) +timezone = str(input(f"Enter time zone ({default_timezone}): ") or default_timezone) +terraform_workspace = str(input(f"Enter your Terraform Workspace, skip if you don't want to use external resources yet ({default_terraform_workspace}): ") or default_terraform_workspace) -# confirm text editor -editor = str(input(f"Text editor ({editor}): ") or editor) +def find_and_replace(pattern: str, replacement: str, directories: list[str]) -> None: + files_with_matches = subprocess.run( + ["git", "grep", "--files-with-matches", pattern, "--"] + directories, + capture_output=True, + text=True, + check=True + ).stdout.splitlines() -# Replace seed repo -seed_repo = str(input(f"Enter seed repo ({seed_repo}): ") or seed_repo) -os.system(f"./scripts/replace-gitops-repo {seed_repo}") - -# Replace domain -domain = str(input(f"Enter your domain ({domain}): ") or domain) -os.system(f"./scripts/replace-domain {domain}") - -# Change hardware info -os.system(f"{editor} 'metal/inventories/prod.yml'") # TODO use var for inventory - -def replace_terraform_workspace(current, new): - filename = 'external/versions.tf' - # Read in the file - with open(filename, 'r') as file: - filedata = file.read() - - # Replace the target string - filedata = filedata.replace(current, new) - - # Write the file out again - with open(filename, 'w') as file: - file.write(filedata) + 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='') -new_terraform_workspace = str( - input(f"Enter your Terraform Workspace ({terraform_workspace}): ") - or terraform_workspace -) +def main() -> None: + find_and_replace( + pattern=default_domain, + replacement=domain, + directories=[ + ".tekton" + "apps", + "bootstrap", + "platform", + "system", + ] + ) -replace_terraform_workspace(terraform_workspace, new_terraform_workspace) + find_and_replace( + pattern=default_seed_repo, + replacement=seed_repo, + directories=[ + "bootstrap", + "platform" + ] + ) -# TODO switch to git lib -os.system("git diff") + find_and_replace( + pattern=default_timezone, + replacement=timezone, + directories=[ + "apps", + "metal" + ] + ) + + find_and_replace( + pattern=default_terraform_workspace, + replacement=terraform_workspace, + directories=[ + "external/versions.tf" + ] + ) + + subprocess.run( + [editor, 'metal/inventories/prod.yml'] + ) + + subprocess.run( + ["git", "diff"] + ) + + +if __name__ == '__main__': + main() diff --git a/scripts/replace-domain b/scripts/replace-domain deleted file mode 100755 index a77e0d84..00000000 --- a/scripts/replace-domain +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/sh - -CURRENT_DOMAIN="khuedoan.com" - -git grep "${CURRENT_DOMAIN}" -- bootstrap system platform apps .tekton | cut -d ':' -f1 | uniq | while read p; do - sed -i "s/${CURRENT_DOMAIN}/${1}/g" $p -done diff --git a/scripts/replace-gitops-repo b/scripts/replace-gitops-repo deleted file mode 100755 index 82e94c1a..00000000 --- a/scripts/replace-gitops-repo +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/sh - -CURRENT_REPO=$(echo 'github.com/khuedoan/homelab' | sed 's/\([.,/]\)/\\\1/g') -NEW_REPO=$(echo "${1}" | sed 's/\([.,/]\)/\\\1/g') - -git grep "${CURRENT_REPO}" -- bootstrap platform | cut -d ':' -f1 | uniq | while read p; do - sed -i "s/${CURRENT_REPO}/${NEW_REPO}/g" $p -done