From c06ef5e0b7028356a320c36af75d02e2c42e71df Mon Sep 17 00:00:00 2001 From: "juvenal.diaz" Date: Fri, 23 Oct 2026 10:07:07 -0600 Subject: [PATCH] feat: add opt-in OCI foundation pipeline --- bootstrap/oci/README.md | 17 ++ bootstrap/oci/main.tf | 277 ++++++++++++++++++++++++++++++++ bootstrap/oci/outputs.tf | 17 ++ bootstrap/oci/variables.tf | 204 +++++++++++++++++++++++ bootstrap/oci/versions.tf | 15 ++ cmd/jeannie-server/main.go | 1 + cmd/jeannie-server/main_test.go | 6 + jeannie | 5 + lib/jeannie/commands.sh | 41 +++++ lib/jeannie/env.sh | 27 ++++ 10 files changed, 610 insertions(+) create mode 100644 bootstrap/oci/README.md create mode 100644 bootstrap/oci/main.tf create mode 100644 bootstrap/oci/outputs.tf create mode 100644 bootstrap/oci/variables.tf create mode 100644 bootstrap/oci/versions.tf diff --git a/bootstrap/oci/README.md b/bootstrap/oci/README.md new file mode 100644 index 0000000..4db618e --- /dev/null +++ b/bootstrap/oci/README.md @@ -0,0 +1,17 @@ +# OCI foundation + +This independent, opt-in stack creates the OCI foundation for the learning +environment: a child compartment (or uses an existing one), VCN, internet and +NAT gateways, public/private subnets, NSGs, and separate edge and email VMs. +It does not configure the operating systems, install a mail server, create DNS +records, or open mail ingress unless `open_mail_ports=true` is explicitly set. + +Credentials come from the standard OCI CLI configuration (`~/.oci/config`); +private keys are not stored in the repository. Use `./jeannie oci-foundation +plan` first, inspect the plan, then apply from the Debian pipeline host. + +At minimum, set `LAB_OCI_FOUNDATION_ENABLED=true`, `LAB_OCI_REGION`, +`LAB_OCI_TENANCY_ID`, `LAB_OCI_ADMIN_CIDR`, `LAB_OCI_SSH_PUBLIC_KEY`, and +the image and availability-domain variables for both VMs. Use +`LAB_OCI_CREATE_COMPARTMENT=false` with `LAB_OCI_COMPARTMENT_ID` when the +tenancy policy does not allow creating child compartments. diff --git a/bootstrap/oci/main.tf b/bootstrap/oci/main.tf new file mode 100644 index 0000000..d423dbd --- /dev/null +++ b/bootstrap/oci/main.tf @@ -0,0 +1,277 @@ +locals { + target_compartment_id = var.enabled && var.create_compartment ? oci_identity_compartment.homelab[0].id : var.compartment_id + public_route_table_id = try(oci_core_route_table.public[0].id, null) + private_route_table_id = try(oci_core_route_table.private[0].id, null) + base_tags = merge(var.freeform_tags, { managed_by = "jeannie", purpose = "homelab-learning" }) +} + +resource "oci_identity_compartment" "homelab" { + count = var.enabled && var.create_compartment ? 1 : 0 + compartment_id = var.tenancy_id + name = var.compartment_name + description = var.compartment_description + enable_delete = true + freeform_tags = local.base_tags +} + +resource "oci_core_vcn" "homelab" { + count = var.enabled ? 1 : 0 + compartment_id = local.target_compartment_id + cidr_blocks = [var.vcn_cidr] + display_name = var.vcn_display_name + dns_label = "homelab" + freeform_tags = local.base_tags +} + +resource "oci_core_internet_gateway" "homelab" { + count = var.enabled ? 1 : 0 + compartment_id = local.target_compartment_id + vcn_id = oci_core_vcn.homelab[0].id + display_name = "homelab-internet-gateway" + enabled = true + freeform_tags = local.base_tags +} + +resource "oci_core_nat_gateway" "homelab" { + count = var.enabled ? 1 : 0 + compartment_id = local.target_compartment_id + vcn_id = oci_core_vcn.homelab[0].id + display_name = "homelab-nat-gateway" + freeform_tags = local.base_tags +} + +resource "oci_core_route_table" "public" { + count = var.enabled ? 1 : 0 + compartment_id = local.target_compartment_id + vcn_id = oci_core_vcn.homelab[0].id + display_name = "homelab-public-routes" + route_rules { + destination = "0.0.0.0/0" + destination_type = "CIDR_BLOCK" + network_entity_id = oci_core_internet_gateway.homelab[0].id + } + freeform_tags = local.base_tags +} + +resource "oci_core_route_table" "private" { + count = var.enabled ? 1 : 0 + compartment_id = local.target_compartment_id + vcn_id = oci_core_vcn.homelab[0].id + display_name = "homelab-private-routes" + route_rules { + destination = "0.0.0.0/0" + destination_type = "CIDR_BLOCK" + network_entity_id = oci_core_nat_gateway.homelab[0].id + } + freeform_tags = local.base_tags +} + +resource "oci_core_subnet" "public" { + count = var.enabled ? 1 : 0 + compartment_id = local.target_compartment_id + vcn_id = oci_core_vcn.homelab[0].id + cidr_block = var.public_subnet_cidr + display_name = "homelab-public-subnet" + dns_label = "public" + route_table_id = local.public_route_table_id + prohibit_public_ip_on_vnic = false + freeform_tags = local.base_tags +} + +resource "oci_core_subnet" "private" { + count = var.enabled ? 1 : 0 + compartment_id = local.target_compartment_id + vcn_id = oci_core_vcn.homelab[0].id + cidr_block = var.private_subnet_cidr + display_name = "homelab-private-subnet" + dns_label = "private" + route_table_id = local.private_route_table_id + prohibit_public_ip_on_vnic = true + freeform_tags = local.base_tags +} + +resource "oci_core_network_security_group" "edge" { + count = var.enabled && var.edge_enabled ? 1 : 0 + compartment_id = local.target_compartment_id + vcn_id = oci_core_vcn.homelab[0].id + display_name = "homelab-edge-nsg" + freeform_tags = local.base_tags +} + +resource "oci_core_network_security_group" "email" { + count = var.enabled && var.email_enabled ? 1 : 0 + compartment_id = local.target_compartment_id + vcn_id = oci_core_vcn.homelab[0].id + display_name = "homelab-email-nsg" + freeform_tags = local.base_tags +} + +resource "oci_core_network_security_group_security_rule" "edge_ssh" { + count = var.enabled && var.edge_enabled ? 1 : 0 + network_security_group_id = oci_core_network_security_group.edge[0].id + direction = "INGRESS" + protocol = "6" + source = var.admin_cidr + source_type = "CIDR_BLOCK" + tcp_options { + destination_port_range { + min = 22 + max = 22 + } + } +} + +resource "oci_core_network_security_group_security_rule" "edge_web" { + count = var.enabled && var.edge_enabled ? 1 : 0 + network_security_group_id = oci_core_network_security_group.edge[0].id + direction = "INGRESS" + protocol = "6" + source = "0.0.0.0/0" + source_type = "CIDR_BLOCK" + tcp_options { + destination_port_range { + min = 80 + max = 80 + } + } +} + +resource "oci_core_network_security_group_security_rule" "edge_https" { + count = var.enabled && var.edge_enabled ? 1 : 0 + network_security_group_id = oci_core_network_security_group.edge[0].id + direction = "INGRESS" + protocol = "6" + source = "0.0.0.0/0" + source_type = "CIDR_BLOCK" + tcp_options { + destination_port_range { + min = 443 + max = 443 + } + } +} + +resource "oci_core_network_security_group_security_rule" "email_ssh" { + count = var.enabled && var.email_enabled ? 1 : 0 + network_security_group_id = oci_core_network_security_group.email[0].id + direction = "INGRESS" + protocol = "6" + source = var.admin_cidr + source_type = "CIDR_BLOCK" + tcp_options { + destination_port_range { + min = 22 + max = 22 + } + } +} + +resource "oci_core_network_security_group_security_rule" "email_ports" { + count = var.enabled && var.email_enabled && var.open_mail_ports ? 1 : 0 + network_security_group_id = oci_core_network_security_group.email[0].id + direction = "INGRESS" + protocol = "6" + source = "0.0.0.0/0" + source_type = "CIDR_BLOCK" + tcp_options { + destination_port_range { + min = 25 + max = 25 + } + } +} + +resource "oci_core_network_security_group_security_rule" "email_submission" { + count = var.enabled && var.email_enabled && var.open_mail_ports ? 1 : 0 + network_security_group_id = oci_core_network_security_group.email[0].id + direction = "INGRESS" + protocol = "6" + source = "0.0.0.0/0" + source_type = "CIDR_BLOCK" + tcp_options { + destination_port_range { + min = 465 + max = 465 + } + } +} + +resource "oci_core_network_security_group_security_rule" "email_submission_starttls" { + count = var.enabled && var.email_enabled && var.open_mail_ports ? 1 : 0 + network_security_group_id = oci_core_network_security_group.email[0].id + direction = "INGRESS" + protocol = "6" + source = "0.0.0.0/0" + source_type = "CIDR_BLOCK" + tcp_options { + destination_port_range { + min = 587 + max = 587 + } + } +} + +resource "oci_core_network_security_group_security_rule" "email_imaps" { + count = var.enabled && var.email_enabled && var.open_mail_ports ? 1 : 0 + network_security_group_id = oci_core_network_security_group.email[0].id + direction = "INGRESS" + protocol = "6" + source = "0.0.0.0/0" + source_type = "CIDR_BLOCK" + tcp_options { + destination_port_range { + min = 993 + max = 993 + } + } +} + +resource "oci_core_instance" "edge" { + count = var.enabled && var.edge_enabled ? 1 : 0 + availability_domain = var.edge_availability_domain + compartment_id = local.target_compartment_id + display_name = var.edge_display_name + shape = var.edge_shape + shape_config { + ocpus = var.edge_ocpus + memory_in_gbs = var.edge_memory_in_gbs + } + source_details { + source_type = "image" + source_id = var.edge_image_id + boot_volume_size_in_gbs = var.edge_boot_volume_size_in_gbs + } + create_vnic_details { + subnet_id = oci_core_subnet.public[0].id + assign_public_ip = true + hostname_label = var.edge_hostname_label + nsg_ids = [oci_core_network_security_group.edge[0].id] + } + metadata = { ssh_authorized_keys = var.ssh_public_key } + freeform_tags = merge(local.base_tags, { role = "edge" }) +} + +resource "oci_core_instance" "email" { + count = var.enabled && var.email_enabled ? 1 : 0 + availability_domain = var.email_availability_domain + compartment_id = local.target_compartment_id + display_name = var.email_display_name + shape = var.email_shape + shape_config { + ocpus = var.email_ocpus + memory_in_gbs = var.email_memory_in_gbs + } + source_details { + source_type = "image" + source_id = var.email_image_id + boot_volume_size_in_gbs = var.email_boot_volume_size_in_gbs + } + create_vnic_details { + subnet_id = oci_core_subnet.public[0].id + assign_public_ip = true + hostname_label = var.email_hostname_label + nsg_ids = [oci_core_network_security_group.email[0].id] + } + metadata = { ssh_authorized_keys = var.ssh_public_key } + freeform_tags = merge(local.base_tags, { role = "email" }) +} diff --git a/bootstrap/oci/outputs.tf b/bootstrap/oci/outputs.tf new file mode 100644 index 0000000..1c0d627 --- /dev/null +++ b/bootstrap/oci/outputs.tf @@ -0,0 +1,17 @@ +output "compartment_id" { value = local.target_compartment_id } +output "vcn_id" { value = try(oci_core_vcn.homelab[0].id, null) } +output "public_subnet_id" { value = try(oci_core_subnet.public[0].id, null) } +output "private_subnet_id" { value = try(oci_core_subnet.private[0].id, null) } +output "edge_instance_id" { value = try(oci_core_instance.edge[0].id, null) } +output "edge_public_ip" { value = try(oci_core_instance.edge[0].public_ip, null) } +output "email_instance_id" { value = try(oci_core_instance.email[0].id, null) } +output "email_public_ip" { value = try(oci_core_instance.email[0].public_ip, null) } +output "next_steps" { + value = [ + "Set LAB_EDGE_HOST to edge_public_ip and run ./jeannie edge to configure the edge proxy.", + "Install and harden the selected mail server on email_public_ip before opening mail ports.", + "Reserve the email public IP and request OCI reverse DNS/PTR before production mail.", + "Request OCI SMTP port 25 access; it is restricted by default for newer tenancies.", + "Add DNS A/AAAA, MX, SPF, DKIM, and DMARC records only after choosing the mail domain.", + ] +} diff --git a/bootstrap/oci/variables.tf b/bootstrap/oci/variables.tf new file mode 100644 index 0000000..2ef2daa --- /dev/null +++ b/bootstrap/oci/variables.tf @@ -0,0 +1,204 @@ +variable "enabled" { + type = bool + default = false +} + +variable "region" { + description = "OCI region." + type = string + default = "" +} + +variable "oci_config_profile" { + type = string + default = "DEFAULT" +} + +variable "tenancy_id" { + description = "Tenancy OCID; used as the parent for the optional child compartment." + type = string + default = "" +} + +variable "create_compartment" { + type = bool + default = true +} + +variable "compartment_id" { + description = "Existing compartment OCID when create_compartment is false." + type = string + default = "" +} + +variable "compartment_name" { + type = string + default = "homelab-learning" +} + +variable "compartment_description" { + type = string + default = "Jeannie-managed OCI learning resources" +} + +variable "vcn_display_name" { + type = string + default = "homelab-vcn" +} + +variable "vcn_cidr" { + type = string + default = "10.50.0.0/16" +} + +variable "public_subnet_cidr" { + type = string + default = "10.50.10.0/24" +} + +variable "private_subnet_cidr" { + type = string + default = "10.50.20.0/24" +} + +variable "admin_cidr" { + description = "CIDR allowed to SSH to the edge and email VMs. Do not leave this at 0.0.0.0/0." + type = string + default = "" +} + +variable "open_mail_ports" { + description = "Open SMTP submission, SMTP, and IMAPS ingress in the email NSG. Keep false until DNS/PTR and hardening are complete." + type = bool + default = false +} + +variable "edge_enabled" { + type = bool + default = true +} + +variable "email_enabled" { + type = bool + default = true +} + +variable "edge_availability_domain" { + type = string + default = "" +} + +variable "email_availability_domain" { + type = string + default = "" +} + +variable "edge_image_id" { + description = "OCI image OCID compatible with edge_shape." + type = string + default = "" +} + +variable "email_image_id" { + description = "OCI image OCID compatible with email_shape." + type = string + default = "" +} + +variable "ssh_public_key" { + type = string + sensitive = true + default = "" +} + +variable "edge_shape" { + type = string + default = "VM.Standard.A1.Flex" +} + +variable "email_shape" { + type = string + default = "VM.Standard.A1.Flex" +} + +variable "edge_ocpus" { + type = number + default = 1 +} + +variable "edge_memory_in_gbs" { + type = number + default = 6 +} + +variable "email_ocpus" { + type = number + default = 1 +} + +variable "email_memory_in_gbs" { + type = number + default = 6 +} + +variable "edge_boot_volume_size_in_gbs" { + type = number + default = 50 +} + +variable "email_boot_volume_size_in_gbs" { + type = number + default = 50 +} + +variable "edge_display_name" { + type = string + default = "homelab-edge" +} + +variable "email_display_name" { + type = string + default = "homelab-mail" +} + +variable "edge_hostname_label" { + type = string + default = "homelab-edge" +} + +variable "email_hostname_label" { + type = string + default = "homelab-mail" +} + +variable "freeform_tags" { + type = map(string) + default = {} +} + +check "foundation_inputs" { + assert { + condition = !var.enabled || ( + trimspace(var.region) != "" && + trimspace(var.tenancy_id) != "" && + (var.create_compartment || trimspace(var.compartment_id) != "") && + trimspace(var.admin_cidr) != "" && + (!var.edge_enabled || (trimspace(var.edge_availability_domain) != "" && trimspace(var.edge_image_id) != "")) && + (!var.email_enabled || (trimspace(var.email_availability_domain) != "" && trimspace(var.email_image_id) != "")) && + trimspace(var.ssh_public_key) != "" + ) + error_message = "When OCI foundation is enabled, region, tenancy_id, admin_cidr, SSH key, and image/availability-domain inputs for each enabled VM are required." + } + + assert { + condition = !var.enabled || alltrue([ + var.edge_ocpus > 0, + var.email_ocpus > 0, + var.edge_memory_in_gbs > 0, + var.email_memory_in_gbs > 0, + var.edge_boot_volume_size_in_gbs >= 50, + var.email_boot_volume_size_in_gbs >= 50, + ]) + error_message = "VM CPU and memory must be positive and boot volumes must be at least 50 GB." + } +} diff --git a/bootstrap/oci/versions.tf b/bootstrap/oci/versions.tf new file mode 100644 index 0000000..bba9956 --- /dev/null +++ b/bootstrap/oci/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 30bc6f9..3976d80 100644 --- a/cmd/jeannie-server/main.go +++ b/cmd/jeannie-server/main.go @@ -64,6 +64,7 @@ var allowedCommands = map[string]bool{ "state": true, "pimox": true, "email-vm": true, + "oci-foundation": 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 c39a61a..2031f1a 100644 --- a/cmd/jeannie-server/main_test.go +++ b/cmd/jeannie-server/main_test.go @@ -147,3 +147,9 @@ func TestEmailVMCommandIsRemotelyEnabled(t *testing.T) { t.Fatal("email-vm must be exposed through the remote API") } } + +func TestOCIFoundationCommandIsRemotelyEnabled(t *testing.T) { + if !allowedCommands["oci-foundation"] { + t.Fatal("oci-foundation must be exposed through the remote API") + } +} diff --git a/jeannie b/jeannie index 3669e2b..b16b3cf 100755 --- a/jeannie +++ b/jeannie @@ -123,6 +123,8 @@ Observability And GitOps 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. + oci-foundation {status|plan|apply|destroy --yes} + Create the opt-in OCI compartment, network, edge VM, and email 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. @@ -291,6 +293,9 @@ case "${1:-}" in email-vm) email_vm_manage "${@:2}" ;; + oci-foundation) + oci_foundation_manage "${@:2}" + ;; edge-cache-flush) edge_flush_caches ;; diff --git a/lib/jeannie/commands.sh b/lib/jeannie/commands.sh index a2699ae..41a9209 100644 --- a/lib/jeannie/commands.sh +++ b/lib/jeannie/commands.sh @@ -3327,6 +3327,47 @@ email_vm_manage() { esac } +oci_foundation_manage() { + require_debian_server "oci-foundation" + export_oci_foundation_tf_vars + local action="${1:-status}" + shift || true + local stack="${REPO_ROOT}/bootstrap/oci" + + case "${action}" in + status) + tofu -chdir="${stack}" show -no-color 2>/dev/null || echo "OCI foundation has no state yet." + ;; + plan|apply) + if [[ "${TF_VAR_enabled:-false}" != "true" ]]; then + echo "OCI foundation is disabled. Set LAB_OCI_FOUNDATION_ENABLED=true and the required LAB_OCI_* inputs first." >&2 + return 2 + fi + tofu -chdir="${stack}" init + if [[ "${action}" == "plan" ]]; then + tofu -chdir="${stack}" plan "$@" + else + record_change_journal "oci-foundation" apply "$@" + tofu -chdir="${stack}" apply "$@" -auto-approve + fi + ;; + destroy) + if [[ "${1:-}" != "--yes" ]]; then + echo "Refusing to destroy OCI foundation without --yes." >&2 + return 2 + fi + shift + record_change_journal "oci-foundation" destroy "$@" + tofu -chdir="${stack}" init + tofu -chdir="${stack}" destroy "$@" -auto-approve + ;; + *) + echo "Usage: ./jeannie oci-foundation {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 dc3949f..8d24889 100644 --- a/lib/jeannie/env.sh +++ b/lib/jeannie/env.sh @@ -324,6 +324,33 @@ export_email_vm_tf_vars() { export_if_unset TF_VAR_domain "${LAB_EMAIL_DOMAIN:-}" } +export_oci_foundation_tf_vars() { + export_if_unset TF_VAR_enabled "${LAB_OCI_FOUNDATION_ENABLED:-}" + export_if_unset TF_VAR_region "${LAB_OCI_REGION:-${LAB_EMAIL_OCI_REGION:-}}" + export_if_unset TF_VAR_oci_config_profile "${LAB_OCI_CONFIG_PROFILE:-${LAB_EMAIL_OCI_CONFIG_PROFILE:-}}" + export_if_unset TF_VAR_tenancy_id "${LAB_OCI_TENANCY_ID:-}" + export_if_unset TF_VAR_create_compartment "${LAB_OCI_CREATE_COMPARTMENT:-}" + export_if_unset TF_VAR_compartment_id "${LAB_OCI_COMPARTMENT_ID:-}" + export_if_unset TF_VAR_compartment_name "${LAB_OCI_COMPARTMENT_NAME:-}" + export_if_unset TF_VAR_admin_cidr "${LAB_OCI_ADMIN_CIDR:-}" + export_if_unset TF_VAR_open_mail_ports "${LAB_OCI_OPEN_MAIL_PORTS:-}" + export_if_unset TF_VAR_edge_enabled "${LAB_OCI_EDGE_ENABLED:-}" + export_if_unset TF_VAR_email_enabled "${LAB_OCI_EMAIL_ENABLED:-}" + export_if_unset TF_VAR_edge_availability_domain "${LAB_OCI_EDGE_AVAILABILITY_DOMAIN:-}" + export_if_unset TF_VAR_email_availability_domain "${LAB_OCI_EMAIL_AVAILABILITY_DOMAIN:-${LAB_EMAIL_AVAILABILITY_DOMAIN:-}}" + export_if_unset TF_VAR_edge_image_id "${LAB_OCI_EDGE_IMAGE_ID:-}" + export_if_unset TF_VAR_email_image_id "${LAB_OCI_EMAIL_IMAGE_ID:-${LAB_EMAIL_IMAGE_ID:-}}" + export_if_unset TF_VAR_ssh_public_key "${LAB_OCI_SSH_PUBLIC_KEY:-${LAB_EMAIL_SSH_PUBLIC_KEY:-}}" + export_if_unset TF_VAR_edge_shape "${LAB_OCI_EDGE_SHAPE:-}" + export_if_unset TF_VAR_email_shape "${LAB_OCI_EMAIL_SHAPE:-${LAB_EMAIL_SHAPE:-}}" + export_if_unset TF_VAR_edge_ocpus "${LAB_OCI_EDGE_OCPUS:-}" + export_if_unset TF_VAR_edge_memory_in_gbs "${LAB_OCI_EDGE_MEMORY_IN_GBS:-}" + export_if_unset TF_VAR_email_ocpus "${LAB_OCI_EMAIL_OCPUS:-${LAB_EMAIL_OCPUS:-}}" + export_if_unset TF_VAR_email_memory_in_gbs "${LAB_OCI_EMAIL_MEMORY_IN_GBS:-${LAB_EMAIL_MEMORY_IN_GBS:-}}" + export_if_unset TF_VAR_edge_boot_volume_size_in_gbs "${LAB_OCI_EDGE_BOOT_VOLUME_SIZE_IN_GBS:-}" + export_if_unset TF_VAR_email_boot_volume_size_in_gbs "${LAB_OCI_EMAIL_BOOT_VOLUME_SIZE_IN_GBS:-${LAB_EMAIL_BOOT_VOLUME_SIZE_IN_GBS:-}}" +} + sops_available() { command -v sops >/dev/null 2>&1 }