From 6daa04860d7b6f0be7d5ef3f07165fff6b72b9f1 Mon Sep 17 00:00:00 2001 From: Peter Pouliot Date: Mon, 20 May 2024 13:12:19 -0400 Subject: [PATCH] metadata cleanup --- images.tf | 29 ++++++++++++++++++++++-- main.tf | 17 ++++++++++++++ metadata.tf | 55 ---------------------------------------------- variables.tf | 4 ++-- virtual_machine.tf | 4 +++- 5 files changed, 49 insertions(+), 60 deletions(-) delete mode 100644 metadata.tf diff --git a/images.tf b/images.tf index d664ef5..31a876c 100644 --- a/images.tf +++ b/images.tf @@ -1,15 +1,30 @@ # Azure Aarch64 Images locals { os_images = { + rockylinux8 = { + os_image = "rocky-linux-8-optimized-gcp-arm64" + default_os_user = "rockylinux" + startup_script = file("${local.startup_script_template_file}") + } rockylinux9 = { os_image = "rocky-linux-9-arm64" default_os_user = "rockylinux" - startup_script = data.template_file.startup_script.rendered + startup_script = file("${local.startup_script_template_file}") } debian11 = { os_image = "debian-11-arm64" default_os_user = "debian" - startup_script = data.template_file.startup_script.rendered + startup_script = file("${local.startup_script_template_file}") + } + debian12 = { + os_image = "debian-12-arm64" + default_os_user = "debian" + startup_script = file("${local.startup_script_template_file}") + } + fedoracoreos = { + os_image = "fedora-coreos-stable-arm64" + default_os_user = "fedora" + startup_script = null } ubuntu1804 = { os_image = "ubuntu-1804-lts-arm64" @@ -26,5 +41,15 @@ locals { default_os_user = "ubuntu" startup_script = null } + ubuntu2404 = { + os_image = "ubuntu-2404-lts-arm64" + default_os_user = "ubuntu" + startup_script = null + } + sles15 = { + os_image = "sles-15-arm64" + default_os_user = "suse" + startup_script = null + } } } diff --git a/main.tf b/main.tf index 9fb9954..c0b0aa3 100644 --- a/main.tf +++ b/main.tf @@ -1,3 +1,12 @@ + +# Cloud-Init file +locals { + # return var.cloud_init_template_path if it's not null + # otherwise return "${path.module}/templates/cloud-init.yaml.tpl" + cloud_init_template_file = coalesce(var.cloud_init_template_file, "${path.module}/templates/cloud-init.yaml.tpl") + startup_script_template_file = coalesce(var.startup_script_template_file, "${path.module}/templates/startup-script.sh.tpl") +} + # ssh keys resource "tls_private_key" "gcp" { algorithm = "RSA" @@ -34,3 +43,11 @@ output "random_uuid" { value = random_uuid.random_id.result sensitive = false } + +resource "google_os_login_ssh_public_key" "cache" { + user = data.google_client_openid_userinfo.me.email +# key = tls_private_key.gcp.public_key_openssh + key = "${replace(tls_private_key.gcp.public_key_openssh, "\n", "")} ${split("@", data.google_client_openid_userinfo.me.email)[0]}" + project = var.project_id +} + diff --git a/metadata.tf b/metadata.tf deleted file mode 100644 index 3e4077f..0000000 --- a/metadata.tf +++ /dev/null @@ -1,55 +0,0 @@ -# Cloud Init Metadata - - -locals { - # return var.cloud_init_template_path if it's not null - # otherwise return "${path.module}/templates/cloud-init.yaml.tpl" - cloud_init_template_file = coalesce(var.cloud_init_template_file, "${path.module}/templates/cloud-init.yaml.tpl") - startup_script_template_file = coalesce(var.startup_script_template_file, "${path.module}/templates/startup-script.sh.tpl") -} - -data "template_file" "cloud_config" { -# template = file("${path.module}/templates/cloud-init.yaml.tpl") - template = file("${local.cloud_init_template_file}") -} - -# Output rendered cloud-init metadata -output "cloud_init" { - value = data.template_file.cloud_config.rendered - sensitive = false -} - -data "template_file" "startup_script" { -# template = file("${path.module}/templates/startup-script.sh.tpl") - template = file("${local.startup_script_template_file}") -} - -# Output rendered startup script -output "startup_script" { - value = data.template_file.startup_script.rendered - sensitive = false -} - -data "cloudinit_config" "cloud_config" { - gzip = false - base64_encode = false - - part { - content_type = "text/cloud-config" - content = data.template_file.cloud_config.rendered - filename = "cloud.cfg" - } -} - -#resource "google_compute_project_metadata_item" "ssh-keys" { -# key = "ssh-keys" -# value = tls_private_key.gcp.public_key_openssh -# value = "${split("@", data.google_client_openid_userinfo.me.email)[0]}:${replace(tls_private_key.gcp.public_key_openssh, "\n", "")} ${split("@", data.google_client_openid_userinfo.me.email)[0]}" -#} - -resource "google_os_login_ssh_public_key" "cache" { - user = data.google_client_openid_userinfo.me.email -# key = tls_private_key.gcp.public_key_openssh - key = "${replace(tls_private_key.gcp.public_key_openssh, "\n", "")} ${split("@", data.google_client_openid_userinfo.me.email)[0]}" - project = var.project_id -} diff --git a/variables.tf b/variables.tf index 7d9531b..62ca3fe 100644 --- a/variables.tf +++ b/variables.tf @@ -62,8 +62,8 @@ variable "gcp_vcn_cidr_subnet" { } variable "gcp_os_image" { -# default = "ubuntu2204" + default = "ubuntu2404" # default = "rockylinux9" - default = "debian11" +# default = "debian11" description = "Default OS Image From the Local Vars" } diff --git a/virtual_machine.tf b/virtual_machine.tf index 3fda990..2e3fdde 100644 --- a/virtual_machine.tf +++ b/virtual_machine.tf @@ -12,7 +12,9 @@ resource "google_compute_instance" "default" { metadata = { enable-oslogin = true ssh-keys = "${split("@", data.google_client_openid_userinfo.me.email)[0]}:${replace(tls_private_key.gcp.public_key_openssh, "\n", "")} ${split("@", data.google_client_openid_userinfo.me.email)[0]}" - user-data = data.template_file.cloud_config.rendered +# user-data = data.template_file.cloud_config.rendered +# user_data = "${base64encode(file("${local.cloud_init_template_file}"))}" + user_data = "${file("${local.cloud_init_template_file}")}" startup-script = local.os_images[var.gcp_os_image].startup_script } boot_disk {