[CentOS-devel] Scripted build of a dockerfile (and run it)
Clint Savage
herlo1 at gmail.com
Wed Apr 13 15:34:39 UTC 2016
Troy wanted to see a copy of my Dockerfile builder. I've sanitized it a
little so it may not run exactly, but the concept is close enough. The goal
was to take one of our tools, and test it in an isolated environment. We
couldn't use virtualenv even though it was all python, due to the fact that
we were modifying 4 different os releases.
Cheers,
herlo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.centos.org/pipermail/centos-devel/attachments/20160413/67a02d41/attachment.html>
-------------- next part --------------
#!/bin/bash
#
# Testing script which spins up a docker instance each time
# a major change is made to a file
DOCKER="/usr/bin/docker"
DOCKER_ACCT_DEST=lf_tooldev
DOCKER_ACCT_SRC=herlolf
DOCKER_IMG_REV=latest
DISTROS=(centos:7 centos:6 ubuntu:14.04 opensuse:13.2)
SCRIPT_HOME=${PWD}
TOOLS_DIR=${SCRIPT_HOME}/tmp/tools/tools
TOOLS_FILES=(__init__.py linux/linuxtools.py tools.pth)
DELETE_IMAGE=y
BUILD_ONLY=n
NOSETESTS="FALSE"
while getopts :d:s:khbn FLAG; do
case "${FLAG}" in
d) #get distro list
IFS=' ' read -a DISTROS <<< ${OPTARG}
# DISTROS="${OPTARG}"
;;
k) # keep image flag
DELETE_IMAGE=n
;;
n) # run nosetests
NOSETESTS="TRUE"
;;
s) # docker source account
DOCKER_ACCT_SRC=${OPTARG}
;;
b) # docker source account
BUILD_ONLY=y
;;
h)
echo "Usage: $0 [-d <distro_list>] [-k] [-s <docker_image_src>] [-h] <dev_path>"
echo
echo "-d: distributions for which to build docker images."
echo ' Default: (centos:7 centos:6 ubuntu:14.04 opensuse:13.2)'
echo
echo "-k: keep generated docker images"
echo
echo "-s: source docker account used for building images"
echo " Default: herlolf"
echo " See examples at https://hub.docker.com/u/herlolf/"
echo
echo "-h: help"
echo
echo -e "(eg $0 -d "centos:7 ubuntu:14.04" -s herlolf -k '/path/to/tool')"\\n
exit 0
;;
:)
echo "Option -${OPTARG} requires an argument"
exit 1
;;
esac
done
shift $((OPTIND-1))
if [ $# -lt 1 ]; then
echo "For Help, use '$0 -h'"
exit 1
else
if [ -d "${1}" ]; then
DEV_PATH=$1
else
echo "${1} is not a valid path. Please check and try again."
exit 1
fi
fi
for file in ${TOOLS_FILES[*]}; do
if [ ! -e "${TOOLS_DIR}/${file}" ]; then
echo "Please copy ${file} from git"
echo "to ${TOOLS_DIR}/."
exit 1
fi
done
if [ "${NOSETESTS}" == "TRUE" ]; then
CMD_TO_RUN="[ \"/srv/tools/run_tests.sh\", \"TRUE\" ]"
else
CMD_TO_RUN="[ \"/srv/tools/run_tests.sh\" ]"
fi
#echo "CMD_TO_RUN: ${CMD_TO_RUN}"
# clean up ${DEV_PATH}
find ${DEV_PATH} -name '.*sw*' -delete
for DISTRO in ${DISTROS[*]}; do
D=${DISTRO/:*/}
V=${DISTRO##*:}
rm -rf ${SCRIPT_HOME}/tmp/${D}-${V}
case ${DISTRO} in
"centos:7" )
PYTHON_PATH='/usr/lib/python2.7/site-packages'
;;
"centos:6" )
PYTHON_PATH='/usr/lib/python2.6/site-packages'
;;
"ubuntu:14.04" )
PYTHON_PATH='/usr/lib/python2.7/dist-packages'
;;
"opensuse:13.2" )
PYTHON_PATH='/usr/lib/python2.7/site-packages'
;;
esac
if [ ! -d "${SCRIPT_HOME}/tmp/${D}-${V}" ]; then
mkdir -p ${SCRIPT_HOME}/tmp/${D}-${V}
fi
if [ ! -d "${SCRIPT_HOME}/tmp/tools" ]; then
mkdir -p ${SCRIPT_HOME}/tmp/tools
fi
cp -pr ${DEV_PATH} ${SCRIPT_HOME}/tmp/${D}-${V}/
cat <<__EOF__ > ${SCRIPT_HOME}/Dockerfile.${D}-${V}
FROM ${DOCKER_ACCT_SRC}/${D}-${V}:${DOCKER_IMG_REV}
MAINTAINER "Clint Savage" <herlo at linuxfoundation.org>
# copy in salt minion configs
COPY salt/conf/minion /etc/salt/minion
# copy in tools to test
COPY tmp/tools/ /srv/tools/tools/
ADD tmp/tools/tools/tools.pth ${PYTHON_PATH}/tools.pth
COPY tmp/${D}-${V} /srv/tools/
COPY run_tests.sh /srv/tools/
RUN chmod +x /srv/tools/run_tests.sh
CMD ${CMD_TO_RUN}
__EOF__
DATE=$(date +%Y%m%d%H%M)
echo
echo "BUILDING DOCKER IMAGE ${DOCKER_ACCT_DEST}/${D}-${V}:${DATE}"
echo "==========================================================="
${DOCKER} build --rm=true --pull=false -t ${DOCKER_ACCT_DEST}/${D}-${V}:${DATE} \
--file=${SCRIPT_HOME}/Dockerfile.${D}-${V} ${SCRIPT_HOME} &> /dev/null
if [ "${BUILD_ONLY}" == "y" ]; then
echo "BUILD_ONLY (-b) flag was passed, stopping"
echo "To run the image, try:"
echo "${DOCKER} run -it ${DOCKER_ACCT_DEST}/${D}-${V}:${DATE} '/bin/bash'"
exit 0
fi
${DOCKER} run -it ${DOCKER_ACCT_DEST}/${D}-${V}:${DATE}
if [ "${DELETE_IMAGE}" == "y" ]; then
echo
echo "DELETING DOCKER IMAGE ${DOCKER_ACCT_DEST}/${D}-${V}:${DATE}"
echo "==========================================================="
${DOCKER} rmi --force ${DOCKER_ACCT_DEST}/${D}-${V}:${DATE} &> /dev/null
fi
done
More information about the CentOS-devel
mailing list