add SUDO support to rtpctl.d + _load

This commit is contained in:
Lockszmith (@wolfpack) 2025-02-26 23:55:11 -05:00
parent a45d393b8e
commit 9c01302f6e
1 changed files with 23 additions and 18 deletions

View File

@ -2,6 +2,8 @@
RPH_UID=${RPH_UID:-${UID}}
[ "${SUDO}" == "sudo" ] || SUDO=
# Identify source path (even if symlinked)
SOURCE=${BASH_SOURCE[0]}
while [ -L "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
@ -50,7 +52,7 @@ dls() {
local base='{{.Status}}\t{{.ID}}\t{{.Names}}\t{{.Image}}' #'\t{{.Networks}}\t{{.Ports}}\t{{.Mounts}}'
local compose='{{.Label "com.docker.compose.project"}}\t{{.Label "com.docker.compose.service"}}'
local format="table $compose\t$base"
docker container ls --all --format "$format" | (sed -u '1s/.*/\U&/; q'; sort-tipi)
${SUDO} docker container ls --all --format "$format" | (sed -u '1s/.*/\U&/; q'; sort-tipi)
}
get-docker-list() {
@ -59,7 +61,7 @@ get-docker-list() {
local PP2='s/ ix-/\nix-/g; s/(^|\n)ix-/\1/g;'
local PP3=''
local PP_SORT='| sort-tipi'
local SRC="" SRC_DOCKER="docker ps -a --format '{{.Label \"com.docker.compose.project\"}}@{{.Names}}' | sed -Ee 's/^@/<_no-compose-project_>:/;'"
local SRC="" SRC_DOCKER="${SUDO} docker ps -a --format '{{.Label \"com.docker.compose.project\"}}@{{.Names}}' | sed -Ee 's/^@/<_no-compose-project_>:/;'"
local FILTER=""
local LIST=''
local MK_SEARCH_LIST="| xargs -r | sed 's/@//g; s/^/^(/; s/ *$/)@/; s/ /|/g;'"
@ -98,10 +100,10 @@ get-docker-list() {
PP2=''
;;
ix|truenas)
LIST="find /mnt/.ix-apps/app_configs/* -maxdepth 0 -printf 'ix-%f@ '"
LIST="${SUDO} find /mnt/.ix-apps/app_configs/* -maxdepth 0 -printf 'ix-%f@ '"
;;
tp|runtipi)
LIST="find apps/* -maxdepth 0 -printf '%f@ ' ; echo runtipi"
LIST="${SUDO} find apps/* -maxdepth 0 -printf '%f@ ' ; echo runtipi"
;;
other)
FILTER="grep -vE \""$(get-docker-list ix -G)"\" | grep -vE \""$(get-docker-list tp -G)"\""
@ -169,7 +171,7 @@ runtipi-app-docker-compose() {
fi
local APP="${1:-Must supply app name}"
docker compose \
${SUDO} docker compose \
$(append_file_param --env-file user-config/.env.local) \
$(append_file_param --env-file app-data/${APP}/app.env) \
$(append_file_param --env-file user-config/${APP}/app.env) \
@ -194,18 +196,18 @@ ix-app-docker-compose() {
local APP="${1:-Must supply app name}"
DCSRC="$(
find /mnt/.ix-apps/app_configs/ -type f \
${SUDO} find /mnt/.ix-apps/app_configs/ -type f \
-path "*/${APP}/*/rendered/docker-compose.yaml" -printf '%T@ %p\n' \
| sort -n | cut -d' ' -f2- | head -1
)"
set -x
docker compose "--file=${DCSRC}" "--project-name=ix-${APP}" ${2:-ps -a} ${3:+"${@:3}"}
${SUDO} docker compose "--file=${DCSRC}" "--project-name=ix-${APP}" ${2:-ps -a} ${3:+"${@:3}"}
}
link-exists() {
local EXIT_CODE=0
printf 'Current status:\n link: '
ip -br -c link show "${1:?Must supply device name}" 2>&1 || return 1
${SUDO} ip -br -c link show "${1:?Must supply device name}" 2>&1 || return 1
}
manage-ipvlan() {
@ -219,19 +221,19 @@ manage-ipvlan() {
case "${1}" in
rm)
manage-ipvlan status "${IPVLAN_IF}" && ( \
ip link delete "${IPVLAN_IF}" \
${SUDO} ip link delete "${IPVLAN_IF}" \
&& printf '%s\n' "${IPVLAN_IF} removed"
) || return 1
;;
add)
link-exists "${IPVLAN_IF}" > /dev/null 2>&1 \
&& link-exists "${IPVLAN_IF}" || ( \
ip link add "${IPVLAN_IF}" link "${NET_IF}" type ipvlan mode l2 \
&& ip addr add ${NET_IP} dev "${IPVLAN_IF}" \
&& ip link set "${IPVLAN_IF}" up \
${SUDO} ip link add "${IPVLAN_IF}" link "${NET_IF}" type ipvlan mode l2 \
&& ${SUDO} ip addr add ${NET_IP} dev "${IPVLAN_IF}" \
&& ${SUDO} ip link set "${IPVLAN_IF}" up \
&& printf '%s\n' "${IPVLAN_IF} created"\
&& link-exists "${IPVLAN_IF}" \
|| ip link delete "${IPVLAN_IF}"
|| ${SUDO} ip link delete "${IPVLAN_IF}"
)
;;
route)
@ -241,9 +243,9 @@ manage-ipvlan() {
&& for IP in "${@:3}"; do
# Only add route if it does not already exists
printf 'Adding route for %-15s ' "$IP"
ip route get "$IP" 2> /dev/null | grep -q "dev ${IPVLAN_IF} src ${NET_IP//\./\\.}" \
${SUDO} ip route get "$IP" 2> /dev/null | grep -q "dev ${IPVLAN_IF} src ${NET_IP//\./\\.}" \
&& printf 'Skipping, already exists.' \
|| ip route add "$IP" dev "${IPVLAN_IF}" \
|| ${SUDO} ip route add "$IP" dev "${IPVLAN_IF}" \
&& printf '\n' \
|| (printf 'Could NOT add %s\n' "$IP" >&2; return 1)
done
@ -258,7 +260,7 @@ manage-ipvlan() {
"$(manage-ipvlan status)" "" ""\
"" "" ""\
"Usage:" "" ""\
" ${BASE_NAME} ipvlan <command>" "" ""\
" [SUDO=sudo] ${BASE_NAME} ipvlan <command>" "" ""\
"" "" "" \
"Available commands:" "" ""\
"" "" "" \
@ -271,7 +273,7 @@ manage-ipvlan() {
( printf 'addr: ' && ip -4 -br -c addr show "${IPVLAN_IF}" \
&& printf 'Routes:\n' \
&& ip -c route show dev "${IPVLAN_IF}" \
&& ${SUDO} ip -c route show dev "${IPVLAN_IF}" \
| awk '{print} END{if (NR==0) print "<none found>"}'
) 2>&1 | sed -e '/[^:]$/s/^/ /' \
|| return 1
@ -320,11 +322,14 @@ case "${1}" in
edit)
${VISUAL:-${EDITOR:-vi}} $0
;;
_load)
echo "alias ${BASE_NAME}cd='cd \"$SCRIPT_DIR/..\"'"
;;
*)
printf '%s %-12s %s\n' \
"" "" ""\
"Usage:" "" ""\
" ${BASE_NAME} <command> [args...]" "" ""\
" [SUDO=sudo] ${BASE_NAME} <command> [args...]" "" ""\
"" "" "" \
"Available commands:" "" ""\
"" "" "" \