Skip to content

Instantly share code, notes, and snippets.

@rjhornsby
Last active February 4, 2023 19:06
Show Gist options
  • Save rjhornsby/359ad009d0bd7ff2f6976a97d9b29f20 to your computer and use it in GitHub Desktop.
Save rjhornsby/359ad009d0bd7ff2f6976a97d9b29f20 to your computer and use it in GitHub Desktop.
cloud-init build+install
#!/usr/bin/env bash
# The shellcheck disable needs to appear before the first command to apply to the whole file
# shellcheck disable=SC2086
echo "$CLOUD_INIT_TMP" | grep -q "\s" && bye "Remove the dang whitespace from CLOUD_INIT_TMP and try again."
CLOUD_INIT_VERSION=22.4
CLOUD_INIT_SERIES_VERSION=$(echo $CLOUD_INIT_VERSION | awk -F. '{print $1 "." $2}')
SUDO_PRIV=1 source /tmp/packer/lib/lib.sh
# CentOS 7 only has cloud-init 19.4 available as an rpm. This is quite old and relies on the long
# deprecated python2. Instead, we'll skip the RPM and install cloud-init ourselves
source_dir=${CLOUD_INIT_TMP}/cloud-init-${CLOUD_INIT_VERSION}
mkdir -p $source_dir || bye "Could not create directory ${source_dir}"
wget -q "https://launchpad.net/cloud-init/trunk/${CLOUD_INIT_SERIES_VERSION}/+download/cloud-init-${CLOUD_INIT_VERSION}.tar.gz" -O "${CLOUD_INIT_TMP}/${CLOUD_INIT_VERSION}.tar.gz"
tar -x -C $CLOUD_INIT_TMP -f ${CLOUD_INIT_TMP}/${CLOUD_INIT_VERSION}.tar.gz
pushd "$source_dir" || bye "Could not change directory to ${source_dir}"
pip3 install -r "${source_dir}/requirements.txt"
py_ver=$(python3 --version | awk -F'[. ]' '{print $2 "." $3}')
# needed for the build
mkdir -p "/usr/local/lib/python${py_ver}"
# build requirements
pip3 install pytest || bye "Could not install build dependencies"
python3 setup.py build
########### fails during this step ###########
python3 setup.py install --init-system=systemd || bye "Failed installing cloud-init"
########### fails during ^ step ###########
popd || bye "cd failed, could not popd"
# do not `systemctl enable cloud-init` here
# this function will be handled by the generator at /usr/lib/systemd/system-generators/cloud-init-generator
# see
# * https://cloudinit.readthedocs.io/en/20.4/topics/boot.html#generator
# * https://www.freedesktop.org/software/systemd/man/systemd.generator.html
systemctl enable cloud-init.service
systemctl enable cloud-config.service
systemctl enable cloud-final.service
systemctl enable cloud-init-local.service
# systemd looks in /usr, while python expects it at /usr/local
ln -s /usr/local/bin/cloud-init /usr/bin/cloud-init
[ -d /var/lib/cloud/scripts ] || mkdir -p /var/lib/cloud/scripts
# Clobber any existing cloud.cfg file
cp -v ${CLOUD_INIT_TMP}/cloud.cfg /etc/cloud/cloud.cfg
is_rhel && cp ${CLOUD_INIT_TMP}/os/rhel.yaml /etc/cloud/cloud.cfg.d/
is_debian && cp ${CLOUD_INIT_TMP}/os/debian.yaml /etc/cloud/cloud.cfg.d/
# Just copy the files, don't clobber any existing directory structure
rsync -av ${CLOUD_INIT_TMP}/cloud.cfg.d/* /etc/cloud/cloud.cfg.d/
rsync -av ${CLOUD_INIT_TMP}/var/lib/cloud/scripts/* /var/lib/cloud/scripts/
rsync -av ${CLOUD_INIT_TMP}/templates/* /etc/cloud/templates
chmod -R 755 /var/lib/cloud/scripts/*
mv -v ${CLOUD_INIT_TMP}/profile.d/cloud-init-msg.sh /etc/profile.d/
chmod 755 /etc/profile.d/cloud-init-msg.sh
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment