Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.

Commit fc91462

Browse files
feat(code-server): add code-server offline and cache support (#184)
1 parent fdbb2e3 commit fc91462

File tree

4 files changed

+109
-13
lines changed

4 files changed

+109
-13
lines changed

code-server/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,30 @@ module "code-server" {
7979
extensions = ["dracula-theme.theme-dracula", "ms-azuretools.vscode-docker"]
8080
}
8181
```
82+
83+
### Offline and Use Cached Modes
84+
85+
By default the module looks for code-server at `/tmp/code-server` but this can be changed with `install_prefix`.
86+
87+
Run an existing copy of code-server if found, otherwise download from GitHub:
88+
89+
```tf
90+
module "code-server" {
91+
source = "registry.coder.com/modules/code-server/coder"
92+
version = "1.0.8"
93+
agent_id = coder_agent.example.id
94+
use_cached = true
95+
extensions = ["dracula-theme.theme-dracula", "ms-azuretools.vscode-docker"]
96+
}
97+
```
98+
99+
Just run code-server in the background, don't fetch it from GitHub:
100+
101+
```tf
102+
module "code-server" {
103+
source = "registry.coder.com/modules/code-server/coder"
104+
version = "1.0.8"
105+
agent_id = coder_agent.example.id
106+
offline = true
107+
}
108+
```

code-server/main.test.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { describe, expect, it } from "bun:test";
2-
import { runTerraformInit, testRequiredVariables } from "../test";
2+
import {
3+
runTerraformApply,
4+
runTerraformInit,
5+
testRequiredVariables,
6+
} from "../test";
37

48
describe("code-server", async () => {
59
await runTerraformInit(import.meta.dir);
@@ -8,5 +12,27 @@ describe("code-server", async () => {
812
agent_id: "foo",
913
});
1014

15+
it("use_cached and offline can not be used together", () => {
16+
const t = async () => {
17+
await runTerraformApply(import.meta.dir, {
18+
agent_id: "foo",
19+
use_cached: "true",
20+
offline: "true",
21+
});
22+
};
23+
expect(t).toThrow("Offline and Use Cached can not be used together");
24+
});
25+
26+
it("offline and extensions can not be used together", () => {
27+
const t = async () => {
28+
await runTerraformApply(import.meta.dir, {
29+
agent_id: "foo",
30+
offline: "true",
31+
extensions: '["1", "2"]',
32+
});
33+
};
34+
expect(t).toThrow("Offline mode does not allow extensions to be installed");
35+
});
36+
1137
// More tests depend on shebang refactors
1238
});

code-server/main.tf

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ variable "order" {
8383
default = null
8484
}
8585

86+
variable "offline" {
87+
type = bool
88+
description = "Just run code-server in the background, don't fetch it from GitHub"
89+
default = false
90+
}
91+
92+
variable "use_cached" {
93+
type = bool
94+
description = "Uses cached copy code-server in the background, otherwise fetched it from GitHub"
95+
default = false
96+
}
97+
8698
resource "coder_script" "code-server" {
8799
agent_id = var.agent_id
88100
display_name = "code-server"
@@ -96,8 +108,22 @@ resource "coder_script" "code-server" {
96108
INSTALL_PREFIX : var.install_prefix,
97109
// This is necessary otherwise the quotes are stripped!
98110
SETTINGS : replace(jsonencode(var.settings), "\"", "\\\""),
111+
OFFLINE : var.offline,
112+
USE_CACHED : var.use_cached,
99113
})
100114
run_on_start = true
115+
116+
lifecycle {
117+
precondition {
118+
condition = !var.offline || length(var.extensions) == 0
119+
error_message = "Offline mode does not allow extensions to be installed"
120+
}
121+
122+
precondition {
123+
condition = !var.offline || !var.use_cached
124+
error_message = "Offline and Use Cached can not be used together"
125+
}
126+
}
101127
}
102128

103129
resource "coder_app" "code-server" {

code-server/run.sh

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,34 @@ EXTENSIONS=("${EXTENSIONS}")
44
BOLD='\033[0;1m'
55
CODE='\033[36;40;1m'
66
RESET='\033[0m'
7+
CODE_SERVER="${INSTALL_PREFIX}/bin/code-server"
8+
9+
function run_code_server() {
10+
echo "👷 Running code-server in the background..."
11+
echo "Check logs at ${LOG_PATH}!"
12+
$CODE_SERVER --auth none --port "${PORT}" --app-name "${APP_NAME}" > "${LOG_PATH}" 2>&1 &
13+
}
14+
15+
# Check if the settings file exists...
16+
if [ ! -f ~/.local/share/code-server/User/settings.json ]; then
17+
echo "⚙️ Creating settings file..."
18+
mkdir -p ~/.local/share/code-server/User
19+
echo "${SETTINGS}" > ~/.local/share/code-server/User/settings.json
20+
fi
21+
22+
# Check if code-server is already installed for offline or cached mode
23+
if [ -f "$CODE_SERVER" ]; then
24+
if [ "${OFFLINE}" = true ] || [ "${USE_CACHED}" = true ]; then
25+
echo "🥳 Found a copy of code-server"
26+
run_code_server
27+
exit 0
28+
fi
29+
fi
30+
# Offline mode always expects a copy of code-server to be present
31+
if [ "${OFFLINE}" = true ]; then
32+
echo "Failed to find a copy of code-server"
33+
exit 1
34+
fi
735

836
printf "$${BOLD}Installing code-server!\n"
937

@@ -22,8 +50,6 @@ if [ $? -ne 0 ]; then
2250
fi
2351
printf "🥳 code-server has been installed in ${INSTALL_PREFIX}\n\n"
2452

25-
CODE_SERVER="${INSTALL_PREFIX}/bin/code-server"
26-
2753
# Install each extension...
2854
IFS=',' read -r -a EXTENSIONLIST <<< "$${EXTENSIONS}"
2955
for extension in "$${EXTENSIONLIST[@]}"; do
@@ -38,13 +64,4 @@ for extension in "$${EXTENSIONLIST[@]}"; do
3864
fi
3965
done
4066

41-
# Check if the settings file exists...
42-
if [ ! -f ~/.local/share/code-server/User/settings.json ]; then
43-
echo "⚙️ Creating settings file..."
44-
mkdir -p ~/.local/share/code-server/User
45-
echo "${SETTINGS}" > ~/.local/share/code-server/User/settings.json
46-
fi
47-
48-
echo "👷 Running code-server in the background..."
49-
echo "Check logs at ${LOG_PATH}!"
50-
$CODE_SERVER --auth none --port ${PORT} --app-name "${APP_NAME}" > ${LOG_PATH} 2>&1 &
67+
run_code_server

0 commit comments

Comments
 (0)