feat: build multiple image
This commit is contained in:
65
enter.sh
65
enter.sh
@@ -1,38 +1,36 @@
|
||||
#!/bin/sh
|
||||
#!/bin/bash
|
||||
# =============================================== #
|
||||
# enter.sh v1.0; job79 #
|
||||
# Enter into an existing or new dev container and #
|
||||
# automatically handle dev container updates. #
|
||||
# enter.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 arguments required for the podman run
|
||||
# command.
|
||||
# run_args returns the podman run arguments required for
|
||||
# starting a new container.
|
||||
run_args() {
|
||||
arg "--name $name"
|
||||
|
||||
# Disable some security settings to make it possible to
|
||||
# mount host directories without problems.
|
||||
# Disable some security settings so host directories can
|
||||
# be mounted without problems.
|
||||
arg "--security-opt label=disable" # disable labeling so mounts don't need to be labeled.
|
||||
arg "--userns=keep-id" # required for ~/.ssh which is usually 700.
|
||||
|
||||
# Configure options to get host-spawn to work inside the
|
||||
# container.
|
||||
[ -e "/run/user/$UID/bus" ] && arg "-v /run/user/$UID/bus:/tmp/bus" # Use host dbus.
|
||||
arg "-e HOST_HOME=$HOME" # Used to translate container path to host.
|
||||
|
||||
# Use host networking.
|
||||
arg "--net=host"
|
||||
|
||||
# Configure options for host-spawn
|
||||
arg "-v /run/user/1000/bus:/tmp/bus" # Use host dbus.
|
||||
arg "-e HOST_HOME=$HOME" # Pas in host $HOME path.
|
||||
|
||||
# Mount the wayland socket. Required to get the system
|
||||
# clipbard (wl-copy) working.
|
||||
[ -e "/run/user/$UID/wayland-0" ] && arg "-v /run/user/$UID/wayland-0:/run/user/1000/wayland-0"
|
||||
|
||||
# Add a volume for the home directory so it survives
|
||||
# container upgrades.
|
||||
arg "-v $name:/home/user:copy"
|
||||
|
||||
# Mount the ssh socket, directory and the git config
|
||||
# directory. This gets the host ssh and git configuration
|
||||
# working inside the container.
|
||||
@@ -40,41 +38,48 @@ run_args() {
|
||||
[ -d "$HOME/.ssh" ] && arg "-v $HOME/.ssh:/home/user/.ssh"
|
||||
[ -d "$HOME/.config/git" ] && arg "-v $HOME/.config/git:/home/user/.config/git"
|
||||
|
||||
# Mount host directories with programming projects.
|
||||
[ -d "$HOME/Code" ] && arg "-v $HOME/Code:/home/user/Code"
|
||||
[ -d "$HOME/Documents" ] && arg "-v $HOME/Documents:/home/user/Documents"
|
||||
[ -d "$HOME/.config/devcontainer" ] && arg "-v $HOME/.config/devcontainer:/home/user/.config/devcontainer"
|
||||
# Add a volume for the home directory so it survives
|
||||
# container updates.
|
||||
arg "-v $name:/home/user:copy"
|
||||
|
||||
# Add custom user configuration.
|
||||
config_file="./containers/$name/config.sh"
|
||||
[ -f "${config_file}" ] && source "${config_file}"
|
||||
}
|
||||
|
||||
### MAIN ###
|
||||
name="devcontainer"
|
||||
image="ghcr.io/job79/devcontainer:main"
|
||||
force=false
|
||||
cd "$(dirname "$(realpath "$0")")"
|
||||
name=$(<"./state/last-name")
|
||||
registry=$(<"./state/registry")
|
||||
pull=false
|
||||
|
||||
while test $# -gt 0; do
|
||||
case "$1" in
|
||||
-i) shift && force=true && image="$1" ;;
|
||||
-f) force=true ;;
|
||||
-p) pull=true ;;
|
||||
-*) log "unknown argument '$1'" 'x' 31 ;;
|
||||
*) name="$1" ;;
|
||||
*)
|
||||
name="$1"
|
||||
echo "$1" >"./state/last-name"
|
||||
;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
if [ "$force" = true ] || [ "$(podman container inspect "$name" -f {{.State.Running}} 2>&1)" != 'true' ]; then
|
||||
if [ "$pull" = true ] || [ "$(podman container inspect "$name" -f {{.State.Running}} 2>&1)" != 'true' ]; then
|
||||
log "fetching devcontainer updates..."
|
||||
if [ "$force" = true ] || [ "$(podman container inspect "$name" -f {{.Image}} 2>&1)" != "$(podman pull -q "$image" 2>&1)" ]; then
|
||||
if [ "$pull" = true ] || [ "$(podman container inspect "$name" -f {{.Image}} 2>&1)" != "$(podman pull -q "$registry/$name" 2>&1)" ]; then
|
||||
if [ $? -eq 0 ]; then
|
||||
log "container image downloaded" '✓' 32
|
||||
else
|
||||
log "failed to download container image" 'x' 31
|
||||
fi
|
||||
podman container rm -f -t 1 "$name" 1>/dev/null
|
||||
podman run -td $(run_args) "$image"
|
||||
run_args
|
||||
podman run -td $(run_args) "$registry/$name"
|
||||
else
|
||||
log "no updates available" '✓' 32
|
||||
fi
|
||||
fi
|
||||
|
||||
podman start "$name" 1>/dev/null
|
||||
podman exec -e ENTER_DIR="${PWD/#$HOME/\~}" --detach-keys "ctrl-@" -it "$name" bash -l
|
||||
podman exec -e CONTAINER_NAME="$name" -e ENTER_DIR="${PWD/#$HOME/\~}" --detach-keys "ctrl-@" -it "$name" bash -l
|
||||
|
||||
Reference in New Issue
Block a user