Skip to content

feat(code-server): add machine settings option #449

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
26 changes: 9 additions & 17 deletions code-server/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
coder = {
source = "coder/coder"
version = ">= 2.1"
version = ">= 0.17"
}
}
}
Expand Down Expand Up @@ -39,11 +39,17 @@ variable "slug" {
}

variable "settings" {
type = any
type = map(string)
description = "A map of settings to apply to code-server."
default = {}
}

variable "machine-settings" {
type = map(string)
description = "A map of template level machine settings to apply to code-server. This will be overwritten at each container start."
default = {}
}

variable "folder" {
type = string
description = "The folder to open in code-server."
Expand Down Expand Up @@ -122,20 +128,6 @@ variable "subdomain" {
default = false
}

variable "open_in" {
type = string
description = <<-EOT
Determines where the app will be opened. Valid values are `"tab"` and `"slim-window" (default)`.
`"tab"` opens in a new tab in the same browser window.
`"slim-window"` opens a new browser window without navigation controls.
EOT
default = "slim-window"
validation {
condition = contains(["tab", "slim-window"], var.open_in)
error_message = "The 'open_in' variable must be one of: 'tab', 'slim-window'."
}
}

resource "coder_script" "code-server" {
agent_id = var.agent_id
display_name = "code-server"
Expand All @@ -149,6 +141,7 @@ resource "coder_script" "code-server" {
INSTALL_PREFIX : var.install_prefix,
// This is necessary otherwise the quotes are stripped!
SETTINGS : replace(jsonencode(var.settings), "\"", "\\\""),
MACHINE_SETTINGS : replace(jsonencode(var.machine-settings), "\"", "\\\""),
OFFLINE : var.offline,
USE_CACHED : var.use_cached,
USE_CACHED_EXTENSIONS : var.use_cached_extensions,
Expand Down Expand Up @@ -180,7 +173,6 @@ resource "coder_app" "code-server" {
subdomain = var.subdomain
share = var.share
order = var.order
open_in = var.open_in

healthcheck {
url = "http://localhost:${var.port}/healthz"
Expand Down
18 changes: 6 additions & 12 deletions code-server/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ if [ ! -f ~/.local/share/code-server/User/settings.json ]; then
echo "${SETTINGS}" > ~/.local/share/code-server/User/settings.json
fi

# Apply/overwrite template based settings
echo "⚙️ Creating machine settings file..."
mkdir -p ~/.local/share/code-server/Machine
echo "${MACHINE_SETTINGS}" > ~/.local/share/code-server/Machine/settings.json

# Check if code-server is already installed for offline
if [ "${OFFLINE}" = true ]; then
if [ -f "$CODE_SERVER" ]; then
Expand All @@ -42,11 +47,6 @@ fi
if [ ! -f "$CODE_SERVER" ] || [ "${USE_CACHED}" != true ]; then
printf "$${BOLD}Installing code-server!\n"

# Clean up from other install (in case install prefix changed).
if [ -n "$CODER_SCRIPT_BIN_DIR" ] && [ -e "$CODER_SCRIPT_BIN_DIR/code-server" ]; then
rm "$CODER_SCRIPT_BIN_DIR/code-server"
fi

ARGS=(
"--method=standalone"
"--prefix=${INSTALL_PREFIX}"
Expand All @@ -63,11 +63,6 @@ if [ ! -f "$CODE_SERVER" ] || [ "${USE_CACHED}" != true ]; then
printf "🥳 code-server has been installed in ${INSTALL_PREFIX}\n\n"
fi

# Make the code-server available in PATH.
if [ -n "$CODER_SCRIPT_BIN_DIR" ] && [ ! -e "$CODER_SCRIPT_BIN_DIR/code-server" ]; then
ln -s "$CODE_SERVER" "$CODER_SCRIPT_BIN_DIR/code-server"
fi

# Get the list of installed extensions...
LIST_EXTENSIONS=$($CODE_SERVER --list-extensions $EXTENSION_ARG)
readarray -t EXTENSIONS_ARRAY <<< "$LIST_EXTENSIONS"
Expand Down Expand Up @@ -114,8 +109,7 @@ if [ "${AUTO_INSTALL_EXTENSIONS}" = true ]; then

if [ -f "$WORKSPACE_DIR/.vscode/extensions.json" ]; then
printf "🧩 Installing extensions from %s/.vscode/extensions.json...\n" "$WORKSPACE_DIR"
# Use sed to remove single-line comments before parsing with jq
extensions=$(sed 's|//.*||g' "$WORKSPACE_DIR"/.vscode/extensions.json | jq -r '.recommendations[]')
extensions=$(jq -r '.recommendations[]' "$WORKSPACE_DIR"/.vscode/extensions.json)
for extension in $extensions; do
if extension_installed "$extension"; then
continue
Expand Down