Auto Update CSIT Jobs to run
[releng/builder.git] / packer / provision / baseline.sh
index b019631912c1ca24b98830cebdf8b4ac938ed30a..5a97699467aab83610b1021ff219886edaee0f4d 100644 (file)
@@ -5,6 +5,33 @@
 # force any errors to cause the script and job to end in failure
 set -xeu -o pipefail
 
+ensure_kernel_install() {
+    # Workaround for mkinitrd failing on occassion.
+    # On CentOS 7 it seems like the kernel install can fail it's mkinitrd
+    # run quietly, so we may not notice the failure. This script retries for a
+    # few times before giving up.
+    initramfs_ver=$(rpm -q kernel | tail -1 | sed "s/kernel-/initramfs-/")
+    grub_conf="/boot/grub/grub.conf"
+    # Public cloud does not use /boot/grub/grub.conf and uses grub2 instead.
+    if [ ! -e "$grub_conf" ]; then
+        echo "$grub_conf not found. Using Grub 2 conf instead."
+        grub_conf="/boot/grub2/grub.cfg"
+    fi
+
+    for i in $(seq 3); do
+        if grep "$initramfs_ver" "$grub_conf"; then
+            break
+        fi
+        echo "Kernel initrd missing. Retrying to install kernel..."
+        yum reinstall -y kernel
+    done
+    if ! grep "$initramfs_ver" "$grub_conf"; then
+        cat /boot/grub/grub.conf
+        echo "ERROR: Failed to install kernel."
+        exit 1
+    fi
+}
+
 rh_systems() {
     # Handle the occurance where SELINUX is actually disabled
     SELINUX=$(grep -E '^SELINUX=(disabled|permissive|enforcing)$' /etc/selinux/config)
@@ -50,12 +77,9 @@ EOF
     echo "---> Updating operating system"
     yum clean all
     yum install -y deltarpm
-
-    # Workaround for kernel panic issue that appears sometimes after kernel update
-    #     https://www.centos.org/forums/viewtopic.php?t=22425
-    yum remove -y kernel
     yum update -y
-    yum install -y kernel
+
+    ensure_kernel_install
 
     # add in components we need or want on systems
     echo "---> Installing base packages"
@@ -85,7 +109,7 @@ EOF
             fi
         ;;
         RedHat|CentOS)
-            if [ "$(echo $FACTER_OSVER | cut -d'.' -f1)" -ge "7" ]
+            if [ "$(echo "$FACTER_OSVER" | cut -d'.' -f1)" -ge "7" ]
             then
                 echo "---> not modifying java alternatives as OpenJDK 1.7.0 does not exist"
             else
@@ -138,7 +162,7 @@ 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
+    sed -i "/127.0.0.1/s/$/ $(hostname)/" /etc/hosts
 
     echo "---> Updating operating system"
 
@@ -149,6 +173,7 @@ EOF
     # Use retry loop to install packages for failing mirrors
     for i in {0..5}
     do
+        echo "Attempt $i of installing base packages..."
         apt-get clean
         apt-get update -m
         apt-get upgrade -m
@@ -156,8 +181,10 @@ EOF
 
         for pkg in unzip xz-utils puppet git git-review libxml-xpath-perl
         do
+            # shellcheck disable=SC2046
             if [ $(dpkg-query -W -f='${Status}' $pkg 2>/dev/null | grep -c "ok installed") -eq 0 ]; then
-              apt-get install $pkg;
+                apt-cache policy $pkg
+                apt-get install $pkg
             fi
         done
     done