321 lines
8.1 KiB
Terraform
321 lines
8.1 KiB
Terraform
terraform {
|
|
required_providers {
|
|
coder = {
|
|
source = "coder/coder"
|
|
version = "~> 0.12.0"
|
|
}
|
|
docker = {
|
|
source = "kreuzwerker/docker"
|
|
version = "~> 3.0.1"
|
|
}
|
|
}
|
|
}
|
|
|
|
locals {
|
|
username = data.coder_workspace.me.owner
|
|
}
|
|
|
|
provider "coder" {
|
|
|
|
}
|
|
|
|
data "coder_provisioner" "me" {
|
|
}
|
|
|
|
provider "docker" {
|
|
}
|
|
|
|
data "coder_workspace" "me" {
|
|
}
|
|
|
|
variable "waka_api_url" {
|
|
description = "URL For WakaAPI"
|
|
type = string
|
|
sensitive = true
|
|
}
|
|
|
|
variable "waka_api_key" {
|
|
description = "API Key for WakaAPI"
|
|
type = string
|
|
sensitive = true
|
|
}
|
|
|
|
variable "gitea_key" {
|
|
description = "Access Key for Gitea"
|
|
type = string
|
|
sensitive = true
|
|
}
|
|
|
|
data "coder_parameter" "git_repo" {
|
|
name = "git_repo"
|
|
display_name= "Git Repo"
|
|
description = "Git Repo to Clone"
|
|
default = " "
|
|
icon = "/icon/github.svg"
|
|
}
|
|
|
|
data "coder_parameter" "language" {
|
|
name = "Code Language"
|
|
description = "Select a language/tools/framework to install"
|
|
type = "string"
|
|
default = "default"
|
|
|
|
option {
|
|
name = "No Language (Pure Image)"
|
|
value = "default"
|
|
icon = "/icon/terminal.svg"
|
|
}
|
|
|
|
option {
|
|
name = "C"
|
|
value = "c"
|
|
icon = "https://i.imgur.com/eVh9nSG.png"
|
|
}
|
|
|
|
option {
|
|
name = "C++"
|
|
value = "c++"
|
|
icon = "https://i.imgur.com/WCRrHje.png"
|
|
}
|
|
|
|
option {
|
|
name = "Java"
|
|
value = "java"
|
|
icon = "/icon/java.svg"
|
|
}
|
|
|
|
option {
|
|
name = "Node"
|
|
value = "node"
|
|
icon = "/icon/javascript.svg"
|
|
}
|
|
|
|
option {
|
|
name = "Python 3"
|
|
value = "python3"
|
|
icon = "/icon/python.svg"
|
|
}
|
|
|
|
option {
|
|
name = "Go"
|
|
value = "golang"
|
|
icon = "/icon/go.svg"
|
|
}
|
|
|
|
option {
|
|
name = "Web"
|
|
value = "web"
|
|
icon = "/icon/terminal.svg"
|
|
}
|
|
}
|
|
|
|
resource "coder_agent" "main" {
|
|
arch = data.coder_provisioner.me.arch
|
|
os = "linux"
|
|
startup_script_timeout = 180
|
|
startup_script_behavior = "blocking"
|
|
display_apps {
|
|
vscode = true
|
|
vscode_insiders = false
|
|
web_terminal = true
|
|
ssh_helper = true
|
|
}
|
|
startup_script = <<-EOT
|
|
set -e
|
|
# Base Script (to be downloaded)
|
|
curl -fsSL https://gitea.nicholaspease.com/npease/CoderTemplatesV2/raw/branch/main/container-scripts/base.sh | sh
|
|
# Specific Script
|
|
curl -fsSL https://gitea.nicholaspease.com/npease/CoderTemplatesV2/raw/branch/main/container-scripts/${data.coder_parameter.language.value}.sh | sh
|
|
# WakaAPI Setup
|
|
echo "[settings]" >> ~/.wakatime.cfg && echo "api_url = ${var.waka_api_url}" >> ~/.wakatime.cfg && echo "api_key = ${var.waka_api_key}" >> ~/.wakatime.cfg
|
|
# Clone Git Repo && Setup Auto Git Login
|
|
git config --global url."https://api:${var.gitea_key}@gitea.nicholaspease.com/".insteadOf "https://gitea.nicholaspease.com/"
|
|
cd ~/
|
|
if [ "${data.coder_parameter.git_repo.value}" != " " ] && [ ! -d "${data.coder_workspace.me.name}" ]
|
|
then
|
|
git clone --recurse-submodules --remote-submodules ${data.coder_parameter.git_repo.value}
|
|
fi
|
|
|
|
EOT
|
|
|
|
# These environment variables allow you to make Git commits right away after creating a
|
|
# workspace. Note that they take precedence over configuration defined in ~/.gitconfig!
|
|
# You can remove this block if you'd prefer to configure Git manually or using
|
|
# dotfiles. (see docs/dotfiles.md)
|
|
env = {
|
|
GIT_AUTHOR_NAME = "Nicholas Pease"
|
|
GIT_COMMITTER_NAME = "Nicholas Pease"
|
|
GIT_AUTHOR_EMAIL = "me@nicholaspease.com"
|
|
GIT_COMMITTER_EMAIL = "me@nicholaspease.com"
|
|
}
|
|
|
|
# The following metadata blocks are optional. They are used to display
|
|
# information about your workspace in the dashboard. You can remove them
|
|
# if you don't want to display any information.
|
|
# For basic resources, you can use the `coder stat` command.
|
|
# If you need more control, you can write your own script.
|
|
metadata {
|
|
display_name = "CPU Usage"
|
|
key = "0_cpu_usage"
|
|
script = "coder stat cpu"
|
|
interval = 10
|
|
timeout = 1
|
|
}
|
|
|
|
metadata {
|
|
display_name = "RAM Usage"
|
|
key = "1_ram_usage"
|
|
script = "coder stat mem"
|
|
interval = 10
|
|
timeout = 1
|
|
}
|
|
|
|
metadata {
|
|
display_name = "Home Disk"
|
|
key = "3_home_disk"
|
|
script = "coder stat disk --path $${HOME}"
|
|
interval = 60
|
|
timeout = 1
|
|
}
|
|
|
|
metadata {
|
|
display_name = "CPU Usage (Host)"
|
|
key = "4_cpu_usage_host"
|
|
script = "coder stat cpu --host"
|
|
interval = 10
|
|
timeout = 1
|
|
}
|
|
|
|
metadata {
|
|
display_name = "Memory Usage (Host)"
|
|
key = "5_mem_usage_host"
|
|
script = "coder stat mem --host"
|
|
interval = 10
|
|
timeout = 1
|
|
}
|
|
|
|
metadata {
|
|
display_name = "Load Average (Host)"
|
|
key = "6_load_host"
|
|
# get load avg scaled by number of cores
|
|
script = <<EOT
|
|
echo "`cat /proc/loadavg | awk '{ print $1 }'` `nproc`" | awk '{ printf "%0.2f", $1/$2 }'
|
|
EOT
|
|
interval = 60
|
|
timeout = 1
|
|
}
|
|
|
|
metadata {
|
|
display_name = "Swap Usage (Host)"
|
|
key = "7_swap_host"
|
|
script = <<EOT
|
|
free -b | awk '/^Swap/ { printf("%.1f/%.1f", $3/1024.0/1024.0/1024.0, $2/1024.0/1024.0/1024.0) }'
|
|
EOT
|
|
interval = 10
|
|
timeout = 1
|
|
}
|
|
}
|
|
|
|
module "vscode-web" {
|
|
source = "registry.coder.com/modules/vscode-web/coder"
|
|
version = "1.0.0"
|
|
agent_id = coder_agent.main.id
|
|
accept_license = true
|
|
}
|
|
|
|
module "filebrowser" {
|
|
source = "registry.coder.com/modules/filebrowser/coder"
|
|
version = "1.0.0"
|
|
agent_id = coder_agent.main.id
|
|
}
|
|
|
|
module "coder-login" {
|
|
source = "registry.coder.com/modules/coder-login/coder"
|
|
version = "1.0.0"
|
|
agent_id = coder_agent.main.id
|
|
}
|
|
|
|
module "git-commit-signing" {
|
|
source = "registry.coder.com/modules/git-commit-signing/coder"
|
|
version = "1.0.0"
|
|
agent_id = coder_agent.main.id
|
|
}
|
|
|
|
resource "docker_volume" "home_volume" {
|
|
name = "coder-${data.coder_workspace.me.id}-home"
|
|
# Protect the volume from being deleted due to changes in attributes.
|
|
lifecycle {
|
|
ignore_changes = all
|
|
}
|
|
# Add labels in Docker to keep track of orphan resources.
|
|
labels {
|
|
label = "coder.owner"
|
|
value = data.coder_workspace.me.owner
|
|
}
|
|
labels {
|
|
label = "coder.owner_id"
|
|
value = data.coder_workspace.me.owner_id
|
|
}
|
|
labels {
|
|
label = "coder.workspace_id"
|
|
value = data.coder_workspace.me.id
|
|
}
|
|
# This field becomes outdated if the workspace is renamed but can
|
|
# be useful for debugging or cleaning out dangling volumes.
|
|
labels {
|
|
label = "coder.workspace_name_at_creation"
|
|
value = data.coder_workspace.me.name
|
|
}
|
|
}
|
|
|
|
resource "docker_image" "main" {
|
|
name = "coder-${data.coder_workspace.me.id}"
|
|
build {
|
|
context = "./dockerfiles/${data.coder_parameter.language.value}"
|
|
build_args = {
|
|
USER = local.username
|
|
}
|
|
}
|
|
triggers = {
|
|
dir_sha1 = sha1(join("", [for f in fileset(path.module, "dockerfiles/${data.coder_parameter.language.value}/*") : filesha1(f)]))
|
|
}
|
|
}
|
|
|
|
resource "docker_container" "workspace" {
|
|
count = data.coder_workspace.me.start_count
|
|
image = docker_image.main.name
|
|
# Uses lower() to avoid Docker restriction on container names.
|
|
name = "coder-${data.coder_workspace.me.owner}-${lower(data.coder_workspace.me.name)}"
|
|
# Hostname makes the shell more user friendly: coder@my-workspace:~$
|
|
hostname = data.coder_workspace.me.name
|
|
# Use the docker gateway if the access URL is 127.0.0.1
|
|
entrypoint = ["sh", "-c", replace(coder_agent.main.init_script, "/localhost|127\\.0\\.0\\.1/", "host.docker.internal")]
|
|
env = ["CODER_AGENT_TOKEN=${coder_agent.main.token}"]
|
|
host {
|
|
host = "host.docker.internal"
|
|
ip = "host-gateway"
|
|
}
|
|
volumes {
|
|
container_path = "/home/${local.username}"
|
|
volume_name = docker_volume.home_volume.name
|
|
read_only = false
|
|
}
|
|
# Add labels in Docker to keep track of orphan resources.
|
|
labels {
|
|
label = "coder.owner"
|
|
value = data.coder_workspace.me.owner
|
|
}
|
|
labels {
|
|
label = "coder.owner_id"
|
|
value = data.coder_workspace.me.owner_id
|
|
}
|
|
labels {
|
|
label = "coder.workspace_id"
|
|
value = data.coder_workspace.me.id
|
|
}
|
|
labels {
|
|
label = "coder.workspace_name"
|
|
value = data.coder_workspace.me.name
|
|
}
|
|
}
|