Skip to content

Commit

Permalink
terraform: Support changing deploy location
Browse files Browse the repository at this point in the history
Support deploying ghaf-infra on other locations too, in addition to the
default location.

Signed-off-by: Henri Rosten <[email protected]>
  • Loading branch information
henrirosten committed Mar 7, 2024
1 parent 04ffaa4 commit c9adbae
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
4 changes: 2 additions & 2 deletions terraform/binary-cache.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module "binary_cache_vm" {

resource_group_name = azurerm_resource_group.infra.name
location = azurerm_resource_group.infra.location
virtual_machine_name = "ghaf-binary-cache-${local.env}"
virtual_machine_name = "ghaf-binary-cache-${local.ws}"
virtual_machine_size = local.opts[local.conf].vm_size_binarycache
virtual_machine_osdisk_size = local.opts[local.conf].osdisk_size_binarycache
virtual_machine_source_image = module.binary_cache_image.image_id
Expand All @@ -39,7 +39,7 @@ module "binary_cache_vm" {
"path" = "/var/lib/rclone-http/env"
},
{
content = "SITE_ADDRESS=ghaf-binary-cache-${local.env}.northeurope.cloudapp.azure.com",
content = "SITE_ADDRESS=ghaf-binary-cache-${local.ws}.${azurerm_resource_group.infra.location}.cloudapp.azure.com",
"path" = "/var/lib/caddy/caddy.env"
},
],
Expand Down
2 changes: 1 addition & 1 deletion terraform/builder.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module "builder_vm" {

resource_group_name = azurerm_resource_group.infra.name
location = azurerm_resource_group.infra.location
virtual_machine_name = "ghaf-builder-${count.index}-${local.env}"
virtual_machine_name = "ghaf-builder-${count.index}-${local.ws}"
virtual_machine_size = local.opts[local.conf].vm_size_builder
virtual_machine_osdisk_size = local.opts[local.conf].osdisk_size_builder
virtual_machine_source_image = module.builder_image.image_id
Expand Down
4 changes: 2 additions & 2 deletions terraform/jenkins-controller.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module "jenkins_controller_vm" {

resource_group_name = azurerm_resource_group.infra.name
location = azurerm_resource_group.infra.location
virtual_machine_name = "ghaf-jenkins-controller-${local.env}"
virtual_machine_name = "ghaf-jenkins-controller-${local.ws}"
virtual_machine_size = local.opts[local.conf].vm_size_controller
virtual_machine_osdisk_size = local.opts[local.conf].osdisk_size_controller
virtual_machine_source_image = module.jenkins_controller_image.image_id
Expand Down Expand Up @@ -64,7 +64,7 @@ module "jenkins_controller_vm" {
"path" = "/var/lib/builder-keyscan/scanlist"
},
{
content = "SITE_ADDRESS=ghaf-jenkins-controller-${local.env}.northeurope.cloudapp.azure.com",
content = "SITE_ADDRESS=ghaf-jenkins-controller-${local.ws}.${azurerm_resource_group.infra.location}.cloudapp.azure.com",
"path" = "/var/lib/caddy/caddy.env"
}
]
Expand Down
38 changes: 22 additions & 16 deletions terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ variable "location" {
description = "Azure region into which the resources will be deployed"
}

variable "envtype" {
type = string
description = "Set the environment type; determines e.g. the Azure VM sizes"
default = "priv"
validation {
condition = contains(["priv", "dev", "prod"], var.envtype)
error_message = "Must be either \"priv\", \"dev\", or \"prod\""
}
}

# Use azure_region module to get the short name of the Azure region,
# see: https://registry.terraform.io/modules/claranet/regions/azurerm/latest
module "azure_region" {
Expand Down Expand Up @@ -98,29 +108,25 @@ locals {
# Read ssh-keys.yaml into local.ssh_keys
ssh_keys = yamldecode(file("../ssh-keys.yaml"))

# Map workspace name to configuration name:
# !"dev" && !"prod" ==> "priv"
# "dev" ==> "dev"
# "prod" ==> "prod"
# This determines the configuration options used in the
# ghaf-infra instance (defines e.g. vm_sizes and number of builders)
# TODO: allow overwriting this with an input variable
conf = local.ws != "dev" && local.ws != "prod" ? "priv" : local.ws

# env is used to identify workspace-specific resources:
env = local.ws

# Selects the persistent data used in the ghaf-infra instance, currently
# either "dev" or "prod"
# (see ./persistent)
# ghaf-infra instance (defines e.g. vm_sizes and number of builders).
# If workspace name is "dev" or "prod" use the workspace name as
# envtype, otherwise, use the value from var.envtype.
conf = local.ws == "dev" || local.ws == "prod" ? local.ws : var.envtype

# Selects the persistent data (see ./persistent) used in the ghaf-infra
# instance; currently either "dev" or "prod" based on the environment type:
# "priv" ==> "dev"
# "dev" ==> "dev"
# "prod" ==> "prod"
persistent_data = local.conf == "priv" ? "dev" : local.conf
}

################################################################################

# Resource group for this ghaf-infra instance
resource "azurerm_resource_group" "infra" {
name = "ghaf-infra-${local.env}"
name = "ghaf-infra-${local.ws}"
location = var.location
}

Expand Down Expand Up @@ -155,7 +161,7 @@ resource "azurerm_subnet" "builders" {
# Storage account and storage container used to store VM images

resource "azurerm_storage_account" "vm_images" {
name = "img${local.env}${local.shortloc}"
name = "img${local.ws}${local.shortloc}"
resource_group_name = azurerm_resource_group.infra.name
location = azurerm_resource_group.infra.location
account_tier = "Standard"
Expand Down

0 comments on commit c9adbae

Please sign in to comment.