diff --git a/check-install.sh b/check-install.sh index 4902db5..6364a11 100755 --- a/check-install.sh +++ b/check-install.sh @@ -5,23 +5,23 @@ source "$DIR/lib/util.sh" function show_usage { echo - echo "Usage: $0 " + echo "Usage: $0 deb_file [image]" echo echo "Paramètres: " echo - echo " - Chemin vers le paquet Debian dont on doit vérifier l'installation" - echo " - Nom de l'image Docker à utiliser comme environnement pour tester l'installation" + echo " - deb_file Chemin vers le paquet Debian dont on doit vérifier l'installation" + echo " - image Optionnel - Nom de l'image Docker à utiliser comme environnement pour tester l'installation. Défaut: debian:jessie" echo } function create_container { # Escape image name - escaped_basename=$(echo "$BASE_IMAGE" | sed 's/[^a-z0-9\-\_\.]/\_/gi') + 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 - temp_dir="$(mktemp -d)" + local temp_dir="$(mktemp -d)" # Link lib folder ln -s $(readlink -f "$DIR/lib") "$temp_dir/lib" @@ -44,20 +44,17 @@ function create_container { EOF # Build image - tar -C "$temp_dir" -czh . | docker build -t "$container_tag" - 1>&2 + tar -C "$temp_dir" -czh . | docker build -t "$container_tag" - 2> >(error) 1> >(info) # Delete temporary folder rm -rf "$temp_dir" - # Return newly created container's tag - echo $container_tag - } function main { # Create container image - container_tag=$(create_container) + create_container # Run container and install package docker run -e "DISTRIB=$BASE_IMAGE" --rm -v="$DEB_DIR:/deb" "$container_tag" "/deb/$DEB_NAME" @@ -72,7 +69,7 @@ function main { } # Test for arguments -if [ -z "$1" ] || [ -z "$2" ]; then +if [ -z "$1" ]; then show_usage exit 1 fi @@ -80,6 +77,6 @@ fi DEB_PATH=$(readlink -f "$1") DEB_NAME=$(basename "$DEB_PATH") DEB_DIR=$(dirname "$DEB_PATH") -BASE_IMAGE="$2" +BASE_IMAGE="${2:-debian:jessie}" main diff --git a/get-updated-manifest-from-git.sh b/get-updated-manifest-from-git.sh deleted file mode 100755 index c45a5cf..0000000 --- a/get-updated-manifest-from-git.sh +++ /dev/null @@ -1,77 +0,0 @@ -#!/usr/bin/env bash - -function show_usage { - echo - echo "Usage: $0 " - echo - echo "Paramètres: " - echo - echo " - Chemin vers le répertoire des sources du projet. Le projet doit être un dépôt Git valide." - echo -} - -function main { - - cd "$SRC_DIR" - - if [ ! -d '.git' ]; then - fatal "The directory $SRC_DIR seems not to be a valid Git repository." - fi - - MANIFEST=$(cat tamarin.json 2>/dev/null) - - if [ -z "$MANIFEST" ]; then - MANIFEST="{}" - fi - - # Extract project info from Git - repo_name=$(basename `git rev-parse --show-toplevel`) - current_commit=$(git log -n 1 --pretty=format:"%h") - # Get commits log as changelog - logs=$(git log --pretty=format:"%an - %h : %s" | sed 's/"/\\"/g') - # Set the top commiter as the maintainer of the project - git_maintainer=$(git log --pretty=short | git shortlog -s -n -e | sed 's/^\s*[0-9]*\s*//g' | head -n 1) - - # Get current version from manifest - current_name=$(echo "$MANIFEST" | jq -r ".name" | sed 's/null//') - current_version=$(echo "$MANIFEST" | jq -r ".version" | sed 's/null//') - current_changelog=$(echo "$MANIFEST" | jq -r ".changelog" | sed 's/null//') - current_maintainer=$(echo "$MANIFEST" | jq -r ".maintainer" | sed 's/null//') - - # Complete manifest - - # Add commit number to version - MANIFEST=$(echo "$MANIFEST" | jq -r ".version = \"${current_version:-0.0.0}~$current_commit\"") - # Set name if not defined - MANIFEST=$(echo "$MANIFEST" | jq -r ".name = \"${current_name:-$repo_name}\"") - # Set maintainer if not defined - MANIFEST=$(echo "$MANIFEST" | jq -r ".maintainer = \"${current_maintainer:-$git_maintainer}\"") - - # Set changelog from git log if not defined - if [ -z "$current_changelog" ]; then - - MANIFEST=$(echo "$MANIFEST" | jq -r ".changelog = []") - - while read -r entry; do - MANIFEST=$(echo "$MANIFEST" | jq -r ".changelog += [\"$entry\"]") - done <<< "$logs" - - fi - - echo "$MANIFEST" - -} - -# Load util lib -DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) -source "$DIR/lib/util.sh" - -# Test for arguments -if [ -z "$1" ]; then - show_usage - exit 1 -fi - -SRC_DIR=$(readlink -f "$1") - -main diff --git a/hooks/debian/install-build-depends-prebuild b/hooks/debian/install-build-depends-prebuild new file mode 100755 index 0000000..f98c2a1 --- /dev/null +++ b/hooks/debian/install-build-depends-prebuild @@ -0,0 +1,8 @@ +#!/bin/bash + +source "${TAMARIN_UTIL}" + +if [ -f debian/control ]; then + info "Installing build dependencies..." + mk-build-deps --install debian/control +fi diff --git a/hooks/git/create-changelog-prebuild b/hooks/git/create-changelog-prebuild new file mode 100755 index 0000000..dbd0bcc --- /dev/null +++ b/hooks/git/create-changelog-prebuild @@ -0,0 +1,38 @@ +#!/usr/bin/env bash + +source "${TAMARIN_UTIL}" + +if [ -f debian/changelog ] || [ ! -d .git ]; then + info "Not a Git repository or Debian changelog already exists !" + exit +fi + +# Get commits log as changelog + +logs=$(git log --pretty=format:"%an - %h : %s" | sed 's/"/\\"/g') + +# Set the top commiter as the maintainer of the project if not defined +top_contributor=$(git log --pretty=short | git shortlog -s -n -e | sed 's/^\s*[0-9]*\s*//g' | head -n 1) +maintainer=$(get_opt maintainer "${top_contributor}") + +project_name=$(get_opt project_name) +version=$(get_opt version 0.0.0) +distribution=$(get_opt distribution UNRELEASED) +urgency=$(get_opt urgency low) + +commit_count=$(git rev-list --count --first-parent HEAD) +current_commit=$(git log -n 1 --pretty=format:"%h") +version_suffix=${commit_count}~${current_commit} + +echo "${project_name} (${version}.tamarin${version_suffix}) ${distribution}; urgency=${urgency}" > debian/changelog + +echo >> debian/changelog + +while read -r entry; do + echo " * ${entry}" >> debian/changelog +done <<< "$(echo -e "${logs}" | sed 's/^"//')" + +echo >> debian/changelog + +echo " -- ${maintainer} $(date -R)" >> debian/changelog +echo >> debian/changelog diff --git a/hooks/git/install-git-containerbuild b/hooks/git/install-git-containerbuild new file mode 100755 index 0000000..585549f --- /dev/null +++ b/hooks/git/install-git-containerbuild @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +echo "RUN apt-get install --yes --no-install-recommends git-core" >> Dockerfile diff --git a/hooks/tamarin/export-dist-postbuild b/hooks/tamarin/export-dist-postbuild new file mode 100755 index 0000000..cd06a60 --- /dev/null +++ b/hooks/tamarin/export-dist-postbuild @@ -0,0 +1,10 @@ +#!/bin/bash + +# Create new directory +mkdir -p /dist + +# Move generated files +mv ../*.deb /dist +mv ../*.changes /dist +mv ../*.dsc /dist +mv ../*.tar.xz /dist diff --git a/lib/build.sh b/lib/build.sh index f48e264..d07ce40 100755 --- a/lib/build.sh +++ b/lib/build.sh @@ -21,6 +21,7 @@ function build_project() local workspace=$(mktemp -p ${BUILD_DIR} -d)/${PROJECT_NAME} info "Build dir is ${workspace}" mkdir -p "${workspace}" + # Copy sources to workspace cd ${SRC_DIR} cp -r ${SRC_DIR}/. "${workspace}" diff --git a/lib/install.sh b/lib/install.sh index 775793c..68ea518 100644 --- a/lib/install.sh +++ b/lib/install.sh @@ -6,7 +6,7 @@ DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) source "${DIR}/util.sh" info "Updating packages definition..." -apt-get update +apt-get update 2> >(error) 1> >(info) info "Installing package $1..." -gdebi --n "$1" +gdebi --n "$1" 2> >(error) 1> >(info) diff --git a/lib/util.sh b/lib/util.sh index 8310de4..cb404e2 100644 --- a/lib/util.sh +++ b/lib/util.sh @@ -1,18 +1,90 @@ #!/usr/bin/env bash +HOOKS_DIR="${BASE_DIR}/hooks" +OPT_FILE="${BASE_DIR}/tmp/.tamarin_opts" + function info { - echo "[${HOSTNAME}] [INFO] $@" + if [ -z "$@" ]; then + while read str; do + log INFO "${str}" + done + else + log INFO "$@" + fi } function debug { - echo "[${HOSTNAME}] [DEBUG] $@" + if [ -z "$@" ]; then + while read str; do + log DEBUG "${str}" + done + else + log DEBUG "$@" + fi } function error { - echo "[${HOSTNAME}] [ERROR] $@" >&2 + if [ -z "$@" ]; then + while read str; do + log ERROR "${str}" >&2 + done + else + log ERROR "$@" >&2 + fi } function fatal { - echo "[${HOSTNAME}] [FATAL] $@" >&2 + if [ -z "$@" ]; then + while read str; do + log FATAL "${str}" >&2 + done + else + log FATAL "$@" >&2 + fi exit 1 } + +function log { + local args=( $@ ) + echo "[${HOSTNAME}] [${args[0]}] ${args[@]:1}" +} + +function get_opt { + local opt_name=${1} + local default_value=${2} + touch "${OPT_FILE}" + source "${OPT_FILE}" + echo ${!opt_name:-${default_value}} +} + +function set_opt { + local opt_name=${1} + local opt_value=${2} + touch "${OPT_FILE}" + sed -i "s/^${opt_name}*$//" "${OPT_FILE}" + echo "${opt_name}=\"${opt_value}\"" >> "${OPT_FILE}" +} + +function exec_hooks { + + local hook=${1} + local workspace=${2} + + local hook_scripts=$( find "${HOOKS_DIR}" -type f -name "*${hook}" -executable) + + for hook_script in ${hook_scripts}; do + + info "[${hook}] Executing ${hook_script}" + + ( cd "${workspace}" && "${hook_script}" ) 2> >(error) 1> >(info) + + # If the script did not execute properly, we stop here + if [ $? != 0 ]; then + fatal "The '${hook_script}' hook script did not finished properly !" + fi + + info "[${hook}] ${hook_script} Done." + + done + +} diff --git a/package-project.sh b/package-project.sh deleted file mode 100755 index d68f264..0000000 --- a/package-project.sh +++ /dev/null @@ -1,86 +0,0 @@ -#!/usr/bin/env bash - -set -e - -DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) -source "$DIR/lib/util.sh" - -function show_usage { - echo - echo "Usage: $0 " - echo - echo "Paramètres: " - echo - echo " - Chemin vers le répertoire des sources du projet à empaqueter" - echo " - Chemin vers le répertoire de destination du paquet à créer" - echo " - 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 - container_tag="tamarin:${escaped_basename}_$(date +%s)" - # Create temporary dir for the Dockerfile - temp_dir="$(mktemp -d)" - - # Link lib folder - ln -s $(readlink -f "$DIR/lib") "$temp_dir/lib" - - # Create Dockerfile - cat << EOF > "$temp_dir/Dockerfile" - FROM $BASE_IMAGE - - ENV DEBIAN_FRONTEND noninteractive - - RUN apt-get update &&\ - apt-get install jq - - ADD ./lib /root/.tamarin - RUN chmod +x /root/.tamarin/build.sh - - VOLUME /src - VOLUME /dist - - CMD /root/.tamarin/build.sh -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 diff --git a/package.sh b/package.sh index 702e59f..e673167 100755 --- a/package.sh +++ b/package.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash + set -e TAMARIN_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) @@ -23,6 +24,7 @@ function show_usage { # 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 @@ -65,7 +67,6 @@ EOF echo " CMD /root/.tamarin/lib/build.sh ${projectName} /build" >> "$temp_dir/Dockerfile" fi - exec_hooks "containerbuild" "$temp_dir" # Build image @@ -78,11 +79,11 @@ EOF # Main function function main { - local res=0 - local project_name="$(basename "${PROJECT_PATH}")" info "Building container from $BASE_IMAGE..." + local project_name=$(basename ${PROJECT_PATH}) + # Create container & "$container_tag" variable create_container ${project_name} diff --git a/src-example/tamarin.json b/src-example/tamarin.json index 565ecf9..be7a6b0 100644 --- a/src-example/tamarin.json +++ b/src-example/tamarin.json @@ -1,14 +1,13 @@ { "name": "hello-world", "dependencies": { - "debian:jessie": [ - "apache2 (>= 2.4)", - "php5 (>= 5.4)" - ] + "debian:jessie": { + "apache2": ">= 2.4", + "php5": ">= 5.4" + } }, "hooks": { "preBuild": "./scripts/pre-build.sh", - "postBuild": "./scripts/post-build.sh", "preInstall": "./scripts/pre-install.sh", "preRemove": "", "postInstall": "",