ci: handle sudo in actions-install.sh
authorVincent Fu <vincent.fu@samsung.com>
Mon, 23 Sep 2024 16:41:35 +0000 (16:41 +0000)
committerVincent Fu <vincent.fu@samsung.com>
Thu, 26 Sep 2024 17:29:45 +0000 (13:29 -0400)
We run our tests as root in containers and VMs. Those platforms do not
have sudo. Use a small bash function to eliminate the need to install
sudo at the very beginning of the dependency installation script.

This function just checks for the existence of sudo and uses it when
installed but does nothing when it is not installed. With this function
we can just add sudo to the list of packages to install like all the
other packages. We may still need sudo in one of our test scripts.

Signed-off-by: Vincent Fu <vincent.fu@samsung.com>
ci/actions-install.sh

index a2d0922c35eb4d3fba85d6d8abf4a45295a9347f..5ad7b959294e55dd28cf884a96706e2b92957e1c 100755 (executable)
@@ -6,16 +6,18 @@ SCRIPT_DIR=$(dirname "$0")
 # shellcheck disable=SC1091
 . "${SCRIPT_DIR}/common.sh"
 
+_sudo() {
+    if type -P sudo >/dev/null; then
+        sudo "$@"
+    else
+        "$@"
+    fi
+}
+
 install_ubuntu() {
     local pkgs
 
-    if [ "${GITHUB_JOB}" == "build-containers" ]; then
-        # containers run as root and do not have sudo
-        apt update
-        apt -y install sudo
-    fi
-
-    cat <<DPKGCFG | sudo tee /etc/dpkg/dpkg.cfg.d/dpkg-speedup > /dev/null
+    cat <<DPKGCFG | _sudo tee /etc/dpkg/dpkg.cfg.d/dpkg-speedup > /dev/null
 # Skip fsync
 force-unsafe-io
 # Don't install documentation
@@ -36,7 +38,7 @@ DPKGCFG
     )
     case "${CI_TARGET_ARCH}" in
         "i686")
-            sudo dpkg --add-architecture i386
+            _sudo dpkg --add-architecture i386
             pkgs=("${pkgs[@]/%/:i386}")
             pkgs+=(
                 gcc-multilib
@@ -62,7 +64,7 @@ DPKGCFG
             )
            if apt list --installed | grep -c "libunwind-14-dev"; then
                    echo "Removing libunwind-14-dev because of conflicts with libunwind-dev"
-                   sudo apt remove -y libunwind-14-dev
+                   _sudo apt remove -y libunwind-14-dev
            fi
            if [ "${CI_TARGET_OS}" == "linux" ] || [ "${CI_TARGET_OS}" == "ubuntu" ]; then
                # Only for Ubuntu
@@ -79,6 +81,7 @@ DPKGCFG
         python3-scipy
        python3-sphinx
        python3-statsmodels
+       sudo
     )
     if [ "${GITHUB_JOB}" == "build-containers" ]; then
         pkgs+=(
@@ -90,9 +93,9 @@ DPKGCFG
     fi
 
     echo "Updating APT..."
-    sudo apt-get -qq update
+    _sudo apt-get -qq update
     echo "Installing packages... ${pkgs[@]}"
-    sudo apt-get install -o APT::Immediate-Configure=false --no-install-recommends -qq -y "${pkgs[@]}"
+    _sudo apt-get install -o APT::Immediate-Configure=false --no-install-recommends -qq -y "${pkgs[@]}"
 }
 
 # Fedora and related distributions