Skip to content

fix: Reduce variables needed for Docker template #3442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1153,7 +1153,7 @@ func startBuiltinPostgres(ctx context.Context, cfg config.Root, logger slog.Logg
stdlibLogger := slog.Stdlib(ctx, logger.Named("postgres"), slog.LevelDebug)
ep := embeddedpostgres.NewDatabase(
embeddedpostgres.DefaultConfig().
Version(embeddedpostgres.V13).
Version(embeddedpostgres.V14).
BinariesPath(filepath.Join(cfg.PostgresPath(), "bin")).
DataPath(filepath.Join(cfg.PostgresPath(), "data")).
RuntimePath(filepath.Join(cfg.PostgresPath(), "runtime")).
Expand Down
23 changes: 4 additions & 19 deletions examples/templates/docker-code-server/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,26 @@ terraform {
required_providers {
coder = {
source = "coder/coder"
version = "0.4.3"
version = "0.4.5"
}
docker = {
source = "kreuzwerker/docker"
version = "~> 2.16.0"
version = "~> 2.20.2"
}
}
}

variable "docker_host" {
description = "Specify location of Docker socket (check `docker context ls` if you're not sure)"
sensitive = true
}

variable "docker_arch" {
description = "Specify architecture of docker host (amd64, arm64, or armv7)"
validation {
condition = contains(["amd64", "arm64", "armv7"], var.docker_arch)
error_message = "Value must be amd64, arm64, or armv7."
}
sensitive = true
}

provider "coder" {
data "coder_provisioner" "me" {
}

provider "docker" {
host = var.docker_host
}

data "coder_workspace" "me" {
}

resource "coder_agent" "main" {
arch = var.docker_arch
arch = data.coder_provisioner.me.arch
os = "linux"
startup_script = "code-server --auth none"

Expand Down
47 changes: 4 additions & 43 deletions examples/templates/docker-image-builds/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,26 @@ terraform {
required_providers {
coder = {
source = "coder/coder"
version = "0.4.3"
version = "0.4.5"
}
docker = {
source = "kreuzwerker/docker"
version = "~> 2.16.0"
version = "~> 2.20.2"
}
}
}

# Admin parameters
variable "step1_docker_host_warning" {
description = <<-EOF
Is Docker running on the Coder host?

This template will use the Docker socket present on
the Coder host, which is not necessarily your local machine.

You can specify a different host in the template file and
suppress this warning.
EOF
validation {
condition = contains(["Continue using /var/run/docker.sock on the Coder host"], var.step1_docker_host_warning)
error_message = "Cancelling template create."
}

sensitive = true
}
variable "step2_arch" {
description = "arch: What architecture is your Docker host on?"
validation {
condition = contains(["amd64", "arm64", "armv7"], var.step2_arch)
error_message = "Value must be amd64, arm64, or armv7."
}
sensitive = true
}
variable "step3_OS" {
description = <<-EOF
What operating system is your Coder host on?
EOF

validation {
condition = contains(["MacOS", "Windows", "Linux"], var.step3_OS)
error_message = "Value must be MacOS, Windows, or Linux."
}
sensitive = true
data "coder_provisioner" "me" {
}

provider "docker" {
host = var.step3_OS == "Windows" ? "npipe:////.//pipe//docker_engine" : "unix:///var/run/docker.sock"
}

provider "coder" {
}

data "coder_workspace" "me" {
}

resource "coder_agent" "main" {
arch = var.step2_arch
arch = data.coder_provisioner.me.arch
os = "linux"
}

Expand Down
13 changes: 6 additions & 7 deletions examples/templates/docker-with-dotfiles/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,19 @@ terraform {
required_providers {
coder = {
source = "coder/coder"
version = "0.4.3"
version = "0.4.5"
}
docker = {
source = "kreuzwerker/docker"
version = "~> 2.16.0"
version = "~> 2.20.2"
}
}
}

provider "docker" {
host = "unix:///var/run/docker.sock"
data "coder_provisioner" "me" {
}

provider "coder" {
provider "docker" {
}

data "coder_workspace" "me" {
Expand All @@ -38,13 +37,13 @@ variable "dotfiles_uri" {
}

resource "coder_agent" "main" {
arch = "amd64"
arch = data.coder_provisioner.me.arch
os = "linux"
startup_script = var.dotfiles_uri != "" ? "coder dotfiles -y ${var.dotfiles_uri}" : null
}

resource "docker_volume" "home_volume" {
name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}-root"
name = "coder-${data.coder_workspace.me.owner}-${lower(data.coder_workspace.me.name)}-root"
}

resource "docker_container" "workspace" {
Expand Down
63 changes: 6 additions & 57 deletions examples/templates/docker/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,26 @@ terraform {
required_providers {
coder = {
source = "coder/coder"
version = "0.4.3"
version = "0.4.5"
}
docker = {
source = "kreuzwerker/docker"
version = "~> 2.16.0"
version = "~> 2.20.2"
}
}
}

# Admin parameters

# Comment this out if you are specifying a different docker
# host on the "docker" provider below.
variable "step1_docker_host_warning" {
description = <<-EOF
This template will use the Docker socket present on
the Coder host, which is not necessarily your local machine.

You can specify a different host in the template file and
suppress this warning.
EOF
validation {
condition = contains(["Continue using /var/run/docker.sock on the Coder host"], var.step1_docker_host_warning)
error_message = "Cancelling template create."
}

sensitive = true
}
variable "step2_arch" {
description = <<-EOF
arch: What architecture is your Docker host on?

note: codercom/enterprise-* images are only built for amd64
EOF

validation {
condition = contains(["amd64", "arm64", "armv7"], var.step2_arch)
error_message = "Value must be amd64, arm64, or armv7."
}
sensitive = true
}
variable "step3_OS" {
description = <<-EOF
What operating system is your Coder host on?
EOF

validation {
condition = contains(["MacOS", "Windows", "Linux"], var.step3_OS)
error_message = "Value must be MacOS, Windows, or Linux."
}
sensitive = true
data "coder_provisioner" "me" {
}

provider "docker" {
host = var.step3_OS == "Windows" ? "npipe:////.//pipe//docker_engine" : "unix:///var/run/docker.sock"
}

provider "coder" {
}

data "coder_workspace" "me" {
}

resource "coder_agent" "main" {
arch = var.step2_arch
arch = data.coder_provisioner.me.arch
os = "linux"
startup_script = <<EOF
#!/bin/sh
Expand Down Expand Up @@ -104,11 +59,10 @@ variable "docker_image" {
"codercom/enterprise-intellij:ubuntu", "codercom/enterprise-golang:ubuntu"], var.docker_image)
error_message = "Invalid Docker image!"
}

}

resource "docker_volume" "home_volume" {
name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}-home"
name = "coder-${data.coder_workspace.me.owner}-${lower(data.coder_workspace.me.name)}-home"
}

resource "docker_container" "workspace" {
Expand All @@ -121,12 +75,7 @@ resource "docker_container" "workspace" {
dns = ["1.1.1.1"]
# Use the docker gateway if the access URL is 127.0.0.1
command = [
"sh", "-c",
<<EOT
trap '[ $? -ne 0 ] && echo === Agent script exited with non-zero code. Sleeping infinitely to preserve logs... && sleep infinity' EXIT
${replace(coder_agent.main.init_script, "localhost", "host.docker.internal")}
EOT
]
"sh", "-c", replace(coder_agent.main.init_script, "localhost", "host.docker.internal")]
env = ["CODER_AGENT_TOKEN=${coder_agent.main.token}"]
host {
host = "host.docker.internal"
Expand Down
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
bat
drpc.defaultPackage.${system}
exa
getopt
git
go-migrate
go_1_19
golangci-lint
gopls
gotestsum
helm
jq
nfpm
nodePackages.typescript
Expand Down