1
0
dotfiles/.config/zsh/alias/containers.zsh

143 lines
3.7 KiB
Bash
Raw Normal View History

2024-02-18 02:32:08 +03:00
#!/bin/zsh
alias bud='buildah bud --isolation chroot --network host --format docker -f '
2024-02-23 21:46:04 +03:00
alias pod-run='podman run -e "TERM=${TERM:-linux}" --rm -it '
2024-02-18 02:32:08 +03:00
alias pod-images='podman images --format "table {{.ID}} {{.Repository}}:{{.Tag}} {{.Size}} {{.Created}} |{{.CreatedAt}}" '
alias pod-inspect='podman inspect '
alias pod-logs='podman logs '
2024-06-16 12:48:58 +03:00
typeset -Uga ZSHU_CNTR_SHELLS=( /bin/bash /bin/sh /bin/ash )
typeset -ga ZSHU_CNTR_FALLBACK_SHELL=( /busybox/busybox sh )
z-pod() { command podman "$@" ; }
z-pod-run() {
z-pod run -e "TERM=${TERM:-linux}" --rm -it "$@"
}
## NB: naive. rewrite!
pod-run-sh() {
local -a cntr_opts=( --network=host --entrypoint='[]' --user=0:0 )
local i
local -a shell
for i ( ${ZSHU_CNTR_SHELLS} ) ; do
echo "pod-run-sh: trying $i as shell" >&2
z-pod-run ${cntr_opts[@]} "$@" "$i" -c ':' 2>/dev/null || continue
shell=($i) ; break
done
while [ -z "${shell}" ] ; do
echo "pod-run-sh: trying '${ZSHU_CNTR_FALLBACK_SHELL[*]}' as last-resort shell" >&2
z-pod-run ${cntr_opts[@]} "$@" ${ZSHU_CNTR_FALLBACK_SHELL[@]} -c ':' 2>/dev/null || break
shell=(${ZSHU_CNTR_FALLBACK_SHELL})
break
done
if [ -z "${shell}" ] ; then
echo "unable to run: $*"
return 1
fi
z-pod-run ${cntr_opts[@]} "$@" ${shell[@]}
}
pod-ps() {
[ $# -ne 0 ] || set -- -a --format 'table {{.ID}} {{.Names}} {{.Image}} {{.CreatedHuman}} {{.Status}}'
command podman ps "$@"
}
2024-02-18 02:32:08 +03:00
sko-inspect() {
2024-02-23 21:46:04 +03:00
local i
i="${1:?}" ; shift
2024-03-04 15:35:55 +03:00
command skopeo inspect "$@" "docker://$i"
2024-02-18 02:32:08 +03:00
}
sko-list-tags() {
2024-02-23 21:46:04 +03:00
local i
i="${1:?}" ; shift
2024-03-04 15:35:55 +03:00
command skopeo list-tags "$@" "docker://$i"
2024-02-18 02:32:08 +03:00
}
pod-dive() {
2024-02-23 21:46:04 +03:00
local i
i="${1:?}" ; shift
2024-03-04 15:35:55 +03:00
command dive "$@" "podman://$i"
2024-02-18 02:32:08 +03:00
}
jq-visual() {
2024-06-16 12:48:58 +03:00
command jq -C | less
2024-02-18 02:32:08 +03:00
}
jq-config() {
2024-06-16 12:48:58 +03:00
command jq '.[].Config'
2024-02-18 02:32:08 +03:00
}
jq-tags() {
2024-06-16 12:48:58 +03:00
command jq -r '.Tags[]'
2024-02-18 02:32:08 +03:00
}
alias dkr='docker '
2024-06-16 12:48:58 +03:00
alias dkr-run='dkr run -e "TERM=${TERM:-linux}" --rm -it '
2024-02-18 02:32:08 +03:00
alias dkr-ps='dkr ps '
alias dkr-images='dkr images --format "table {{.ID}}\\t{{.Repository}}:{{.Tag}}\\t{{.Size}}\\t{{.CreatedAt}}" '
alias dkr-inspect='dkr inspect '
alias dkr-logs='dkr logs '
2024-06-16 12:48:58 +03:00
z-dkr() { command docker "$@" ; }
z-dkr-run() {
z-dkr run -e "TERM=${TERM:-linux}" --rm -it "$@"
}
## NB: naive. rewrite!
dkr-run-sh() {
local -a cntr_opts=( --network=host --entrypoint='' --user=0:0 )
local i
local -a shell
for i ( ${ZSHU_CNTR_SHELLS} ) ; do
echo "dkr-run-sh: trying $i as shell" >&2
z-dkr-run ${cntr_opts[@]} "$@" "$i" -c ':' 2>/dev/null || continue
shell=($i) ; break
done
while [ -z "${shell}" ] ; do
echo "dkr-run-sh: trying '${ZSHU_CNTR_FALLBACK_SHELL[*]}' as last-resort shell" >&2
z-dkr-run ${cntr_opts[@]} "$@" ${ZSHU_CNTR_FALLBACK_SHELL[@]} -c ':' 2>/dev/null || break
shell=(${ZSHU_CNTR_FALLBACK_SHELL})
break
done
if [ -z "${shell}" ] ; then
echo "unable to run: $*"
return 1
fi
z-dkr-run ${cntr_opts[@]} "$@" ${shell[@]}
}
dkr-dive() {
local i
i="${1:?}" ; shift
command dive "$@" "docker://$i"
}
2024-06-16 12:51:37 +03:00
typeset -g ZSHU_GRP_DOCKER=docker
2024-06-16 12:48:58 +03:00
z-adjust-docker() {
[ ${UID} -eq 0 ] && return 0
getent group "${ZSHU_GRP_DOCKER}" >/dev/null || return 1
(( ${+commands[docker]} )) || return 127
local _users=$(getent group "${ZSHU_GRP_DOCKER}" | cut -d: -f4)
local -a users=("${(@s[,])_users}")
local i found
for i ( ${users}) ; do
if [ "$i" = "${USERNAME}" ] ; then
found=1
break
fi
done
[ -n "${found}" ] && return 0
(( ${+commands[sudo]} )) || return 127
alias docker='sudo docker '
z-dkr() { command sudo docker "$@" ; }
return 0
}