Skip to content

Commit 6153345

Browse files
committed
revert gcp-template
1 parent 817b277 commit 6153345

File tree

3 files changed

+38
-31
lines changed

3 files changed

+38
-31
lines changed

examples/examples.gen.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
"linux",
7777
"gcp"
7878
],
79-
"markdown": "\n# Remote Development on Google Compute Engine (Linux)\n\n## Prerequisites\n\n### Authentication\n\nThis template assumes that coderd is run in an environment that is authenticated\nwith Google Cloud. For example, run `gcloud auth application-default login` to\nimport credentials on the system and user running coderd. For other ways to\nauthenticate [consult the Terraform\ndocs](https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/getting_started#adding-credentials).\n\nCoder requires a Google Cloud Service Account to provision workspaces. To create\na service account:\n\n1. Navigate to the [CGP\n console](https://console.cloud.google.com/projectselector/iam-admin/serviceaccounts/create),\n and select your Cloud project (if you have more than one project associated\n with your account)\n\n1. Provide a service account name (this name is used to generate the service\n account ID)\n\n1. Click **Create and continue**, and choose the following IAM roles to grant to\n the service account:\n\n - Compute Admin\n - Service Account User\n\n Click **Continue**.\n\n1. Click on the created key, and navigate to the **Keys** tab.\n\n1. Click **Add key** \u003e **Create new key**.\n\n1. Generate a **JSON private key**, which will be what you provide to Coder\n during the setup process.\n\n## Architecture\n\nThis template provisions the following resources:\n\n- GCP VM (persistent)\n- GCP Disk (persistent, mounted to root)\n\nCoder 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.\n\n\u003e **Note**\n\u003e This template is designed to be a starting point! Edit the Terraform to extend the template to support your use case.\n\n## code-server\n\n`code-server` is installed via the `startup_script` argument in the `coder_agent`\nresource block. The `coder_app` resource is defined to access `code-server` through\nthe dashboard UI over `localhost:13337`.\n"
79+
"markdown": "\n# Remote Development on Google Compute Engine (Linux)\n\n## Prerequisites\n\n### Authentication\n\nThis template assumes that coderd is run in an environment that is authenticated\nwith Google Cloud. For example, run `gcloud auth application-default login` to\nimport credentials on the system and user running coderd. For other ways to\nauthenticate [consult the Terraform\ndocs](https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/getting_started#adding-credentials).\n\nCoder requires a Google Cloud Service Account to provision workspaces. To create\na service account:\n\n1. Navigate to the [CGP\n console](https://console.cloud.google.com/projectselector/iam-admin/serviceaccounts/create),\n and select your Cloud project (if you have more than one project associated\n with your account)\n\n1. Provide a service account name (this name is used to generate the service\n account ID)\n\n1. Click **Create and continue**, and choose the following IAM roles to grant to\n the service account:\n\n - Compute Admin\n - Service Account User\n\n Click **Continue**.\n\n1. Click on the created key, and navigate to the **Keys** tab.\n\n1. Click **Add key** \u003e **Create new key**.\n\n1. Generate a **JSON private key**, which will be what you provide to Coder\n during the setup process.\n\n## Architecture\n\nThis template provisions the following resources:\n\n- GCP VM (ephemeral)\n- GCP Disk (persistent, mounted to root)\n\nCoder 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.\n\n\u003e **Note**\n\u003e This template is designed to be a starting point! Edit the Terraform to extend the template to support your use case.\n\n## code-server\n\n`code-server` is installed via the `startup_script` argument in the `coder_agent`\nresource block. The `coder_app` resource is defined to access `code-server` through\nthe dashboard UI over `localhost:13337`.\n"
8080
},
8181
{
8282
"id": "gcp-vm-container",

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 (persistent)
52+
- GCP VM (ephemeral)
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: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,13 @@ resource "google_compute_disk" "root" {
6666
name = "coder-${data.coder_workspace.me.id}-root"
6767
type = "pd-ssd"
6868
zone = data.coder_parameter.zone.value
69-
image = "debian-cloud/debian-12"
69+
image = "debian-cloud/debian-11"
7070
lifecycle {
7171
ignore_changes = [name, image]
7272
}
7373
}
7474

7575
resource "coder_agent" "main" {
76-
count = data.coder_workspace.me.start_count
7776
auth = "google-instance-identity"
7877
arch = "amd64"
7978
os = "linux"
@@ -82,7 +81,7 @@ resource "coder_agent" "main" {
8281
set -e
8382
8483
# install and start code-server
85-
curl -fsSL https://code-server.dev/install.sh | sh -s -- --method=standalone --prefix=/tmp/code-server
84+
curl -fsSL https://code-server.dev/install.sh | sh -s -- --method=standalone --prefix=/tmp/code-server --version 4.11.0
8685
/tmp/code-server/bin/code-server --auth none --port 13337 >/tmp/code-server.log 2>&1 &
8786
EOT
8887

@@ -91,28 +90,39 @@ resource "coder_agent" "main" {
9190
display_name = "CPU Usage"
9291
interval = 5
9392
timeout = 5
94-
script = "coder stat cpu"
93+
script = <<-EOT
94+
#!/bin/bash
95+
set -e
96+
top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4 "%"}'
97+
EOT
9598
}
9699
metadata {
97100
key = "memory"
98101
display_name = "Memory Usage"
99102
interval = 5
100103
timeout = 5
101-
script = "coder stat mem"
104+
script = <<-EOT
105+
#!/bin/bash
106+
set -e
107+
free -m | awk 'NR==2{printf "%.2f%%\t", $3*100/$2 }'
108+
EOT
102109
}
103110
metadata {
104111
key = "disk"
105112
display_name = "Disk Usage"
106113
interval = 600 # every 10 minutes
107114
timeout = 30 # df can take a while on large filesystems
108-
script = "coder stat disk"
115+
script = <<-EOT
116+
#!/bin/bash
117+
set -e
118+
df /home/coder | awk '$NF=="/"{printf "%s", $5}'
119+
EOT
109120
}
110121
}
111122

112123
# code-server
113124
resource "coder_app" "code-server" {
114-
count = data.coder_workspace.me.start_count
115-
agent_id = coder_agent.main[0].id
125+
agent_id = coder_agent.main.id
116126
slug = "code-server"
117127
display_name = "code-server"
118128
icon = "/icon/code.svg"
@@ -128,11 +138,10 @@ resource "coder_app" "code-server" {
128138
}
129139

130140
resource "google_compute_instance" "dev" {
131-
zone = data.coder_parameter.zone.value
132-
name = "coder-${lower(data.coder_workspace.me.owner)}-${lower(data.coder_workspace.me.name)}-root"
133-
machine_type = "e2-medium"
134-
desired_status = (data.coder_workspace.me.owner == "default" || data.coder_workspace.me.start_count == 1 ? "RUNNING" : "TERMINATED")
135-
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"
136145
network_interface {
137146
network = "default"
138147
access_config {
@@ -150,20 +159,18 @@ resource "google_compute_instance" "dev" {
150159
# The startup script runs as root with no $HOME environment set up, so instead of directly
151160
# running the agent init script, create a user (with a homedir, default shell and sudo
152161
# permissions) and execute the init script as that user.
153-
metadata = {
154-
startup_script = <<-EOMETA
155-
#!/usr/bin/env sh
156-
set -eux
157-
158-
# If user does not exist, create it and set up passwordless sudo
159-
if ! id -u "${local.linux_user}" >/dev/null 2>&1; then
160-
useradd -m -s /bin/bash "${local.linux_user}"
161-
echo "${local.linux_user} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/coder-user
162-
fi
163-
# Start the agent
164-
exec sudo -u "${local.linux_user}" sh -c '${try(coder_agent.main[0].init_script, "")}'
165-
EOMETA
166-
}
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
167174
}
168175

169176
locals {
@@ -173,11 +180,11 @@ locals {
173180

174181
resource "coder_metadata" "workspace_info" {
175182
count = data.coder_workspace.me.start_count
176-
resource_id = google_compute_instance.dev.id
183+
resource_id = google_compute_instance.dev[0].id
177184

178185
item {
179186
key = "type"
180-
value = google_compute_instance.dev.machine_type
187+
value = google_compute_instance.dev[0].machine_type
181188
}
182189
}
183190

0 commit comments

Comments
 (0)