Merge branch 'feature/debian_hooks' into develop

This commit is contained in:
William Petit 2015-07-04 18:42:56 +02:00
commit d190dcb143
6 changed files with 166 additions and 23 deletions

85
check-install.sh Executable file
View File

@ -0,0 +1,85 @@
#!/usr/bin/env bash
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
source "$DIR/lib/util.sh"
function show_usage {
echo
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
}
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 --yes gdebi-core lintian
ADD ./lib /root/.tamarin
RUN chmod +x /root/.tamarin/install.sh
VOLUME /deb
ENTRYPOINT ["/root/.tamarin/install.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
}
function main {
# Create container image
container_tag=$(create_container)
# Run container and install package
docker run -e "DISTRIB=$BASE_IMAGE" --rm -v="$DEB_DIR:/deb" "$container_tag" "/deb/$DEB_NAME"
# Check for return
if [ $? != 0 ]; then
fatal "Installation did not complete correctly !"
fi
info "Installation complete."
}
# Test for arguments
if [ -z "$1" ] || [ -z "$2" ]; then
show_usage
exit 1
fi
DEB_PATH=$(readlink -f "$1")
DEB_NAME=$(basename "$DEB_PATH")
DEB_DIR=$(dirname "$DEB_PATH")
BASE_IMAGE="$2"
main

View File

@ -43,18 +43,16 @@ function exec_hook {
# If the script did not execute properly, we stop here # If the script did not execute properly, we stop here
if [ $? != 0 ]; then if [ $? != 0 ]; then
error "The '${hook_name}' hook script did not finished properly !" fatal "The '${hook_name}' hook script did not finished properly !"
exit 1
fi fi
} }
function create_debian_metadata { function create_debian_control_file {
build_dir="${1}" debian_dir="${1}"
mkdir -p "${build_dir}/DEBIAN"
control_file="${build_dir}/DEBIAN/control" control_file="${debian_dir}/control"
touch "${control_file}" touch "${control_file}"
package_name=$(get_project_opt .name) package_name=$(get_project_opt .name)
@ -69,8 +67,8 @@ function create_debian_metadata {
package_priority=$(get_project_opt .priority) package_priority=$(get_project_opt .priority)
echo "Priority: ${package_priority:-optional}" >> "${control_file}" echo "Priority: ${package_priority:-optional}" >> "${control_file}"
package_arch=$(get_project_opt .priority) package_arch=$(get_project_opt .arch)
echo "Architecture: ${package_arch:-any}" >> "${control_file}" echo "Architecture: ${package_arch:-all}" >> "${control_file}"
dependencies=$( get_project_opt ".dependencies | .[\"${DISTRIB}\"] | @sh" | sed "s/' '/, /g" | sed "s/'//g" ) dependencies=$( get_project_opt ".dependencies | .[\"${DISTRIB}\"] | @sh" | sed "s/' '/, /g" | sed "s/'//g" )
@ -86,6 +84,49 @@ function create_debian_metadata {
} }
function create_debian_hooks {
debian_dir="${1}"
pre_install="$(get_project_opt .hooks.preInstall)"
if [ ! -z "${pre_install}" ]; then
cp "${SRC_DIR}/${pre_install}" "${debian_dir}/preinst"
chmod +x "${debian_dir}/preinst"
fi
post_install="$(get_project_opt .hooks.postInstall)"
if [ ! -z "${post_install}" ]; then
cp "${SRC_DIR}/${post_install}" "${debian_dir}/postinst"
chmod +x "${debian_dir}/postinst"
fi
pre_remove="$(get_project_opt .hooks.preRemove)"
if [ ! -z "${pre_remove}" ]; then
cp "${SRC_DIR}/${pre_remove}" "${debian_dir}/prerm"
chmod +x "${debian_dir}/prerm"
fi
post_remove="$(get_project_opt .hooks.postRemove)"
if [ ! -z "${post_remove}" ]; then
cp "${SRC_DIR}/${post_remove}" "${debian_dir}/postrm"
chmod +x "${debian_dir}/postrm"
fi
}
function create_debian_metadata {
build_dir="${1}"
debian_dir="${build_dir}/DEBIAN"
# Ensure debian dir exists
mkdir -p "${debian_dir}"
create_debian_control_file "${debian_dir}"
create_debian_hooks "${debian_dir}"
}
function build_project { function build_project {
project_name="$(get_project_opt '.name')" project_name="$(get_project_opt '.name')"
@ -107,24 +148,22 @@ function build_project {
exec_hook "preBuild" "${build_dir}" exec_hook "preBuild" "${build_dir}"
# Ensure $DIST_DIR exists
mkdir -p "${DIST_DIR}"
cd "${build_dir}/.." cd "${build_dir}/.."
dpkg-deb --build "${project_name}" dpkg-deb --build "${project_name}" ${DIST_DIR}
info "Package created ! (${build_dir}/${project_name}.deb)" info "Package created ! (${build_dir}/${project_name}.deb)"
if [ $? != 0 ]; then if [ $? != 0 ]; then
error "The build process has not completed successfuly !" fatal "The build process has not completed successfuly !"
exit 1
fi fi
cd / cd ${SRC_DIR}
exec_hook "postBuild" "${build_dir}" exec_hook "postBuild" "${build_dir}"
# Copie du paquet nouvellement créé dans le répertoire "dist"
mkdir -p "${DIST_DIR}"
cp "${build_dir}/../${project_name}.deb" "${DIST_DIR}/${project_name}.deb"
} }
function main { function main {

15
lib/install.sh Normal file
View File

@ -0,0 +1,15 @@
#!/usr/bin/env bash
set -e
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
source "${DIR}/util.sh"
info "Analysing package $1..."
lintian "$1"
info "Updating packages definition..."
apt-get update
info "Installing package $1..."
gdebi --n "$1"

View File

@ -2,7 +2,8 @@
set -e set -e
source ./lib/util.sh DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
source "$DIR/lib/util.sh"
function show_usage { function show_usage {
echo echo
@ -22,12 +23,12 @@ function create_container {
# Escape image name # Escape image name
escaped_basename=$(echo "$BASE_IMAGE" | sed 's/[^a-z0-9\-\_\.]/\_/gi') escaped_basename=$(echo "$BASE_IMAGE" | sed 's/[^a-z0-9\-\_\.]/\_/gi')
# Generate container tag # Generate container tag
container_tag="bricklayer:${escaped_basename}_$(date +%s)" container_tag="tamarin:${escaped_basename}_$(date +%s)"
# Create temporary dir for the Dockerfile # Create temporary dir for the Dockerfile
temp_dir="$(mktemp -d)" temp_dir="$(mktemp -d)"
# Link lib folder # Link lib folder
ln -s $(readlink -f ./lib) "$temp_dir/lib" ln -s $(readlink -f "$DIR/lib") "$temp_dir/lib"
# Create Dockerfile # Create Dockerfile
cat << EOF > "$temp_dir/Dockerfile" cat << EOF > "$temp_dir/Dockerfile"
@ -38,13 +39,13 @@ function create_container {
RUN apt-get update &&\ RUN apt-get update &&\
apt-get install jq apt-get install jq
ADD ./lib /bricklayer ADD ./lib /root/.tamarin
RUN chmod +x /bricklayer/build.sh RUN chmod +x /root/.tamarin/build.sh
VOLUME /src VOLUME /src
VOLUME /dist VOLUME /dist
CMD /bricklayer/build.sh CMD /root/.tamarin/build.sh
EOF EOF
# Build image # Build image

View File

@ -0,0 +1,3 @@
#!/bin/sh
echo "Test pre-install"

View File

@ -9,7 +9,7 @@
"hooks": { "hooks": {
"preBuild": "./scripts/pre-build.sh", "preBuild": "./scripts/pre-build.sh",
"postBuild": "./scripts/post-build.sh", "postBuild": "./scripts/post-build.sh",
"preInstall": "", "preInstall": "./scripts/pre-install.sh",
"preRemove": "", "preRemove": "",
"postInstall": "", "postInstall": "",
"postRemove": "" "postRemove": ""