From 4cce60af674c85d4d8293fb50ca244d19c3a4d17 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Thu, 7 Jan 2021 11:28:25 -0500 Subject: [PATCH] Extract packer-backed droplet logic to a shared module --- prod/syncthing/terraform/main.tf | 48 ++++------------ prod/syncthing/terraform/outputs.tf | 4 +- prod/wallabag/terraform/main.tf | 42 ++------------ prod/wallabag/terraform/outputs.tf | 4 +- terraform-modules/packer_droplet/interface.tf | 55 +++++++++++++++++++ terraform-modules/packer_droplet/main.tf | 43 +++++++++++++++ terraform-modules/packer_droplet/terraform.tf | 8 +++ 7 files changed, 127 insertions(+), 77 deletions(-) create mode 100644 terraform-modules/packer_droplet/interface.tf create mode 100644 terraform-modules/packer_droplet/main.tf create mode 100644 terraform-modules/packer_droplet/terraform.tf diff --git a/prod/syncthing/terraform/main.tf b/prod/syncthing/terraform/main.tf index 82e7676..a89efca 100644 --- a/prod/syncthing/terraform/main.tf +++ b/prod/syncthing/terraform/main.tf @@ -4,43 +4,12 @@ provider "digitalocean" { spaces_secret_key = var.spaces_secret_key } -data "terraform_remote_state" "do_ssh_keys" { - backend = "s3" - - config = { - skip_credentials_validation = true - skip_metadata_api_check = true - access_key = var.spaces_access_id - secret_key = var.spaces_secret_key - region = "us-east-1" - endpoint = "nyc3.digitaloceanspaces.com" - bucket = "jdormit-tf-state" - key = "mgmt/do-ssh-keys.tfstate" - } -} - -data "local_file" "packer_manifest" { - filename = "${path.module}/../packer/packer-manifest.json" -} - -locals { - packer_manifest = jsondecode(data.local_file.packer_manifest.content) - packer_builds = local.packer_manifest.builds - packer_image_str = element(local.packer_builds, length(local.packer_builds) - 1).artifact_id - packer_image = split(":", local.packer_image_str)[1] -} - -resource "digitalocean_droplet" "syncthing" { - name = "syncthing" - image = local.packer_image - region = "nyc1" - size = "s-1vcpu-1gb" - backups = true - volume_ids = [digitalocean_volume.syncthing_volume.id] - ssh_keys = [ - data.terraform_remote_state.do_ssh_keys.outputs.jdormit_laptop_ssh_key_id, - data.terraform_remote_state.do_ssh_keys.outputs.jdormit_macbook_lola_ssh_key_id - ] +module "packer_droplet" { + source = "../../../terraform-modules/packer_droplet" + name = "syncthing" + do_token = var.do_token + spaces_access_id = var.spaces_access_id + spaces_secret_key = var.spaces_secret_key } resource "digitalocean_volume" "syncthing_volume" { @@ -55,3 +24,8 @@ resource "digitalocean_volume" "syncthing_volume" { prevent_destroy = true } } + +resource "digitalocean_volume_attachment" "syncthing" { + droplet_id = module.packer_droplet.droplet_id + volume_id = digitalocean_volume.syncthing_volume.id +} diff --git a/prod/syncthing/terraform/outputs.tf b/prod/syncthing/terraform/outputs.tf index cc7cf3f..0a27054 100644 --- a/prod/syncthing/terraform/outputs.tf +++ b/prod/syncthing/terraform/outputs.tf @@ -1,9 +1,9 @@ output "ip_address" { - value = digitalocean_droplet.syncthing.ipv4_address + value = module.packer_droplet.droplet_ip_address } output "urn" { - value = digitalocean_droplet.syncthing.urn + value = module.packer_droplet.droplet_urn } output "volume_urn" { diff --git a/prod/wallabag/terraform/main.tf b/prod/wallabag/terraform/main.tf index f1fa3f3..cf416da 100644 --- a/prod/wallabag/terraform/main.tf +++ b/prod/wallabag/terraform/main.tf @@ -4,40 +4,10 @@ provider "digitalocean" { spaces_secret_key = var.spaces_secret_key } -data "terraform_remote_state" "do_ssh_keys" { - backend = "s3" - - config = { - skip_credentials_validation = true - skip_metadata_api_check = true - access_key = var.spaces_access_id - secret_key = var.spaces_secret_key - region = "us-east-1" - endpoint = "nyc3.digitaloceanspaces.com" - bucket = "jdormit-tf-state" - key = "mgmt/do-ssh-keys.tfstate" - } -} - -data "local_file" "packer_manifest" { - filename = "${path.module}/../packer/packer-manifest.json" -} - -locals { - packer_manifest = jsondecode(data.local_file.packer_manifest.content) - packer_builds = local.packer_manifest.builds - packer_image_str = element(local.packer_builds, length(local.packer_builds) - 1).artifact_id - packer_image = split(":", local.packer_image_str)[1] -} - -resource "digitalocean_droplet" "wallabag" { - name = "wallabag" - image = local.packer_image - region = "nyc1" - size = "s-1vcpu-1gb" - backups = true - ssh_keys = [ - data.terraform_remote_state.do_ssh_keys.outputs.jdormit_laptop_ssh_key_id, - data.terraform_remote_state.do_ssh_keys.outputs.jdormit_macbook_lola_ssh_key_id - ] +module "packer_droplet" { + source = "../../../terraform-modules/packer_droplet" + name = "wallabag" + do_token = var.do_token + spaces_access_id = var.spaces_access_id + spaces_secret_key = var.spaces_secret_key } diff --git a/prod/wallabag/terraform/outputs.tf b/prod/wallabag/terraform/outputs.tf index fe22f0f..d92b685 100644 --- a/prod/wallabag/terraform/outputs.tf +++ b/prod/wallabag/terraform/outputs.tf @@ -1,7 +1,7 @@ output "ip_address" { - value = digitalocean_droplet.wallabag.ipv4_address + value = module.packer_droplet.droplet_ip_address } output "urn" { - value = digitalocean_droplet.wallabag.urn + value = module.packer_droplet.droplet_urn } diff --git a/terraform-modules/packer_droplet/interface.tf b/terraform-modules/packer_droplet/interface.tf new file mode 100644 index 0000000..82c8d31 --- /dev/null +++ b/terraform-modules/packer_droplet/interface.tf @@ -0,0 +1,55 @@ +variable "do_token" { + type = string +} + +variable "spaces_access_id" { + type = string +} + +variable "spaces_secret_key" { + type = string +} + +variable "name" { + description = "Droplet name" + type = string +} + +variable "packer_manifest" { + description = "Path to the packer manifest JSON file" + type = string + default = "../packer/packer-manifest.json" +} + +variable "region" { + description = "Droplet region" + type = string + default = "nyc1" +} + +variable "droplet_size" { + description = "Droplet size descriptor" + type = string + default = "s-1vcpu-1gb" +} + +variable "backups" { + description = "Whether to enable droplet backups" + type = bool + default = true +} + +output "droplet_id" { + description = "Droplet ID" + value = digitalocean_droplet.droplet.id +} + +output "droplet_ip_address" { + description = "Droplet IPv4 address" + value = digitalocean_droplet.droplet.ipv4_address +} + +output "droplet_urn" { + description = "Droplet URN" + value = digitalocean_droplet.droplet.urn +} diff --git a/terraform-modules/packer_droplet/main.tf b/terraform-modules/packer_droplet/main.tf new file mode 100644 index 0000000..bf437fd --- /dev/null +++ b/terraform-modules/packer_droplet/main.tf @@ -0,0 +1,43 @@ +provider "digitalocean" { + token = var.do_token + spaces_access_id = var.spaces_access_id + spaces_secret_key = var.spaces_secret_key +} + +data "terraform_remote_state" "do_ssh_keys" { + backend = "s3" + + config = { + skip_credentials_validation = true + skip_metadata_api_check = true + access_key = var.spaces_access_id + secret_key = var.spaces_secret_key + region = "us-east-1" + endpoint = "nyc3.digitaloceanspaces.com" + bucket = "jdormit-tf-state" + key = "mgmt/do-ssh-keys.tfstate" + } +} + +data "local_file" "packer_manifest" { + filename = "${path.root}/${var.packer_manifest}" +} + +locals { + packer_manifest = jsondecode(data.local_file.packer_manifest.content) + packer_builds = local.packer_manifest.builds + packer_image_str = element(local.packer_builds, length(local.packer_builds) - 1).artifact_id + packer_image = split(":", local.packer_image_str)[1] +} + +resource "digitalocean_droplet" "droplet" { + name = var.name + image = local.packer_image + region = var.region + size = var.droplet_size + backups = var.backups + ssh_keys = [ + data.terraform_remote_state.do_ssh_keys.outputs.jdormit_laptop_ssh_key_id, + data.terraform_remote_state.do_ssh_keys.outputs.jdormit_macbook_lola_ssh_key_id + ] +} diff --git a/terraform-modules/packer_droplet/terraform.tf b/terraform-modules/packer_droplet/terraform.tf new file mode 100644 index 0000000..141009b --- /dev/null +++ b/terraform-modules/packer_droplet/terraform.tf @@ -0,0 +1,8 @@ +terraform { + required_providers { + digitalocean = { + source = "digitalocean/digitalocean" + version = "~> 2.3.0" + } + } +}