Skip to content
Snippets Groups Projects
Unverified Commit 18cc6460 authored by Antoine R. Dumont's avatar Antoine R. Dumont
Browse files

Fix build iso routine to be able to deal with optional config keys

Earlier this year, we had to adapt the routine to be able to define the net devices and
the interfaces diffently than the default names (net0, ...). It worked at the time
because we declared those in the $(hostname).yaml configuration file. But now, trying to
use the default values (without specifying those new entries), the routine does not
work. The cli j2 is not happy about non declared template values (IPXE_NET, ...) [1]

This fixes it by using temporary templates which gets adapted through sed to fill-in the
holes. Once done, the temporary templates are cleaned up.

[1]
```
jinja2.exceptions.UndefinedError: 'IPXE_NET' is undefined
```

Refs. sysadm-environment#5519
parent c3014cbf
No related branches found
No related tags found
No related merge requests found
......@@ -18,40 +18,62 @@ if ! [ -f "${CONFIG}" ]; then
exit 2
fi
j2 -f yaml "${BASEDIR}/configs/template.ipxe.j2" "${CONFIG}" >"${IPXE_SCRIPT}"
TMPDIR="$(mktemp -d)"
trap 'rm -rf -- "${TMPDIR}"' EXIT
# If not provided in the hostname configuration, fallback to the net0
sed -i s'/{{IPXE_NET}}/net0/g' "${IPXE_SCRIPT}"
TEMPLATE_IPXE_TMP="${TMPDIR}/template.ipxe.j2"
cp ${BASEDIR}/configs/template.ipxe.j2 $TEMPLATE_IPXE_TMP
# Set optional value IPXE_NET directly if not provided in the yaml
# (missing templating variable will make the j2 call fail otherwise)
grep "IPXE_NET" $CONFIG >/dev/null || sed -i s'/{{IPXE_NET}}/net0/g' "${TEMPLATE_IPXE_TMP}"
j2 -f yaml "${TEMPLATE_IPXE_TMP}" "${CONFIG}" > "${IPXE_SCRIPT}"
make -C "${BASEDIR}/src" -j8 "${IPXE_TARGET}" EMBED="${IPXE_SCRIPT}" DEBUG=eth_slow NO_WERROR=1
mv "${BASEDIR}/src/${IPXE_TARGET}" "${BASEDIR}/configs/${HOSTNAME}.iso"
echo "built iso image ${BASEDIR}/configs/${HOSTNAME}.iso" 1>&2
[[ -e "${PASSWORDS_FILE}" ]] && PASSWORD=$(awk -F: "\$1 == \"${HOSTNAME}\" { print \$2 }" ${PASSWORDS_FILE})
[[ -e "${PASSWORDS_FILE}" ]] && \
PASSWORD=$(awk -F: "\$1 == \"${HOSTNAME}\" { print \$2 }" ${PASSWORDS_FILE})
if [ -z "$PASSWORD" ]; then
PASSWORD="$(xkcdpass -d- -a "${HOSTNAME:0:4}")"
echo "${HOSTNAME}:${PASSWORD}" >>${PASSWORDS_FILE}
fi
CRYPTED_PASSWORD="$(echo "${PASSWORD}" | mkpasswd -s)"
PRESEED_FILE="${BASEDIR}/configs/preseeding/${HOSTNAME}.txt"
env CRYPTED_PASSWORD=$CRYPTED_PASSWORD j2 -f yaml -e env "${BASEDIR}/configs/preseeding/preseed.txt.j2" "${CONFIG}" >"${PRESEED_FILE}"
PRESEED_DIR="${BASEDIR}/configs/preseeding"
PRESEED_FILE="${PRESEED_DIR}/${HOSTNAME}.txt"
# If not provided in the hostname configuration, fallback to the default interface
sed -i s'/{{PRESEED_INTERFACE0}}/ens10f0np0/g' "${PRESEED_FILE}"
sed -i s'/{{PRESEED_INTERFACE1}}/ens10f0np1/g' "${PRESEED_FILE}"
TEMPLATE_PRESEED_TMP="${TMPDIR}/preseed.txt.j2"
cp ${PRESEED_DIR}/preseed.txt.j2 $TEMPLATE_PRESEED_TMP
# If not provided in the hostname configuration, fallback to the default interfaces
# (required to be done prior to the j2 call otherwise, it will fail)
grep "PRESEED_INTERFACE0" $CONFIG >/dev/null || \
sed -i s'/{{PRESEED_INTERFACE0}}/ens10f0np0/g' "${TEMPLATE_PRESEED_TMP}"
grep "PRESEED_INTERFACE1" $CONFIG >/dev/null || \
sed -i s'/{{PRESEED_INTERFACE1}}/ens10f0np1/g' "${TEMPLATE_PRESEED_TMP}"
FINISH_INSTALL="${BASEDIR}/configs/preseeding/finish_install/${HOSTNAME}.sh"
j2 -f yaml -e env "${BASEDIR}/configs/preseeding/finish_install/finish_install.sh.j2" "${CONFIG}" >"${FINISH_INSTALL}"
env CRYPTED_PASSWORD=$CRYPTED_PASSWORD \
j2 -f yaml -e env "${TEMPLATE_PRESEED_TMP}" "${CONFIG}" > "${PRESEED_FILE}"
FINISH_INSTALL_DIR="${PRESEED_DIR}/finish_install"
FINISH_INSTALL="${FINISH_INSTALL_DIR}/${HOSTNAME}.sh"
TEMPLATE_FINISH_INSTALL_TMP="${TMPDIR}/finish_install.sh.j2"
cp ${FINISH_INSTALL_DIR}/finish_install.sh.j2 $TEMPLATE_FINISH_INSTALL_TMP
# If not provided in the hostname configuration, fallback to the default interface
sed -i s'/{{PRESEED_INTERFACE0}}/ens10f0np0/g' "${FINISH_INSTALL}"
sed -i s'/{{PRESEED_INTERFACE1}}/ens10f0np1/g' "${FINISH_INSTALL}"
grep "PRESEED_INTERFACE0" $CONFIG >/dev/null || \
sed -i s'/{{PRESEED_INTERFACE0}}/ens10f0np0/g' "${TEMPLATE_FINISH_INSTALL_TMP}"
grep "PRESEED_INTERFACE1" $CONFIG >/dev/null || \
sed -i s'/{{PRESEED_INTERFACE1}}/ens10f0np1/g' "${TEMPLATE_FINISH_INSTALL_TMP}"
j2 -f yaml -e env "${TEMPLATE_FINISH_INSTALL_TMP}" "${CONFIG}" >"${FINISH_INSTALL}"
echo "Generated preseeding config in ${PRESEED_FILE} and ${FINISH_INSTALL}." 1>&2
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment