kolla-ansible/tools/build-docker-image
Martin André 0764fd7dea Allow changing base image
By changing the PREFIX variable in the .buildconf one is now able to
build docker images from different bases.

For example, add the following line to your .buildconf file to build
CentOS based images:

    PREFIX=centos-rdo-

Default base image is Fedora. For now only RH family is supported.

Additionally, changing the namespace either with the NAMESPACE variable
in .buildconf or via --namespace commandline option now changes the
source namespace as well from the default kollaglue one.

Implements: blueprint multi-baseos
Co-Authored-By: Steven Dake <stdake@cisco.com>
Change-Id: I3964cd2292789ea883a1f2d2738a5731a4fff49b
2015-03-02 14:08:06 +09:00

114 lines
2.3 KiB
Bash
Executable File

#!/bin/bash
TOPDIR=$(git rev-parse --show-toplevel)
IMGDIR="$(cd "$(dirname "$0")" && pwd)"
RELEASE_NAMESPACE=kollaglue
NAMESPACE=kollaglue
PREFIX=fedora-rdo-
TAG=$(git rev-parse --short HEAD)
usage () {
cat <<EOF
Usage: $0 [options]
Options:
--namespace, -n <namespace>
--tag, -t <tag>
--push, -p
--no-cache, -N
--release
EOF
}
[ -f $TOPDIR/.buildconf ] && . $TOPDIR/.buildconf
[ -f $IMGDIR/.buildconf ] && . $IMGDIR/.buildconf
ARGS=$(getopt -o hn:t:pN -l help,namespace:,push,release,tag:,no-cache -- "$@") || { usage >&2; exit 2; }
eval set -- "$ARGS"
while :; do
case "$1" in
(--help|-h) usage
exit 0
;;
(--release) MODE=release
NAMESPACE=$RELEASE_NAMESPACE
TAG=latest
;;
(--tag|-t) shift
TAG="$1"
;;
(--push|-p) PUSH=1
;;
(--no-cache|-N)
BUILDFLAGS="${BUILDFLAGS} --no-cache"
;;
(--namespace|-n)
shift
NAMESPACE="$1"
;;
(--) break
;;
esac
shift
done
if [ "$NAMESPACE" = "$RELEASE_NAMESPACE" ] \
&& [ "$TAG" = "latest" ] \
&& ! [ "$MODE" = "release" ]; then
echo "ERROR: use --release to build a release image" >&2
exit 1
fi
IMAGE="${PREFIX}${IMGDIR##*/}"
FULLIMAGE="${NAMESPACE}/${IMAGE}${TAG:+:${TAG}}"
cat <<EOF
======================================================================
$FULLIMAGE
======================================================================
EOF
if [ "$MODE" = "release" ]; then
echo "*** YOU ARE BUILDING A RELEASE IMAGE ***"
echo
fi
TMPDIR=$(mktemp -d /tmp/output.XXXXXXXXXX)
cp -aL $IMGDIR/* $TMPDIR
# Use an extension for in-place editing for portability, as GNU and BSD
# versions of sed behave differently otherwise
sed -i.bak "s/%%KOLLA_NAMESPACE%%/${NAMESPACE}/g" $TMPDIR/Dockerfile
sed -i.bak "s/%%KOLLA_PREFIX%%/${PREFIX}/g" $TMPDIR/Dockerfile
if ! docker build ${BUILDFLAGS} -t "$FULLIMAGE" $TMPDIR; then
echo "ERROR: failed to build $FULLIMAGE"
exit 1
fi
rm -rf $TMPDIR
echo "Built: $FULLIMAGE"
if [ "$PUSH" = 1 ]; then
if ! docker push "$FULLIMAGE"; then
echo "ERROR: failed to push $FULLIMAGE"
exit 1
fi
echo "Pushed: $FULLIMAGE"
fi