/ruby-playground

A vs code codespace for ruby which desgined to be wipped after tempering

Primary LanguageRubyThe UnlicenseUnlicense

Ruby Playground

Ruby Playground is an isolated environment for building, testing, and playing around with ruby. With environment preconfigured, writing code is simply starting the container, and connect your vs code to the created tunnel. We may clean up the sandbox after tinkering with just one command, and it will be reseated and be ready for next code.

This playground is based on Rust Playground, a sandbox environment for running Rust code.

Installation

To get started, docker is required to be installed on the host machine that we planed to host it on. Then, follow the following steps to spin up the container. And a GitHub account is required to connect the tunnel to the cloud so we can access it.

  1. Clone this repo into your machine and cd into it.
git clone https://github.com/kelvinchin12070811/ruby-playground
cd ruby-playground
  1. Build the docker image.
rake build
  1. Run the docker image.

Once the image has build, we can then spin up the image as bellow:

Bash/ zsh:

docker run /
-d /
-h <name you want the vs code tunnel be> /
--volume "<local playground>:/home/playground" /
--volume "<vs code binary data>:/root/.vscode" /
--volume "<vs code server data>:/root/.vscode-server" /
--name <name for the container> /
kelvinchin1207/ruby-playground:latest

example:

docker run /
-d /
-h ruby-playground /
--volume "~/documents/ruby-playground/playground:/home/playground" /
--volume "~/documents/ruby-playground/.vscode:/root/.vscode" /
--volume "~/documents/ruby-playground/.vscode-server:/root/.vscode-server" /
--name ruby-playground /
kelvinchin1207/ruby-playground:latest

This will require 3 docker volumes to store the following data:

  • Playground workspace - The main location to store your code and will be wiped when called.
  • .vscode folder - This is where the server version of vs code will be installed, also with the tunnel authentication token.
  • .vscode-server - This is the folder where vs code used to store the server side data such as extensions.

To enable reset feature, set the environment variable FEATURE_RESET as following:

docker run /
-d /
-h ruby-playground /
-e FEATURE_RESET=1 /
--volume "~/documents/ruby-playground/playground:/home/playground" /
--volume "~/documents/ruby-playground/.vscode:/root/.vscode" /
--volume "~/documents/ruby-playground/.vscode-server:/root/.vscode-server" /
--name ruby-playground /
kelvinchin1207/ruby-playground:latest

Or use docker compose for easier spin up, to run with sample docker-compose config, run:

rake up

To stop the example compose stacks:

rake down

Refer the docker-compose.yaml file in the repo for reference.

  1. Authenticate the tunnel for first time use

Once the container is started, we will need to authorize the tunnel to allow it to be accessed from the vs code or remotely from vscode.dev.

We first tail the docker log to get the auth link generated by the vscode cli tool.

docker logs -n 50 your-container-tunnel

We will get the following:

*
* Visual Studio Code Server
*
* By using the software, you agree to
* the Visual Studio Code Server License Terms (https://aka.ms/vscode-server-license) and
* the Microsoft Privacy Statement (https://privacy.microsoft.com/en-US/privacystatement).
*
To grant access to the server, please log into https://github.com/login/device and use code ****-****

Follow the instruction on the screen and login to the given link, we will need to login to our github account and authorize the connection.

Once again, run the following command again:

docker logs -n 50 your-container-tunnel

We will get output as bellow:

[2023-01-01 05:20:30] info Creating tunnel with the name: <the-assigned-name-of-your-tunnel>

Open this link in your browser https://vscode.dev/tunnel/<the-assigned-name-of-your-tunnel>

Now we can access to the tunnel via the link given. We can also able to access the tunnel via vs code's remote development extension or from vscode.dev. Login to the same github account on prompt and we are ready to rock it.

As the docker file pull the ruby image via the latest tag, to update the ruby version, simply rebuild the image and redeploy it. VS Code cli should updated by it self when needed.

Usage

The folder we going to write our ruby code is at /home/playground. If you are not in this directory, you can use the hop command to quickly jump into that folder. There's also a code-hop command which will launch new vs code on the playground directory.

Once finished with the mock, reset command helps to clean up the playground and restore it to initial state. This is a dangerous command so please backup any things that might be needed after. This command is by default disabled but can be enabled with the environment variable FEATURE_RESET.

Bellow is a list of custom commands that may come into help:

Command Description
hop Quickly jump to the playground folder.
code-hop Open new VS Code Window (or tab if on vscode.dev) in the playground folder.
cls Alias to clear, just to save a few typing.
reset Default disabled, reset the playground folder to initial state. Can be enabled by setting the environment variable FEATURE_RESET to 1.
about Nothing fancy, just prints the about text.

Credit

This container did get some reference from the article Run VS Code Remote Tunnels in a container by Anh Dang on setting up the vs code cli in the container. You may refer to the article on the details on how to setup a container with vscode tunnel.