Skip to content
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

add rstudio bash script #137

Merged
merged 9 commits into from
Sep 21, 2023
2 changes: 1 addition & 1 deletion .devcontainer/4.1/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

"postCreateCommand": "bash ./.devcontainer/postCreateCommand.sh",

"postAttachCommand": "$BROWSER \"https://${CODESPACE_NAME}-8787.${GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}/\"",
"postAttachCommand": "$BROWSER \"https://${CODESPACE_NAME}-8787.${GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}/\"; echo 'To open rstudio in your browser, just type rstudio in the terminal'",

"customizations": {
"vscode": {
Expand Down
2 changes: 1 addition & 1 deletion .devcontainer/4.2/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

"postCreateCommand": "bash ./.devcontainer/postCreateCommand.sh",

"postAttachCommand": "$BROWSER \"https://${CODESPACE_NAME}-8787.${GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}/\"",
"postAttachCommand": "$BROWSER \"https://${CODESPACE_NAME}-8787.${GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}/\"; echo 'To open rstudio in your browser, just type rstudio in the terminal'",

"customizations": {
"vscode": {
Expand Down
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

"postCreateCommand": "bash ./.devcontainer/postCreateCommand.sh",

"postAttachCommand": "$BROWSER \"https://${CODESPACE_NAME}-8787.${GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}/\"",
"postAttachCommand": "$BROWSER \"https://${CODESPACE_NAME}-8787.${GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}/\"; echo 'To open rstudio in your browser, just type rstudio in the terminal'",

"customizations": {
"vscode": {
Expand Down
7 changes: 6 additions & 1 deletion .devcontainer/postCreateCommand.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#!/bin/bash

# move script rstudio into /usr/bin
cp "$(pwd)/.devcontainer/rstudio.sh" /usr/bin/rstudio
galachad marked this conversation as resolved.
Show resolved Hide resolved

# Restore renv and install staged dependencies
R -q -e 'renv::activate(profile = paste(R.version$major, substr(R.version$minor, 1, 1), sep = ".")); renv::restore(); staged.dependencies::install_deps(staged.dependencies::dependency_table(project = ".", verbose = 1), verbose = 1);'

jq --arg folder "$(pwd)/" '. + { "initial_working_directory": $folder }' .devcontainer/rstudio-prefs.json > ~/.config/rstudio/rstudio-prefs.json
# Define rstudio default working directory
jq --arg folder "$(pwd)/" '. + { "initial_working_directory": $folder }' .devcontainer/rstudio-prefs.json > ~/.config/rstudio/rstudio-prefs.json
galachad marked this conversation as resolved.
Show resolved Hide resolved
29 changes: 29 additions & 0 deletions .devcontainer/rstudio.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

echo "Waiting for rstudio-server to be ready.."

# Get rstudio url and open it
url=$(jq -r ".CODESPACE_NAME" /workspaces/.codespaces/shared/environment-variables.json)
url_rstudio="https://$url-8787.app.github.dev"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The url is not possible to reach without gitlab auth.

I will propose to keep it simple.

$BROWSER "https://${CODESPACE_NAME}-8787.${GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}"

If you would like to check if rstudio is stareted we should query http://localhost:8787.


# loop over url_rstudio until getting success status
MAX_RETRIES=10
WAIT_TIME=5

retries=0

while [ $retries -lt $MAX_RETRIES ]; do
http_status=$(curl -s -o /dev/null -w "%{http_code}" "$url_rstudio")

# Check status code (exit successfully if code in success/redirection category)
if [ $http_status -ge 200 ]; then
echo "Rstudio server ready ! You can now go to this url : $url_rstudio"
exit 0
else
sleep $WAIT_TIME
((retries++))
fi
done

echo "Error accessing rstudio ($url_rstudio) - Timeout reached."
exit 1
galachad marked this conversation as resolved.
Show resolved Hide resolved
12 changes: 6 additions & 6 deletions vignettes/docker_and_codespaces.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ Note: Pressing directly `+` button will automatically create codespace for the l

#### Accessing Rstudio

To access Rstudio you need to go under vscode PORTS section :
To open rstudio in your browser, type `rstudio` on your current terminal. This script will wait until rstudio server is ready, and display the url in the terminal.

Known issue:

Sometimes ports are not automatically forwarded at the codespace creation. If you have some problems trying to access rstudio url, check the PORTS section on vscode :

```{r, echo = FALSE}
knitr::include_graphics("./assets/codespaces_rstudio_forward_port.png", dpi = 144)
```

Go on the row with port `8787` and open the url in a new browser.

Known issues:
- Sometimes ports are not automatically forwarded at the codespace creation. If you don't see `8787` port, then you need to manually add it (using "Add Port" button).
- The Rstudio server can takes some times to set-up after the codespace creation, so it might be not available right after the creation, and user might have to wait some minutes. We added a bash script `.devcontainer/codespaces_rstudio_status.sh` to check rstudio server status but it still an experimental feature.
If you don't see `8787` port, then you need to manually add it (using "Add Port" button).

### Usage limit / Pricing

Expand Down