Skip to content

BUG(code-server): coder scripts running twice #23

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

Open
djarbz opened this issue Apr 26, 2025 · 0 comments
Open

BUG(code-server): coder scripts running twice #23

djarbz opened this issue Apr 26, 2025 · 0 comments

Comments

@djarbz
Copy link
Contributor

djarbz commented Apr 26, 2025

While debugging one of my templates, I noticed that the logs showed a double execution of the scripts.

To troubleshoot, I started with a scratch template and slowly added components back in until I noticed that the execution was duplicated.
It appears that the startup script defined in the resource "coder_agent" "main" block is unaffected.
The execution was only duplicated when I added the code-server module.

Testing Template

terraform {
  required_providers {
    coder = {
      source = "coder/coder"
    }
    # docker = {
    #   source = "kreuzwerker/docker"
    # }
    docker = {
      source = "bamhm182/docker"
    }
  }
}

variable "socket" {
  type        = string
  description = <<-EOF
  The Unix socket that the Docker daemon listens on and how containers
  communicate with the Docker daemon.

  Either Unix or TCP
  e.g., unix:///var/run/docker.sock

  EOF
  default = "unix:///var/run/docker.sock"
}

provider "coder" {
}

provider "docker" {
  host = var.socket
}

data "coder_provisioner" "me" {}

data "coder_workspace" "me" {}

data "coder_workspace_owner" "me" {}

resource "coder_agent" "main" {
  arch = data.coder_provisioner.me.arch
  os   = data.coder_provisioner.me.os
  display_apps {
    vscode          = false
    vscode_insiders = false
    web_terminal    = true
    ssh_helper      = true
  }

  startup_script_behavior = "blocking"
  startup_script = <<-EOT
    set -euo pipefail

    # Prepare user home with default files on first start.
    if [ ! -f ~/.init_done ]; then
      cp -rT /etc/skel ~
      touch ~/.init_done
    fi

    # Add any commands that should be executed at workspace startup (e.g install requirements, start a program, etc) here

    # Cleanup old JetBrains versions
    DIST_DIR="$${HOME}/.cache/JetBrains/RemoteDev/dist/"
    if [[ -d $${DIST_DIR} ]]; then
      find $${DIST_DIR} -mindepth 1 -maxdepth 1 -type d -printf '%T@\t%p\n' \
        | sort -n | head -n -1 | cut -f2- | xargs -I {} rm -rf "{}"
    else
      echo "JetBrains Dist Cache Directory $${DIST_DIR} does not exist."
    fi

    # Cleanup old Code-Server versions
    DIST_DIR="$${HOME}/.cache/code-server/"
    if [[ -d $${DIST_DIR} ]]; then
      find $${DIST_DIR} -mindepth 1 -maxdepth 1 -type d -printf '%T@\t%p\n' \
        | sort -n | head -n -1 | cut -f2- | xargs -I {} rm -rf "{}"
    else
      echo "Code-Server Cache Directory $${DIST_DIR} does not exist."
    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     = coalesce(data.coder_workspace_owner.me.full_name, data.coder_workspace_owner.me.name)
    GIT_AUTHOR_EMAIL    = "${data.coder_workspace_owner.me.email}"
    GIT_COMMITTER_NAME  = coalesce(data.coder_workspace_owner.me.full_name, data.coder_workspace_owner.me.name)
    GIT_COMMITTER_EMAIL = "${data.coder_workspace_owner.me.email}"
  }

  # 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
  }
}



resource "docker_container" "workspace" {
  image = "docker.io/codercom/enterprise-base:ubuntu"
  # Uses lower() to avoid Docker restriction on container names.
  name = "coder-${data.coder_workspace_owner.me.name}-${lower(data.coder_workspace.me.name)}"
  # Hostname makes the shell more user friendly: coder@my-workspace:~$
  hostname = data.coder_workspace.me.name
  # Memory limit for the container in MBs. 0 is unrestricted.
  memory = 0
  # 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"
  }
  # Add labels in Docker to keep track of orphan resources.
  labels {
    label = "coder.owner"
    value = data.coder_workspace_owner.me.name
  }
  labels {
    label = "coder.owner_id"
    value = data.coder_workspace_owner.me.id
  }
  labels {
    label = "coder.workspace_id"
    value = data.coder_workspace.me.id
  }
  labels {
    label = "coder.workspace_name"
    value = data.coder_workspace.me.name
  }
}



# Use this to set environment variables in your workspace
# details: https://registry.terraform.io/providers/coder/coder/latest/docs/resources/env
resource "coder_env" "welcome_message" {
  agent_id = coder_agent.main.id
  name     = "WELCOME_MESSAGE"
  value    = "Welcome to your Coder workspace!"
}

# Stores Git configuration from Coder credentials
module "git-config" {
  count     = data.coder_workspace.me.start_count
  source    = "registry.coder.com/modules/git-config/coder"
  version   = ">= 1.0.0"
  agent_id  = coder_agent.main.id
  # allow_username_change = true
  # allow_email_change    = true
}

# A file browser for your workspace
module "filebrowser" {
  source        = "registry.coder.com/modules/filebrowser/coder"
  version       = "1.0.27"  # Use 1.0.27 until base-path is fixed in release 1.0.31
  agent_id      = coder_agent.main.id
  database_path = "$HOME/.config/filebrowser.db"
  folder        = "$HOME"
}

# Automatically install code-server in a workspace, create an app to access it via the dashboard, install extensions, and pre-configure editor settings.
module "code-server" {
  source      = "registry.coder.com/modules/code-server/coder"
  version     = ">= 1.0.0"
  agent_id    = coder_agent.main.id
}

Logs

�[0;1mInstalling filebrowser 

⚙️ Creating settings file...
�[0;1mInstalling code-server!
Downloading File Browser for linux/amd64...
JetBrains Dist Cache Directory /home/coder/.cache/JetBrains/RemoteDev/dist/ does not exist.
Code-Server Cache Directory /home/coder/.cache/code-server/ does not exist.
https://github.com/filebrowser/filebrowser/releases/download/v2.32.0/linux-amd64-filebrowser.tar.gz
Extracting...
Putting filemanager in /usr/local/bin (may require password)
Successfully installed
🥳 Installation complete! 

👷 Starting filebrowser in background... 

📂 Serving /home/coder at http://localhost:13339 

Running 'filebrowser --noauth --root /home/coder --port 13339 -d /home/coder/.config/filebrowser.db' 

📝 Logs at /tmp/filebrowser.log 
                        
🥳 code-server has been installed in /tmp/code-server

👷 Running code-server in the background...
Check logs at /tmp/code-server.log!
https://github.com/filebrowser/filebrowser/releases/download/v2.32.0/linux-amd64-filebrowser.tar.gz
Extracting...
Putting filemanager in /usr/local/bin (may require password)
Successfully installed
🥳 Installation complete! 

👷 Starting filebrowser in background... 

📂 Serving /home/coder at http://localhost:13339 

Running 'filebrowser --noauth --root /home/coder --port 13339 -d /home/coder/.config/filebrowser.db' 

📝 Logs at /tmp/filebrowser.log 
                        
🥳 code-server has been installed in /tmp/code-server

👷 Running code-server in the background...
Check logs at /tmp/code-server.log!
@Parkreiner Parkreiner transferred this issue from coder/modules May 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant