Skip to content

Commit 886837c

Browse files
committed
Update GCP VM to use persistent VM
1 parent 24dd048 commit 886837c

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

examples/templates/gcp-linux/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ a service account:
4949

5050
This template provisions the following resources:
5151

52-
- GCP VM (ephemeral)
52+
- GCP VM (persistent)
5353
- GCP Disk (persistent, mounted to root)
5454

5555
Coder persists the root volume. The full filesystem is preserved when the workspace restarts. See this [community example](https://github.com/bpmct/coder-templates/tree/main/aws-linux-ephemeral) of an ephemeral AWS instance.

examples/templates/gcp-linux/main.tf

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ resource "google_compute_disk" "root" {
7373
}
7474

7575
resource "coder_agent" "main" {
76+
count = data.coder_workspace.me.start_count
7677
auth = "google-instance-identity"
7778
arch = "amd64"
7879
os = "linux"
@@ -81,7 +82,7 @@ resource "coder_agent" "main" {
8182
set -e
8283
8384
# install and start code-server
84-
curl -fsSL https://code-server.dev/install.sh | sh -s -- --method=standalone --prefix=/tmp/code-server --version 4.11.0
85+
curl -fsSL https://code-server.dev/install.sh | sh -s -- --method=standalone --prefix=/tmp/code-server
8586
/tmp/code-server/bin/code-server --auth none --port 13337 >/tmp/code-server.log 2>&1 &
8687
EOT
8788

@@ -93,7 +94,7 @@ resource "coder_agent" "main" {
9394
script = <<-EOT
9495
#!/bin/bash
9596
set -e
96-
top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4 "%"}'
97+
coder stat cpu
9798
EOT
9899
}
99100
metadata {
@@ -104,7 +105,7 @@ resource "coder_agent" "main" {
104105
script = <<-EOT
105106
#!/bin/bash
106107
set -e
107-
free -m | awk 'NR==2{printf "%.2f%%\t", $3*100/$2 }'
108+
coder stat mem
108109
EOT
109110
}
110111
metadata {
@@ -115,7 +116,7 @@ resource "coder_agent" "main" {
115116
script = <<-EOT
116117
#!/bin/bash
117118
set -e
118-
df /home/coder | awk '$NF=="/"{printf "%s", $5}'
119+
coder stat disk
119120
EOT
120121
}
121122
}
@@ -138,10 +139,12 @@ resource "coder_app" "code-server" {
138139
}
139140

140141
resource "google_compute_instance" "dev" {
141-
zone = data.coder_parameter.zone.value
142-
count = data.coder_workspace.me.start_count
143-
name = "coder-${lower(data.coder_workspace.me.owner)}-${lower(data.coder_workspace.me.name)}-root"
144-
machine_type = "e2-medium"
142+
zone = data.coder_parameter.zone.value
143+
count = data.coder_workspace.me.start_count
144+
name = "coder-${lower(data.coder_workspace.me.owner)}-${lower(data.coder_workspace.me.name)}-root"
145+
machine_type = "e2-medium"
146+
desired_status = (data.coder_workspace.me.owner == "default" || data.coder_workspace.me.start_count == 1 ? "RUNNING" : "TERMINATED")
147+
145148
network_interface {
146149
network = "default"
147150
access_config {
@@ -159,18 +162,20 @@ resource "google_compute_instance" "dev" {
159162
# The startup script runs as root with no $HOME environment set up, so instead of directly
160163
# running the agent init script, create a user (with a homedir, default shell and sudo
161164
# permissions) and execute the init script as that user.
162-
metadata_startup_script = <<EOMETA
163-
#!/usr/bin/env sh
164-
set -eux
165-
166-
# If user does not exist, create it and set up passwordless sudo
167-
if ! id -u "${local.linux_user}" >/dev/null 2>&1; then
168-
useradd -m -s /bin/bash "${local.linux_user}"
169-
echo "${local.linux_user} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/coder-user
170-
fi
171-
172-
exec sudo -u "${local.linux_user}" sh -c '${coder_agent.main.init_script}'
173-
EOMETA
165+
metadata = {
166+
startup_script = <<-EOMETA
167+
#!/usr/bin/env sh
168+
set -eux
169+
170+
# If user does not exist, create it and set up passwordless sudo
171+
if ! id -u "${local.linux_user}" >/dev/null 2>&1; then
172+
useradd -m -s /bin/bash "${local.linux_user}"
173+
echo "${local.linux_user} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/coder-user
174+
fi
175+
# Start the agent
176+
exec sudo -u "${local.linux_user}" sh -c '${try(coder_agent.mainp[0].init_script, "")}'
177+
EOMETA
178+
}
174179
}
175180

176181
locals {

0 commit comments

Comments
 (0)