diff --git a/edbterraform/data/templates/user/inventory.yml.tftpl b/edbterraform/data/templates/user/inventory.yml.tftpl deleted file mode 100644 index aac69cf5..00000000 --- a/edbterraform/data/templates/user/inventory.yml.tftpl +++ /dev/null @@ -1,217 +0,0 @@ -# Original filename: inventory.yml.tftpl -# This inventory file is meant to be used alongside edb-ansible: https://github.com/EnterpriseDB/edb-ansible -# and relies on tags set within the infrastructure file to properly generate. -# Sample infrastructure file for this template can be found under infrastructure-examples/aws-edb-ra-3.yml -# -# As a sample template, it will not cause terraform CLI and so it might be invalid. -# To fail upon errors, remove any try() function. ---- -all: - children: -# PEM servers: tags.type.pem_server -%{ for key, values in {for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "pem_server" && tonumber(try(values.tags.index, -1)) == 0 } ~} - pemserver: - hosts: -%{ if lower(try(values.tags.reference_architecture, "")) == "edb-always-on" ~} - pemserver${ 1 + tonumber(try(values.tags.index, -2)) }: -%{ else ~} - pemserver${ 1 + tonumber(values.tags.index) }.${ values.tags.cluster_name }.internal: -%{ endif ~} - ansible_host: ${ values.public_ip } - private_ip: ${ values.private_ip } - ansible_user: ${ values.operating_system.ssh_user } - ansible_ssh_private_key_file: ${ values.operating_system.ssh_private_key_file } -%{ endfor ~} - -# Barman servers: tags.type.barman_server -%{ for key, values in {for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "barman_server" && tonumber(try(values.tags.index, -1)) == 0 } ~} - barmanserver: - hosts: -%{ endfor ~} -%{ for key, values in {for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "barman_server"} ~} -%{ if lower(try(values.tags.reference_architecture, "")) == "edb-always-on" ~} - barmandc${ 1 + tonumber(try(values.tags.index -2)) }: -%{ else ~} - barmanserver${ 1 + tonumber(try(values.tags.index, "")) }.${ values.tags.cluster_name }.internal: -%{ endif ~} - ansible_host: ${ values.public_ip } - private_ip: ${ values.private_ip } - ansible_user: ${ values.operating_system.ssh_user } - ansible_ssh_private_key_file: ${ values.operating_system.ssh_private_key_file } -%{ for key, values in {for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "pem_server" && tonumber(try(values.tags.index, -1)) == 0 } ~} - pem_agent: true - pem_server_private_ip: ${ values.private_ip } -%{ endfor ~} -%{ endfor ~} - -# Postgres servers: tags.type.postgres_server -%{ for key,values in { for key,values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "postgres_server" && tonumber(try(values.tags.index, -1)) == 0 } ~} - primary: - hosts: -%{ if lower(try(values.tags.pg_type, "")) == "epas" ~} - epas${ 1 + tonumber(values.tags.index) }.${ values.tags.cluster_name }.internal: -%{ else ~} - pgsql${ 1 + tonumber(values.tags.index) }.${ values.tags.cluster_name }.internal: -%{ endif ~} - ansible_host: ${ values.public_ip } - private_ip: ${ values.private_ip } - ansible_user: ${ values.operating_system.ssh_user } - ansible_ssh_private_key_file: ${ values.operating_system.ssh_private_key_file } -%{ for key, values in {for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "barman_server" && tonumber(try(values.tags.index, -1)) == 0} ~} - barman: true - barman_server_private_ip: ${ values.private_ip } - barman_backup_method: postgres -%{ endfor ~} -%{ for key, values in {for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "dbt2_client" } ~} -%{ if tonumber(try(values.tags.index, -1)) == 0 ~} - dbt2: true -%{ endif ~} - dbt2_client_private_ip${ 1 + tonumber(try(values.tags.index, -2)) }: ${ values.private_ip } -%{ endfor ~} -%{ for key, values in {for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "hammerdb_server" && tonumber(try(values.tags.index, -1)) == 0 } ~} - hammerdb: true - hammerdb_server_private_ip: ${ values.private_ip } -%{ endfor ~} -%{ if tobool(try(values.tags.pooler_local, false)) && lower(try(values.tags.pg_pooler, "")) == "pgbouncer" ~} - pgbouncer: true -%{ endif ~} -%{ for key, values in {for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "pem_server" && tonumber(try(values.tags.index, -1)) == 0 } ~} - pem_agent: true - pem_server_private_ip: ${ values.private_ip } -%{ endfor ~} - standby: - hosts: -%{ endfor ~} -%{ for key,values in { for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "postgres_server" && tonumber(try(values.tags.index, 0)) != 0 } ~} -%{ if lower(try(values.tags.pg_type, "")) == "epas" ~} - epas${ 1 + tonumber(values.tags.index) }.${ values.tags.cluster_name }.internal: -%{ else ~} - pgsql${ 1 + tonumber(values.tags.index) }.${ values.tags.cluster_name }.internal: -%{ endif ~} - ansible_host: ${ values.public_ip } - private_ip: ${ values.private_ip } - ansible_user: ${ values.operating_system.ssh_user } - ansible_ssh_private_key_file: ${ values.operating_system.ssh_private_key_file } -%{ for key, values in {for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "barman_server" && tonumber(try(values.tags.index, -1)) == 0} ~} - barman: true - barman_server_private_ip: ${ values.private_ip } - barman_backup_method: postgres -%{ endfor ~} -%{ for key, values in {for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "dbt2_client" } ~} - dbt2_client_private_ip${ 1 + tonumber(try(values.tags.index, -2)) }: ${ values.private_ip } -%{ endfor ~} -%{ for key, values in {for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "hammerdb_server" && tonumber(try(values.tags.index, -1)) == 0} ~} - hammerdb: true - hammerdb_server_private_ip: ${ values.private_ip } -%{ endfor ~} -%{ if tobool(try(values.tags.pooler_local, false)) && lower(try(values.tags.pg_pooler, "")) == "pgbouncer" ~} - pgbouncer: true -%{ endif ~} - replication_type: ${ values.tags.replication_type } -%{ for key, values in {for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "postgres_server" && tonumber(try(values.tags.index, -1)) == 0 } ~} - upstream_node_private_ip: ${ values.private_ip } -%{ endfor ~} -%{ for key, values in {for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "pem_server" && tonumber(try(values.tags.index, -1)) == 0 } ~} - pem_agent: true - pem_server_private_ip: ${ values.private_ip } -%{ endfor ~} -%{ endfor ~} - -# BDR servers: tags.type.bdr_server -%{ for key, values in {for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "bdr_server" && tonumber(try(values.tags.index, -1)) == 0 } ~} - primary: - hosts: -%{ endfor ~} -%{ for key, values in {for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "bdr_server" } ~} -%{ if lower(try(values.tags.pg_type, "")) == "epas" ~} - epas${ 1 + tonumber(try(values.tags.index, -2)) ~}: -%{ else ~} - pgsql${ 1 + tonumber(try(values.tags.index, -2)) ~}: -%{ endif ~} - ansible_host: ${ values.public_ip } - private_ip: ${ values.private_ip } - ansible_user: ${ values.operating_system.ssh_user } - ansible_ssh_private_key_file: ${ values.operating_system.ssh_private_key_file } -%{ for key, values in {for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "pem_server" && tonumber(try(values.tags.index), -1) == 0 } ~} - pem_agent: true - pem_server_private_ip: ${ values.private_ip } -%{ endfor ~} -%{ endfor ~} -%{ for key, values in {for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "bdr_witness_server" } ~} -%{ if lower(try(values.tags.pg_type, "")) == "epas" ~} - epas${ 1 + tonumber(try(values.tags.index, -2)) + length([ for key, values in try(servers.machines, {}): key if lower(try(values.tags.type, "")) == "bdr_server"]) }: -%{ else ~} - pgsql${ 1 + tonumber(try(values.tags.index,-2)) + length([ for key, values in try(servers.machines, {}): key if lower(try(values.tags.type, "")) == "bdr_server"]) }: -%{ endif ~} - ansible_host: ${ values.public_ip } - private_ip: ${ values.private_ip } - ansible_user: ${ values.operating_system.ssh_user } - ansible_ssh_private_key_file: ${ values.operating_system.ssh_private_key_file } -%{ for key, values in { for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "pem_server" && tonumber(try(values.tags.index), -1) == 0 } ~} - pem_agent: true - pem_server_private_ip: ${ values.private_ip } -%{ endfor ~} -%{ endfor ~} - -# Pool connection servers: tags.type.pooler_server -%{ for key, values in {for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "pooler_server" && tonumber(try(values.tags.index, -1)) == 0 } ~} - ${ values.tags.pooler_type }: - hosts: -%{ endfor ~} -%{ for key, values in {for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "pooler_server" } ~} -%{ if length([ for key, values in try(servers.machines, {}): key if lower(try(values.tags.type, "")) == "bdr_server"]) > 0 ~} - pgbouncer${ 1 + tonumber(try(values.tags.index, -2)) }: -%{ else ~} - ${ lower(try(values.tags.pooler_type, "")) }-${ 1 + tonumber(try(values.tags.index, -2)) }.${ values.tags.cluster_name }.internal: -%{ endif ~} - ansible_host: ${ values.public_ip } - private_ip: ${ values.private_ip } - ansible_user: ${ values.operating_system.ssh_user } - ansible_ssh_private_key_file: ${ values.operating_system.ssh_private_key_file } -%{ for key,values in { for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "postgres_server" && tonumber(try(values.tags.index, -1)) == 0 } ~} - primary_private_ip: ${ values.private_ip } -%{ endfor ~} -%{ for key, values in {for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "pem_server" && tonumber(try(values.tags.index, -1)) == 0 } ~} - pem_agent: true - pem_server_private_ip: ${ values.private_ip } -%{ endfor ~} -%{ endfor ~} - -# DBT2 servers: tags.type.dbt2_client -%{ for key, values in { for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "dbt2_client" && tonumber(try(values.tags.index, -1)) == 0 } ~} - dbt2_client: - hosts: -%{ endfor ~} -%{ for key, values in { for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "dbt2_client" } ~} - dbt2_client${ 1 + tonumber(try(values.tags.index, -2)) }.${ values.tags.cluster_name }.internal: - ansible_host: ${ values.public_ip } - private_ip: ${ values.private_ip } - ansible_user: ${ values.operating_system.ssh_user } - ansible_ssh_private_key_file: ${ values.operating_system.ssh_private_key_file } -%{ endfor ~} - -# DBT2 servers: tags.type.dbt2_driver -%{ for key, values in { for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "dbt2_driver" && tonumber(try(values.tags.index, -1)) == 0 } ~} - dbt2_driver: - hosts: -%{ endfor ~} -%{ for key, values in { for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "dbt2_driver" } ~} - dbt2_driver${ 1 + tonumber(try(values.tags.index, -2)) }.${ values.tags.cluster_name }.internal: - ansible_host: ${ values.public_ip } - private_ip: ${ values.private_ip } - ansible_user: ${ values.operating_system.ssh_user } - ansible_ssh_private_key_file: ${ values.operating_system.ssh_private_key_file } -%{ endfor ~} - -# HammerDB servers: tags.type.hammerdb_server -%{ for key, values in { for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "hammerdb_server" && tonumber(try(values.tags.index, -1)) == 0 } ~} - hammerdbserver: - hosts: -%{ endfor ~} -%{ for key, values in { for key, values in try(servers.machines, {}): key=>values if lower(try(values.tags.type, "")) == "hammerdb_server" } ~} - hammerdbserver${ 1 + tonumber(try(values.tags.index, -2)) }.${ values.tags.cluster_name }.internal: - ansible_host: ${ values.public_ip } - private_ip: ${ values.private_ip } - ansible_user: ${ values.operating_system.ssh_user } - ansible_ssh_private_key_file: ${ values.operating_system.ssh_private_key_file } -%{ endfor ~} diff --git a/edbterraform/lib.py b/edbterraform/lib.py index 18fe49e9..9c5c5ccd 100644 --- a/edbterraform/lib.py +++ b/edbterraform/lib.py @@ -127,37 +127,6 @@ def update_terraform_blocks(file, template_vars, infra_vars, cloud_service_provi except Exception as e: raise Exception('ERROR: could not update terraform blocks in %s - (%s)' % (file, repr(e))) -def save_default_templates(templates_directory): - ''' - Save any predefined templates into the 'directory/templates' for consistent referencing - If the filename already exists, it should be skipped to avoid overriding user customizations. - ''' - # Templates are located in parent_directory/data/templates/user - script_dir = Path(__file__).parent.resolve() - predefined_templates = script_dir / 'data' / 'templates' / 'user' - templates_directory = Path(templates_directory) - logger.info(f'Copy templates from {predefined_templates} into {templates_directory}') - try: - if not templates_directory.exists(): - logger.info(f'Creating predefined template directory: {templates_directory}') - templates_directory.mkdir(parents=True, exist_ok=True) - - for template in predefined_templates.iterdir(): - if not template.is_file(): - logger.warning(f'Skipping {template} as it is not a file') - continue - - if not (templates_directory / template.name).exists(): - shutil.copy2(str(template), str(templates_directory)) - else: - logger.info(f''' - Skipping: {template} already exists in {templates_directory}. - To copy the latest pre-defined templates, erase any conflicting template file names. - ''') - except Exception as e: - logger.error("ERROR: cannot create template directory %s (%s)" % (templates_directory, e)) - sys.exit(1) - def create_project_dir( project_directory, cloud_service_provider, @@ -420,9 +389,6 @@ def generate_terraform( # Get final instrastructure variables after rendering it if it is a jinja2 template infra_vars = load_yaml_file(render_template(template_file=infra_file, values=infra_template_variables)) - # Save default templates into dot directory - save_default_templates(f'{__dot_project__}/templates') - # Duplicate terraform code into target project directory create_project_dir(project_path, csp, infra_file, infra_template_variables, infra_vars, hcl_lock_file)