2024-09-17 18:32:24 +00:00
|
|
|
#!/bin/sh
|
2024-09-29 14:48:50 +00:00
|
|
|
# =============================================== #
|
|
|
|
# enter.sh v1.0; job79 #
|
|
|
|
# Enter into an existing or new dev container and #
|
|
|
|
# automatically handle dev container updates. #
|
2024-12-12 19:42:39 +00:00
|
|
|
# #
|
|
|
|
# TODO #
|
|
|
|
# ----------------------------------------------- #
|
|
|
|
# remove --security-opt label=disable #
|
|
|
|
# configurable mount directories #
|
2024-09-29 14:48:50 +00:00
|
|
|
# =============================================== #
|
|
|
|
set -e
|
2024-12-12 20:10:35 +00:00
|
|
|
log() { printf '\e[%sm%s\e[0m %s\n' "${3:-36}" "${2:-○}" "$1"; }
|
2024-09-29 14:48:50 +00:00
|
|
|
|
2024-12-12 19:42:39 +00:00
|
|
|
# run_args returns the arguments required for the podman run
|
|
|
|
# command.
|
|
|
|
run_args() {
|
|
|
|
arg() { echo -n " $@"; }
|
|
|
|
arg "--name $name"
|
2024-09-29 14:48:50 +00:00
|
|
|
|
2024-12-12 19:42:39 +00:00
|
|
|
# Disable some security settings to make it possible to
|
|
|
|
# mount host directories without problems.
|
|
|
|
arg "--security-opt label=disable" # disable labeling so mounts don't need to be labeled.
|
2024-09-29 14:48:50 +00:00
|
|
|
arg "--userns=keep-id" # required for ~/.ssh which is usually 700.
|
2024-12-12 19:42:39 +00:00
|
|
|
|
|
|
|
# Use host networking.
|
|
|
|
arg "--net=host"
|
2024-09-29 14:48:50 +00:00
|
|
|
|
|
|
|
# Mount the wayland socket. Required to get the system
|
|
|
|
# clipbard (wl-copy) working.
|
2024-12-12 19:42:39 +00:00
|
|
|
[ -e "/run/user/$UID/wayland-0" ] && arg "-v /run/user/$UID/wayland-0:/run/user/1000/wayland-0"
|
2024-09-29 14:48:50 +00:00
|
|
|
|
2024-12-12 19:42:39 +00:00
|
|
|
# Mount the ssh socket, directory and the git config
|
|
|
|
# directory. This gets the host ssh and git configuration
|
|
|
|
# working inside the container.
|
|
|
|
[ -e "$SSH_AUTH_SOCK" ] && arg "-v $SSH_AUTH_SOCK:/run/user/1000/ssh-auth-sock"
|
|
|
|
[ -d "$HOME/.ssh" ] && arg "-v $HOME/.ssh:/home/user/.ssh"
|
|
|
|
[ -d "$HOME/.config/git" ] && arg "-v $HOME/.config/git:/home/user/.config/git"
|
2024-09-29 14:48:50 +00:00
|
|
|
|
|
|
|
# Mount host directories with programming projects.
|
2024-12-12 19:42:39 +00:00
|
|
|
[ -d "$HOME/Documents" ] && arg "-v $HOME/Documents:/home/user/Documents"
|
|
|
|
[ -d "$HOME/.local/share/devcontainer" ] && arg "-v $HOME/.local/share/devcontainer:/home/user/.dev"
|
2024-09-29 14:48:50 +00:00
|
|
|
|
|
|
|
# Add volumes for .local and .cache so these survive
|
|
|
|
# container restarts.
|
|
|
|
arg "-v $name-local:/home/user/.local"
|
|
|
|
arg "-v $name-cache:/home/user/.cache"
|
|
|
|
}
|
|
|
|
|
|
|
|
### MAIN ###
|
|
|
|
name="dev"
|
|
|
|
image="git.plabble.org/job79/dev:latest"
|
2024-12-12 20:10:35 +00:00
|
|
|
fetch=false
|
2024-09-29 14:48:50 +00:00
|
|
|
|
|
|
|
while test $# -gt 0; do
|
|
|
|
case "$1" in
|
2024-12-12 19:42:39 +00:00
|
|
|
--image | -i)
|
|
|
|
shift
|
|
|
|
image="$1"
|
|
|
|
;;
|
|
|
|
--name | -n)
|
|
|
|
shift
|
|
|
|
name="$1"
|
|
|
|
;;
|
2024-12-12 20:10:35 +00:00
|
|
|
--fetch | -f)
|
|
|
|
fetch=true
|
2024-12-12 19:42:39 +00:00
|
|
|
;;
|
2024-12-12 20:10:35 +00:00
|
|
|
*) log "unknown argument '$1'" 'x' 31 ;;
|
2024-09-29 14:48:50 +00:00
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
2024-12-12 20:10:35 +00:00
|
|
|
if "$fetch" = true ] || [ "$(podman container inspect "$name" -f {{.State.Running}})" = 'false' ]; then
|
|
|
|
log "fetching updates..."
|
|
|
|
if [ "$(podman pull -q "$image")" != "$(podman container inspect "$name" -f {{.Image}})" ]; then
|
|
|
|
log "new container image downloaded" '✓' 32
|
|
|
|
podman container rm -f -t 1 "$name" 1>/dev/null
|
|
|
|
podman run -it $(run_args) "$image"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
log "no updates available" '✓' 32
|
2024-09-29 14:48:50 +00:00
|
|
|
fi
|
2024-12-12 20:10:35 +00:00
|
|
|
|
|
|
|
podman start "$name" 1>/dev/null
|
|
|
|
podman exec -it "$name" bash -l
|