diff --git a/Dockerfile b/Dockerfile index f1c4edf5..173eec7e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,15 +13,27 @@ ENV SHELL=/bin/bash RUN sudo apt-get update && sudo apt-get install unzip -y RUN curl https://rclone.org/install.sh | sudo bash -# You can add custom software and dependencies for your environment here. Some examples: - -# RUN code-server --install-extension esbenp.prettier-vscode -# RUN sudo apt-get install -y build-essential -# RUN COPY myTool /home/coder/myTool +# Copy rclone tasks to /tmp, to potentially be used +COPY deploy-container/rclone-tasks.json /tmp/rclone-tasks.json # Fix permissions for code-server RUN sudo chown -R coder:coder /home/coder/.local +# You can add custom software and dependencies for your environment below +# ----------- + +# Install a VS Code extension: +# Note: we use a different marketplace than VS Code. See https://github.com/cdr/code-server/blob/main/docs/FAQ.md#differences-compared-to-vs-code +# RUN code-server --install-extension esbenp.prettier-vscode + +# Install apt packages: +# RUN sudo apt-get install -y ubuntu-make + +# Copy files: +# COPY deploy-container/myTool /home/coder/myTool + +# ----------- + # Port ENV PORT=8080 diff --git a/deploy-container/README.md b/deploy-container/README.md index 02db877e..02f46757 100644 --- a/deploy-container/README.md +++ b/deploy-container/README.md @@ -13,14 +13,17 @@ Docker Hub: `bencdr/code-server-deploy-container` ## Environment variables: -| Variable Name | Description | Default Value | -| ------------- | ---------------------------------------------------------- | ------------- | -| `PASSWORD` | Password for code-server | -| `USE_LINK` | Use code-server --link instead of a password (coming soon) | false | -| `GIT_REPO` | A git repository to clone | | - +| Variable Name | Description | Default Value | +| ----------------- | ------------------------------------------------------------------------------------------------ | ------------------- | +| `PASSWORD` | Password for code-server | | +| `HASHED_PASSWORD` | Overrrides PASSWORD. [SHA-256 hash](https://xorbin.com/tools/sha256-hash-calculator) of password | +| `USE_LINK` | Use code-server --link instead of a password (coming soon) | false | +| `GIT_REPO` | A git repository to clone | | +| `START_DIR` | The directory code-server opens (and clones repos in) | /home/coder/project | --- +Other code-server environment variables (such as `CODE_SERVER_CONFIG`) can also be used. See the [code-server FAQ](https://github.com/cdr/code-server/blob/main/docs/FAQ.md) for details. + ## 💾 Persist your filesystem with `rclone` This image has built-in support for [rclone](https://rclone.org/) so that your files don't get lost when code-server is re-deployed. @@ -44,18 +47,24 @@ Now, you can add the following the environment variables in the code-server clou | Environment Variable | Description | Default Value | Required | | -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------- | -------- | -| RCLONE_DATA | the encoded rclone config you copied in step 3 | n/a | ✅ | +| RCLONE_DATA | the encoded rclone config you copied in step 3 | n/a | ✅ | | RCLONE_REMOTE_NAME | the name of the remote you added in step 2.
check with `$ rclone listremotes` | code-server-remote | | | RCLONE_SOURCE | source directory to sync files in the code-server container | the project directory: `/home/coder/project` | | | RCLONE_DESTINATION | the path in the remote that rclone syncs to. change this if you have multiple code-server environments, or if you want to better organize your files. | code-server-files | | - +| RCLONE_VSCODE_TASKS | import push and pull shortcuts into VS Code ![rclone screenshot from VS Code](../img/rclone-vscode-tasks.png) | true | +| RCLONE_AUTO_PUSH | automatically push files on startup if the rclone remote is empty (environment -> rclone remote) | true | | +| RCLONE_AUTO_PULL | automatically pull files on startup if the rclone remote is not empty (rclone -> environment remote) | true | | +| RCLONE_FLAGS | additional flags to attach to the push and pull script. type `rclone help flags for a list." | | | ```sh -# How to use: +# --- How to use --- +# Terminal: $ sh /home/coder/push_remote.sh # save your uncomitted files to the remote - $ sh /home/coder/pull_remote.sh # get latest files from the remote + +# In VS Code: +# ctrl + P, run task: push_remote or pull_remote ``` --- diff --git a/deploy-container/entrypoint.sh b/deploy-container/entrypoint.sh index d8db0ffb..a450f3a2 100755 --- a/deploy-container/entrypoint.sh +++ b/deploy-container/entrypoint.sh @@ -1,39 +1,81 @@ #!/bin/bash -START_DIR=/home/coder/project +START_DIR="${START_DIR:-/home/coder/project}" + +PREFIX="deploy-code-server" + +mkdir -p $START_DIR + +# function to clone the git repo or add a user's first file if no repo was specified. +project_init () { + [ -z "${GIT_REPO}" ] && echo "[$PREFIX] No GIT_REPO specified" && echo "Example file. Have questions? Join us at https://community.coder.com" > $START_DIR/coder.txt || git clone $GIT_REPO $START_DIR +} # add rclone config and start rclone, if supplied if [[ -z "${RCLONE_DATA}" ]]; then - echo "RCLONE_DATA is not specified. Files will not persist" + echo "[$PREFIX] RCLONE_DATA is not specified. Files will not persist" - # Clone the git repo, if it exists - [ -z "${GIT_REPO}" ] && echo "No GIT_REPO specified"; git clone $GIT_REPO $START_DIR + # start the project + project_init else - echo "Copying rclone config..." + echo "[$PREFIX] Copying rclone config..." mkdir -p /home/coder/.config/rclone/ touch /home/coder/.config/rclone/rclone.conf echo $RCLONE_DATA | base64 -d > /home/coder/.config/rclone/rclone.conf + # defasult to true + RCLONE_VSCODE_TASKS="${RCLONE_VSCODE_TASKS:-true}" + RCLONE_AUTO_PUSH="${RCLONE_AUTO_PUSH:-true}" + RCLONE_AUTO_PULL="${RCLONE_AUTO_PULL:-true}" + + if [ $RCLONE_VSCODE_TASKS = "true" ]; then + # copy our tasks config to VS Code + echo "[$PREFIX] Applying VS Code tasks for rclone" + cp /tmp/rclone-tasks.json /home/coder/.local/share/code-server/User/tasks.json + # install the extension to add to menu bar + code-server --install-extension actboy168.tasks& + else + # user specified they don't want to apply the tasks + echo "[$PREFIX] Skipping VS Code tasks for rclone" + fi + + + # Full path to the remote filesystem RCLONE_REMOTE_PATH=${RCLONE_REMOTE_NAME:-code-server-remote}:${RCLONE_DESTINATION:-code-server-files} RCLONE_SOURCE_PATH=${RCLONE_SOURCE:-$START_DIR} - echo "rclone sync $RCLONE_SOURCE_PATH $RCLONE_REMOTE_PATH -vv" > /home/coder/push_remote.sh - echo "rclone sync $RCLONE_REMOTE_PATH $RCLONE_SOURCE_PATH -vv" > /home/coder/pull_remote.sh + echo "rclone sync $RCLONE_SOURCE_PATH $RCLONE_REMOTE_PATH $RCLONE_FLAGS -vv" > /home/coder/push_remote.sh + echo "rclone sync $RCLONE_REMOTE_PATH $RCLONE_SOURCE_PATH $RCLONE_FLAGS -vv" > /home/coder/pull_remote.sh chmod +x push_remote.sh pull_remote.sh if rclone ls $RCLONE_REMOTE_PATH; then - # grab the files from the remote instead of re-cloning the git repo - echo "Pulling existing files from remote..." - /home/coder/pull_remote.sh& + + if [ $RCLONE_AUTO_PULL = "true" ]; then + # grab the files from the remote instead of running project_init() + echo "[$PREFIX] Pulling existing files from remote..." + /home/coder/pull_remote.sh& + else + # user specified they don't want to apply the tasks + echo "[$PREFIX] Auto-pull is disabled" + fi + else - # we need to clone the git repo and sync - echo "Pushing initial files to remote..." - [ -z "${GIT_REPO}" ] && echo "No GIT_REPO specified" && mkdir -p $START_DIR && echo "intial file" > $START_DIR/file.txt; git clone $GIT_REPO $START_DIR - /home/coder/push_remote.sh& + + if [ $RCLONE_AUTO_PUSH = "true" ]; then + # we need to clone the git repo and sync + echo "[$PREFIX] Pushing initial files to remote..." + project_init + /home/coder/push_remote.sh& + else + # user specified they don't want to apply the tasks + echo "[$PREFIX] Auto-push is disabled" + fi + fi fi +echo "[$PREFIX] Starting code-server..." # Now we can run code-server with the default entrypoint /usr/bin/entrypoint.sh --bind-addr 0.0.0.0:8080 $START_DIR \ No newline at end of file diff --git a/deploy-container/myTool/test.sh b/deploy-container/myTool/test.sh new file mode 100644 index 00000000..2cea5b72 --- /dev/null +++ b/deploy-container/myTool/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +echo "This is a demo tool that could be brought into the code-server workspace" \ No newline at end of file diff --git a/deploy-container/rclone-tasks.json b/deploy-container/rclone-tasks.json new file mode 100644 index 00000000..d095796d --- /dev/null +++ b/deploy-container/rclone-tasks.json @@ -0,0 +1,33 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "push_remote", + "type": "shell", + "command": "sh /home/coder/push_remote.sh", + "presentation": { + "reveal": "always" + }, + "problemMatcher": [], + "options": { + "statusbar": { + "label": "$(repo-push) rclone: push" + } + } + }, + { + "label": "pull_remote", + "type": "shell", + "command": "sh /home/coder/pull_remote.sh", + "presentation": { + "reveal": "always" + }, + "problemMatcher": [], + "options": { + "statusbar": { + "label": "$(repo-pull) rclone: pull" + } + } + } + ] +} \ No newline at end of file diff --git a/guides/heroku.md b/guides/heroku.md index e07ee2d9..d7968c4c 100644 --- a/guides/heroku.md +++ b/guides/heroku.md @@ -50,3 +50,5 @@ Press the button in the top right of the repo, or or click to [use this template 1. Head back to Heroku and notice a new deployment has started. After it has completed, you can use these tools in your environment. 1. (Optional): [Configure rclone](https://github.com/cdr/deploy-code-server/tree/main/deploy-container#-persist-your-filesystem-with-rclone) so that you can save your VS Code config and files without commiting + +See the [deploy-container README](../deploy-container) for other config vars for your environment. \ No newline at end of file diff --git a/guides/railway.md b/guides/railway.md index fe41fdc2..ae0d4b53 100644 --- a/guides/railway.md +++ b/guides/railway.md @@ -42,3 +42,5 @@ You also need to specity a `PASSWORD` and a `GIT_REPO` to clone in your environm 1. Head back to Railway and notice a new deployment was created. After it has completed, you can use these tools in your environment. 1. (Optional): [Configure rclone](https://github.com/cdr/deploy-code-server/tree/main/deploy-container#-persist-your-filesystem-with-rclone) so that you can save your VS Code config and files without commiting + +See the [deploy-container README](../deploy-container) for other config vars for your environment. \ No newline at end of file diff --git a/img/rclone-vscode-tasks.png b/img/rclone-vscode-tasks.png new file mode 100644 index 00000000..76368870 Binary files /dev/null and b/img/rclone-vscode-tasks.png differ