2015-07-02 14:04:14 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
2015-07-04 18:42:08 +02:00
|
|
|
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
|
|
|
|
source "$DIR/lib/util.sh"
|
2015-07-02 14:04:14 +02:00
|
|
|
|
|
|
|
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> Nom de l'image Docker à utiliser comme base pourl'environnement de build. Exemple: debian:jessie"
|
|
|
|
echo
|
|
|
|
}
|
|
|
|
|
|
|
|
# Create a build container based on the $BASE_IMAGE argument
|
|
|
|
function create_container {
|
|
|
|
|
|
|
|
# Escape image name
|
|
|
|
escaped_basename=$(echo "$BASE_IMAGE" | sed 's/[^a-z0-9\-\_\.]/\_/gi')
|
|
|
|
# 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
|
|
|
|
temp_dir="$(mktemp -d)"
|
|
|
|
|
|
|
|
# Link lib folder
|
2015-07-04 18:42:08 +02:00
|
|
|
ln -s $(readlink -f "$DIR/lib") "$temp_dir/lib"
|
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 &&\
|
|
|
|
apt-get install jq
|
|
|
|
|
2015-07-02 17:01:26 +02:00
|
|
|
ADD ./lib /root/.tamarin
|
|
|
|
RUN chmod +x /root/.tamarin/build.sh
|
2015-07-02 14:04:14 +02:00
|
|
|
|
|
|
|
VOLUME /src
|
|
|
|
VOLUME /dist
|
|
|
|
|
2015-07-02 17:01:26 +02:00
|
|
|
CMD /root/.tamarin/build.sh
|
2015-07-02 14:04:14 +02:00
|
|
|
EOF
|
|
|
|
|
|
|
|
# Build image
|
|
|
|
tar -C "$temp_dir" -czh . | docker build -t "$container_tag" - 1>&2
|
|
|
|
|
|
|
|
# Delete temporary folder
|
|
|
|
rm -rf "$temp_dir"
|
|
|
|
|
|
|
|
# Return newly created container's tag
|
|
|
|
echo $container_tag
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
# Main function
|
|
|
|
function main {
|
|
|
|
|
|
|
|
info "Building container from $BASE_IMAGE..."
|
|
|
|
container_tag="$(create_container)"
|
|
|
|
|
|
|
|
info "Launching container..."
|
|
|
|
docker run -e "DISTRIB=$BASE_IMAGE" --rm -v="$PROJECT_PATH:/src" -v="$PROJECT_DIST:/dist" "$container_tag"
|
|
|
|
|
|
|
|
info "Cleaning container data..."
|
|
|
|
|
|
|
|
info "Done"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Test for arguments
|
|
|
|
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
|
|
|
|
show_usage
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
PROJECT_PATH="$(readlink -f $1)"
|
|
|
|
PROJECT_DIST="$(readlink -f $2)"
|
|
|
|
BASE_IMAGE="$3"
|
|
|
|
|
|
|
|
main
|