2015-07-02 14:04:14 +02:00
|
|
|
#!/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"
|
2015-07-02 14:04:14 +02:00
|
|
|
|
|
|
|
function show_usage {
|
|
|
|
echo
|
2015-08-04 17:45:10 +02:00
|
|
|
echo "Usage: $0 src dist [image]"
|
2015-07-02 14:04:14 +02:00
|
|
|
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"
|
2015-07-02 14:04:14 +02:00
|
|
|
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')
|
2015-07-02 14:04:14 +02:00
|
|
|
# Generate container tag
|
2015-07-02 17:01:26 +02:00
|
|
|
container_tag="tamarin:${escaped_basename}_$(date +%s)"
|
2015-07-02 14:04:14 +02:00
|
|
|
# Create temporary dir for the Dockerfile
|
2015-08-05 15:49:04 +02:00
|
|
|
local temp_dir="$(mktemp -d)"
|
2015-07-02 14:04:14 +02:00
|
|
|
|
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"
|
2015-07-02 14:04:14 +02:00
|
|
|
|
|
|
|
# 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-07-02 14:04:14 +02:00
|
|
|
|
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
|
2015-07-02 14:04:14 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2015-07-02 14:04:14 +02:00
|
|
|
EOF
|
|
|
|
|
2015-08-05 15:49:04 +02:00
|
|
|
exec_hooks "containerbuild" "$temp_dir"
|
|
|
|
|
2015-07-02 14:04:14 +02:00
|
|
|
# Build image
|
2015-08-05 15:49:04 +02:00
|
|
|
tar -C "$temp_dir" -czh . | docker build -t "$container_tag" - 2> >(error) 1> >(info)
|
2015-07-02 14:04:14 +02:00
|
|
|
|
|
|
|
# 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"
|
2015-07-02 14:04:14 +02:00
|
|
|
|
|
|
|
info "Done"
|
2015-08-05 15:49:04 +02:00
|
|
|
|
2015-07-02 14:04:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Test for arguments
|
2015-08-04 17:45:10 +02:00
|
|
|
if [ -z "$1" ] || [ -z "$2" ]; then
|
2015-07-02 14:04:14 +02:00
|
|
|
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}"
|
2015-07-02 14:04:14 +02:00
|
|
|
|
|
|
|
main
|