Skip to content

Commit

Permalink
rjeffman: this is a fixup for infra/images/build.sh
Browse files Browse the repository at this point in the history
This patch modifies the image building script by adding:

- An usage message.
- An option "-I" to NOT install IPA to the generated container.
- An opiton "-c NAME" to both set the name and use an existing container
  to ONLY install IPA.
- Rename "scenario" to "DISTRO" as "scenario" should be used for the
  container scenario usage, rather than the distro (I'll change the name
  also in the Azure scripts)
- Use 'log' (from shlog) to print messages.
  • Loading branch information
rjeffman committed Jul 20, 2024
1 parent d9a9e46 commit ffb892a
Showing 1 changed file with 90 additions and 32 deletions.
122 changes: 90 additions & 32 deletions infra/image/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,113 @@
BASEDIR="$(readlink -f "$(dirname "$0")")"
TOPDIR="$(readlink -f "${BASEDIR}/../..")"

scenario=${1:-}
. "${TOPDIR}/utils/shfun"

valid_distro() {
find "${BASEDIR}/dockerfile" -printf "%f\n" | tr "\n" " "
}

usage() {
local prog="${0##*/}"
cat << EOF
usage: ${prog} [-h] [i] DISTRO
${prog} build a container image to test ansible-freeipa.
EOF
}

help() {
cat << EOF
positional arguments:
DISTRO The base distro to build the test container.
Availble distros: $(valid_distro)
optionol arguments:
-c CONTAINER Use existing container CONTAINER as base.
-I Do not install an IPA files on the container.
(Implies -C)
EOF
}

name="ansible-test"
hostname="ipaserver.test.local"
cpus="2"
memory="4g"
REBUILD="Y"
INSTALL="Y"

if [ -z "${scenario}" ]; then
echo "ERROR: Image needs to be given"
exit 1
fi
if [ ! -f "${BASEDIR}/dockerfile/${scenario}" ]; then
echo "ERROR: ${scenario} is not a valid image"
exit 1
while getopts ":hc:CI" option
do
case "${option}" in
h) help && exit 0 ;;
c) REBUILD="N" ; name="${OPTARG}" ;;
I) INSTALL="N" ;;
*) die -u "Invalid option: ${option}" ;;
esac
done

shift $((OPTIND - 1))
DISTRO=${1:-}

[ -n "${DISTRO}" ] || die "Distro needs to be given.\nUse one of: $(valid_distro)"

[ -f "${BASEDIR}/dockerfile/${DISTRO}" ] \
|| die "${DISTRO} is not a valid distro target.\nUse one of: $(valid_distro)"

if [ "${INSTALL}" == "Y" ]
then
[ -n "$(command -v "ansible-playbook")" ] || die "ansible-playbook is required to install FreeIPA."

deploy_playbook="${TOPDIR}/playbooks/install-server.yml"
[ -f "${deploy_playbook}" ] || die "Can't find playbook '${deploy_playbook}'"

inventory_file="${BASEDIR}/inventory"
[ -f "${inventory_file}" ] || die "Can't find inventory '${inventory_file}'"
fi

echo "= Cleanup possible existing ${scenario} ="
podman image rm "${scenario}" --force
echo
container_state="$(podman ps -q --all --format "{{.State}}" --filter "name=${name}")"

echo "= Building ${scenario} ="
podman build -t "${scenario}" -f "${BASEDIR}/dockerfile/${scenario}" \
"${BASEDIR}"
echo
if [ "${REBUILD}" == "Y" ] || [ -z "${container_state}" ]
then
log info "= Cleanup possible existing ${DISTRO} ="
podman image rm "${DISTRO}" --force
echo

echo "= Creating ${name} ="
podman create --privileged --name "${name}" --hostname "${hostname}" \
--network bridge:interface_name=eth0 --systemd true \
--cpus "${cpus}" --memory "${memory}" --memory-swap -1 --no-hosts \
--replace "${scenario}"
echo
log info "= Building ${DISTRO} ="
podman build -t "${DISTRO}" -f "${BASEDIR}/dockerfile/${DISTRO}" \
"${BASEDIR}"
echo

echo "= Starting ${name} ="
podman start "${name}"
echo
log info "= Creating ${name} ="
podman create --privileged --name "${name}" --hostname "${hostname}" \
--network bridge:interface_name=eth0 --systemd true \
--cpus "${cpus}" --memory "${memory}" --memory-swap -1 --no-hosts \
--replace "${DISTRO}"
echo
fi

echo "= Installing IPA ="
ansible-playbook -i "${BASEDIR}/inventory" "${TOPDIR}/playbooks/install-server.yml"
log info "= Starting ${name} ="
[ "${container_state}" == "running" ] || podman start "${name}"
echo

echo "= Enabling additional services ="
podman exec "${name}" systemctl enable fixnet
podman exec "${name}" systemctl enable fixipaip
echo
if [ "${INSTALL}" == "Y" ]
then
log info "= Installing IPA ="
ansible-playbook -i "${inventory_file}" "${deploy_playbook}"
echo

log info "= Enabling additional services ="
podman exec "${name}" systemctl enable fixnet
podman exec "${name}" systemctl enable fixipaip
echo
fi

echo "= Stopping ${name} ="
log info "= Stopping container ${name} ="
podman stop "${name}"
echo

echo "= DONE ="
log info "= DONE: Image created. ="

# For tests:
# podman start "${name}"
Expand Down

0 comments on commit ffb892a

Please sign in to comment.