Merge "Update cloud image CentOS7 builder x86_64"
[releng/builder.git] / packer / provision / baseline.sh
index 2fa3473766d0aa1316776b60be5c0716141625e6..aae30d44b1e3be34881adf56e5646a8ef1fedde7 100644 (file)
@@ -7,6 +7,8 @@ set -xeu -o pipefail
 
 enable_service() {
     # Enable services for Ubuntu instances
+    # We purposely want to allow globbing to build the package array
+    # shellcheck disable=SC2206
     services=($@)
 
     for service in "${services[@]}"; do
@@ -72,6 +74,8 @@ ensure_ubuntu_install() {
     # On Ubuntu sometimes the mirrors fail to install a package. This wrapper
     # checks that a package is successfully installed before moving on.
 
+    # We purposely want to allow globbing to build the package array
+    # shellcheck disable=SC2206
     packages=($@)
 
     for pkg in "${packages[@]}"
@@ -79,6 +83,10 @@ ensure_ubuntu_install() {
         # Retry installing package 5 times if necessary
         for i in {0..5}
         do
+
+            # Wait for any background apt processes to finish before running apt
+            while pgrep apt > /dev/null; do sleep 1; done
+
             echo "$i: Installing $pkg"
             if [ "$(dpkg-query -W -f='${Status}' "$pkg" 2>/dev/null | grep -c "ok installed")" -eq 0 ]; then
                 apt-cache policy "$pkg"
@@ -277,9 +285,6 @@ Dpkg::Options {
 
 EOF
 
-    # Add hostname to /etc/hosts to fix 'unable to resolve host' issue with sudo
-    sed -i "/127.0.0.1/s/$/ $(hostname)/" /etc/hosts
-
     echo "---> Updating operating system"
 
     # add additional repositories
@@ -440,6 +445,11 @@ all_systems() {
             echo "No extra steps for $FACTER_OS"
         ;;
     esac
+
+    # Update /etc/nss-switch.conf to map hostname with IP instead of using `localhost`
+    # from /etc/hosts which is required by some of the Java API's to avoid
+    # Java UnknownHostException: "Name or service not known" error.
+    sed -i "/^hosts:/s/$/ myhostname/" /etc/nsswitch.conf
 }
 
 echo "---> Attempting to detect OS"