diff --git a/check-install.sh b/check-install.sh new file mode 100755 index 0000000..4a84594 --- /dev/null +++ b/check-install.sh @@ -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 " + 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 +} + +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 diff --git a/lib/build.sh b/lib/build.sh index c8a327a..8c08aba 100755 --- a/lib/build.sh +++ b/lib/build.sh @@ -43,18 +43,16 @@ function exec_hook { # If the script did not execute properly, we stop here if [ $? != 0 ]; then - error "The '${hook_name}' hook script did not finished properly !" - exit 1 + fatal "The '${hook_name}' hook script did not finished properly !" fi } -function create_debian_metadata { +function create_debian_control_file { - build_dir="${1}" - mkdir -p "${build_dir}/DEBIAN" + debian_dir="${1}" - control_file="${build_dir}/DEBIAN/control" + control_file="${debian_dir}/control" touch "${control_file}" package_name=$(get_project_opt .name) @@ -69,8 +67,8 @@ function create_debian_metadata { package_priority=$(get_project_opt .priority) echo "Priority: ${package_priority:-optional}" >> "${control_file}" - package_arch=$(get_project_opt .priority) - echo "Architecture: ${package_arch:-any}" >> "${control_file}" + package_arch=$(get_project_opt .arch) + echo "Architecture: ${package_arch:-all}" >> "${control_file}" 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 { project_name="$(get_project_opt '.name')" @@ -107,24 +148,22 @@ function build_project { exec_hook "preBuild" "${build_dir}" + # Ensure $DIST_DIR exists + mkdir -p "${DIST_DIR}" + cd "${build_dir}/.." - dpkg-deb --build "${project_name}" + dpkg-deb --build "${project_name}" ${DIST_DIR} info "Package created ! (${build_dir}/${project_name}.deb)" if [ $? != 0 ]; then - error "The build process has not completed successfuly !" - exit 1 + fatal "The build process has not completed successfuly !" fi - cd / + cd ${SRC_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 { diff --git a/lib/install.sh b/lib/install.sh new file mode 100644 index 0000000..e2724d2 --- /dev/null +++ b/lib/install.sh @@ -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" diff --git a/package-project.sh b/package-project.sh index 042e865..d68f264 100755 --- a/package-project.sh +++ b/package-project.sh @@ -2,7 +2,8 @@ set -e -source ./lib/util.sh +DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) +source "$DIR/lib/util.sh" function show_usage { echo @@ -22,12 +23,12 @@ function create_container { # Escape image name escaped_basename=$(echo "$BASE_IMAGE" | sed 's/[^a-z0-9\-\_\.]/\_/gi') # Generate container tag - container_tag="bricklayer:${escaped_basename}_$(date +%s)" + container_tag="tamarin:${escaped_basename}_$(date +%s)" # Create temporary dir for the Dockerfile temp_dir="$(mktemp -d)" # Link lib folder - ln -s $(readlink -f ./lib) "$temp_dir/lib" + ln -s $(readlink -f "$DIR/lib") "$temp_dir/lib" # Create Dockerfile cat << EOF > "$temp_dir/Dockerfile" @@ -38,13 +39,13 @@ function create_container { RUN apt-get update &&\ apt-get install jq - ADD ./lib /bricklayer - RUN chmod +x /bricklayer/build.sh + ADD ./lib /root/.tamarin + RUN chmod +x /root/.tamarin/build.sh VOLUME /src VOLUME /dist - CMD /bricklayer/build.sh + CMD /root/.tamarin/build.sh EOF # Build image diff --git a/src-example/scripts/pre-install.sh b/src-example/scripts/pre-install.sh new file mode 100644 index 0000000..b7007f7 --- /dev/null +++ b/src-example/scripts/pre-install.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +echo "Test pre-install" diff --git a/src-example/tamarin.json b/src-example/tamarin.json index 74245fa..565ecf9 100644 --- a/src-example/tamarin.json +++ b/src-example/tamarin.json @@ -9,7 +9,7 @@ "hooks": { "preBuild": "./scripts/pre-build.sh", "postBuild": "./scripts/post-build.sh", - "preInstall": "", + "preInstall": "./scripts/pre-install.sh", "preRemove": "", "postInstall": "", "postRemove": ""