Merge branch 'feature/default_opts' into develop

This commit is contained in:
wpetit 2015-11-18 10:45:38 +01:00
commit 6054755cdc
8 changed files with 117 additions and 63 deletions

View File

@ -18,7 +18,6 @@ then
git checkout ${BUILD_BRANCH} git checkout ${BUILD_BRANCH}
tags=$(git tag master -l "release/*"|sort -r) tags=$(git tag master -l "release/*"|sort -r)
else else
info "DEBUG DEBUG DEBUG "
tagbranch="build-tag-${BUILD_TAG}" tagbranch="build-tag-${BUILD_TAG}"
git checkout -b ${tagbranch} git checkout -b ${tagbranch}
set_opt "tag_branch" "${tag_branch}" set_opt "tag_branch" "${tag_branch}"
@ -48,16 +47,7 @@ do
distribution=$(get_opt distribution UNRELEASED) distribution=$(get_opt distribution UNRELEASED)
urgency=$(get_opt urgency low) urgency=$(get_opt urgency low)
commit_count=$(git rev-list --count --first-parent HEAD) package_version=${version}
current_commit=$(git log -n 1 --pretty=format:"%h")
version_suffix=tamarin${commit_count}~${current_commit}
package_version=${version}-${version_suffix}
# Define package_version opt if not defined
if [ -z "$(get_opt package_version)" ]; then
# Share computed package version
set_opt package_version "${package_version}"
fi
# Define project_version opt if not defined # Define project_version opt if not defined
if [ -z "$(get_opt project_version)" ]; then if [ -z "$(get_opt project_version)" ]; then
@ -65,7 +55,7 @@ do
set_opt project_version "${version}" set_opt project_version "${version}"
fi fi
echo "${project_name} (${package_version}) ${distribution}; urgency=${urgency}" >> debian/changelog echo "${project_name} (${version}) ${distribution}; urgency=${urgency}" >> debian/changelog
echo >> debian/changelog echo >> debian/changelog

View File

@ -0,0 +1,19 @@
#!/usr/bin/env bash
source "${TAMARIN_UTIL}"
if [ ! -f debian/changelog ]; then
info "No changelog. Skipping..."
exit
fi
if [ -d .git ]; then
info "It seems to be a Git repository. Generating version suffix based on Git history..."
commit_count=$(git rev-list --count HEAD)
current_commit=$(git log -n 1 --pretty=format:"%h")
version_suffix=tamarin${commit_count}~${current_commit}
else
info "Not a Git project. Fallback to timestamp for suffix generation..."
version_suffix=tamarin$(date +%Y%m%d%H%M)
fi
sed -i "0,/(\(.*\))/s/(\(.*\))/(\1-${version_suffix})/" debian/changelog

View File

@ -1,8 +1,6 @@
#!/bin/bash #!/bin/bash
source "${TAMARIN_UTIL}"
if [ -f debian/control ]; then if [ -f debian/control ]; then
info "Installing build dependencies..." echo "Installing build dependencies..."
mk-build-deps -r -t "apt-get --force-yes -y --no-install-recommends" --install debian/control mk-build-deps -r -t "apt-get --force-yes -y --no-install-recommends" --install debian/control
fi fi

View File

@ -9,7 +9,6 @@ DIST_DIR="${BASE_DIR}/dist"
SRC_DIR="${BASE_DIR}/src" SRC_DIR="${BASE_DIR}/src"
PROJECT_NAME=${1} PROJECT_NAME=${1}
BUILD_BRANCH=${2} BUILD_BRANCH=${2}
BUILD_DIR=${3}
BUILD_TAG=${4} BUILD_TAG=${4}
function build_project() function build_project()
@ -17,12 +16,13 @@ function build_project()
info "Building project '${PROJECT_NAME}' for ${TARGET_ARCH} architecture..." info "Building project '${PROJECT_NAME}' for ${TARGET_ARCH} architecture..."
load_default_opts
set_opt project_name "${PROJECT_NAME}" set_opt project_name "${PROJECT_NAME}"
set_opt build_dir "${BUILD_DIR}"
set_opt build_branch "${BUILD_BRANCH}" set_opt build_branch "${BUILD_BRANCH}"
set_opt build_tag "${BUILD_TAG}" set_opt build_tag "${BUILD_TAG}"
local workspace=$(mktemp -p ${BUILD_DIR} -d)/${PROJECT_NAME} local workspace=$(mktemp -d)/${PROJECT_NAME}
info "Build dir is ${workspace}" info "Build dir is ${workspace}"
mkdir -p "${workspace}" mkdir -p "${workspace}"
@ -34,7 +34,7 @@ function build_project()
cd "${workspace}" cd "${workspace}"
dpkg-buildpackage -b -a "${TARGET_ARCH}" 2> >(error) 1> >(info) dpkg-buildpackage -b -a "${TARGET_ARCH}" 2> >(stderr) 1> >(stdout)
if [ $? != 0 ]; then if [ $? != 0 ]; then
fatal "The build process has not completed successfuly !" 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" source "${DIR}/util.sh"
info "Updating packages definition..." info "Updating packages definition..."
apt-get update 2> >(error) 1> >(info) apt-get update 2> >(stderr) 1> >(stdout)
info "Installing package $1..." info "Installing package $1..."
gdebi --n "$1" 2> >(error) 1> >(info) gdebi --n "$1" 2> >(stderr) 1> >(stdout)

View File

@ -1,9 +1,40 @@
#!/usr/bin/env bash #!/usr/bin/env bash
HOOKS_DIR="${BASE_DIR}/hooks" HOOKS_DIR="${BASE_DIR}/hooks"
OPT_FILE="${BASE_DIR}/tmp/.tamarin_opts" DEFAULT_OPTS_FILE="${BASE_DIR}/tmp/default_opts"
OPT_FILE="${BASE_DIR}/tmp/tamarin/opts"
OPT_PREFIX="tamarin_opt_" OPT_PREFIX="tamarin_opt_"
# Colors
COLOR_INFO='\e[0;36m'
COLOR_FATAL='\e[0;31m'
COLOR_WARN='\e[0;33m'
COLOR_SUCCESS='\e[0;32m'
COLOR_ERR='\e[0;37m'
COLOR_OUT='\e[0;37m'
COLOR_DEBUG='\e[0;35m'
function stderr {
if [ -z "$@" ]; then
while read str; do
log ERR "${str}"
done
else
log stderr "$@"
fi
}
function stdout {
if [ -z "$@" ]; then
while read str; do
log OUT "${str}"
done
else
log OUT "$@"
fi
}
function info { function info {
if [ -z "$@" ]; then if [ -z "$@" ]; then
while read str; do while read str; do
@ -14,6 +45,16 @@ function info {
fi fi
} }
function warn {
if [ -z "$@" ]; then
while read str; do
log WARN "${str}"
done
else
log WARN "$@"
fi
}
function debug { function debug {
if [ -z "$@" ]; then if [ -z "$@" ]; then
while read str; do while read str; do
@ -24,16 +65,6 @@ function debug {
fi fi
} }
function error {
if [ -z "$@" ]; then
while read str; do
log ERROR "${str}" >&2
done
else
log ERROR "$@" >&2
fi
}
function fatal { function fatal {
if [ -z "$@" ]; then if [ -z "$@" ]; then
while read str; do while read str; do
@ -45,9 +76,32 @@ function fatal {
exit 1 exit 1
} }
function success {
if [ -z "$@" ]; then
while read str; do
log SUCCESS "${str}"
done
else
log SUCCESS "$@"
fi
}
function log { function log {
local args=( $@ ) local args=( $@ )
echo "[${HOSTNAME}] [${args[0]}] ${args[@]:1}" local color=COLOR_${args[0]}
echo -e "${!color}[${args[0]}] $(remove_ansi ${args[@]:1})\e[0m"
}
function remove_ansi {
echo "$@" | sed 's,\x1B\[[0-9;]*[a-zA-Z],,g'
}
function load_default_opts {
if [[ -e "${DEFAULT_OPTS_FILE}" ]]; then
info "Loading default opts..."
else
info "No default opts found."
fi
} }
function get_opt { function get_opt {
@ -61,6 +115,7 @@ function get_opt {
function set_opt { function set_opt {
local opt_name=${1} local opt_name=${1}
local opt_value=${2} local opt_value=${2}
mkdir -p "$(dirname ${OPT_FILE})"
touch "${OPT_FILE}" touch "${OPT_FILE}"
sed -i "s/^${OPT_PREFIX}${opt_name}*$//" "${OPT_FILE}" sed -i "s/^${OPT_PREFIX}${opt_name}*$//" "${OPT_FILE}"
echo "${OPT_PREFIX}${opt_name}=\"${opt_value}\"" >> "${OPT_FILE}" echo "${OPT_PREFIX}${opt_name}=\"${opt_value}\"" >> "${OPT_FILE}"
@ -75,16 +130,16 @@ function exec_hooks {
for hook_script in ${hook_scripts}; do for hook_script in ${hook_scripts}; do
info "[${hook}] Executing ${hook_script}" info "[>> ${hook}] ${hook_script}"
( cd "${workspace}" && "${hook_script}" ) 2> >(error) 1> >(info) ( cd "${workspace}" && "${hook_script}" ) 2> >(stderr) 1> >(stdout)
# 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
fatal "The '${hook_script}' hook script did not finished properly !" fatal "The '${hook_script}' hook script did not finished properly !"
fi fi
info "[${hook}] ${hook_script} Done." info "[<< ${hook}] ${hook_script}"
done done

View File

@ -4,21 +4,20 @@ set -e
TAMARIN_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) TAMARIN_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
BASE_DIR="$TAMARIN_DIR" source "$TAMARIN_DIR/lib/util.sh" BASE_DIR="$TAMARIN_DIR" source "$TAMARIN_DIR/lib/util.sh"
function show_usage { function show_usage {
echo echo
echo "Usage: $0 -p project_path [-d destination] [-i image] [-k]" echo "Usage: $0 -p project_path [-a arch] [-d destination] [-i image] [-k]"
echo echo
echo "Parameters: " echo "Parameters: "
echo echo
echo " -p Path to the project to build" echo " -p Path to the project to build"
echo " -a Optional : Target architecture (default amd64)" echo " -a Optional : Target architecture (default amd64)"
echo " -o Optional : Default options file to load (default none)"
echo " -d Optional : Destination of the builed packages (default ./packages)" echo " -d Optional : Destination of the builed packages (default ./packages)"
echo " -i Optional : Name of the Docker image to use for build (default: debian:jessie)" echo " -i Optional : Name of the Docker image to use for build (default: debian:jessie)"
echo " -k Optional : Keep the Docker container after build " echo " -k Optional : Keep the Docker container after build "
echo " -b Optional : Build directory (default /tmp)"
echo " -B Optional : Build branch (for git projects only) (default dist/ubuntu/precise/master)" echo " -B Optional : Build branch (for git projects only) (default dist/ubuntu/precise/master)"
echo echo
exit 2 exit 2
@ -35,7 +34,6 @@ function create_container {
local temp_dir="$(mktemp -d)" local temp_dir="$(mktemp -d)"
local projectName=${1} local projectName=${1}
local buildDir=${2}
# Link lib & hooks folders # Link lib & hooks folders
ln -s $(readlink -f "$TAMARIN_DIR/lib") "$temp_dir/lib" ln -s $(readlink -f "$TAMARIN_DIR/lib") "$temp_dir/lib"
@ -43,7 +41,7 @@ function create_container {
# Create Dockerfile # Create Dockerfile
cat << EOF > "$temp_dir/Dockerfile" cat << EOF > "$temp_dir/Dockerfile"
FROM $BASE_IMAGE FROM ${BASE_IMAGE}
ENV DEBIAN_FRONTEND noninteractive ENV DEBIAN_FRONTEND noninteractive
@ -59,20 +57,21 @@ function create_container {
VOLUME /src VOLUME /src
VOLUME /dist VOLUME /dist
EOF EOF
# Add default opts file if defined
if [[ -e "${DEFAULT_OPTS}" ]]; then
ln -s "${DEFAULT_OPTS}" "$temp_dir/default_opts"
echo "ADD ./default_ops /tmp/tamarin/default_opts" >> "$temp_dir/Dockerfile"
fi
exec_hooks "containerbuild" "$temp_dir" exec_hooks "containerbuild" "$temp_dir"
if [[ -z ${BUILD_DIR} ]] echo "CMD /root/.tamarin/lib/build.sh ${projectName} ${BUILD_BRANCH} /tmp ${BUILD_TAG}" >> "$temp_dir/Dockerfile"
then
echo " CMD /root/.tamarin/lib/build.sh ${projectName} ${BUILD_BRANCH} /tmp ${BUILD_TAG}" >> "$temp_dir/Dockerfile"
else
echo " VOLUME /build" >> "$temp_dir/Dockerfile"
echo " CMD /root/.tamarin/lib/build.sh ${projectName} ${BUILD_BRANCH} /build ${BUILD_TAG}" >> "$temp_dir/Dockerfile"
fi
# Build image # Build image
tar -C "$temp_dir" -czh . | docker build -t "$container_tag" - 2> >(error) 1> >(info) tar -C "$temp_dir" -czh . | docker build -t "$container_tag" - 2> >(stderr) 1> >(stdout)
# Delete temporary folder # Delete temporary folder
rm -rf "$temp_dir" rm -rf "$temp_dir"
@ -108,26 +107,21 @@ function main {
docker_opt="${docker_opt}" docker_opt="${docker_opt}"
fi fi
if [[ -z ${BUILD_DIR} ]]
then
docker_opt="${docker_opt} -v=\"$PROJECT_PATH:/src:ro\" -v=\"$PROJECT_DEST:/dist:rw\" $container_tag" docker_opt="${docker_opt} -v=\"$PROJECT_PATH:/src:ro\" -v=\"$PROJECT_DEST:/dist:rw\" $container_tag"
else
docker_opt="${docker_opt} -v=\"$PROJECT_PATH:/src:ro\" -v=\"$PROJECT_DEST:/dist:rw\" -v=\"${BUILD_DIR}:/build\" $container_tag"
fi
info "Switching to container..." info "Switching to container..."
debug "docker ${docker_opt}" debug "docker ${docker_opt}"
docker ${docker_opt} docker ${docker_opt}
res=${?} res=${?}
info "Done" success "Done"
return ${res} return ${res}
} }
# #
# Parsing options # Parsing options
# #
while getopts "kp:d:i:b:B:t:a:" option while getopts "kp:d:i:B:t:a:o:" option
do do
case $option in case $option in
k) k)
@ -142,9 +136,6 @@ do
i) i)
BASE_IMAGE="${OPTARG}" BASE_IMAGE="${OPTARG}"
;; ;;
b)
BUILD_DIR=$(readlink -f ${OPTARG})
;;
B) B)
BUILD_BRANCH=${OPTARG} BUILD_BRANCH=${OPTARG}
;; ;;
@ -154,6 +145,9 @@ do
a) a)
TARGET_ARCH=${OPTARG} TARGET_ARCH=${OPTARG}
;; ;;
o)
DEFAULT_OPTS=$(readlink -f ${OPTARG})
;;
*) *)
show_usage show_usage
;; ;;
@ -173,9 +167,7 @@ done
if [[ -n ${http_proxy} ]] if [[ -n ${http_proxy} ]]
then then
info "-" warn "You have a proxy defined please make sure docker deamon is configured to use this proxy"
info "[WARN] You have a proxy defined please make sure docker deamon is configured to use this proxy"
info "-"
fi fi
main main