diff --git a/.icons/coder-white.svg b/.icons/coder-white.svg new file mode 100644 index 0000000..3bb941d --- /dev/null +++ b/.icons/coder-white.svg @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/.images/coder-login.png b/.images/coder-login.png new file mode 100644 index 0000000..a085450 Binary files /dev/null and b/.images/coder-login.png differ diff --git a/coder-login/README.md b/coder-login/README.md new file mode 100644 index 0000000..def4c97 --- /dev/null +++ b/coder-login/README.md @@ -0,0 +1,20 @@ +--- +display_name: Coder Login +description: Automatically logs the user into Coder on their workspace +icon: ../.icons/coder-white.svg +maintainer_github: coder +verified: true +tags: [helper] +--- + +# Coder Login + +Automatically logs the user into Coder when creating their workspace. + +```hcl +module "coder-login" { + agent_id = coder_agent.example.id +} +``` + +![Coder Login Logs](../.images/coder-login.png) diff --git a/coder-login/main.test.ts b/coder-login/main.test.ts new file mode 100644 index 0000000..d8fba35 --- /dev/null +++ b/coder-login/main.test.ts @@ -0,0 +1,15 @@ +import { describe, expect, it } from "bun:test"; +import { + executeScriptInContainer, + runTerraformApply, + runTerraformInit, + testRequiredVariables, +} from "../test"; + +describe("coder-login", async () => { + await runTerraformInit(import.meta.dir); + + testRequiredVariables(import.meta.dir, { + agent_id: "foo", + }); +}); diff --git a/coder-login/main.tf b/coder-login/main.tf new file mode 100644 index 0000000..2d3ac8b --- /dev/null +++ b/coder-login/main.tf @@ -0,0 +1,30 @@ +terraform { + required_version = ">= 1.0" + + required_providers { + coder = { + source = "coder/coder" + version = ">= 0.12" + } + } +} + +variable "agent_id" { + type = string + description = "The ID of a Coder agent." +} + +data "coder_workspace" "me" {} + +resource "coder_script" "coder-login" { + agent_id = var.agent_id + script = templatefile("${path.module}/run.sh", { + CODER_USER_TOKEN : data.coder_workspace.me.owner_session_token, + CODER_DEPLOYMENT_URL : data.coder_workspace.me.access_url + }) + display_name = "Coder Login" + icon = "http://svgur.com/i/y5G.svg" + run_on_start = true + start_blocks_login = true +} + diff --git a/coder-login/run.sh b/coder-login/run.sh new file mode 100644 index 0000000..e5a58ec --- /dev/null +++ b/coder-login/run.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env sh + +# Automatically authenticate the user if they are not +# logged in to another deployment + +BOLD='\033[0;1m' + +printf "$${BOLD}Logging into Coder...\n\n$${RESET}" + +if ! coder list >/dev/null 2>&1; then + set +x; coder login --token="${CODER_USER_TOKEN}" --url="${CODER_DEPLOYMENT_URL}" +else + echo "You are already authenticated with coder." +fi