2015-08-20 12:09:15 +02:00
|
|
|
#!/usr/bin/env bash
|
2015-07-02 14:04:14 +02:00
|
|
|
|
2015-08-20 12:09:15 +02:00
|
|
|
set -e
|
|
|
|
|
|
|
|
TAMARIN_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
|
|
|
|
|
|
|
|
BASE_DIR="$TAMARIN_DIR" source "$TAMARIN_DIR/lib/util.sh"
|
|
|
|
|
|
|
|
function show_usage {
|
|
|
|
echo
|
2015-10-29 17:40:26 +01:00
|
|
|
echo "Usage: $0 -p project_path [-a arch] [-d destination] [-i image] [-k]"
|
2015-08-20 12:09:15 +02:00
|
|
|
echo
|
|
|
|
echo "Parameters: "
|
|
|
|
echo
|
|
|
|
echo " -p Path to the project to build"
|
2015-10-22 14:02:51 +02:00
|
|
|
echo " -a Optional : Target architecture (default amd64)"
|
2015-10-29 17:40:26 +01:00
|
|
|
echo " -o Optional : Default options file to load (default none)"
|
2015-08-20 12:09:15 +02:00
|
|
|
echo " -d Optional : Destination of the builed packages (default ./packages)"
|
|
|
|
echo " -i Optional : Name of the Docker image to use for build (default: debian:jessie)"
|
|
|
|
echo " -k Optional : Keep the Docker container after build "
|
2015-08-20 16:29:36 +02:00
|
|
|
echo " -B Optional : Build branch (for git projects only) (default dist/ubuntu/precise/master)"
|
2015-08-20 12:09:15 +02:00
|
|
|
echo
|
|
|
|
exit 2
|
|
|
|
}
|
|
|
|
|
|
|
|
# Create a build container based on the $BASE_IMAGE argument
|
|
|
|
function create_container {
|
2015-07-02 14:04:14 +02:00
|
|
|
|
2015-08-20 12:09:15 +02:00
|
|
|
# Escape image name
|
|
|
|
local escaped_basename=$(echo "$BASE_IMAGE" | sed 's/[^a-z0-9\-\_\.]/\_/gi')
|
|
|
|
# Generate container tag
|
|
|
|
container_tag="tamarin:${escaped_basename}_$(date +%s)"
|
|
|
|
# Create temporary dir for the Dockerfile
|
|
|
|
local temp_dir="$(mktemp -d)"
|
|
|
|
|
|
|
|
local projectName=${1}
|
|
|
|
|
|
|
|
# Link lib & hooks folders
|
|
|
|
ln -s $(readlink -f "$TAMARIN_DIR/lib") "$temp_dir/lib"
|
|
|
|
ln -s $(readlink -f "$TAMARIN_DIR/hooks") "$temp_dir/hooks"
|
|
|
|
|
|
|
|
# Create Dockerfile
|
|
|
|
cat << EOF > "$temp_dir/Dockerfile"
|
2015-10-29 17:40:26 +01:00
|
|
|
FROM ${BASE_IMAGE}
|
2015-08-20 12:09:15 +02:00
|
|
|
|
|
|
|
ENV DEBIAN_FRONTEND noninteractive
|
|
|
|
|
|
|
|
RUN apt-get update &&\
|
2015-09-23 16:54:53 +02:00
|
|
|
apt-get install --yes --no-install-recommends build-essential devscripts equivs
|
2015-08-20 12:09:15 +02:00
|
|
|
|
|
|
|
RUN mkdir /root/.tamarin
|
|
|
|
RUN mkdir /project
|
|
|
|
|
|
|
|
ADD ./lib /root/.tamarin/lib
|
|
|
|
ADD ./hooks /hooks
|
|
|
|
RUN chmod +x /root/.tamarin/lib/build.sh
|
|
|
|
|
|
|
|
VOLUME /src
|
|
|
|
VOLUME /dist
|
|
|
|
|
2015-10-29 17:40:26 +01:00
|
|
|
EOF
|
2015-08-20 12:09:15 +02:00
|
|
|
|
2015-10-22 16:55:54 +02:00
|
|
|
# Add default opts file if defined
|
|
|
|
if [[ -e "${DEFAULT_OPTS}" ]]; then
|
|
|
|
ln -s "${DEFAULT_OPTS}" "$temp_dir/default_opts"
|
2015-10-29 17:40:26 +01:00
|
|
|
echo "ADD ./default_ops /tmp/tamarin/default_opts" >> "$temp_dir/Dockerfile"
|
2015-10-22 16:55:54 +02:00
|
|
|
fi
|
|
|
|
|
2015-10-29 17:40:26 +01:00
|
|
|
exec_hooks "containerbuild" "$temp_dir"
|
|
|
|
|
2015-11-18 10:45:20 +01:00
|
|
|
echo "CMD /root/.tamarin/lib/build.sh ${projectName} ${BUILD_BRANCH} /tmp ${BUILD_TAG}" >> "$temp_dir/Dockerfile"
|
|
|
|
|
2015-08-20 12:09:15 +02:00
|
|
|
# Build image
|
2015-10-22 16:55:54 +02:00
|
|
|
tar -C "$temp_dir" -czh . | docker build -t "$container_tag" - 2> >(stderr) 1> >(stdout)
|
2015-08-20 12:09:15 +02:00
|
|
|
|
|
|
|
# Delete temporary folder
|
|
|
|
rm -rf "$temp_dir"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
# Main function
|
|
|
|
function main {
|
|
|
|
|
|
|
|
info "Building container from $BASE_IMAGE..."
|
|
|
|
|
2015-08-20 12:18:36 +02:00
|
|
|
local project_name=$(basename ${PROJECT_PATH})
|
2015-08-05 15:49:04 +02:00
|
|
|
|
2015-08-20 12:09:15 +02:00
|
|
|
# Create container & "$container_tag" variable
|
|
|
|
create_container ${project_name}
|
|
|
|
|
|
|
|
local docker_opt="run -e \"DISTRIB=$BASE_IMAGE\" -e \"PROJECT_NAME=$project_name\""
|
|
|
|
|
2015-09-23 16:54:53 +02:00
|
|
|
# Expose host uid & gid
|
|
|
|
docker_opt="${docker_opt} -e HOST_UID=$(id -u) -e HOST_GID=$(id -g)"
|
2015-10-22 14:02:51 +02:00
|
|
|
|
2015-10-20 17:17:02 +02:00
|
|
|
# Expose host proxy variables
|
|
|
|
docker_opt="${docker_opt} -e HTTP_PROXY=${HTTP_PROXY} -e HTTPS_PROXY=${HTTPS_PROXY}"
|
|
|
|
docker_opt="${docker_opt} -e http_proxy=${http_proxy} -e https_proxy=${https_proxy}"
|
2015-09-23 16:54:53 +02:00
|
|
|
|
2015-10-22 14:02:51 +02:00
|
|
|
# Target architecture
|
|
|
|
docker_opt="${docker_opt} -e TARGET_ARCH=${TARGET_ARCH}"
|
|
|
|
|
2015-09-23 16:54:53 +02:00
|
|
|
if [[ ${PERSIST_CONTAINER} -eq 0 ]]
|
2015-08-20 12:09:15 +02:00
|
|
|
then
|
|
|
|
docker_opt="${docker_opt} --rm "
|
|
|
|
else
|
|
|
|
docker_opt="${docker_opt}"
|
|
|
|
fi
|
2015-09-23 16:54:53 +02:00
|
|
|
|
2015-10-29 17:40:26 +01:00
|
|
|
docker_opt="${docker_opt} -v=\"$PROJECT_PATH:/src:ro\" -v=\"$PROJECT_DEST:/dist:rw\" $container_tag"
|
2015-08-20 12:09:15 +02:00
|
|
|
|
|
|
|
info "Switching to container..."
|
2015-09-23 16:54:53 +02:00
|
|
|
debug "docker ${docker_opt}"
|
2015-08-20 12:09:15 +02:00
|
|
|
docker ${docker_opt}
|
|
|
|
res=${?}
|
|
|
|
|
2015-10-22 16:55:54 +02:00
|
|
|
success "Done"
|
2015-08-20 12:09:15 +02:00
|
|
|
return ${res}
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
2015-09-23 16:54:53 +02:00
|
|
|
# Parsing options
|
|
|
|
#
|
2015-10-29 17:40:26 +01:00
|
|
|
while getopts "kp:d:i:B:t:a:o:" option
|
2015-08-20 12:09:15 +02:00
|
|
|
do
|
|
|
|
case $option in
|
|
|
|
k)
|
|
|
|
PERSIST_CONTAINER=1
|
|
|
|
;;
|
|
|
|
p)
|
|
|
|
PROJECT_PATH=$(readlink -f ${OPTARG})
|
|
|
|
;;
|
|
|
|
d)
|
|
|
|
PROJECT_DEST=$(readlink -f ${OPTARG})
|
|
|
|
;;
|
|
|
|
i)
|
|
|
|
BASE_IMAGE="${OPTARG}"
|
|
|
|
;;
|
2015-08-20 16:29:36 +02:00
|
|
|
B)
|
|
|
|
BUILD_BRANCH=${OPTARG}
|
|
|
|
;;
|
2015-08-20 17:35:43 +02:00
|
|
|
t)
|
|
|
|
BUILD_TAG=${OPTARG}
|
|
|
|
;;
|
2015-10-22 14:02:51 +02:00
|
|
|
a)
|
|
|
|
TARGET_ARCH=${OPTARG}
|
|
|
|
;;
|
2015-10-29 17:40:26 +01:00
|
|
|
o)
|
2015-10-22 16:55:54 +02:00
|
|
|
DEFAULT_OPTS=$(readlink -f ${OPTARG})
|
|
|
|
;;
|
2015-08-20 12:09:15 +02:00
|
|
|
*)
|
|
|
|
show_usage
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
[[ -z ${PROJECT_PATH} ]] && show_usage
|
|
|
|
[[ -z ${PROJECT_DEST} ]] && PROJECT_DEST=$(readlink -f "./packages")
|
|
|
|
[[ -z ${BASE_IMAGE} ]] && BASE_IMAGE="debian:jessie"
|
2015-08-20 16:29:36 +02:00
|
|
|
[[ -z ${BUILD_BRANCH} ]] && BUILD_BRANCH="dist/ubuntu/precise/master"
|
2015-08-20 12:09:15 +02:00
|
|
|
[[ -z ${PERSIST_CONTAINER} ]] && PERSIST_CONTAINER=0
|
2015-10-22 14:02:51 +02:00
|
|
|
[[ -z ${TARGET_ARCH} ]] && TARGET_ARCH=amd64
|
2015-08-20 12:09:15 +02:00
|
|
|
|
|
|
|
#
|
|
|
|
# Warn user about "proxy"
|
|
|
|
#
|
|
|
|
|
|
|
|
if [[ -n ${http_proxy} ]]
|
|
|
|
then
|
2015-10-22 16:55:54 +02:00
|
|
|
warn "You have a proxy defined please make sure docker deamon is configured to use this proxy"
|
2015-08-20 12:09:15 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
main
|