Skip to content

Commit c241e6f

Browse files
committed
feat: Restructured and added App link for dashboard when dev server is running
1 parent e86088d commit c241e6f

1 file changed

Lines changed: 57 additions & 28 deletions

File tree

templates/club-website/main.tf

Lines changed: 57 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,17 @@ resource "coder_agent" "main" {
3434
arch = data.coder_provisioner.me.arch
3535
startup_script = <<-EOT
3636
set -e
37+
38+
# Configure Global Identity
39+
git config --global user.name "${local.git_author_name}"
40+
git config --global user.email "${local.git_author_email}"
41+
42+
# Set the Coder Credential Helper
43+
# This tells Git: "When you need a password for GitHub, ask the Coder CLI"
44+
coder git-auth setup github
3745
EOT
38-
dir = "/workspaces"
3946

40-
# To authenticate and clone private repos using user's access token
41-
env = {
42-
GITHUB_TOKEN = data.coder_external_auth.github.access_token
43-
GIT_AUTHOR_NAME = local.git_author_name
44-
GIT_AUTHOR_EMAIL = local.git_author_email
45-
GIT_COMMITTER_NAME = local.git_author_name
46-
GIT_COMMITTER_EMAIL = local.git_author_email
47-
}
47+
dir = "/workspaces/${element(split("/", local.repo_url), length(split("/", local.repo_url)) - 1)}"
4848

4949
# The following metadata blocks are optional. They are used to display
5050
# information about your workspace in the dashboard. You can remove them
@@ -124,42 +124,58 @@ module "code-server" {
124124
order = 1
125125
}
126126

127+
# Show the dev server through a link on the Coder dashboard
128+
resource "coder_app" "webapp" {
129+
agent_id = coder_agent.main.id
130+
slug = "webapp"
131+
display_name = "Dev Server"
132+
url = "http://localhost:3000" # Coder handles the proxying/TLS for you
133+
icon = "https://raw.githubusercontent.com/fortawesome/Font-Awesome/6.x/svgs/solid/globe.svg"
134+
subdomain = true # Recommended: gives the app its own unique subdomain
135+
share = "owner" # Only the workspace owner can see this link
136+
order = 2
137+
}
138+
139+
resource "coder_metadata" "web_status" {
140+
count = data.coder_workspace.me.start_count
141+
resource_id = coder_agent.main.id
142+
item {
143+
key = "Web Server"
144+
# This script checks if port 3000 is open
145+
script = "nc -z localhost 3000 && echo 'Online' || echo 'Starting...'"
146+
interval = 5
147+
}
148+
}
127149
resource "coder_metadata" "container_info" {
128150
count = data.coder_workspace.me.start_count
129151
resource_id = coder_agent.main.id
130152
item {
131153
key = "workspace image"
132-
value = var.cache_repo == "" ? local.devcontainer_builder_image : envbuilder_cached_image.cached.0.image
154+
value = local.devcontainer_builder_image
133155
}
134156
item {
135157
key = "git url"
136158
value = local.repo_url
137159
}
138160
item {
139161
key = "cache repo"
140-
value = var.cache_repo == "" ? "not enabled" : var.cache_repo
162+
value = "not enabled"
141163
}
142164
}
143165

144166
locals {
145167
deployment_name = "coder-${lower(data.coder_workspace.me.id)}"
146-
devcontainer_builder_image = data.coder_parameter.devcontainer_builder.value
168+
devcontainer_builder_image = "ghcr.io/coder/envbuilder:1.3.0"
147169
git_author_name = coalesce(data.coder_workspace_owner.me.full_name, data.coder_workspace_owner.me.name)
148170
git_author_email = data.coder_workspace_owner.me.email
149-
repo_url = "https://github.com/UMLCloudComputing/UMLCloudComputing.github.io.git"
171+
repo_url = "https://github.com/UMLCloudComputing/UMLCloudComputing.github.io.git" # Edit this to clone a different repo into the dev container
150172
# The envbuilder provider requires a key-value map of environment variables.
151173
envbuilder_env = {
152174
"CODER_AGENT_TOKEN" : coder_agent.main.token,
153-
# Use the docker gateway if the access URL is 127.0.0.1
154-
"CODER_AGENT_URL" : replace(data.coder_workspace.me.access_url, "/localhost|127\\.0\\.0\\.1/", "host.docker.internal"),
155-
# ENVBUILDER_GIT_URL and ENVBUILDER_CACHE_REPO will be overridden by the provider
156-
# if the cache repo is enabled.
175+
"CODER_AGENT_URL" : data.coder_workspace.me.access_url
157176
"ENVBUILDER_GIT_URL" : local.repo_url,
158-
# Use the docker gateway if the access URL is 127.0.0.1
159-
"ENVBUILDER_INIT_SCRIPT" : replace(coder_agent.main.init_script, "/localhost|127\\.0\\.0\\.1/", "host.docker.internal"),
160-
"ENVBUILDER_FALLBACK_IMAGE" : data.coder_parameter.fallback_image.value,
161-
"ENVBUILDER_DOCKER_CONFIG_BASE64" : base64encode(try(data.kubernetes_secret_v1.cache_repo_dockerconfig_secret[0].data[".dockerconfigjson"], "")),
162-
"ENVBUILDER_PUSH_IMAGE" : var.cache_repo == "" ? "" : "true"
177+
"ENVBUILDER_GIT_TOKEN" : data.coder_external_auth.github.access_token
178+
"ENVBUILDER_INIT_SCRIPT" : coder_agent.main.init_script
163179
}
164180
}
165181

@@ -197,7 +213,7 @@ resource "kubernetes_persistent_volume_claim_v1" "workspaces" {
197213
resource "kubernetes_deployment_v1" "main" {
198214
count = data.coder_workspace.me.start_count
199215
depends_on = [
200-
kubernetes_persistent_volume_claim_v1.home
216+
kubernetes_persistent_volume_claim_v1.workspaces
201217
]
202218
wait_for_rollout = false
203219
metadata {
@@ -242,11 +258,24 @@ resource "kubernetes_deployment_v1" "main" {
242258
name = "dev"
243259
image = local.devcontainer_builder_image
244260
image_pull_policy = "Always"
245-
command = ["sh", "-c", coder_agent.main.init_script]
246-
security_context {}
261+
command = ["/envbuilder", "run"]
262+
security_context {
263+
run_as_user=1000
264+
}
265+
dynamic "env" {
266+
for_each = local.envbuilder_env
267+
content {
268+
name = env.key
269+
value = env.value
270+
}
271+
}
272+
env {
273+
name = "GIT_AUTHOR_NAME"
274+
value = local.git_author_name
275+
}
247276
env {
248-
name = "CODER_AGENT_TOKEN"
249-
value = coder_agent.main.token
277+
name = "GIT_AUTHOR_EMAIL"
278+
value = local.git_author_email
250279
}
251280
resources {
252281
requests = {
@@ -268,7 +297,7 @@ resource "kubernetes_deployment_v1" "main" {
268297
volume {
269298
name = "workspaces"
270299
persistent_volume_claim {
271-
claim_name = kubernetes_persistent_volume_claim_v1.home.metadata.0.name
300+
claim_name = kubernetes_persistent_volume_claim_v1.workspaces.metadata.0.name
272301
read_only = false
273302
}
274303
}

0 commit comments

Comments
 (0)