97 lines
2.2 KiB
Bash
Executable File
97 lines
2.2 KiB
Bash
Executable File
#!/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 src dist [image]"
|
|
echo
|
|
echo "Paramètres: "
|
|
echo
|
|
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"
|
|
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
|
|
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)"
|
|
|
|
# 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
|
|
|
|
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
|
|
|
|
CMD /root/.tamarin/lib/build.sh
|
|
|
|
EOF
|
|
|
|
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..."
|
|
|
|
# Create container & "$container_tag" variable
|
|
create_container
|
|
|
|
local project_name="$(basename "${PROJECT_PATH}")"
|
|
|
|
info "Switching to container..."
|
|
docker run -e "DISTRIB=$BASE_IMAGE" -e "PROJECT_NAME=$project_name" --rm -v="$PROJECT_PATH:/src" -v="$PROJECT_DIST:/dist" "$container_tag"
|
|
|
|
info "Done"
|
|
|
|
}
|
|
|
|
# Test for arguments
|
|
if [ -z "$1" ] || [ -z "$2" ]; then
|
|
show_usage
|
|
exit 1
|
|
fi
|
|
|
|
PROJECT_PATH="$(readlink -f $1)"
|
|
PROJECT_DIST="$(readlink -f $2)"
|
|
BASE_IMAGE="${3:-debian:jessie}"
|
|
|
|
main
|