Skip to content

Commit 9452763

Browse files
authored
add: rstudio module (#327)
1 parent 7732865 commit 9452763

File tree

5 files changed

+206
-0
lines changed

5 files changed

+206
-0
lines changed

.icons/rstudio.svg

Lines changed: 1 addition & 0 deletions
Loading
302 KB
Loading
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
display_name: RStudio Server
3+
description: Deploy the Rocker Project distribution of RStudio Server in your Coder workspace.
4+
icon: ../../../../.icons/rstudio.svg
5+
verified: true
6+
tags: [rstudio, ide, web]
7+
---
8+
9+
# RStudio Server
10+
11+
> [!NOTE]
12+
> This module requires `docker` to be available in the workspace. Check [Docker in Workspaces](https://coder.com/docs/admin/templates/extending-templates/docker-in-workspaces) to learn how you can set it up.
13+
14+
Deploy the Rocker Project distribution of RStudio Server in your Coder workspace.
15+
16+
![RStudio Server](../../.images/rstudio-server.png)
17+
18+
```tf
19+
module "rstudio-server" {
20+
count = data.coder_workspace.me.start_count
21+
source = "registry.coder.com/coder/rstudio-server/coder"
22+
version = "0.9.0"
23+
agent_id = coder_agent.example.id
24+
}
25+
```
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
terraform {
2+
required_version = ">= 1.0"
3+
4+
required_providers {
5+
coder = {
6+
source = "coder/coder"
7+
version = ">= 2.5"
8+
}
9+
}
10+
}
11+
12+
# Add required variables for your modules and remove any unneeded variables
13+
variable "agent_id" {
14+
type = string
15+
description = "The ID of a Coder agent."
16+
}
17+
18+
variable "docker_socket" {
19+
type = string
20+
description = "(Optional) Docker socket URI"
21+
default = ""
22+
}
23+
24+
variable "rstudio_server_version" {
25+
type = string
26+
description = "RStudio Server version"
27+
default = "4.5.1"
28+
}
29+
30+
variable "disable_auth" {
31+
type = bool
32+
description = "Disable auth"
33+
default = true
34+
}
35+
36+
variable "rstudio_user" {
37+
type = string
38+
description = "RStudio user"
39+
default = "rstudio"
40+
sensitive = true
41+
}
42+
43+
variable "rstudio_password" {
44+
type = string
45+
description = "RStudio password"
46+
default = "rstudio"
47+
sensitive = true
48+
}
49+
50+
variable "project_path" {
51+
type = string
52+
description = "The path to RStudio project, it will be mounted in the container."
53+
default = null
54+
}
55+
56+
variable "port" {
57+
type = number
58+
description = "The port to run rstudio-server on."
59+
default = 8787
60+
}
61+
62+
variable "enable_renv" {
63+
type = bool
64+
description = "If renv.lock exists, renv will restore the environment and install dependencies"
65+
default = true
66+
}
67+
68+
variable "renv_cache_volume" {
69+
type = string
70+
description = "The name of the volume used by Renv to preserve dependencies between container restarts"
71+
default = "renv-cache-volume"
72+
}
73+
74+
variable "share" {
75+
type = string
76+
default = "owner"
77+
validation {
78+
condition = var.share == "owner" || var.share == "authenticated" || var.share == "public"
79+
error_message = "Incorrect value. Please set either 'owner', 'authenticated', or 'public'."
80+
}
81+
}
82+
83+
variable "order" {
84+
type = number
85+
description = "The order determines the position of app in the UI presentation. The lowest order is shown first and apps with equal order are sorted by name (ascending order)."
86+
default = null
87+
}
88+
89+
variable "group" {
90+
type = string
91+
description = "The name of a group that this app belongs to."
92+
default = null
93+
}
94+
95+
resource "coder_script" "rstudio-server" {
96+
agent_id = var.agent_id
97+
display_name = "rstudio-server"
98+
icon = "/icon/rstudio.svg"
99+
script = templatefile("${path.module}/run.sh", {
100+
DOCKER_HOST : var.docker_socket,
101+
SERVER_VERSION : var.rstudio_server_version,
102+
DISABLE_AUTH : var.disable_auth,
103+
RSTUDIO_USER : var.rstudio_user,
104+
RSTUDIO_PASSWORD : var.rstudio_password,
105+
PROJECT_PATH : var.project_path,
106+
PORT : var.port,
107+
ENABLE_RENV : var.enable_renv,
108+
RENV_CACHE_VOLUME : var.renv_cache_volume,
109+
})
110+
run_on_start = true
111+
}
112+
113+
resource "coder_app" "rstudio-server" {
114+
agent_id = var.agent_id
115+
slug = "rstudio-server"
116+
display_name = "RStudio Server"
117+
url = "http://localhost:${var.port}"
118+
icon = "/icon/rstudio.svg"
119+
subdomain = true
120+
share = var.share
121+
order = var.order
122+
group = var.group
123+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env sh
2+
3+
set -eu
4+
5+
BOLD='\033[0;1m'
6+
RESET='\033[0m'
7+
8+
printf "$${BOLD}Starting RStudio Server (Rocker)...$${RESET}\n"
9+
10+
# Wait for docker to become ready
11+
max_attempts=10
12+
delay=2
13+
attempt=1
14+
15+
while ! docker ps; do
16+
if [ $attempt -ge $max_attempts ]; then
17+
echo "Failed to list containers after $${max_attempts} attempts."
18+
exit 1
19+
fi
20+
echo "Attempt $${attempt} failed, retrying in $${delay}s..."
21+
sleep $delay
22+
attempt=$(expr "$attempt" + 1)
23+
delay=$(expr "$delay" \* 2) # exponential backoff
24+
done
25+
26+
# Pull the specified version
27+
IMAGE="rocker/rstudio:${SERVER_VERSION}"
28+
docker pull "$${IMAGE}"
29+
30+
# Create (or reuse) a persistent renv cache volume
31+
docker volume create "${RENV_CACHE_VOLUME}"
32+
33+
# Run container (auto-remove on stop)
34+
docker run -d --rm \
35+
--name rstudio-server \
36+
-p "${PORT}:8787" \
37+
-e DISABLE_AUTH="${DISABLE_AUTH}" \
38+
-e USER="${RSTUDIO_USER}" \
39+
-e PASSWORD="${RSTUDIO_PASSWORD}" \
40+
-e RENV_PATHS_CACHE="/renv/cache" \
41+
-v "${PROJECT_PATH}:/home/${RSTUDIO_USER}/project" \
42+
-v "${RENV_CACHE_VOLUME}:/renv/cache" \
43+
"$${IMAGE}"
44+
45+
# Make RENV_CACHE_VOLUME writable to USER
46+
docker exec rstudio-server bash -c 'chmod -R 0777 /renv/cache'
47+
48+
# Optional renv restore
49+
if [ "${ENABLE_RENV}" = "true" ] && [ -f "${PROJECT_PATH}/renv.lock" ]; then
50+
echo "Restoring R environment via renv..."
51+
docker exec -u "${RSTUDIO_USER}" rstudio-server R -q -e \
52+
'if (!requireNamespace("renv", quietly = TRUE)) install.packages("renv", repos="https://cloud.r-project.org"); renv::restore(prompt = FALSE)'
53+
fi
54+
55+
[ "${DISABLE_AUTH}" != "true" ] && echo "User: ${RSTUDIO_USER}"
56+
57+
printf "\n$${BOLD}RStudio Server ${SERVER_VERSION} is running on port ${PORT}$${RESET}\n"

0 commit comments

Comments
 (0)