Tamarin/package.sh

97 lines
2.2 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
set -e
2015-08-04 23:58:43 +02:00
TAMARIN_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
2015-08-05 15:49:04 +02:00
BASE_DIR="$TAMARIN_DIR" source "$TAMARIN_DIR/lib/util.sh"
function show_usage {
echo
2015-08-04 17:45:10 +02:00
echo "Usage: $0 src dist [image]"
echo
echo "Paramètres: "
echo
2015-08-04 17:45:10 +02:00
echo " - src Chemin vers le répertoire des sources du projet à empaqueter"
echo " - dist Chemin vers le répertoire de destination du paquet à créer"
2015-08-04 23:58:43 +02:00
echo " - image Optionel - Nom de l'image Docker à utiliser comme base pourl'environnement de build. Défaut: debian:jessie"
echo
}
# Create a build container based on the $BASE_IMAGE argument
function create_container {
# Escape image name
2015-08-05 15:49:04 +02:00
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
2015-08-05 15:49:04 +02:00
local temp_dir="$(mktemp -d)"
2015-08-04 23:58:43 +02:00
# 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 &&\
2015-08-04 23:58:43 +02:00
apt-get install --yes build-essential devscripts
2015-08-04 23:58:43 +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-08-04 23:58:43 +02:00
CMD /root/.tamarin/lib/build.sh
2015-08-05 15:49:04 +02:00
EOF
2015-08-05 15:49:04 +02:00
exec_hooks "containerbuild" "$temp_dir"
# Build image
2015-08-05 15:49:04 +02:00
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..."
2015-08-05 15:49:04 +02:00
# Create container & "$container_tag" variable
create_container
local project_name="$(basename "${PROJECT_PATH}")"
info "Switching to container..."
2015-08-04 17:45:10 +02:00
docker run -e "DISTRIB=$BASE_IMAGE" -e "PROJECT_NAME=$project_name" --rm -v="$PROJECT_PATH:/src" -v="$PROJECT_DIST:/dist" "$container_tag"
info "Done"
2015-08-05 15:49:04 +02:00
}
# Test for arguments
2015-08-04 17:45:10 +02:00
if [ -z "$1" ] || [ -z "$2" ]; then
show_usage
exit 1
fi
PROJECT_PATH="$(readlink -f $1)"
PROJECT_DIST="$(readlink -f $2)"
2015-08-04 17:45:10 +02:00
BASE_IMAGE="${3:-debian:jessie}"
main