my-homelab-configs/bootstrap/oci/README.md

207 lines
11 KiB
Markdown

# OCI foundation
## Edge agent recovery
`./jeannie oci agent-status` reads the managed edge identity from the selected
foundation state and reports OCI agent configuration and key plugin statuses.
It is also available as `./jeannie remote oci agent-status`. An `INVALID` or
stale plugin status does not identify which guest component failed.
`./jeannie oci restart-edge` requests an OCI `SOFTRESET` for the edge instance
recorded in the selected foundation state, after checking the instance is
`RUNNING`. It does not replace the instance or require SSH. OCI can power-cycle
the VM if the guest has not shut down after its grace period. The command
reports request acceptance, not completion; check the edge and agent status
afterward. Run it on Debian or through a direct private API route: the public
API route through the edge will be interrupted by the reboot.
The managed edge definition explicitly enables the Compute Instance Run Command
plugin. Edge bootstrap installs a five-minute systemd watchdog for a native
`oracle-cloud-agent.service`: if the unit exists but is stopped, it tries to
start it. It does not install an agent or use Snap. A missing unit is reported
in the watchdog journal. A running service does not prove it can reach OCI; the
watchdog cannot repair an unresponsive running service or missing plugin.
The existing `./jeannie edge apply` installs the watchdog over its configured
SSH transport; it cannot install it on an unreachable instance. Review the
foundation plan for agent configuration changes before applying it. Do not
replace a working edge simply to install the watchdog.
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.
The foundation plan/apply path installs OCI CLI into
`~/.local/share/homelab/venvs/oci-cli` on Debian by default. Set
`LAB_OCI_CLI_AUTO_INSTALL=false` to disable that behavior, or install it
explicitly with `./jeannie oci-auth install`.
Jeannie uses `~/.config/homelab/oci-foundation.env` as the canonical
configuration for foundation and edge. If that file has no state pointer and
`oci-foundation-fresh.env` is present, the resolver merges its infrastructure
inputs into the canonical file, preserves backup/monitoring settings, and uses
the exact state file recorded by the fresh configuration. Both original configs
are backed up in an `oci-foundation-archive-*` directory; the fresh config is
retired there. State files are never merged, copied over each other, or selected
by modification time. Explicit `LAB_OCI_FOUNDATION_CONFIG_FILE` overrides remain
available for intentionally separate test stacks.
The resolver reads generated configs as data and writes shell-quoted values
atomically with mode 0600. It repairs the legacy SSH-key escaping and uses exact
resource addresses in state to preserve managed VM, network, and Bastion
ownership. Managed VMs keep compute enabled; the normal foundation workflow
cannot disable/destroy them through an accidentally persisted flag.
Before each plan/apply, read-only OCI lookups validate recorded instance IDs
and resolve VNICs using each instance's real compartment. Discovery uses exact
names and scopes and refuses ambiguous matches or authorization failures.
Surviving VMs supply their VCN/subnet identity when state is missing.
Already-created NSGs, matching rules, backup buckets, lifecycle policies,
monitoring topics, and alarms are adopted using generated import declarations
in `bootstrap/oci/jeannie-imports.tf`. Imports are visible in the plan and enter
state only during apply. Ordinary apply executes the saved, checked plan and
refuses any resource deletion or replacement. `destroy --yes` remains a separate,
explicit operation.
From Debian, validate a migration without applying infrastructure changes:
```bash
./jeannie oci-foundation verify
./jeannie oci-foundation plan
```
Review imports and additions in the complete plan. A second successful apply
should produce no further imports or infrastructure changes. The offline
regressions use temporary files and mocks; they never contact OCI or SSH.
Edge deployment defaults to `LAB_EDGE_SSH_TRANSPORT=auto`. It resolves the
recorded OCI VM and its public address for DNS, then tries the single online
Tailscale peer named `homelab-oci-edge` for SSH. Override the peer name with
`LAB_EDGE_TAILSCALE_HOSTNAME`. If the peer is unavailable or its SSH preflight
fails, Jeannie selects an OCI Bastion port-forwarding session to the edge's private IP.
`LAB_EDGE_SSH_TRANSPORT=tailscale` and `bastion` select either path explicitly.
The Bastion path requires a reachable Bastion service, a working SSH server on
the edge, an authorized guest SSH key, and an allowed private subnet. It does
not require the OCI guest agent and never falls back to public SSH. A fresh edge
without Tailscale uses Bastion to bootstrap it before reconciling subnet routes.
Both paths use bounded SSH preflight and preserve known host keys.
Before creating a Bastion session, Jeannie checks Debian's direct public IPv4
against the Bastion allowlist. If it changed, Jeannie adds the current `/32`,
preserves existing ranges, stores the resulting list in the foundation config,
and creates a new session. The old ranges remain authorized until reviewed and
removed deliberately. If the public address, Bastion ETag, or update cannot be
verified, the operation stops without opening a broader range.
Routine applies no longer restart caches; use `./jeannie edge-cache-flush`
when a cache reset is intended.
Use `./jeannie remote edge diagnose` to inspect a failed deployment through
the API. It reports the bootstrap substep, the last Compose command output,
container startup errors, port listeners, and disk usage without printing
rendered configuration or container environments. Edge host metrics use the
systemd `prometheus-node-exporter` on port 9100; Compose only runs cAdvisor
on 9101, avoiding duplicate exporters trying to bind the same host port.
Prepare the config with `./jeannie oci-auth init`. The command creates the
private key locally, prints the public key for the one-time OCI user upload,
and writes the config with restrictive permissions. It never sends the private
key to the API or stores it in Git. Run `./jeannie oci-auth check` after the
public key has been uploaded.
The defaults use the Always Free `VM.Standard.E2.1.Micro` shape and the
Canonical Ubuntu 24.04 Minimal image in `us-ashburn-1` supplied for this lab.
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.
To test a complete compartment/network foundation without consuming another
Always Free compute slot, set `LAB_OCI_COMPUTE_ENABLED=false`. Edge/email
subnets and security groups are still created; the VM resources are deferred
until the variable is set back to `true`.
If the tenancy has exhausted its NAT gateway quota, set
`LAB_OCI_NAT_ENABLED=false` for a public-only foundation test. The private
subnet remains available but does not receive a NAT route until this is
enabled later.
For a deliberate clean-slate infrastructure reset, use the guarded OCI
cleanup command from Debian. It never deletes the tenancy, root compartment,
IAM users, groups, policies, or API keys:
```text
./jeannie oci cleanup plan
./jeannie oci cleanup apply --confirm TENANCY-CLEANUP
```
The first command inventories infrastructure only. The second deletes
instances, volumes, networking, and empty child compartments. Re-run `plan`
after OCI termination completes; IAM remains available for the next Jeannie
foundation run.
For a test alongside an existing foundation, also set
`LAB_OCI_FOUNDATION_STATE_FILE` to a new local state path. This prevents the
test plan from treating the production foundation as resources to destroy.
After a successful apply, Jeannie persists the discovered edge IP and uses it
as the next-run edge target instead of the static inventory value. To update
DuckDNS automatically when OCI assigns a replacement public IP, provide the
token outside Git and list the DuckDNS subdomain labels to update:
```bash
export LAB_DUCKDNS_TOKEN='REDACTED_DUCKDNS_TOKEN'
export LAB_DUCKDNS_DOMAINS='lab2025,api,auth,grafana,office,docs'
```
The update is enabled by default when a token is present. Set
`LAB_OCI_AUTO_UPDATE_DUCKDNS=false` to disable it. The email VM baseline
(Postfix, Dovecot, fail2ban, UFW, and node-exporter) is also installed
automatically after the new VM accepts SSH; set
`LAB_OCI_AUTO_CONFIGURE_EMAIL=false` to defer that step.
The foundation also creates an OCI Bastion service by default. It is a
service-managed, time-limited SSH entry point and does not consume a compute
VM. Its client allow-list defaults to `LAB_OCI_ADMIN_CIDR`. For a new VCN,
Jeannie permits SSH from the Bastion target subnet to the managed edge and
email instances. When reusing an existing VCN, set
`LAB_OCI_BASTION_SSH_SOURCE_CIDR` if its NSGs do not already allow that
subnet. Set `LAB_OCI_BASTION_TARGET_SUBNET_ID` to use a private target subnet.
After `oci-foundation apply`, the Bastion and discovered edge instance OCIDs
are persisted in the local OCI config. Open a managed SSH session with:
```text
./jeannie oci bastion ssh
```
Explicit OCIDs and key paths can also be supplied:
```text
./jeannie oci bastion ssh BASTION_OCID INSTANCE_OCID ubuntu ~/.ssh/id_ed25519.pub ~/.ssh/id_ed25519
```
The same operation is available through the remote API as
`./jeannie remote oci bastion ssh`; it executes on the Debian pipeline host,
keeping OCI credentials and the private SSH key off the client.
The foundation also creates a private, versioned Object Storage bucket named
`homelab-backups` by default. Jeannie persists the discovered bucket name,
namespace, and OCID as `LAB_OCI_BACKUP_BUCKET`,
`LAB_OCI_BACKUP_BUCKET_NAMESPACE`, and `LAB_OCI_BACKUP_BUCKET_ID` in the local
foundation inventory. Backup archives are expected to be encrypted with age
before upload. The bucket has a Terraform `prevent_destroy` guard so a normal
foundation destroy cannot remove backup data; reset it only as an explicitly
reviewed backup-reset change. Validate readiness without changing storage with:
```text
./jeannie backup oci status
./jeannie remote backup oci status
```