diff --git a/core/src/epicli/cli/engine/providers/azure/InfrastructureBuilder.py b/core/src/epicli/cli/engine/providers/azure/InfrastructureBuilder.py index 1f9b7a9394..3f9af30d3f 100644 --- a/core/src/epicli/cli/engine/providers/azure/InfrastructureBuilder.py +++ b/core/src/epicli/cli/engine/providers/azure/InfrastructureBuilder.py @@ -1,13 +1,48 @@ from cli.helpers.Step import Step - +from cli.helpers.naming_helpers import resource_name +from cli.helpers.doc_list_helpers import select_single, select_all +from cli.helpers.doc_list_helpers import select_first +from cli.helpers.data_loader import load_yaml_obj, types class InfrastructureBuilder(Step): def __init__(self, docs): super().__init__(__name__) + self.cluster_model = select_single(docs, lambda x: x.kind == 'epiphany-cluster') + self.cluster_name = self.cluster_model.specification.name.lower() + self.cluster_prefix = self.cluster_model.specification.prefix.lower() + self.resource_group_name = resource_name(self.cluster_prefix, self.cluster_name, 'rg') + self.region = self.cluster_model.specification.cloud.region self.docs = docs def run(self): infrastructure = [] + resource_group = self.get_resource_group() + infrastructure.append(resource_group) + + vnet = self.get_virtual_network() + infrastructure.append(vnet) + return infrastructure + def get_resource_group(self): + resource_group = self.get_config_or_default(self.docs, 'infrastructure/resource-group') + resource_group.specification.name = self.resource_group_name + resource_group.specification.region = self.cluster_model.specification.cloud.region + return resource_group + + def get_virtual_network(self): + vnet = self.get_config_or_default(self.docs, 'infrastructure/vnet') + vnet.specification.name = resource_name(self.cluster_prefix, self.cluster_name, 'vnet') + vnet.specification.address_space = self.cluster_model.specification.cloud.vnet_address_pool + vnet.specification.resource_group_name = self.resource_group_name + vnet.specification.location = self.cluster_model.specification.cloud.region + return vnet + + @staticmethod + def get_config_or_default(docs, kind): + config = select_first(docs, lambda x: x.kind == kind) + if config is None: + return load_yaml_obj(types.DEFAULT, 'azure', kind) + return config + diff --git a/core/src/epicli/cli/engine/terraform/TerraformRunner.py b/core/src/epicli/cli/engine/terraform/TerraformRunner.py index 00b8d178f9..c379367f5c 100644 --- a/core/src/epicli/cli/engine/terraform/TerraformRunner.py +++ b/core/src/epicli/cli/engine/terraform/TerraformRunner.py @@ -4,6 +4,7 @@ from cli.helpers.Step import Step from cli.helpers.build_saver import get_terraform_path, save_sp, SP_FILE_NAME from cli.helpers.data_loader import load_yaml_file +from cli.helpers.naming_helpers import resource_name class TerraformRunner(Step): @@ -13,6 +14,7 @@ def __init__(self, cluster_model, config_docs): self.cluster_model = cluster_model self.config_docs = config_docs self.terraform = TerraformCommand(get_terraform_path(self.cluster_model.specification.name)) + self.new_env = os.environ.copy() def __enter__(self): super().__enter__() @@ -22,17 +24,15 @@ def run(self): pass def build(self): - new_env = os.environ.copy() - self.terraform.init(env=new_env) + self.terraform.init(env=self.new_env) if self.cluster_model.provider == 'azure': self.azure_login() - self.terraform.apply(auto_approve=True, env=new_env) + self.terraform.apply(auto_approve=True, env=self.new_env) def delete(self): - new_env = os.environ.copy() if self.cluster_model.provider == 'azure': self.azure_login() - self.terraform.destroy(auto_approve=True, env=new_env) + self.terraform.destroy(auto_approve=True, env=self.new_env) def azure_login(self): # From the 4 methods terraform provides to login to @@ -47,14 +47,17 @@ def azure_login(self): sp_file = os.path.join(get_terraform_path(self.cluster_model.specification.name), SP_FILE_NAME) if not os.path.exists(sp_file): self.logger.info('Creating service principal') - sp = apiproxy.create_sp(self.cluster_model.specification.cloud.resource_group_name, subscription['id']) + cluster_name = self.cluster_model.specification.name.lower() + cluster_prefix = self.cluster_model.specification.prefix.lower() + resource_group_name = resource_name(cluster_prefix, cluster_name, 'rg') + sp = apiproxy.create_sp(resource_group_name, subscription['id']) save_sp(sp, self.cluster_model.specification.name) else: self.logger.info('Using service principal from file') sp = load_yaml_file(sp_file) #Setup environment variables for Terraform when working with Azure and service principal. - new_env['ARM_SUBSCRIPTION_ID'] = subscription['id'] - new_env['ARM_TENANT_ID'] = sp['tenant'] - new_env['ARM_CLIENT_ID'] = sp['appId'] - new_env['ARM_CLIENT_SECRET'] = sp['password'] + self.new_env['ARM_SUBSCRIPTION_ID'] = subscription['id'] + self.new_env['ARM_TENANT_ID'] = sp['tenant'] + self.new_env['ARM_CLIENT_ID'] = sp['appId'] + self.new_env['ARM_CLIENT_SECRET'] = sp['password'] diff --git a/core/src/epicli/data/aws/terraform/epiphany-cluster.j2 b/core/src/epicli/data/aws/terraform/epiphany-cluster.j2 index 9ead9d53a8..6fbdc1d37e 100644 --- a/core/src/epicli/data/aws/terraform/epiphany-cluster.j2 +++ b/core/src/epicli/data/aws/terraform/epiphany-cluster.j2 @@ -1,5 +1,5 @@ ##################################################### -# DO NOT Modify by hand - Manage by Automation +# DO NOT Modify by hand - Managed by Automation ##################################################### ##################################################### # This file can be used as a base template to build other Terraform files. It attempts to use as much diff --git a/core/src/epicli/data/aws/terraform/infrastructure/default-security-group.j2 b/core/src/epicli/data/aws/terraform/infrastructure/default-security-group.j2 index 38481111d2..62e81a8511 100644 --- a/core/src/epicli/data/aws/terraform/infrastructure/default-security-group.j2 +++ b/core/src/epicli/data/aws/terraform/infrastructure/default-security-group.j2 @@ -1,5 +1,5 @@ ##################################################### -# DO NOT Modify by hand - Manage by Automation +# DO NOT Modify by hand - Managed by Automation ##################################################### ##################################################### # This file can be used as a base template to build other Terraform files. It attempts to use as much diff --git a/core/src/epicli/data/aws/terraform/infrastructure/efs-storage.j2 b/core/src/epicli/data/aws/terraform/infrastructure/efs-storage.j2 index a98be6c7e8..7b74e429bd 100644 --- a/core/src/epicli/data/aws/terraform/infrastructure/efs-storage.j2 +++ b/core/src/epicli/data/aws/terraform/infrastructure/efs-storage.j2 @@ -1,5 +1,5 @@ ##################################################### -# DO NOT Modify by hand - Manage by Automation +# DO NOT Modify by hand - Managed by Automation ##################################################### ##################################################### # This file can be used as a base template to build other Terraform files. It attempts to use as much diff --git a/core/src/epicli/data/aws/terraform/infrastructure/internet-gateway.j2 b/core/src/epicli/data/aws/terraform/infrastructure/internet-gateway.j2 index 52a0f4d099..a911226da4 100644 --- a/core/src/epicli/data/aws/terraform/infrastructure/internet-gateway.j2 +++ b/core/src/epicli/data/aws/terraform/infrastructure/internet-gateway.j2 @@ -1,5 +1,5 @@ ##################################################### -# DO NOT Modify by hand - Manage by Automation +# DO NOT Modify by hand - Managed by Automation ##################################################### ##################################################### # This file can be used as a base template to build other Terraform files. It attempts to use as much diff --git a/core/src/epicli/data/aws/terraform/infrastructure/launch-configuration.j2 b/core/src/epicli/data/aws/terraform/infrastructure/launch-configuration.j2 index f4687ad76c..9c9b7fff9a 100644 --- a/core/src/epicli/data/aws/terraform/infrastructure/launch-configuration.j2 +++ b/core/src/epicli/data/aws/terraform/infrastructure/launch-configuration.j2 @@ -1,5 +1,5 @@ ##################################################### -# DO NOT Modify by hand - Manage by Automation +# DO NOT Modify by hand - Managed by Automation ##################################################### ##################################################### # This file can be used as a base template to build other Terraform files. It attempts to use as much diff --git a/core/src/epicli/data/aws/terraform/infrastructure/public-key.j2 b/core/src/epicli/data/aws/terraform/infrastructure/public-key.j2 index 8d24d59718..8b92c58736 100644 --- a/core/src/epicli/data/aws/terraform/infrastructure/public-key.j2 +++ b/core/src/epicli/data/aws/terraform/infrastructure/public-key.j2 @@ -1,5 +1,5 @@ ##################################################### -# DO NOT Modify by hand - Manage by Automation +# DO NOT Modify by hand - Managed by Automation ##################################################### ##################################################### # This file can be used as a base template to build other Terraform files. It attempts to use as much diff --git a/core/src/epicli/data/aws/terraform/infrastructure/resource-group.j2 b/core/src/epicli/data/aws/terraform/infrastructure/resource-group.j2 index eda3926457..1d1e0501fa 100644 --- a/core/src/epicli/data/aws/terraform/infrastructure/resource-group.j2 +++ b/core/src/epicli/data/aws/terraform/infrastructure/resource-group.j2 @@ -1,5 +1,5 @@ ##################################################### -# DO NOT Modify by hand - Manage by Automation +# DO NOT Modify by hand - Managed by Automation ##################################################### ##################################################### # This file can be used as a base template to build other Terraform files. It attempts to use as much diff --git a/core/src/epicli/data/aws/terraform/infrastructure/route-table-association.j2 b/core/src/epicli/data/aws/terraform/infrastructure/route-table-association.j2 index 2ed79bd14b..81349c3167 100644 --- a/core/src/epicli/data/aws/terraform/infrastructure/route-table-association.j2 +++ b/core/src/epicli/data/aws/terraform/infrastructure/route-table-association.j2 @@ -1,5 +1,5 @@ ##################################################### -# DO NOT Modify by hand - Manage by Automation +# DO NOT Modify by hand - Managed by Automation ##################################################### ##################################################### # This file can be used as a base template to build other Terraform files. It attempts to use as much diff --git a/core/src/epicli/data/aws/terraform/infrastructure/route-table.j2 b/core/src/epicli/data/aws/terraform/infrastructure/route-table.j2 index f7143b3e5e..4918839328 100644 --- a/core/src/epicli/data/aws/terraform/infrastructure/route-table.j2 +++ b/core/src/epicli/data/aws/terraform/infrastructure/route-table.j2 @@ -1,5 +1,5 @@ ##################################################### -# DO NOT Modify by hand - Manage by Automation +# DO NOT Modify by hand - Managed by Automation ##################################################### ##################################################### # This file can be used as a base template to build other Terraform files. It attempts to use as much diff --git a/core/src/epicli/data/aws/terraform/infrastructure/security-group-rule.j2 b/core/src/epicli/data/aws/terraform/infrastructure/security-group-rule.j2 index dbbe3df6a0..788f9f07b2 100644 --- a/core/src/epicli/data/aws/terraform/infrastructure/security-group-rule.j2 +++ b/core/src/epicli/data/aws/terraform/infrastructure/security-group-rule.j2 @@ -1,5 +1,5 @@ ##################################################### -# DO NOT Modify by hand - Manage by Automation +# DO NOT Modify by hand - Managed by Automation ##################################################### ##################################################### # This file can be used as a base template to build other Terraform files. It attempts to use as much diff --git a/core/src/epicli/data/aws/terraform/infrastructure/security-group.j2 b/core/src/epicli/data/aws/terraform/infrastructure/security-group.j2 index 4db45f6c9a..8bfb1ba812 100644 --- a/core/src/epicli/data/aws/terraform/infrastructure/security-group.j2 +++ b/core/src/epicli/data/aws/terraform/infrastructure/security-group.j2 @@ -1,5 +1,5 @@ ##################################################### -# DO NOT Modify by hand - Manage by Automation +# DO NOT Modify by hand - Managed by Automation ##################################################### ##################################################### # This file can be used as a base template to build other Terraform files. It attempts to use as much diff --git a/core/src/epicli/data/aws/terraform/infrastructure/subnet.j2 b/core/src/epicli/data/aws/terraform/infrastructure/subnet.j2 index 48e4a8c821..45d985dca1 100644 --- a/core/src/epicli/data/aws/terraform/infrastructure/subnet.j2 +++ b/core/src/epicli/data/aws/terraform/infrastructure/subnet.j2 @@ -1,5 +1,5 @@ ##################################################### -# DO NOT Modify by hand - Manage by Automation +# DO NOT Modify by hand - Managed by Automation ##################################################### ##################################################### # This file can be used as a base template to build other Terraform files. It attempts to use as much diff --git a/core/src/epicli/data/aws/terraform/infrastructure/virtual-machine.j2 b/core/src/epicli/data/aws/terraform/infrastructure/virtual-machine.j2 index 538d6798db..0f5b88abba 100644 --- a/core/src/epicli/data/aws/terraform/infrastructure/virtual-machine.j2 +++ b/core/src/epicli/data/aws/terraform/infrastructure/virtual-machine.j2 @@ -1,5 +1,5 @@ ##################################################### -# DO NOT Modify by hand - Manage by Automation +# DO NOT Modify by hand - Managed by Automation ##################################################### ##################################################### # This file can be used as a base template to build other Terraform files. It attempts to use as much diff --git a/core/src/epicli/data/aws/terraform/infrastructure/vpc.j2 b/core/src/epicli/data/aws/terraform/infrastructure/vpc.j2 index a103ffd1c1..b9e026b042 100644 --- a/core/src/epicli/data/aws/terraform/infrastructure/vpc.j2 +++ b/core/src/epicli/data/aws/terraform/infrastructure/vpc.j2 @@ -1,5 +1,5 @@ ##################################################### -# DO NOT Modify by hand - Manage by Automation +# DO NOT Modify by hand - Managed by Automation ##################################################### ##################################################### # This file can be used as a base template to build other Terraform files. It attempts to use as much diff --git a/core/src/epicli/data/azure/defaults/infrastructure/network.yml b/core/src/epicli/data/azure/defaults/infrastructure/network.yml deleted file mode 100644 index 11f72ce882..0000000000 --- a/core/src/epicli/data/azure/defaults/infrastructure/network.yml +++ /dev/null @@ -1,7 +0,0 @@ -kind: infrastructure/network -version: 0.3.0 -title: "Network Config" -provider: azure -name: default -specification: - some_setting: "setting" diff --git a/core/src/epicli/data/azure/defaults/infrastructure/resource-group.yml b/core/src/epicli/data/azure/defaults/infrastructure/resource-group.yml new file mode 100644 index 0000000000..1835a6fb3e --- /dev/null +++ b/core/src/epicli/data/azure/defaults/infrastructure/resource-group.yml @@ -0,0 +1,8 @@ +kind: infrastructure/resource-group +version: 0.3.0 +title: "Resource Group" +provider: azure +name: default +specification: + name: SET_BY_AUTOMATION + region: SET_BY_AUTOMATION \ No newline at end of file diff --git a/core/src/epicli/data/azure/defaults/infrastructure/vnet.yml b/core/src/epicli/data/azure/defaults/infrastructure/vnet.yml new file mode 100644 index 0000000000..b4866fdd3e --- /dev/null +++ b/core/src/epicli/data/azure/defaults/infrastructure/vnet.yml @@ -0,0 +1,10 @@ +kind: infrastructure/vnet +version: 0.3.0 +title: "VNET Config" +provider: azure +name: default +specification: + name: SET_BY_AUTOMATION + address_space: SET_BY_AUTOMATION + location: SET_BY_AUTOMATION + resource_group_name: SET_BY_AUTOMATION diff --git a/core/src/epicli/data/azure/terraform/epiphany-cluster.j2 b/core/src/epicli/data/azure/terraform/epiphany-cluster.j2 index f853cd59dd..faca2beeb6 100644 --- a/core/src/epicli/data/azure/terraform/epiphany-cluster.j2 +++ b/core/src/epicli/data/azure/terraform/epiphany-cluster.j2 @@ -1,5 +1,5 @@ ##################################################### -# DO NOT Modify by hand - Manage by Automation +# DO NOT Modify by hand - Managed by Automation ##################################################### ##################################################### # This file can be used as a base template to build other Terraform files. It attempts to use as much @@ -12,8 +12,3 @@ provider "azurerm" { } - -resource "azurerm_resource_group" "rg" { - name = "{{ specification.cloud.resource_group_name }}" - location = "{{ specification.cloud.region }}" -} diff --git a/core/src/epicli/data/azure/terraform/infrastructure/net.j2 b/core/src/epicli/data/azure/terraform/infrastructure/net.j2 deleted file mode 100644 index 12bf580733..0000000000 --- a/core/src/epicli/data/azure/terraform/infrastructure/net.j2 +++ /dev/null @@ -1 +0,0 @@ -# TODO: Fill template diff --git a/core/src/epicli/data/azure/terraform/infrastructure/network.j2 b/core/src/epicli/data/azure/terraform/infrastructure/network.j2 deleted file mode 100644 index 98a88ee414..0000000000 --- a/core/src/epicli/data/azure/terraform/infrastructure/network.j2 +++ /dev/null @@ -1,5 +0,0 @@ -##################################################### -# Network - {{ name }} -##################################################### - -# TODO: Fill template diff --git a/core/src/epicli/data/azure/terraform/infrastructure/resource-group.j2 b/core/src/epicli/data/azure/terraform/infrastructure/resource-group.j2 new file mode 100644 index 0000000000..11c6cb870c --- /dev/null +++ b/core/src/epicli/data/azure/terraform/infrastructure/resource-group.j2 @@ -0,0 +1,17 @@ +##################################################### +# DO NOT Modify by hand - Managed by Automation +##################################################### +##################################################### +# This file can be used as a base template to build other Terraform files. It attempts to use as much +# Terraform interprolation as possible by creating Terraform variables instead of changing inline +# this approach provides an easier way to do creative looping, fetch IDs of created resources etc. +##################################################### +##################################################### +# {{ specification.name }} +##################################################### + + +resource "azurerm_resource_group" "rg" { + name = "{{ specification.name }}" + location = "{{ specification.region }}" +} \ No newline at end of file diff --git a/core/src/epicli/data/azure/terraform/infrastructure/vnet.j2 b/core/src/epicli/data/azure/terraform/infrastructure/vnet.j2 new file mode 100644 index 0000000000..e4e64f4ad1 --- /dev/null +++ b/core/src/epicli/data/azure/terraform/infrastructure/vnet.j2 @@ -0,0 +1,18 @@ +##################################################### +# DO NOT Modify by hand - Managed by Automation +##################################################### +##################################################### +# This file can be used as a base template to build other Terraform files. It attempts to use as much +# Terraform interprolation as possible by creating Terraform variables instead of changing inline +# this approach provides an easier way to do creative looping, fetch IDs of created resources etc. +##################################################### +##################################################### +# {{ specification.name }} +##################################################### + +resource "azurerm_virtual_network" "vnet" { + name = "{{ specification.name }}" + address_space = ["{{ specification.address_space }}"] + location = "{{ specification.location }}" + resource_group_name = "{{ specification.resource_group_name }}" +} \ No newline at end of file diff --git a/core/src/epicli/data/azure/validation/infrastructure/network.yml b/core/src/epicli/data/azure/validation/infrastructure/resource-group.yml similarity index 100% rename from core/src/epicli/data/azure/validation/infrastructure/network.yml rename to core/src/epicli/data/azure/validation/infrastructure/resource-group.yml diff --git a/core/src/epicli/data/azure/validation/infrastructure/vnet.yml b/core/src/epicli/data/azure/validation/infrastructure/vnet.yml new file mode 100644 index 0000000000..89807aa970 --- /dev/null +++ b/core/src/epicli/data/azure/validation/infrastructure/vnet.yml @@ -0,0 +1 @@ +$ref: '#/definitions/unvalidated_specification' \ No newline at end of file diff --git a/core/src/epicli/data/common/defaults/epiphany-cluster.yml b/core/src/epicli/data/common/defaults/epiphany-cluster.yml index 2ecfb928f5..19934a2d31 100644 --- a/core/src/epicli/data/common/defaults/epiphany-cluster.yml +++ b/core/src/epicli/data/common/defaults/epiphany-cluster.yml @@ -11,7 +11,6 @@ specification: key_path: /root/.ssh/epiphany-operations/id_rsa # YOUR-SSH-KEY-PATH cloud: subscription_name: YOUR-SUB-NAME - resource_group_name: YOUR-RESOURCE-GROUP-NAME vnet_address_pool: 10.1.0.0/20 use_public_ips: False # When not using public IPs you have to provide connectivity via private IPs (VPN) use_service_principal: False