Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save amard33p/523b653dc146965f1981c662fef6800f to your computer and use it in GitHub Desktop.
Save amard33p/523b653dc146965f1981c662fef6800f to your computer and use it in GitHub Desktop.
Foreman hooks after build script to extract and setup an ESXi ISO for installation
#!/bin/bash
# Location for ESXi ISOs
ESXI_BUILDS_LOCAL_PATH=/home/esxi_builds
# FTP root dir
FTP_ROOT=/var/ftp
# Functions to parse hook_data
. $(dirname $0)/hook_functions.sh
# event name (create, before_destroy etc.)
event=${HOOK_EVENT}
# to_s representation of the object, e.g. host's fqdn
object=${HOOK_OBJECT}
exec >> /tmp/${event}.log
exec 2>&1
# Entities captured from hook_data
system_name=$(hook_data host.name)
system_operatingsystem_name=$(hook_data host.operatingsystem_name)
echo "$(date): received ${event} on ${object}"
echo "Hostname: ${system_name} OS: ${system_operatingsystem_name}"
# Where should we extract the ISO?
iso_mountpoint=${FTP_ROOT}/${system_operatingsystem_name}
if [ ! -d ${iso_mountpoint} ]; then
# Search for the ISO using ${system_operatingsystem_name}.
# ESXi-6.7.0-8169922 = VMware-VMvisor-Installer-6.7.0-8169922.x86_64.iso
iso_path_in_builds="$(find ${ESXI_BUILDS_LOCAL_PATH} -xdev -type f \( -name "*$(echo ${system_operatingsystem_name} | cut -d '-' -f2,3)*" -and -name "*$(echo ${system_operatingsystem_name} | cut -d '-' -f4).iso" \) -print -quit)"
echo ${iso_path_in_builds}
if [ -z ${iso_path_in_builds} ]; then
echo "ISO image not found. Exiting."
exit 1
fi
# ISO located. Let's extract it
mkdir -p ${iso_mountpoint}
7z x ${iso_path_in_builds} -o${iso_mountpoint}
cd ${iso_mountpoint}
# Convert all files, dirs to lowercase
for SRC in `find . -depth`
do
DST=`dirname "${SRC}"`/`basename "${SRC}" | tr '[A-Z]' '[a-z]'`
if [ "${SRC}" != "${DST}" ]
then
[ ! -e "${DST}" ] && mv -T "${SRC}" "${DST}" || echo "${SRC} was not renamed"
fi
done
# Change permissions of directories to 755
find . -type d -exec chmod 755 {} \;
# Add prefix to boot.cfg. Change to your FOREMAN FQDN/IP
sed -i -e "s#/##g" -e "3s#^#prefix=ftp://foreman.local/${system_operatingsystem_name}\n#" boot.cfg
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment