import sys import os import shutil import yaml config = yaml.safe_load(open(sys.argv[1])) env = config['env'] region = config['region'] # step1: copy template template_dir_list = { "prod": "prod-japaneast", "staging": "staging-eastus2", "dev": "dev-eastus" } base_dir = "./" def traverse_and_copy(base_dir): def copy_directory(source_dir, target_dir): # set it to true if you recreate the directory force = False if force or not os.path.exists(target_dir): if force: shutil.rmtree(target_dir) shutil.copytree(source_dir, target_dir) print(f"Directory copied from {source_dir} to {target_dir}") else: print(f"Directory {target_dir} already exists") dir_key_word = template_dir_list[env] found = False for subdir in os.listdir(base_dir): subdir_path = os.path.join(base_dir, subdir) if os.path.isdir(subdir_path): if dir_key_word in subdir: source_dir = subdir_path target_dir = subdir_path.replace(dir_key_word, f"{env}-{region}") copy_directory(source_dir, target_dir) found = True if not found: for subdir in os.listdir(base_dir): subdir_path = os.path.join(base_dir, subdir) if os.path.isdir(subdir_path): traverse_and_copy(subdir_path) # step2: modify azure identity information import re def set_yaml_value(file_path, key_keyword, new_value): # Step 1: Read the content of the YAML file as a string with open(file_path, 'r') as file: content = file.read() # Step 2: Use regex to find and replace the key that matches the provided keyword # This regex ensures that only the value part is replaced, preserving comments and formatting pattern = re.compile(r'(\b' + re.escape(key_keyword) + r'\b\s*:\s*)([^\n#]*)') content = pattern.sub(r'\g<1>' + new_value, content) # Step 3: Write the updated content back to the YAML file with open(file_path, 'w') as file: file.write(content) def replace_value_in_yaml(file_path, old_value, new_value): # Step 1: Read the content of the YAML file as a string with open(file_path, 'r') as file: content = file.read() # Step 2: Use regex to find and replace the value that matches the provided old_value pattern = re.compile(re.escape(old_value)) content = pattern.sub(new_value, content) # Step 3: Write the updated content back to the YAML file with open(file_path, 'w') as file: file.write(content) traverse_and_copy(base_dir) params = [ (f"dedicated-cluster-service/cluster-controller/envs/{env}-{region}/serviceaccount.yaml", "azure.workload.identity/client-id", config['cluster-controller-client-id']), (f"dedicated-regional-server/cert-manager/envs/{env}-{region}/values.yaml", "azure.workload.identity/client-id", config['cert-manager-client-id']), (f"dedicated-regional-server/crossplane-azure-provider/envs/{env}-{region}/providerconfig.yaml", "clientID", config['crossplane-azure-provider-client-id']), (f"tcidm/import-controller/envs/{env}-{region}/root-config.yaml", "crossplaneAzureProviderClientID", config['crossplane-azure-provider-client-id']), (f"dedicated-cluster-service/cluster-controller/envs/{env}-{region}/root-config.yaml", "crossplaneAzureProviderClientID", config['crossplane-azure-provider-client-id']), (f"dedicated-regional-server/issuers/envs/{env}-{region}/letsencrypt-issuer.yaml", "clientID", config['cert-manager-client-id']), (f"dedicated-regional-server/secretstore/envs/{env}-{region}/serviceaccount.yaml", "azure.workload.identity/client-id", config['external-secrets-operator-client-id']), (f"dedicated-regional-server/secretstore/envs/{env}-{region}/secretstore.yaml", "vaultUrl", config['key-vault-url']), (f"dedicated-regional-server/regional-server/envs/azure-{env}-{region}/service_account.yaml", "azure.workload.identity/client-id", config['regional-server-client-id']), (f"dedicated-regional-server/regional-server/envs/azure-{env}-{region}/account/providerconfig.yaml", "clientID", config['crossplane-azure-provider-client-id']), (f"dedicated-regional-server/secretstore/envs/{env}-{region}/secretstore.yaml", "vaultUrl", config['key-vault-url']) ] for file_path, key, value in params: set_yaml_value(file_path, key, value) # replace region for path in [f"dedicated-cluster-service/cluster-controller/envs/{env}-{region}/root-config.yaml",f"dedicated-cluster-service/cluster-server/envs/{env}-{region}/args-patch.yaml", f"dedicated-regional-server/crossplane-azure-provider/envs/{env}-{region}/deploymentruntimeconfig-aws-family.yaml", f"dedicated-regional-server/gateway-crds/envs/{env}-{region}/certificate.yaml", f"dedicated-regional-server/gateway-crds/envs/{env}-{region}/kong-gateway.yaml", f"dedicated-regional-server/issuers/envs/{env}-{region}/letsencrypt-issuer.yaml", f"tcidm/import-controller/envs/{env}-{region}/root-config.yaml", f"dedicated-regional-server/regional-server/envs/azure-{env}-{region}/deployment.yaml", f"dedicated-regional-server/regional-server/envs/azure-{env}-{region}/config/root-config.yaml", f"dedicated-regional-server/regional-server/envs/azure-{env}-{region}/account/providerconfig.yaml" ]: replace_value_in_yaml(path, "japaneast", config['region']) # set nat ip address(regional ip, base ip is set)