Tamarin/package

177 lines
4.4 KiB
Plaintext
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"
DEFAULT_64_IMAGE="debian:jessie"
DEFAULT_32_IMAGE="32bit/debian:jessie"
2015-08-20 12:09:15 +02:00
function show_usage {
echo
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-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 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}
# 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}
2015-08-20 12:09:15 +02:00
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-08-20 12:09:15 +02: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
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 running in terminal, set docker to interactive
if [[ -t 1 ]]; then
docker_opt="${docker_opt} -it"
fi
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
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..."
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: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}"
;;
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-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 ${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
if [[ "${TARGET_ARCH}" =~ ^i[0-9]86$ ]] && [ -z "${BASE_IMAGE}" ]; then
info "32bit architecture specified and no specific image given, switching to default 32bit image..."
BASE_IMAGE=${DEFAULT_32_IMAGE}
fi
[[ -z ${BASE_IMAGE} ]] && BASE_IMAGE=${DEFAULT_64_IMAGE}
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