#!/usr/bin/env bash set -e TAMARIN_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) BASE_DIR="$TAMARIN_DIR" source "$TAMARIN_DIR/lib/util.sh" function show_usage { echo echo "Usage: $0 -p project_path [-d destination] [-i image] [-k]" echo echo "Parameters: " echo echo " -p Path to the project to build" 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 " echo " -b Optional : Build directory (default /tmp)" echo exit 2 } # Create a build container based on the $BASE_IMAGE argument function create_container { # 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} local buildDir=${2} # 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" FROM $BASE_IMAGE ENV DEBIAN_FRONTEND noninteractive RUN apt-get update &&\ apt-get install --yes build-essential devscripts equivs 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 EOF if [[ -z ${BUILD_DIR} ]] then echo " CMD /root/.tamarin/lib/build.sh ${projectName} /tmp" >> "$temp_dir/Dockerfile" else echo " VOLUME /build" >> "$temp_dir/Dockerfile" echo " CMD /root/.tamarin/lib/build.sh ${projectName} /build" >> "$temp_dir/Dockerfile" fi exec_hooks "containerbuild" "$temp_dir" # Build image tar -C "$temp_dir" -czh . | docker build -t "$container_tag" - 2> >(error) 1> >(info) # Delete temporary folder rm -rf "$temp_dir" } # Main function function main { info "Building container from $BASE_IMAGE..." local project_name=$(basename ${PROJECT_PATH}) # Create container & "$container_tag" variable create_container ${project_name} local docker_opt="run -e \"DISTRIB=$BASE_IMAGE\" -e \"PROJECT_NAME=$project_name\"" if [[ ${PERSIST_CONTAINER} -eq 0 ]] then docker_opt="${docker_opt} --rm " else docker_opt="${docker_opt}" fi if [[ -z ${BUILD_DIR} ]] then docker_opt="${docker_opt} -v=\"$PROJECT_PATH:/src\" -v=\"$PROJECT_DEST:/dist\" $container_tag" else docker_opt="${docker_opt} -v=\"$PROJECT_PATH:/src\" -v=\"$PROJECT_DEST:/dist\" -v=\"${BUILD_DIR}:/build\" $container_tag" fi info "Switching to container..." echo "docker ${docker_opt}" docker ${docker_opt} res=${?} info "Done" return ${res} } # # Parsing options # while getopts "kp:d:i:b:" option do case $option in k) PERSIST_CONTAINER=1 ;; p) PROJECT_PATH=$(readlink -f ${OPTARG}) ;; d) PROJECT_DEST=$(readlink -f ${OPTARG}) ;; i) BASE_IMAGE="${OPTARG}" ;; b) BUILD_DIR=$(readlink -f ${OPTARG}) ;; *) show_usage ;; esac done [[ -z ${PROJECT_PATH} ]] && show_usage [[ -z ${PROJECT_DEST} ]] && PROJECT_DEST=$(readlink -f "./packages") [[ -z ${BASE_IMAGE} ]] && BASE_IMAGE="debian:jessie" [[ -z ${PERSIST_CONTAINER} ]] && PERSIST_CONTAINER=0 # # Warn user about "proxy" # if [[ -n ${http_proxy} ]] then info "-" info "[WARN] You have a proxy defined please make sure docker deamon is configured to use this proxy" info "-" fi main