2022-01-04 22:08:53 +07:00
#!/usr/bin/python
2022-01-16 22:12:58 +07:00
# WIP
2022-02-01 00:57:23 +07:00
# TODO clean this up
2022-01-16 22:12:58 +07:00
2022-01-04 22:08:53 +07:00
"""
Basic configure script for new users
"""
2022-02-20 20:44:54 +07:00
import fileinput
2022-01-04 22:27:46 +07:00
import os
2022-02-20 20:44:54 +07:00
import subprocess
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 "
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 )
2022-02-20 20:55:42 +07:00
def find_and_replace ( pattern : str , replacement : str , paths : list [ str ] ) - > None :
2022-02-20 20:44:54 +07:00
files_with_matches = subprocess . run (
2022-02-20 20:55:42 +07:00
[ " git " , " grep " , " --files-with-matches " , pattern , " -- " ] + paths ,
2022-02-20 20:44:54 +07:00
capture_output = True ,
2022-02-21 21:52:38 +07:00
text = True
2022-02-20 20:44:54 +07:00
) . 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 :
find_and_replace (
pattern = default_domain ,
replacement = domain ,
2022-02-20 20:55:42 +07:00
paths = [
2022-02-22 18:48:53 +07:00
" .tekton " ,
2022-02-20 20:44:54 +07:00
" apps " ,
" bootstrap " ,
" platform " ,
" system " ,
2022-02-25 01:32:32 +07:00
" external "
2022-02-20 20:44:54 +07:00
]
)
find_and_replace (
pattern = default_seed_repo ,
replacement = seed_repo ,
2022-02-20 20:55:42 +07:00
paths = [
2022-02-20 20:44:54 +07:00
" bootstrap " ,
" platform "
]
)
find_and_replace (
pattern = default_timezone ,
replacement = timezone ,
2022-02-20 20:55:42 +07:00
paths = [
2022-02-20 20:44:54 +07:00
" apps " ,
2022-02-25 01:32:32 +07:00
" system " ,
2022-02-20 20:44:54 +07:00
" metal "
]
)
find_and_replace (
pattern = default_terraform_workspace ,
replacement = terraform_workspace ,
2022-02-20 20:55:42 +07:00
paths = [
2022-02-20 20:44:54 +07:00
" external/versions.tf "
]
)
subprocess . run (
[ editor , ' metal/inventories/prod.yml ' ]
)
subprocess . run (
[ " git " , " diff " ]
)
if __name__ == ' __main__ ' :
main ( )