-
-
Save gramidt/a6b64ce3d8c19e80b9d56776e61494fe to your computer and use it in GitHub Desktop.
Helix devcontainer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -e | |
remove_flag="" | |
if [ "$1" = "true" ]; then | |
remove_flag="--remove-existing-container" | |
fi | |
# Generate certificate | |
path=tmp/devcontainers | |
mkdir -p $path && pushd $path | |
ssh_key=$(cat /dev/urandom | tr -dc '[:alpha:]' | fold -w ${1:-20} | head -n 1) | |
ssh-keygen -q -N '' -t rsa -f $ssh_key | |
popd | |
# Start container | |
devcontainer up $remove_flag \ | |
--mount "type=bind,source=$HOME/.config/helix,target=/home/archlinux/.config/helix" \ | |
--mount "type=bind,source=$HOME/.wakatime.cfg,target=/home/archlinux/.wakatime.cfg" \ | |
--workspace-folder . | |
script=" | |
# Copy generated keys | |
mkdir -p \$HOME/.ssh | |
cat \$PWD/$path/$ssh_key.pub > \$HOME/.ssh/authorized_keys | |
chmod 644 \$HOME/.ssh/authorized_keys | |
chmod 700 \$HOME/.ssh | |
" | |
# Add pub key to SSH allow list | |
devcontainer exec --workspace-folder . sh -c "$script" | |
devcontainer exec --workspace-folder . sudo /usr/local/share/ssh-init.sh | |
name=$(devcontainer read-configuration --workspace-folder . | jq -r '.configuration.name') | |
container_name="${name}_devcontainer-app-1" # you might also need to change this if `devcontainer-cli` creates a differently named container | |
ip_addr=$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $container_name) | |
PORT=${PORT:-2222} | |
# Create a `known_hosts` file that will be used by only this project | |
ssh-keyscan -t ssh-rsa -p $PORT $ip_addr >> tmp/known_hosts | |
# Connect directly via IP address instead of localhost | |
ssh -t -i $PWD/$path/$ssh_key \ | |
-o UserKnownHostsFile=tmp/known_hosts \ | |
-o NoHostAuthenticationForLocalhost=yes \ | |
-o UserKnownHostsFile=/dev/null \ | |
-o GlobalKnownHostsFile=/dev/null \ | |
-p $PORT "archlinux@$ip_addr" \ | |
"cd /workspaces/${PWD##*/}; fish --login" # you might need to change this according to the shell inside the image |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment