changeset 692:d1cb22ba641d

fpkg: Modernize fpkg: local variables, jail enumeration and sorting. Also now allow spaces in all jail names.
author Franz Glasner <fzglas.hg@dom66.de>
date Tue, 01 Oct 2024 16:01:44 +0200
parents 3395d7e9cd15
children 3633a41b862b
files sbin/fpkg
diffstat 1 files changed, 77 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/sbin/fpkg	Tue Oct 01 15:22:16 2024 +0200
+++ b/sbin/fpkg	Tue Oct 01 16:01:44 2024 +0200
@@ -157,10 +157,15 @@
     : ' Do a local `freebsd-version -u` and also for all running jails
 
     '
-    echo "LOCALHOST: $(/bin/freebsd-version -u)"
-    for _jail in $(jls -N | awk '{if(NR>1)print $1}' | sort); do
-        echo "${_jail}: $(jexec -l -- "${_jail}" /bin/freebsd-version -u)"
+    local _jail _OLDIFS
+
+    printf 'LOCALHOST: %s\n' "$(/bin/freebsd-version -u)"
+    _OLDIFS="$IFS"
+    IFS=$'\n'
+    for _jail in $(/usr/sbin/jls name | LC_ALL=C.UTF-8 /usr/bin/sort); do
+        printf '%s: %s\n' "${_jail}" "$(jexec -l -- "${_jail}" /bin/freebsd-version -u)"
     done
+    IFS="$_OLDIFS"
 }
 
 
@@ -168,17 +173,21 @@
     : 'Do a local `pkg audit -Fr` and also for all running jails
 
     '
-    echo "${FPKG_SIGN}LOCALHOST"
+    local _j _OLDIFS
+
+    printf '%sLOCALHOST\n' "${FPKG_SIGN}"
     pkg audit ${FPKG_AUDIT_FLAGS}
-    for _j in $(jls -N | awk '{if(NR>1)print $1}' | sort); do
-        echo ""
-        echo "${FPKG_SIGN}JAIL: ${_j}"
+    _OLDIFS="$IFS"
+    IFS=$'\n'
+    for _j in $(/usr/sbin/jls name | LC_ALL=C.UTF-8 /usr/bin/sort); do
+        printf '\n%sJAIL: %s\n' "${FPKG_SIGN}" "${_j}"
         if has_same_userland_version "${_j}"; then
             pkg -j "${_j}" audit ${FPKG_AUDIT_FLAGS}
         else
-            echo "${FPKG_SKIPSIGN}SKIPPED because of different userland"
+            printf '%s%s\n' "${FPKG_SKIPSIGN}" "SKIPPED because of different userland"
         fi
     done
+    IFS="$_OLDIFS"
 }
 
 
@@ -186,17 +195,21 @@
     : 'Do a local `pkg update` and also for all running jails
 
     '
-    echo "${FPKG_SIGN}LOCALHOST"
+    local _j _OLDIFS
+
+    printf '%sLOCALHOST\n' "${FPKG_SIGN}"
     pkg update ${FPKG_UPDATE_FLAGS}
-    for _j in $(jls -N | awk '{if(NR>1)print $1}' | sort); do
-        echo ""
-        echo "${FPKG_SIGN}JAIL: ${_j}"
+    _OLDIFS="$IFS"
+    IFS=$'\n'
+    for _j in $(/usr/sbin/jls name | LC_ALL=C.UTF-8 /usr/bin/sort); do
+        printf '\n%sJAIL: %s\n' "${FPKG_SIGN}" "${_j}"
         if has_same_userland_version "${_j}"; then
             pkg -j "${_j}" update ${FPKG_UPDATE_FLAGS}
         else
-            echo "${FPKG_SKIPSIGN}SKIPPED because of different userland"
+            printf '%s%s\n' "${FPKG_SKIPSIGN}" "SKIPPED because of different userland"
         fi
     done
+    IFS="$_OLDIFS"
 }
 
 
@@ -204,17 +217,21 @@
     : 'Do a local `pkg upgrade` and also for all running jails
 
     '
-    echo "${FPKG_SIGN}LOCALHOST"
+    local _j _OLDIFS
+
+    printf '%sLOCALHOST\n' "${FPKG_SIGN}"
     pkg upgrade ${FPKG_UPGRADE_FLAGS}
-    for _j in $(jls -N | awk '{if(NR>1)print $1}' | sort); do
-        echo ""
-        echo "${FPKG_SIGN}JAIL: ${_j}"
+    _OLDIFS="$IFS"
+    IFS=$'\n'
+    for _j in $(/usr/sbin/jls name | LC_ALL=C.UTF-8 /usr/bin/sort); do
+        printf '\n%sJAIL: %s\n' "${FPKG_SIGN}" "${_j}"
         if has_same_userland_version "${_j}"; then
             pkg -j "${_j}" upgrade ${FPKG_UPGRADE_FLAGS}
         else
-            echo "${FPKG_SKIPSIGN}SKIPPED because of different userland"
+            printf '%s%s\n' "${FPKG_SKIPSIGN}" "SKIPPED because of different userland"
         fi
     done
+    IFS="$_OLDIFS"
 }
 
 
@@ -222,17 +239,21 @@
     : 'Do a local `pkg upgrade -n` and also for all running jails
 
     '
-    echo "${FPKG_SIGN}LOCALHOST"
+    local _j _OLDIFS
+
+    printf '%sLOCALHOST\n' "${FPKG_SIGN}"
     pkg upgrade -n ${FPKG_UPGRADE_FLAGS}
-    for _j in $(jls -N | awk '{if(NR>1)print $1}' | sort); do
-        echo ""
-        echo "${FPKG_SIGN}JAIL: ${_j}"
+    _OLDIFS="$IFS"
+    IFS=$'\n'
+    for _j in $(/usr/sbin/jls name | LC_ALL=C.UTF-8 /usr/bin/sort); do
+        printf '\n%sJAIL: %s\n' "${FPKG_SIGN}" "${_j}"
         if has_same_userland_version "${_j}"; then
             pkg -j "${_j}" upgrade -n ${FPKG_UPGRADE_FLAGS}
         else
-            echo "${FPKG_SKIPSIGN}SKIPPED because of different userland"
+            printf '%s%s\n' "${FPKG_SKIPSIGN}" "SKIPPED because of different userland"
         fi
     done
+    IFS="$_OLDIFS"
 }
 
 
@@ -245,45 +266,46 @@
         LOCALBSDPORTS_REPO: the fast-track repository name
 
     '
-    local _name _repo _j
+    local _name _repo _j _OLDIFS
 
-    echo "${FPKG_SIGN}LOCALHOST"
+    printf '%sLOCALHOST\n' "${FPKG_SIGN}"
     pkg query '%n %R' |
         while read -r _name _repo; do
             if [ "${_repo}" = "${LOCALBSDPORTS_REPO}" ]; then
-                echo "   ${_name}"
+                printf '   %s\n' "${_name}"
                 printf '      %-15s : %s\n' "${LOCALBSDPORTS_REPO}" "$(pkg version -U -r "${LOCALBSDPORTS_REPO}" -n "${_name}" -v)"
                 printf '      %-15s : %s\n' "${FREEBSD_REPO}" "$(pkg version -U -r "${FREEBSD_REPO}" -n "${_name}" -v)"
             fi
         done
-    for _j in $(jls -N | awk '{if(NR>1)print $1}' | sort); do
-        echo ""
-        echo "${FPKG_SIGN}JAIL: ${_j}"
+    _OLDIFS="$IFS"
+    IFS=$'\n'
+    for _j in $(/usr/sbin/jls name | LC_ALL=C.UTF-8 /usr/bin/sort); do
+        printf '\n%sJAIL: %s\n' "${FPKG_SIGN}" "${_j}"
         if has_same_userland_version "${_j}"; then
             pkg -j "${_j}" query '%n %R' |
-                while read -r _name _repo; do
+                while IFS="$_OLDIFS" read -r _name _repo; do
                     if [ "${_repo}" = "${LOCALBSDPORTS_REPO}" ]; then
-                        echo "   ${_name}"
+                        printf '   %s\n' "${_name}"
                         printf '      %s-15s : %s\n' "${LOCALBSDPORTS_REPO}" "$(pkg -j "${_j}" version -U -r "${LOCALBSDPORTS_REPO}" -n "${_name}" -v)"
                         printf '      %-15s : %s\n' "${FREEBSD_REPO}" "$(pkg -j "${_j}" version -U -r "${FREEBSD_REPO}" -n "${_name}" -v)"
             fi
                 done
         else
-            echo "${FPKG_SKIPSIGN}SKIPPED because of different userland"
+            printf '%s%s\n' "${FPKG_SKIPSIGN}" "SKIPPED because of different userland"
         fi
     done
+    IFS="$_OLDIFS"
 }
 
 
 command_config() {
-    : 'The `pkg config name` command on the host and all running
-    compatible jails
+    : 'The `pkg config name` command on the host and all running jails
 
     Args:
         _name: the configuration option to retrieve to
 
     '
-    local _name
+    local _name _j _OLDIFS
 
     _name="$1"
 
@@ -291,19 +313,18 @@
         echo "Usage: fpkg config <name>" >&2
         return 1
     fi
-    echo "${FPKG_SIGN}LOCALHOST"
+    printf '%sLOCALHOST\n' "${FPKG_SIGN}"
     pkg config "${_name}"
-    for _j in $(jls -N | awk '{if(NR>1)print $1}' | sort); do
-        echo ""
-        echo "${FPKG_SIGN}JAIL: ${_j}"
-        if has_same_userland_version "${_j}"; then
-            # This prints the value on the *host* also
-            #pkg -j "${_j}" config "${_name}"
-            jexec -- "${_j}" pkg config "${_name}"
-        else
-            echo "${FPKG_SKIPSIGN}SKIPPED because of different userland"
-        fi
+    _OLDIFS="$IFS"
+    IFS=$'\n'
+    for _j in $(/usr/sbin/jls name | LC_ALL=C.UTF-8 /usr/bin/sort); do
+        printf '\n%sJAIL: %s\n' "${FPKG_SIGN}" "${_j}"
+        # This prints the value on the *host* also
+        #pkg -j "${_j}" config "${_name}"
+        # with jexec it can be run on all jails
+        LC_ALL=C.UTF-8 /usr/sbin/jexec -- "${_j}" pkg config "${_name}"
     done
+    IFS="$_OLDIFS"
 }
 
 
@@ -311,17 +332,21 @@
     : 'The `pkg -vv` command on the host and all running compatible jails
 
     '
-    echo "${FPKG_SIGN}LOCALHOST"
+    local _j _OLDIFS
+
+    printf '%sLOCALHOST\n' "${FPKG_SIGN}"
     pkg -vv
-    for _j in $(jls -N | awk '{if(NR>1)print $1}' | sort); do
-        echo ""
-        echo "${FPKG_SIGN}JAIL: ${_j}"
+    _OLDIFS="$IFS"
+    IFS=$'\n'
+    for _j in $(/usr/sbin/jls name | LC_ALL=C.UTF-8 /usr/bin/sort); do
+        printf '\n%sJAIL: %s\n' "${FPKG_SIGN}" "${_j}"
         if has_same_userland_version "${_j}"; then
             pkg -j "${_j}" -vv
         else
-            echo "${FPKG_SKIPSIGN}SKIPPED because of different userland"
+            printf '%s%s\n' "${FPKG_SKIPSIGN}" "SKIPPED because of different userland"
         fi
     done
+    IFS="$_OLDIFS"
 }
 
 
@@ -330,7 +355,7 @@
 
     '
     local _j _OLDIFS
-    
+
     printf '%sLOCALHOST\n' "${FPKG_SIGN}"
     /usr/sbin/etcupdate status
     _OLDIFS="$IFS"