Tamarin/package.sh

188 lines
4.8 KiB
Bash
Raw Normal View History

2015-08-20 12:09:15 +02:00
#!/usr/bin/env bash
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
echo "Usage: $0 -p project_path [-d destination] [-i image] [-k]"
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-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 "
echo " -b Optional : Build directory (default /tmp)"
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-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}
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 --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
EOF
2015-09-23 21:21:30 +02:00
exec_hooks "containerbuild" "$temp_dir"
if [[ -z "${BUILD_DIR}" ]]; then
2015-08-20 17:35:43 +02: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
else
echo " VOLUME /build" >> "$temp_dir/Dockerfile"
2015-08-20 17:35:43 +02:00
echo " CMD /root/.tamarin/lib/build.sh ${projectName} ${BUILD_BRANCH} /build ${BUILD_TAG}" >> "$temp_dir/Dockerfile"
2015-08-20 12:09:15 +02:00
fi
# Add default opts file if defined
if [[ -e "${DEFAULT_OPTS}" ]]; then
ln -s "${DEFAULT_OPTS}" "$temp_dir/default_opts"
echo " ADD ./default_ops /root/.tamarin/default_opts" >> "$temp_dir/Dockerfile"
fi
2015-08-20 12:09:15 +02:00
# Build image
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\""
# 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
# 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-10-22 14:02:51 +02:00
# Target architecture
docker_opt="${docker_opt} -e TARGET_ARCH=${TARGET_ARCH}"
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-08-20 12:09:15 +02:00
if [[ -z ${BUILD_DIR} ]]
then
docker_opt="${docker_opt} -v=\"$PROJECT_PATH:/src:ro\" -v=\"$PROJECT_DEST:/dist:rw\" $container_tag"
2015-08-20 12:09:15 +02:00
else
docker_opt="${docker_opt} -v=\"$PROJECT_PATH:/src:ro\" -v=\"$PROJECT_DEST:/dist:rw\" -v=\"${BUILD_DIR}:/build\" $container_tag"
2015-08-20 12:09:15 +02:00
fi
info "Switching to container..."
debug "docker ${docker_opt}"
2015-08-20 12:09:15 +02:00
docker ${docker_opt}
res=${?}
success "Done"
2015-08-20 12:09:15 +02:00
return ${res}
}
#
# Parsing options
#
while getopts "kp:d:i:b:B:t:a:c:" 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}"
;;
b)
BUILD_DIR=$(readlink -f ${OPTARG})
;;
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}
;;
c)
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"
[[ -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
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