Skip to content

Commit c0782f2

Browse files
committed
Allow adding files and run commands to the VM
1 parent 8b45cf5 commit c0782f2

4 files changed

Lines changed: 37 additions & 4 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ Deploy a docker compose project to a Google Cloud Compute Instance.
7676
| <a name="input_os"></a> [os](#input\_os) | The host OS to install on the GCP instance | `string` | `"cos-125-19216-104-25"` | no |
7777
| <a name="input_overlay_source_instance"></a> [overlay\_source\_instance](#input\_overlay\_source\_instance) | Name of production instance to get latest snapshot from (e.g., 'ojs-production'). Terraform will automatically use the most recent snapshot from this instance's data disk. Leave empty for production environments. | `string` | `""` | no |
7878
| <a name="input_region"></a> [region](#input\_region) | GCP region for resources | `string` | `"us-east5"` | no |
79+
| <a name="input_rootfs"></a> [rootfs](#input\_rootfs) | Path to additional rootfs files to copy into the VM. Files will be merged with the base rootfs. Example: '../platform/terraform/rootfs' | `string` | `""` | no |
7980
| <a name="input_run_snapshots"></a> [run\_snapshots](#input\_run\_snapshots) | Enable daily snapshots of the data disk (recommended for production). Last seven days of snapshots are available. Also weekly snapshots for past year. | `bool` | `false` | no |
81+
| <a name="input_runcmd"></a> [runcmd](#input\_runcmd) | Additional commands to run during cloud-init. Commands are executed after the main initialization. | `list(string)` | `[]` | no |
8082
| <a name="input_users"></a> [users](#input\_users) | Map of usernames to lists of SSH public keys. Users will be created with docker group membership. Example: { "alice" = ["ssh-rsa AAAA..."], "bob" = ["ssh-ed25519 AAAA...", "ssh-rsa BBBB..."] } | `map(list(string))` | `{}` | no |
8183
| <a name="input_volume_names"></a> [volume\_names](#input\_volume\_names) | List of docker volumes to overlay from production snapshot (e.g., ['compose\_ojs-public']). Production data is mounted read-only as lower layer, staging writes go to upper layer. | `list(string)` | `[]` | no |
8284
| <a name="input_zone"></a> [zone](#input\_zone) | GCP zone for resources | `string` | `"us-east5-b"` | no |

main.tf

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,27 @@ provider "google" {
1717
resource "time_static" "snapshot_time_static" {}
1818

1919
locals {
20-
rootFs = "${path.module}/rootfs"
20+
rootFs = "${path.module}/rootfs"
21+
additional_rootfs = var.rootfs != "" ? var.rootfs : ""
22+
23+
# Get files from base rootfs
24+
base_files = fileset(local.rootFs, "**")
25+
26+
# Get files from additional rootfs if path is provided
27+
additional_files = local.additional_rootfs != "" ? fileset(local.additional_rootfs, "**") : []
28+
29+
# Combine both file sets (additional files will override base files with same path)
30+
all_files = merge(
31+
{ for file in local.base_files : file => "${local.rootFs}/${file}" },
32+
{ for file in local.additional_files : file => "${local.additional_rootfs}/${file}" }
33+
)
34+
2135
write_files_content = join("\n", [
22-
for file in fileset(local.rootFs, "**") : <<-EOT
23-
- path: "/${replace(file, "${local.rootFs}/", "")}"
36+
for file, fullpath in local.all_files : <<-EOT
37+
- path: "/${file}"
2438
permissions: "0644"
2539
content: |
26-
${indent(4, file("${local.rootFs}/${file}"))}
40+
${indent(4, file(fullpath))}
2741
EOT
2842
])
2943
docker_compose_scripts = join("\n", [
@@ -67,6 +81,7 @@ EOT
6781
USE_OVERLAY = local.use_overlay,
6882
DOCKER_VOLUME_OVERLAYS = var.volume_names,
6983
SSH_USERS = var.users,
84+
ADDITIONAL_RUNCMD = var.runcmd,
7085
})
7186

7287
# have prod snapshot begin ten minutes after the initial run

templates/cloud-init.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ runcmd:
3939
%{ for VOLUME in DOCKER_VOLUME_OVERLAYS ~}
4040
- bash /home/cloud-compose/overlay-init.sh "${VOLUME}" >> /home/cloud-compose/run.log 2>&1
4141
%{ endfor ~}
42+
%{ for CMD in ADDITIONAL_RUNCMD ~}
43+
- ${CMD}
44+
%{ endfor ~}

variables.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,16 @@ variable "users" {
134134
default = {}
135135
description = "Map of usernames to lists of SSH public keys. Users will be created with docker group membership. Example: { \"alice\" = [\"ssh-rsa AAAA...\"], \"bob\" = [\"ssh-ed25519 AAAA...\", \"ssh-rsa BBBB...\"] }"
136136
}
137+
138+
variable "rootfs" {
139+
type = string
140+
default = ""
141+
description = "Path to additional rootfs files to copy into the VM. Files will be merged with the base rootfs. Example: '/path/to/custom/rootfs'"
142+
}
143+
144+
variable "runcmd" {
145+
type = list(string)
146+
default = []
147+
description = "Additional commands to run during cloud-init. Commands are executed after the main initialization."
148+
}
149+

0 commit comments

Comments
 (0)