diff --git a/dev/Dockerfile b/dev/Dockerfile new file mode 100644 index 0000000..6e639f2 --- /dev/null +++ b/dev/Dockerfile @@ -0,0 +1,14 @@ +FROM johncolvin/bluetradingsystems:spark-dev + +ARG docker_user +ARG docker_uid +ARG docker_pass + +RUN \ + groupadd -g "$docker_uid" "$docker_user" \ + && useradd -u "$docker_uid" -g "$docker_uid" "$docker_user" \ + && usermod --password "$(openssl passwd -1 "$docker_pass")" "$docker_user" \ + && usermod -aG sudo "$docker_user" + +# Set the default user +USER $docker_user:$docker_user diff --git a/dev/README.md b/dev/README.md new file mode 100644 index 0000000..52a8adb --- /dev/null +++ b/dev/README.md @@ -0,0 +1,40 @@ +# Spark Dev Container +## How to create a Spark VS Code Devcontainer + + +## 1. Build the Docker image +Run the script `build.sh` located in this directory to build the image +``` +build.sh +``` +***Notes*** +* Builds an image named `$USER:spark-dev` +* Adds a user to the image with the same name/uid/gid as the current user so + it has proper file permissions in mounted directories +* Run `build.sh --help` to see available options + + +## 2. Generate a devcontainer.json +Run the script `gen_devcontainer.sh` located in this directory to create a devcontainer.json file.
+***Note:** The file path must be `/.devcontainer/devcontainer.json`* + +``` +# Dump `gen_devcontainer.sh` output to devcontainer.json +gen_devcontainer.sh > $workspaceFolder/.devcontainer/devcontainer.json +``` + +***Notes*** +* Mounts the following directories (`:`): + - `/home/:/home//.spark-dev/mounts/` + - `/home//spark:/path/to/this/repo` +* Run `gen_devcontainer.sh --help` to see available options + + +## 3. Open VS Code in Container +1. Launch **VS Code** and open the repo folder on the host machine. +1. Run **Dev Containers: Open Folder in Container** from the command palette.
+ +***Notes** + * The container stops when you close VS Code. All processes, including + `sparkd`, will be suspened until you open the workspace again (or manually + restart the container). diff --git a/dev/build.sh b/dev/build.sh new file mode 100755 index 0000000..2ddb21c --- /dev/null +++ b/dev/build.sh @@ -0,0 +1,123 @@ +#!/usr/bin/env bash + +function print_help_and_exit() { + cat < in the image (defaults to the username in the + image) + -i,--image The name to give the image (defaults to "USER:spark-dev") + -h,--help Print this help message and exit + +EOF + exit "$1" +} + + +declare docker_user="" +declare docker_pass="" +declare image_name="" +declare -i docker_pull=1 + +while [[ $# -gt 0 ]]; do + case "$1" in + -i) + ;& + --image) + if [[ -z "$2" ]]; then + echo "Image (-i,--image) must be non-empty" + print_help_and_exit 1 + fi + image_name="$2" + shift 2 + ;; + -u) + ;& + --user) + if [[ -z "$2" ]]; then + echo "User (-u,--user) must be non-empty" + print_help_and_exit 1 + fi + docker_user="$2" + shift 2 + ;; + -p) + ;& + --pass) + if [[ -z "$2" ]]; then + echo "Password (-p,--pass) must be non-empty" + print_help_and_exit 1 + fi + docker_pass="$2" + shift 2 + ;; + --no-pull) + docker_pull=0 + shift 1 + ;; + -h) + ;& + --help) + print_help_and_exit 0 + ;; + *) + echo "Unexpected argument '$1'" + print_help_and_exit 1 + ;; + esac +done + +declare -a docker_user_args=($(echo "$docker_user" | sed -e 's/:/ /g')) +case ${#docker_user_args[@]} in + 0) + docker_user="$USER" + docker_uid="$(id -u)" + ;; + 1) + docker_user="${docker_user_args[0]}" + docker_uid="$(id -u)" + ;; + 2) + docker_user="${docker_user_args[0]}" + docker_uid="${docker_user_args[1]}" + ;; + *) + echo "Invalid docker_user: $docker_user" + print_help_and_exit 1 + ;; +esac + +if [[ $docker_pull -eq 1 ]]; then + # Pull the base image from Docker Hub + base_image_name="johncolvin/bluetradingsystems:spark-dev" + echo "Pulling image '$base_image_name' from Docker Hub" + docker pull "$base_image_name" + declare -i pull_status=$? + if [[ $pull_status -ne 0 ]]; then + echo "Docker pull exited with code $pull_status" + exit 1 + fi +fi + +if [[ -z "$image_name" ]]; then + image_name="${docker_user}:spark-dev" +fi + +echo "Building image +image_name : $image_name +user : $docker_user" + +if [[ -z "$docker_pass" ]]; then + docker_pass="$docker_user" + echo "Giving user '$docker_user' a secure password: $docker_pass" +fi + +docker build \ + --build-arg docker_user="$docker_user" \ + --build-arg docker_pass="$docker_pass" \ + --build-arg docker_uid="$docker_uid" \ + -t "$image_name" \ + . diff --git a/dev/gen_devcontainer_json.sh b/dev/gen_devcontainer_json.sh new file mode 100755 index 0000000..5b9675f --- /dev/null +++ b/dev/gen_devcontainer_json.sh @@ -0,0 +1,76 @@ +#!/usr/bin/env bash + +function print_help_and_exit() { + cat <