93 lines
3.0 KiB
Bash
Executable File
93 lines
3.0 KiB
Bash
Executable File
#!/bin/bash
|
|
# =============================================== #
|
|
# devc.sh v2.0; job79 #
|
|
# Dev container enter script. Handles setting up #
|
|
# different dev containers, resuming sessions and #
|
|
# automatic container updates. #
|
|
# =============================================== #
|
|
set -eu
|
|
log() { printf '\e[%sm%s\e[0m %s\n' "${3:-36}" "${2:-○}" "$1"; }
|
|
arg() { echo -n " $@"; }
|
|
|
|
# run_args returns the podman run arguments required for
|
|
# starting a new container.
|
|
default_args() {
|
|
arg "--name $name"
|
|
arg "--hostname $name"
|
|
|
|
# Pull newer container image if available.
|
|
arg "--pull=newer"
|
|
|
|
# Use keep-id so the container user matches the host user.
|
|
arg "--userns=keep-id"
|
|
|
|
# Disable selinux labeling so unix sockets can be mounted
|
|
# without problems.
|
|
arg "--security-opt label=disable"
|
|
|
|
# Mount the wayland socket. Required to get the system
|
|
# clipboard (wl-copy) and gui applications working.
|
|
[ -e "/run/user/$UID/wayland-0" ] && arg "-v /run/user/$UID/wayland-0:/run/user/1000/wayland-0"
|
|
|
|
# Make the user home dir a volume so it survives container
|
|
# restarts. Use copy to keep the homedir files from the image.
|
|
arg "-v $name:/home/user:copy"
|
|
|
|
# If there is custom configuration for the container, load
|
|
# it here.
|
|
config_file="$(dirname "$(realpath "$0")")/containers/$name/config.sh"
|
|
[ -f "${config_file}" ] && source "${config_file}"
|
|
}
|
|
|
|
# param_args returns the podman run arguments based on the
|
|
# arguments provided to this script.
|
|
param_args() {
|
|
while test $# -gt 0; do
|
|
case "$1" in
|
|
-gpu) # Enable gpu acceleration.
|
|
arg "--device /dev/dri" ;;
|
|
-host-spawn) # Enable spawning host commands from inside the container using host-spawn.
|
|
arg "-v /run/user/$UID/bus:/tmp/bus"
|
|
arg "-e HOST_HOME=$HOME" # Use to translate paths.
|
|
arg "-e DBUS_SESSION_BUS_ADDRESS='unix:path=/tmp/bus'"
|
|
;;
|
|
*) # Use unknown arguments a podman arguments.
|
|
arg "$1" ;;
|
|
esac
|
|
shift
|
|
done
|
|
}
|
|
|
|
### MAIN ###
|
|
# Get container registry from DEVC_REGISTRY env variable.
|
|
if [ -n "${DEVC_REGISTRY:-}" ]; then
|
|
registry="$DEVC_REGISTRY"
|
|
else
|
|
log "registry unknown; set the DEVC_REGISTRY environment variable" 'x' 31
|
|
exit 1
|
|
fi
|
|
|
|
# Get the devcontainer name from the first argument. If not
|
|
# provided, use the last used name when possible.
|
|
if [[ $# -gt 0 ]] && [[ ${1:-} != -* ]]; then
|
|
image="$1"
|
|
echo "$image" >"$HOME/.local/share/devc-previous-container"
|
|
shift
|
|
elif [ -f "$HOME/.local/share/devc-previous-container" ]; then
|
|
image=$(<"$HOME/.local/share/devc-previous-container")
|
|
else
|
|
log "no container name specified" 'x' 31
|
|
exit 1
|
|
fi
|
|
name="${image%:*}"
|
|
|
|
# Create a new container when the container is not running or
|
|
# when any arguments are provided.
|
|
if [ "$(podman container inspect "$name" -f {{.State.Running}} 2>&1)" != 'true' ] || [[ $# -gt 0 ]]; then
|
|
log "starting devcontainer..."
|
|
podman container rm -f -t 0 "$name" 1>/dev/null
|
|
podman run -td $(default_args) $(param_args $@) "$registry/$name"
|
|
fi
|
|
|
|
podman exec -it "$name" bash -l
|