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

devcontainers: add initial dev-container setup and scripts #374

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "exercism-generic",
"image": "vpayno/ci-generic-debian:latest",
"customizations": {
"vscode": {
"settings": {},
"extensions": []
}
},
"portsAttributes": {},
"postCreateCommand": "cd /workspaces/exercism-workspace/; tree .devcontainer/; ./.devcontainer/scripts/dc-up-post-create-command.sh",
"remoteUser": "root",
"shutdownAction": "none"
}
13 changes: 13 additions & 0 deletions .devcontainer/scripts/dc-down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

declare devcontainer_id

devcontainer_id="$(docker ps --format 'json' | jq -r '. | select(.Labels | contains("exercism-workspace")) | select(.Image == "vpayno/ci-generic-debian:latest") | .ID')"

# delete old container
if [[ -n ${devcontainer_id} ]]; then
printf "Removing old container...\n"
docker rm -f "${devcontainer_id}"
printf "done.\n"
fi
printf "\n"
15 changes: 15 additions & 0 deletions .devcontainer/scripts/dc-run
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

set -e
set -x

if [[ -f ./.devcontainer/scripts/data.tmp ]]; then
# shellcheck disable=SC1091 source=.devcontainer/scripts/data.tmp
source ./.devcontainer/scripts/data.tmp
fi

if [[ ${1} == --root ]]; then
devcontainer exec --workspace-folder "${PWD}" bash -i
else
devcontainer exec --workspace-folder "${PWD}" sudo -u "${devcontainer_username:-USER}" -i
fi
52 changes: 52 additions & 0 deletions .devcontainer/scripts/dc-up
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env bash

# shellcheck disable=SC2120
rword() {
local -a words
local word
local -i word_size=${1:-0}
local -i tries

mapfile -t words </usr/share/dict/words
if [[ ${word_size} -le 7 ]]; then
((word_size = (RANDOM % 11) + 5))
fi

tries=0
while [[ ${#word} -le $((word_size - (RANDOM % 5))) ]] || [[ ${#word} -ge $((word_size - (RANDOM % 5))) ]]; do
word=${words[$((RANDOM * (RANDOM % 2) + RANDOM * (RANDOM % 2 + 2)))]}
((tries += 1))
[[ ${tries} -ge ${#words[@]} ]] && break
done

echo "${word}"
}

declare devcontainer_id

devcontainer_id="$(docker ps --format 'json' | jq -r '. | select(.Labels | contains("exercism-workspace")) | select(.Image == "vpayno/ci-generic-debian:latest") | .ID')"

declare -x devcontainer_username
declare -x devcontainer_password
declare -x -i devcontainer_uid
declare -x -i devcontainer_gid

# create data for setup scripts that run inside dev-container
cat >./.devcontainer/scripts/data.tmp <<-EOF
devcontainer_username="$(id --name --user)"
devcontainer_password="$(rword)"
devcontainer_uid="$(id --user)"
devcontainer_gid="$(id --group)"
EOF

# delete old container
if [[ -n ${devcontainer_id} ]]; then
printf "Removing old container...\n"
docker rm -f "${devcontainer_id}"
printf "done.\n"
fi
printf "\n"

# starts up container
time devcontainer up --mount type=bind,source="${HOME}",target="${HOME}" --workspace-folder "${PWD}"
printf "\n"
35 changes: 35 additions & 0 deletions .devcontainer/scripts/dc-up-post-create-command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash

set -x
set -e

# shellcheck disable=SC1091 source=.devcontainer/scripts/data.tmp
source /workspaces/exercism-workspace/.devcontainer/scripts/data.tmp

# shellcheck disable=SC2154
{
groupadd --gid "${devcontainer_gid}" "${devcontainer_username}"
useradd --uid "${devcontainer_uid}" --gid "${devcontainer_gid}" --no-create-home --groups docker "${devcontainer_username}"
chsh --shell /bin/bash "${devcontainer_username}"
printf "%s\n%s\n" "${devcontainer_password}" "${devcontainer_password}" | passwd "${devcontainer_username}"
id "${devcontainer_username}"
}

declare -a cargo_pkgs=(
bacon
cargo-fuzz
cargo-generate
cargo-scaffold
cargo-spellcheck
git-cliff
irust
ripgrep
)

# these commands also run as root

cargo install --locked "${cargo_pkgs[@]}"
printf "\n"

rustup component add rust-analyzer
printf "\n"
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,6 @@ rust/index.scip
# wasm
node_modules/
package-lock.json

# dev-container
/.devcontainer/scripts/data.tmp
Loading