Empaquetage à partir des hooks ok

This commit is contained in:
wpetit 2015-08-05 15:49:04 +02:00
parent 5b0882ce7a
commit 0861421561
8 changed files with 104 additions and 151 deletions

View File

@ -5,23 +5,23 @@ source "$DIR/lib/util.sh"
function show_usage {
echo
echo "Usage: $0 <deb_file> <image>"
echo "Usage: $0 deb_file [image]"
echo
echo "Paramètres: "
echo
echo " - <deb_file> Chemin vers le paquet Debian dont on doit vérifier l'installation"
echo " - <image> 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

View File

@ -1,78 +0,0 @@
#!/usr/bin/env bash
function show_usage {
echo
echo "Usage: $0 <src>"
echo
echo "Paramètres: "
echo
echo " - <src> 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//')
timestamp=$(date +%Y%m%d%H%M)
# Complete manifest
# Add commit number to version
MANIFEST=$(echo "$MANIFEST" | jq -r ".version = \"${current_version:-0.0.0}+$timestamp~$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

View File

@ -7,11 +7,6 @@ if [ -f debian/changelog ] || [ ! -d .git ]; then
exit
fi
if [ -z $(which git) ]; then
info "Installing Git..."
apt-get install --yes --no-install-recommends git-core
fi
# Get commits log as changelog
logs=$(git log --pretty=format:"%an - %h : %s" | sed 's/"/\\"/g')
@ -27,9 +22,9 @@ 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}
version_suffix=${commit_count}~${current_commit}
echo "${project_name} (${version}) ${distribution}; urgency=${urgency}" > debian/changelog
echo "${project_name} (${version}.tamarin${version_suffix}) ${distribution}; urgency=${urgency}" > debian/changelog
echo >> debian/changelog

View File

@ -0,0 +1,3 @@
#!/usr/bin/env bash
echo "RUN apt-get install --yes --no-install-recommends git-core" >> Dockerfile

View File

@ -7,31 +7,6 @@ source "${TAMARIN_UTIL}"
DIST_DIR="${BASE_DIR}/dist"
SRC_DIR="${BASE_DIR}/src"
HOOKS_DIR="${BASE_DIR}/hooks"
function exec_hooks {
hook=${1}
workspace=${2}
hook_scripts=$( find "${HOOKS_DIR}" -type f -name "*${hook}" -executable)
for hook_script in ${hook_scripts}; do
info "[hook-${hook}] Executing ${hook_script}"
( cd "${workspace}" && "${hook_script}" )
# 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}] ${hook_script} done."
done
}
function build_project {
@ -39,7 +14,8 @@ function build_project {
set_opt project_name "${PROJECT_NAME}"
workspace=$(mktemp -d)
local workspace=$(mktemp -d)/${PROJECT_NAME}
mkdir -p "${workspace}"
# Copy sources to workspace
cd ${SRC_DIR}
@ -48,7 +24,7 @@ function build_project {
exec_hooks "prebuild" "${workspace}"
cd "${workspace}"
dpkg-buildpackage -us -uc
dpkg-buildpackage -us -uc 2> >(error) 1> >(info)
if [ $? != 0 ]; then
fatal "The build process has not completed successfuly !"

View File

@ -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)

View File

@ -1,36 +1,90 @@
#!/usr/bin/env bash
OPT_FILE="/tmp/.tamarin_opts"
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 {
opt_name=${1}
default_value=${2}
touch "${OPT_FILE}"
source "${OPT_FILE}"
echo ${!opt_name:-${default_value}}
local opt_name=${1}
local default_value=${2}
touch "${OPT_FILE}"
source "${OPT_FILE}"
echo ${!opt_name:-${default_value}}
}
function set_opt {
opt_name=${1}
opt_value=${2}
touch "${OPT_FILE}"
sed -i "s/^${opt_name}*$//" "${OPT_FILE}"
echo "${opt_name}=\"${opt_value}\"" >> "${OPT_FILE}"
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
}

View File

@ -3,7 +3,9 @@
set -e
TAMARIN_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
source "$TAMARIN_DIR/lib/util.sh"
BASE_DIR="$TAMARIN_DIR" source "$TAMARIN_DIR/lib/util.sh"
function show_usage {
echo
@ -21,11 +23,11 @@ function show_usage {
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 & hooks folders
ln -s $(readlink -f "$TAMARIN_DIR/lib") "$temp_dir/lib"
@ -51,30 +53,34 @@ function create_container {
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" - 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
}
# Main function
function main {
info "Building container from $BASE_IMAGE..."
container_tag="$(create_container)"
project_name="$(basename "${PROJECT_PATH}")"
info "Launching container..."
# 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