diff --git a/infra/image/build.sh b/infra/image/build.sh index 679c545103..9964026d19 100755 --- a/infra/image/build.sh +++ b/infra/image/build.sh @@ -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}"