playground/docker-stack/coder/docker-service-simple/main.tf

146 lines
3.5 KiB
HCL

terraform {
required_providers {
coder = {
source = "coder/coder"
version = "0.5.3"
}
docker = {
source = "kreuzwerker/docker"
version = "~> 2.20.2"
}
}
}
data "coder_provisioner" "me" {
}
provider "docker" {
}
data "coder_workspace" "me" {
}
locals {
dcw_owner = replace(replace("${data.coder_workspace.me.owner}", "~", "~~"), "-", "~-")
dcw_name = replace(replace("${data.coder_workspace.me.name}", "~", "~~"), "-", "~-")
##workspace_name = "${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}"
workspace_name = "${local.dcw_owner}-${local.dcw_name}"
## this points to the workspace parent directory accessible from the docker host.
workspace_dir = "/home/gfs-data/coder/workspace/${local.workspace_name}"
## this indicates the view of workspace_dir from within the coder server container.
## the binding mount directory in coder.yml must match the leading directory in this item.
server_workspace_dir = "/workspace/${local.workspace_name}"
}
resource "null_resource" "workspace" {
count = 1
triggers = {
dir = local.server_workspace_dir
}
provisioner "local-exec" {
command = "mkdir ${self.triggers.dir}"
}
provisioner "local-exec" {
when = destroy
command = "rm -rf ${self.triggers.dir}"
}
}
resource "coder_agent" "main" {
arch = data.coder_provisioner.me.arch
os = "linux"
startup_script = "code-server --auth none"
# 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 = "${data.coder_workspace.me.owner}"
GIT_COMMITTER_NAME = "${data.coder_workspace.me.owner}"
GIT_AUTHOR_EMAIL = "${data.coder_workspace.me.owner_email}"
GIT_COMMITTER_EMAIL = "${data.coder_workspace.me.owner_email}"
}
}
resource "coder_app" "code-server" {
agent_id = coder_agent.main.id
name = "code-server"
url = "http://localhost:8080/?folder=/home/coder"
icon = "/icon/code.svg"
subdomain = false
share = "owner"
healthcheck {
url = "http://localhost:8080/healthz"
interval = 3
threshold = 10
}
}
##resource "docker_volume" "home_volume" {
## name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}-root"
##}
resource "docker_service" "workspace" {
##depends_on = [local_file.workspace]
depends_on = [null_resource.workspace]
count = data.coder_workspace.me.start_count
name = "coder-${data.coder_workspace.me.owner}-${lower(data.coder_workspace.me.name)}"
task_spec {
container_spec {
image = "codercom/code-server:4.8.3"
hostname = lower(data.coder_workspace.me.name)
command = ["sh"]
##args = ["-c", replace(coder_agent.main.init_script, "/localhost|127\\.0\\.0\\.1/", "host.docker.internal")]
args = ["-c", replace(coder_agent.main.init_script, "/localhost|127\\.0\\.0\\.1/", "coder-service-host") ]
env = {
CODER_AGENT_TOKEN = "${coder_agent.main.token}"
}
#hosts {
# host = "host.docker.internal"
# ip = "host-gateway"
#}
mounts {
target = "/home/coder"
source = local.workspace_dir
type = "bind"
read_only = false
}
##dir = "/home/coder"
##user = "root"
}
resources {
limits {
memory_bytes = 1000000000
}
}
#placement {
# constraints = [
# "node.role == manager"
# ]
#}
force_update = 0
networks = ["coder-network"]
}
mode {
replicated {
replicas = 1
}
}
}