From c6602fc237c3d2590ab3f2060fed9f7f9be1b4e0 Mon Sep 17 00:00:00 2001 From: "juvenal.diaz" Date: Fri, 23 Oct 2026 10:00:22 -0600 Subject: [PATCH] feat: add opt-in OCI email VM pipeline --- bootstrap/email/README.md | 32 ++++++++ bootstrap/email/main.tf | 36 +++++++++ bootstrap/email/outputs.tf | 29 ++++++++ bootstrap/email/variables.tf | 128 ++++++++++++++++++++++++++++++++ bootstrap/email/versions.tf | 15 ++++ cmd/jeannie-server/main.go | 1 + cmd/jeannie-server/main_test.go | 6 ++ jeannie | 4 + lib/jeannie/commands.sh | 41 ++++++++++ lib/jeannie/env.sh | 20 +++++ 10 files changed, 312 insertions(+) create mode 100644 bootstrap/email/README.md create mode 100644 bootstrap/email/main.tf create mode 100644 bootstrap/email/outputs.tf create mode 100644 bootstrap/email/variables.tf create mode 100644 bootstrap/email/versions.tf diff --git a/bootstrap/email/README.md b/bootstrap/email/README.md new file mode 100644 index 0000000..abb786c --- /dev/null +++ b/bootstrap/email/README.md @@ -0,0 +1,32 @@ +# OCI email learning VM + +This is an independent, opt-in OpenTofu stack. It creates one OCI compute VM +inside an existing compartment and subnet; it does not install a mail server, +change DNS, or open SMTP ports. Keep `enabled=false` until the OCI image, +network, and SSH inputs are known. + +The default `VM.Standard.A1.Flex` shape is suitable for an ARM64 Ampere image +and may fit OCI Always Free capacity. Capacity is not guaranteed, so override +the shape and image when needed. + +Example on the Debian pipeline host: + +```sh +export LAB_EMAIL_VM_ENABLED=true +export LAB_EMAIL_OCI_REGION=us-phoenix-1 +export LAB_EMAIL_COMPARTMENT_ID=ocid1.compartment... +export LAB_EMAIL_AVAILABILITY_DOMAIN='...' +export LAB_EMAIL_SUBNET_ID=ocid1.subnet... +export LAB_EMAIL_IMAGE_ID=ocid1.image... +export LAB_EMAIL_SSH_PUBLIC_KEY="$(cat ~/.ssh/id_ed25519.pub)" +export LAB_EMAIL_DOMAIN=example.org +# Optional existing NSGs, as a JSON list. Do not open mail ingress yet. +export LAB_EMAIL_NSG_IDS='["ocid1.networksecuritygroup..."]' +./jeannie email-vm plan +./jeannie email-vm apply +``` + +The provider reads OCI credentials from the normal OCI CLI configuration +(`~/.oci/config`) and never stores private keys in this repository. `destroy` +requires `--yes`. The stack is intentionally separate from `platform`, `apps`, +and `up`, so mail experiments cannot modify the Kubernetes cluster. diff --git a/bootstrap/email/main.tf b/bootstrap/email/main.tf new file mode 100644 index 0000000..f10fe8a --- /dev/null +++ b/bootstrap/email/main.tf @@ -0,0 +1,36 @@ +resource "oci_core_instance" "email" { + count = var.enabled ? 1 : 0 + + availability_domain = var.availability_domain + compartment_id = var.compartment_id + display_name = var.display_name + shape = var.shape + + shape_config { + ocpus = var.ocpus + memory_in_gbs = var.memory_in_gbs + } + + source_details { + source_type = "image" + source_id = var.image_id + boot_volume_size_in_gbs = var.boot_volume_size_in_gbs + } + + create_vnic_details { + subnet_id = var.subnet_id + assign_public_ip = var.assign_public_ip + hostname_label = var.hostname_label + skip_source_dest_check = false + nsg_ids = var.nsg_ids + } + + metadata = { + ssh_authorized_keys = var.ssh_public_key + } + + freeform_tags = merge(var.freeform_tags, { + managed_by = "jeannie" + purpose = "email-learning" + }) +} diff --git a/bootstrap/email/outputs.tf b/bootstrap/email/outputs.tf new file mode 100644 index 0000000..defd757 --- /dev/null +++ b/bootstrap/email/outputs.tf @@ -0,0 +1,29 @@ +output "instance_id" { + value = try(oci_core_instance.email[0].id, null) +} + +output "private_ip" { + value = try(oci_core_instance.email[0].private_ip, null) +} + +output "public_ip" { + value = try(oci_core_instance.email[0].public_ip, null) +} + +output "domain" { + value = var.domain +} + +output "ssh_command" { + value = try("ssh opc@${oci_core_instance.email[0].public_ip}", null) +} + +output "mail_prerequisites" { + value = [ + "Use a reserved public IP before production mail.", + "Request OCI reverse DNS/PTR for the public IP.", + "Request OCI SMTP port 25 access; it is restricted by default for newer tenancies.", + "Create SPF, DKIM, DMARC, MX, and A/AAAA records for the chosen domain.", + "Open mail ports only in an explicit NSG/security-list change after DNS and hardening are ready.", + ] +} diff --git a/bootstrap/email/variables.tf b/bootstrap/email/variables.tf new file mode 100644 index 0000000..4446618 --- /dev/null +++ b/bootstrap/email/variables.tf @@ -0,0 +1,128 @@ +variable "enabled" { + description = "Create the learning email VM. Keep false until all OCI inputs are set." + type = bool + default = false +} + +variable "region" { + description = "OCI region, normally supplied by TF_VAR_email_region or OCI_REGION." + type = string + default = "" +} + +variable "oci_config_profile" { + description = "OCI CLI config profile used by the provider." + type = string + default = "DEFAULT" +} + +variable "compartment_id" { + description = "OCI compartment OCID for the VM." + type = string + default = "" +} + +variable "availability_domain" { + description = "OCI availability domain name." + type = string + default = "" +} + +variable "subnet_id" { + description = "Existing public subnet OCID for the VM VNIC." + type = string + default = "" +} + +variable "image_id" { + description = "ARM64 or x86 image OCID compatible with the selected shape." + type = string + default = "" +} + +variable "ssh_public_key" { + description = "Public SSH key installed in the VM." + type = string + default = "" + sensitive = true +} + +variable "shape" { + description = "OCI shape. VM.Standard.A1.Flex is free-tier eligible when capacity is available." + type = string + default = "VM.Standard.A1.Flex" +} + +variable "ocpus" { + description = "Number of OCPUs. Keep within the free-tier allowance." + type = number + default = 1 +} + +variable "memory_in_gbs" { + description = "Memory in GB. Keep within the free-tier allowance." + type = number + default = 6 +} + +variable "boot_volume_size_in_gbs" { + description = "Boot volume size in GB." + type = number + default = 50 +} + +variable "display_name" { + type = string + default = "homelab-mail" +} + +variable "hostname_label" { + type = string + default = "homelab-mail" +} + +variable "assign_public_ip" { + description = "Assign an ephemeral public IP to the VM VNIC. Reserve a static IP before production mail use." + type = bool + default = true +} + +variable "nsg_ids" { + description = "Existing OCI Network Security Group OCIDs. Keep mail ingress closed until DNS/PTR are ready." + type = list(string) + default = [] +} + +variable "domain" { + description = "Future mail domain, recorded in outputs only until explicitly configured." + type = string + default = "" +} + +variable "freeform_tags" { + type = map(string) + default = {} +} + +locals { + required_inputs = { + compartment_id = var.compartment_id + availability_domain = var.availability_domain + subnet_id = var.subnet_id + image_id = var.image_id + ssh_public_key = var.ssh_public_key + region = var.region + } +} + +check "email_vm_inputs" { + assert { + condition = !var.enabled || alltrue([for value in values(local.required_inputs) : trimspace(value) != ""]) + error_message = "When email VM creation is enabled, region, compartment_id, availability_domain, subnet_id, image_id, and ssh_public_key are required." + } + + assert { + condition = var.ocpus > 0 && var.memory_in_gbs > 0 && var.boot_volume_size_in_gbs >= 50 + error_message = "ocpus and memory_in_gbs must be positive and boot_volume_size_in_gbs must be at least 50." + } +} diff --git a/bootstrap/email/versions.tf b/bootstrap/email/versions.tf new file mode 100644 index 0000000..bba9956 --- /dev/null +++ b/bootstrap/email/versions.tf @@ -0,0 +1,15 @@ +terraform { + required_version = ">= 1.5.0" + + required_providers { + oci = { + source = "oracle/oci" + version = "~> 6.0" + } + } +} + +provider "oci" { + region = var.region + config_file_profile = var.oci_config_profile +} diff --git a/cmd/jeannie-server/main.go b/cmd/jeannie-server/main.go index 80d0b5c..30bc6f9 100644 --- a/cmd/jeannie-server/main.go +++ b/cmd/jeannie-server/main.go @@ -63,6 +63,7 @@ var allowedCommands = map[string]bool{ "state-backup": true, "fix-debian-docker-root": true, "state": true, "pimox": true, + "email-vm": true, "secrets-init": true, "secrets-check": true, "tailnet-policy-check": true, "secrets": true, "ai-index": true, "ai-check": true, "ask": true, "ai-evals": true, diff --git a/cmd/jeannie-server/main_test.go b/cmd/jeannie-server/main_test.go index 9b33a85..c39a61a 100644 --- a/cmd/jeannie-server/main_test.go +++ b/cmd/jeannie-server/main_test.go @@ -141,3 +141,9 @@ func TestPimoxCommandIsRemotelyEnabled(t *testing.T) { t.Fatal("pimox must be exposed through the remote API") } } + +func TestEmailVMCommandIsRemotelyEnabled(t *testing.T) { + if !allowedCommands["email-vm"] { + t.Fatal("email-vm must be exposed through the remote API") + } +} diff --git a/jeannie b/jeannie index 7d1d6a5..3669e2b 100755 --- a/jeannie +++ b/jeannie @@ -122,6 +122,7 @@ Observability And GitOps platform [all|calico|metrics|dns|metallb|ingress|storage|argocd|security|logging|monitoring|tools] Deploy one platform component or the full platform stack. edge Deploy/check the OCI edge stack only. + email-vm {status|plan|apply|destroy --yes} Manage the independent OCI email learning VM. edge-cache-flush Clear Nginx, Varnish, and Squid edge caches. promote {plan|validate|rollback APP} Run canary promotion gates and rollback plan. cert-check Check public DNS, TLS, and edge URL health. @@ -287,6 +288,9 @@ case "${1:-}" in run_step "Edge OpenTofu apply" edge_apply echo "Log: ${JEANNIE_LOG_FILE}" ;; + email-vm) + email_vm_manage "${@:2}" + ;; edge-cache-flush) edge_flush_caches ;; diff --git a/lib/jeannie/commands.sh b/lib/jeannie/commands.sh index 3f0bb27..a2699ae 100644 --- a/lib/jeannie/commands.sh +++ b/lib/jeannie/commands.sh @@ -3286,6 +3286,47 @@ pimox_oob() { "${REPO_ROOT}/scripts/pimox-oob" "$@" } +email_vm_manage() { + require_debian_server "email-vm" + export_email_vm_tf_vars + local action="${1:-status}" + shift || true + local stack="${REPO_ROOT}/bootstrap/email" + + case "${action}" in + status) + tofu -chdir="${stack}" show -no-color 2>/dev/null || echo "OCI email VM has no state yet." + ;; + plan|apply) + if [[ "${TF_VAR_enabled:-false}" != "true" ]]; then + echo "OCI email VM is disabled. Set LAB_EMAIL_VM_ENABLED=true and the required LAB_EMAIL_* inputs first." >&2 + return 2 + fi + tofu -chdir="${stack}" init + if [[ "${action}" == "plan" ]]; then + tofu -chdir="${stack}" plan "$@" + else + record_change_journal "email-vm" "${action}" "$@" + tofu -chdir="${stack}" apply "$@" -auto-approve + fi + ;; + destroy) + if [[ "${1:-}" != "--yes" ]]; then + echo "Refusing to destroy the OCI email VM without --yes." >&2 + return 2 + fi + shift + record_change_journal "email-vm" destroy "$@" + tofu -chdir="${stack}" init + tofu -chdir="${stack}" destroy "$@" -auto-approve + ;; + *) + echo "Usage: ./jeannie email-vm {status|plan|apply|destroy --yes}" >&2 + return 2 + ;; + esac +} + storage_status() { require_debian_server "storage" local kubeconfig="${KUBECONFIG_PATH}" diff --git a/lib/jeannie/env.sh b/lib/jeannie/env.sh index 627fd89..dc3949f 100644 --- a/lib/jeannie/env.sh +++ b/lib/jeannie/env.sh @@ -304,6 +304,26 @@ export_homelab_inventory_tf_vars() { export_if_unset LAB_PIMOX_WORKER_STATIC_IPS "${LAB_CLUSTER_PIMOX_WORKER_STATIC_IPS:-}" } +export_email_vm_tf_vars() { + export_if_unset TF_VAR_enabled "${LAB_EMAIL_VM_ENABLED:-}" + export_if_unset TF_VAR_region "${LAB_EMAIL_OCI_REGION:-}" + export_if_unset TF_VAR_oci_config_profile "${LAB_EMAIL_OCI_CONFIG_PROFILE:-}" + export_if_unset TF_VAR_compartment_id "${LAB_EMAIL_COMPARTMENT_ID:-}" + export_if_unset TF_VAR_availability_domain "${LAB_EMAIL_AVAILABILITY_DOMAIN:-}" + export_if_unset TF_VAR_subnet_id "${LAB_EMAIL_SUBNET_ID:-}" + export_if_unset TF_VAR_image_id "${LAB_EMAIL_IMAGE_ID:-}" + export_if_unset TF_VAR_ssh_public_key "${LAB_EMAIL_SSH_PUBLIC_KEY:-}" + export_if_unset TF_VAR_shape "${LAB_EMAIL_SHAPE:-}" + export_if_unset TF_VAR_ocpus "${LAB_EMAIL_OCPUS:-}" + export_if_unset TF_VAR_memory_in_gbs "${LAB_EMAIL_MEMORY_IN_GBS:-}" + export_if_unset TF_VAR_boot_volume_size_in_gbs "${LAB_EMAIL_BOOT_VOLUME_SIZE_IN_GBS:-}" + export_if_unset TF_VAR_display_name "${LAB_EMAIL_DISPLAY_NAME:-}" + export_if_unset TF_VAR_hostname_label "${LAB_EMAIL_HOSTNAME_LABEL:-}" + export_if_unset TF_VAR_assign_public_ip "${LAB_EMAIL_ASSIGN_PUBLIC_IP:-}" + export_if_unset TF_VAR_nsg_ids "${LAB_EMAIL_NSG_IDS:-}" + export_if_unset TF_VAR_domain "${LAB_EMAIL_DOMAIN:-}" +} + sops_available() { command -v sops >/dev/null 2>&1 }