20 Commits

Author SHA1 Message Date
d99305182e Arrêt du processus en cas d'erreur 2017-03-20 18:35:59 +01:00
6d5e898cb8 Bump default rkt version 2017-03-15 18:45:23 +01:00
1d566f8de8 Extraction automatique du nom de projet avant le packaging 2017-03-10 22:04:00 +01:00
448a16f659 Base gestion mode debug 2017-03-10 21:51:57 +01:00
9780cafffd Ajout argument debug et base, acbuild sans modification de l'image 2017-02-18 11:26:25 +01:00
0bf686d5f3 Correction execution des tests 2017-02-16 10:03:38 +01:00
e58ab60bd8 Refactoring et mise en place des tests 2017-02-11 11:51:01 +01:00
24dbf2fe47 Exposition des variables http_proxy* dans le conteneur 2017-02-10 15:31:42 +01:00
97df663236 Correction détection présence de systemd 2017-02-10 09:23:10 +01:00
15643cf145 Correction point d'entrée conteneur 2017-02-09 22:38:04 +01:00
2375dd6571 POC fonctionnel 2017-02-09 21:53:24 +01:00
38d236e09b Build de l'image de base + execution build.py dans le conteneur 2017-01-25 14:49:30 +01:00
ce05d9a339 Génération de l'image avec mise en cache 2017-01-24 23:15:13 +01:00
82fb9d31e1 Gestion de l'image de base 2017-01-24 17:32:19 +01:00
487c97e88c Construction/execution d'un conteneur à partir de l'image de base spécifiée dans le profil 2017-01-20 17:25:48 +01:00
b88493523f Début génération d'images pour construction des paquets via acbuild 2017-01-19 22:56:17 +01:00
8469bf2c08 Base réécriture script package 2017-01-19 17:39:55 +01:00
86216e639e Séparation méthodes en modules, début docstring 2017-01-18 17:43:46 +01:00
cc5df7cfcd Début implémentation wrapper Python sur rkt/acbuild 2017-01-11 14:51:31 +01:00
0028db764a Merge tag '0.0.1' into develop
Première version de Tamarin
2016-09-22 09:10:51 +02:00
39 changed files with 679 additions and 483 deletions

4
.gitignore vendored
View File

@ -1,3 +1,7 @@
packages
*.log
*~
__pycache__
*.pyc
*.changes
*.deb

3
Makefile Normal file
View File

@ -0,0 +1,3 @@
.PHONY: test
test:
python3 -m unittest discover "./test" "*_test.py"

View File

@ -1,26 +1,38 @@
# Tamarin
Usine à paquets binaires Debian basée sur Docker.
Usine à paquets expérimentale basée sur rkt/acbuild.
## Formats de paquets/distributions supportés
- `.deb` (Debian, Ubuntu)
## Dépendances
Vous devez avoir [docker](https://docs.docker.com/installation/) installé sur votre machine.
- [Python 3](https://www.python.org/downloads/)
- Un noyau Linux > 2.6.24 (avec support des cgroups)
## Utilisation
**Optionnel mais conseillé**
- [systemd](https://freedesktop.org/wiki/Software/systemd/)
## Usage
```bash
./package --help
```
Usage: ./package.sh -p project_path [-d destination] [-i image] [-k]
## Fonctionnement
Parameters:
### Les profils de construction
-p Path to the project to build
-a Optional : Architecture choice (-a i386 for example)
-d Optional : Destination of the builed packages (default ./packages)
-i Optional : Name of the Docker image to use for build (default: debian:jessie)
-k Optional : Keep the Docker container after build
-b Optional : Build directory (default /tmp)
-B Optional : Build branch (for git projects only) (default dist/ubuntu/precise/master)
```
TODO
### Les attributs de construction
TODO
### Répertoire de travail et mise en cache des images
TODO
## Licence

View File

@ -1,82 +0,0 @@
#!/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 Optionnel - Nom de l'image Docker à utiliser comme environnement pour tester l'installation. Défaut: debian:jessie"
echo
}
function create_container {
# Escape image name
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
local 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
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" - 2> >(error) 1> >(info)
# Delete temporary folder
rm -rf "$temp_dir"
}
function main {
# Create container image
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" ]; then
show_usage
exit 1
fi
DEB_PATH=$(readlink -f "$1")
DEB_NAME=$(basename "$DEB_PATH")
DEB_DIR=$(dirname "$DEB_PATH")
BASE_IMAGE="${2:-debian:jessie}"
main

4
hooks/build/debian/build Executable file
View File

@ -0,0 +1,4 @@
#!/usr/bin/env bash
cd src
dpkg-buildpackage -b -a"${TAMARIN_TARGET_ARCH}"

View File

@ -0,0 +1,27 @@
#!/usr/bin/env bash
set -e
${TAMARIN_ACBUILD} environment add DEBIAN_FRONTEND noninteractive
if [ "${TAMARIN_ACBUILD_ENGINE}" == 'chroot' ]; then
# Ugly fix for Python installation in chrooted environment (require /dev/urandom)
head -c 65536 /dev/urandom > ./urandom
${TAMARIN_ACBUILD} copy ./urandom /dev/urandom
fi
sudo -E /usr/bin/env bash - <<EOF
export PATH=${PATH}
${TAMARIN_ACBUILD} run --engine "${TAMARIN_ACBUILD_ENGINE}" -- apt-get update
${TAMARIN_ACBUILD} run --engine "${TAMARIN_ACBUILD_ENGINE}" -- apt-get install --yes --no-install-recommends build-essential devscripts equivs python3
EOF
if [ "${TAMARIN_ACBUILD_ENGINE}" == 'chroot' ]; then
# Clean up Python fix (see above)
sudo -E /usr/bin/env bash - <<EOF
export PATH=${PATH}
${TAMARIN_ACBUILD} run --engine "${TAMARIN_ACBUILD_ENGINE}" -- rm -f /dev/urandom
EOF
fi
${TAMARIN_ACBUILD} environment remove DEBIAN_FRONTEND

View File

@ -0,0 +1,11 @@
#!/usr/bin/env bash
set -e
${TAMARIN_ACBUILD} environment add DEBIAN_FRONTEND noninteractive
sudo -E /usr/bin/env bash - <<EOF
export PATH=${PATH}
${TAMARIN_ACBUILD} run --engine "${TAMARIN_ACBUILD_ENGINE}" -- apt-get update
${TAMARIN_ACBUILD} run --engine "${TAMARIN_ACBUILD_ENGINE}" -- apt-get install --yes --no-install-recommends git-core
EOF
${TAMARIN_ACBUILD} environment remove DEBIAN_FRONTEND

View File

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

View File

@ -1,9 +1,7 @@
#!/bin/bash
source "${TAMARIN_UTIL}"
function move_output_to_dist {
find '../' -maxdepth 1 -name "$1" -type f -print0 | xargs -0r mv -t /dist/
find . -maxdepth 1 -name "$1" -type f -print0 | xargs -0r mv -t /dist/
}
# Create new directory
@ -14,6 +12,3 @@ move_output_to_dist "*.deb"
move_output_to_dist "*.changes"
move_output_to_dist "*.dsc"
move_output_to_dist "*.tar.{bz2,gz,lzma,xz}"
# Configure files permissions
chown -R ${HOST_UID}:${HOST_GID} /dist

View File

@ -1,23 +1,24 @@
#!/usr/bin/env bash
source "${TAMARIN_UTIL}"
cd src
if [ ! -f debian/changelog ]; then
info "No changelog. Skipping..."
tamarin_info "No changelog. Skipping..."
exit
fi
if [ $(get_opt no_version_suffix 'no') == 'yes' ]; then
info "Not adding version suffix."
if [ $(tamarin_db get no_version_suffix 'no') == 'yes' ]; then
tamarin_info "Not adding version suffix."
exit
fi
if [ -d .git ]; then
info "It seems to be a Git repository. Generating version suffix based on Git history..."
tamarin_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..."
tamarin_info "Not a Git project. Fallback to timestamp for suffix generation..."
version_suffix=tamarin$(date +%Y%m%d%H%M)
fi

View File

@ -0,0 +1,13 @@
#!/usr/bin/env bash
cd src
if [ -z "$(tamarin_db get project_name)" ]; then
if [ ! -d ".git" ]; then
tamarin_error "This project is not managed with Git ! Cannot extract the project's name without it !"
exit 1
fi
project_name=$(basename $(git config --get remote.origin.url) | sed 's/.git$//' | tr '[:upper:]' '[:lower:]')
tamarin_info "Extracted project name from Git metadata: \"${project_name}\""
tamarin_db set project_name "${project_name}"
fi

View File

@ -0,0 +1,7 @@
#!/usr/bin/env bash
tamarin_info "Copying sources to workspace 'src' directory..."
mkdir src
# Copying all read-only sources to the current workspace src
cp -r /src/. ./src

View File

@ -1,16 +1,17 @@
#!/usr/bin/env bash
source "${TAMARIN_UTIL}"
cd src
if [ -f debian/changelog ] || [ ! -d .git ]; then
info "Not a Git repository or Debian changelog already exists !"
tamarin_info "Not a Git repository or Debian changelog already exists !"
exit
fi
# Get commits log as changelog
BUILD_TAG=$(get_opt build_tag "last")
BUILD_TAG=$(tamarin_db get build_tag "last")
echo "BUILD TAG IS ${BUILD_TAG}"
tamarin_debug "BUILD TAG IS ${BUILD_TAG}"
if [[ ${BUILD_TAG} == "last" ]]
then
@ -18,13 +19,13 @@ then
else
tagbranch="build-tag-${BUILD_TAG}"
git checkout -b ${tagbranch}
set_opt "tag_branch" "${tag_branch}"
tamarin_db set "tag_branch" "${tag_branch}"
local tags="${BUILD_TAG}"
fi
if [[ -z ${tags} ]]
then
warn "No release tag found, you repo must have a tag like 'release/X.X'"
tamarin_warn "No release tag found, you repo must have a tag like 'release/X.X'"
exit
fi
@ -36,21 +37,21 @@ do
# 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}")
maintainer=$(tamarin_db get maintainer "${top_contributor}")
project_name=$(get_opt project_name)
project_name=$(tamarin_db get project_name)
version=${tag#*/} #$(get_opt version 0.0.0)
version=${version/_/-} #$(get_opt version 0.0.0)
distribution=$(get_opt distribution UNRELEASED)
urgency=$(get_opt urgency low)
distribution=$(tamarin_db get distribution UNRELEASED)
urgency=$(tamarin_db get urgency low)
package_version=${version}
# Define project_version opt if not defined
if [ -z "$(get_opt project_version)" ]; then
# Define project_version if not defined
if [ -z "$(tamarin_db get project_version)" ]; then
# Share computed project version
set_opt project_version "${version}"
tamarin_db set project_version "${version}"
fi
echo "${project_name} (${version}) ${distribution}; urgency=${urgency}" >> debian/changelog

View File

@ -1,14 +1,15 @@
#!/usr/bin/env bash
source "${TAMARIN_UTIL}"
cd src
if [ -f debian/changelog ] || [ ! -d .git ]; then
info "Not a Git repository or Debian changelog already exists !"
tamarin_info "Not a Git repository or Debian changelog already exists !"
exit
fi
changelog="debian/changelog"
project_name=$(get_opt project_name)
project_version=$(get_opt project_version 0.0.0)
project_name=$(tamarin_db get project_name)
project_version=$(tamarin_db get project_version 0.0.0)
date=$(date -R)
top_contributor=$(git log --pretty=short | git shortlog -s -n -e | sed 's/^\s*[0-9]*\s*//g' | head -n 1)
current_commit=$(git log -n 1 --pretty=format:%h)

View File

@ -1,5 +1,7 @@
#!/bin/bash
cd src
if [ -f debian/control ]; then
echo "Installing build dependencies..."
apt-get update

View File

@ -1,3 +1,3 @@
#!/usr/bin/env bash
echo $1
tamarin_db load

32
lib/build.py Normal file
View File

@ -0,0 +1,32 @@
import sys, os, argparse, tempfile
sys.path.append(os.path.dirname(__file__) + '/lib')
import tamarin, system, rkt
def get_args_parser():
parser = argparse.ArgumentParser(description="Tamarin's container entrypoint")
# Define available/required arguments and flags
parser.add_argument("profile", help="The selected profile to use for the build")
parser.add_argument("arch", help="The selected profile to use for the build")
return parser
def get_buildtools_dir():
return os.path.realpath(os.path.dirname(os.path.abspath(__file__)) + "/buildtools")
if __name__ == '__main__':
parser = get_args_parser()
args = parser.parse_args()
profile = tamarin.load_profile(args.profile)
workspace = tempfile.mkdtemp(prefix="tamarin_")
env = os.environ.copy()
env['TAMARIN_TARGET_ARCH'] = args.arch
env['TAMARIN_WORKSPACE'] = workspace
env['PATH'] = env['PATH'] + ':' + get_buildtools_dir()
tamarin.run_profile_hooks(profile, 'prebuild', cwd=workspace, env=env)
tamarin.run_profile_hooks(profile, 'build', cwd=workspace, env=env)
tamarin.run_profile_hooks(profile, 'postbuild', cwd=workspace, env=env)

View File

@ -1,47 +0,0 @@
#!/usr/bin/env bash
LIB_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
export TAMARIN_UTIL="${LIB_DIR}/util.sh"
source "${TAMARIN_UTIL}"
DIST_DIR="${BASE_DIR}/dist"
SRC_DIR="${BASE_DIR}/src"
PROJECT_NAME=${1}
BUILD_BRANCH=${2}
BUILD_TAG=${4}
function build_project()
{
info "Building project '${PROJECT_NAME}' for ${TARGET_ARCH} architecture..."
# Initalize opts
set_opt project_name "${PROJECT_NAME}"
set_opt build_branch "${BUILD_BRANCH}"
set_opt build_tag "${BUILD_TAG}"
local workspace=$(mktemp -d)/${PROJECT_NAME}
info "Build dir is ${workspace}"
mkdir -p "${workspace}"
# Copy sources to workspace
cd ${SRC_DIR}
cp -r ${SRC_DIR}/. "${workspace}"
cd "$workspace"
load_local_opts
exec_hooks "prebuild" "${workspace}"
dpkg-buildpackage -b -a"${TARGET_ARCH}" 2> >(stderr) 1> >(stdout)
if [ $? != 0 ]; then
fatal "The build process has not completed successfuly !"
fi
exec_hooks "postbuild" "${workspace}"
}
build_project

53
lib/buildtools/tamarin_db Executable file
View File

@ -0,0 +1,53 @@
#!/usr/bin/env bash
PROJECT_DB_FILE="/src/.tamarinrc"
GLOBAL_DB_FILE="${TAMARIN_WORKSPACE}/.tamarinrc"
KEY_PREFIX="tamarin_db_"
function tamarin_load_project_db {
if [ -e "${PROJECT_DB_FILE}" ]; then
tamarin_info "Loading project database..."
while read line; do
if [[ ! "${line}" =~ ^\s*# ]]; then
set -- $(echo $line | tr '=' ' ')
local key=$1
local value=$2
tamarin_debug "Load $key=$value"
tamarin_db_set $key $value
fi
done < "${PROJECT_DB_FILE}"
fi
}
function tamarin_db_get {
local opt_name=${KEY_PREFIX}${1}
local default_value=${2}
touch "${GLOBAL_DB_FILE}"
source "${GLOBAL_DB_FILE}"
echo ${!opt_name:-${default_value}}
}
function tamarin_db_set {
local opt_name=${1}
local opt_value=${2}
mkdir -p "$(dirname ${GLOBAL_DB_FILE})"
touch "${GLOBAL_DB_FILE}"
sed -i "s/^${KEY_PREFIX}${opt_name}*$//" "${GLOBAL_DB_FILE}"
echo "local ${KEY_PREFIX}${opt_name}=\"${opt_value}\"" >> "${GLOBAL_DB_FILE}"
}
case $1 in
set)
tamarin_db_set ${@:2}
;;
get)
tamarin_db_get ${@:2}
;;
load)
tamarin_load_project_db
;;
*)
tamarin_error "Invalid action '$1'. Must be get, set or load !"
exit 1
;;
esac

9
lib/buildtools/tamarin_debug Executable file
View File

@ -0,0 +1,9 @@
#!/usr/bin/env bash
if [ -z "$@" ]; then
while read str; do
tamarin_log DEBUG "${str}"
done
else
tamarin_log DEBUG "$@"
fi

9
lib/buildtools/tamarin_error Executable file
View File

@ -0,0 +1,9 @@
#!/usr/bin/env bash
if [ -z "$@" ]; then
while read str; do
tamarin_log ERROR "${str}"
done
else
tamarin_log ERROR "$@"
fi

9
lib/buildtools/tamarin_info Executable file
View File

@ -0,0 +1,9 @@
#!/usr/bin/env bash
if [ -z "$@" ]; then
while read str; do
tamarin_log INFO "${str}"
done
else
tamarin_log INFO "$@"
fi

23
lib/buildtools/tamarin_log Executable file
View File

@ -0,0 +1,23 @@
#!/usr/bin/env bash
# Colors
COLOR_INFO='\e[0;36m'
COLOR_ERROR='\e[0;31m'
COLOR_WARN='\e[0;33m'
COLOR_SUCCESS='\e[0;32m'
COLOR_DEBUG='\e[0;35m'
function log {
local args=( $@ )
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'
}
if [ $# -ne 0 ]; then
log $@
fi

9
lib/buildtools/tamarin_success Executable file
View File

@ -0,0 +1,9 @@
#!/usr/bin/env bash
if [ -z "$@" ]; then
while read str; do
tamarin_log SUCCESS "${str}"
done
else
tamarin_log SUCCESS "$@"
fi

9
lib/buildtools/tamarin_warn Executable file
View File

@ -0,0 +1,9 @@
#!/usr/bin/env bash
if [ -z "$@" ]; then
while read str; do
tamarin_log WARN "${str}"
done
else
tamarin_log WARN "$@"
fi

View File

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

38
lib/rkt.py Normal file
View File

@ -0,0 +1,38 @@
import system, subprocess, os, tamarin, json, re
def run(args, as_root = False, capture_output=False, debug=False):
"""Run rkt with the specified args (use the local copy if rkt is not found in the $PATH)"""
rkt_bin = system.which('rkt', tamarin.get_workspace_subdir('rkt'))
cmd = ( ["sudo", "-E", rkt_bin] if os.geteuid() != 0 and as_root == True else [rkt_bin] ) + args
if debug:
print(" ".join(cmd))
if capture_output:
return subprocess.check_output(cmd, stdin=subprocess.PIPE)
else:
return subprocess.check_call(cmd, stdin=subprocess.PIPE)
def get_images_list(rkt_flags = [], debug=False):
output = run([
"image",
"list",
"--format=json"
] + rkt_flags, capture_output=True, debug=debug)
# Fetch the list of installed images
return json.loads(output.decode('utf-8'))
def find_image_by_name(name_pattern, rkt_flags = []):
if type(name_pattern) is str:
name_pattern = re.compile(name_pattern)
images_list = get_images_list(rkt_flags = rkt_flags)
for image in images_list:
if name_pattern.search(image['name']):
return image
return None
def export_image(image_id, dest_file, rkt_flags = [], debug=False):
run([
"image",
"export",
image_id,
dest_file,
] + rkt_flags, debug=debug)

29
lib/system.py Normal file
View File

@ -0,0 +1,29 @@
import tarfile, os
def extract_tar(file_path, dest_dir = ".", debug=False):
if debug:
print('Extracting "{:s}" to "{:s}"'.format(file_path, dest_dir))
with tarfile.open(file_path) as tar:
tar.extractall(dest_dir)
tar.close()
def which(program, additional_paths = None):
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
paths = os.environ["PATH"].split(os.pathsep);
if additional_paths != None:
paths.append(additional_paths)
for path in paths:
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None

87
lib/tamarin.py Normal file
View File

@ -0,0 +1,87 @@
import os, glob, subprocess, configparser
import web, system
import codecs
def run_profile_hooks(profile, step, cwd=None, env=None, debug=False):
hooks_dir = get_hooks_dir()
step_hooks = profile[step]["hooks"]
if not step_hooks:
return
for hook_name in step_hooks.split(","):
trimmed_hook_name = hook_name.strip(' \t\n\r')
if not trimmed_hook_name:
continue
hook_path = os.path.join(hooks_dir, trimmed_hook_name)
code = subprocess.check_call(hook_path, cwd=cwd, stdin=subprocess.PIPE, env=env)
def get_hooks_dir():
return os.path.realpath(os.path.dirname(os.path.abspath(__file__)) + "/../hooks")
def get_lib_dir():
return os.path.realpath(os.path.dirname(os.path.abspath(__file__)) + "/../lib")
def load_profile(profile_name, debug=False):
profile_filename = profile_name+".conf"
for profile_file in get_available_profiles():
if profile_filename == os.path.basename(profile_file):
config = configparser.ConfigParser()
with codecs.open(profile_file, encoding = 'utf-8', mode = 'r') as handle:
config.read_file(handle)
return config
return None
def get_profiles_dir():
return os.path.realpath(os.path.dirname(os.path.abspath(__file__)) + "/../profiles")
def get_available_profiles():
return glob.glob(get_profiles_dir() + '/*.conf')
def get_available_profile_names():
profile_files = get_available_profiles()
return [os.path.splitext(os.path.basename(f))[0] for f in profile_files]
def get_workspace_dir():
"""Return the absolute path to the tamarin workspace ($HOME/.tamarin)"""
home = os.environ["HOME"]
return os.path.join(os.sep, home, '.tamarin')
def get_workspace_subdir(subdir):
"""Return the absolute path to a subdirectory in tamarin workspace"""
dir_path = os.path.join(os.sep, get_workspace_dir(), subdir)
os.makedirs(dir_path, exist_ok=True)
return dir_path
def get_acbuild_achive_dest_dir():
"""Return the first path matching the acbuild archive extraction destination in tamarin workspace"""
workspace_tmp = get_workspace_subdir('tmp')
return glob.glob(os.path.join(os.sep, workspace_tmp, 'acbuild-v*'))[0]
def get_rkt_achive_dest_dir():
"""Return the first path matching the rkt archive extraction destination in tamarin workspace"""
workspace_tmp = get_workspace_subdir('tmp')
return glob.glob(os.path.join(os.sep, workspace_tmp, 'rkt-v*'))[0]
def download_rkt(debug=False):
"""Download a local copy of rkt in the tamarin workspace and return the absolute path to the archive"""
url = "https://github.com/coreos/rkt/releases/download/v1.25.0/rkt-v1.25.0.tar.gz"
file_path=os.path.join(os.sep, get_workspace_subdir('tmp'), "rkt.tar.gz")
web.download_file(file_url=url, dest_path=file_path)
return file_path
def download_acbuild(debug=False):
"""Download a local copy of acbuild in the tamarin workspace and return the absolute path to the archive"""
url = "https://github.com/containers/build/releases/download/v0.4.0/acbuild-v0.4.0.tar.gz"
file_path=os.path.join(os.sep, get_workspace_subdir('tmp'), "acbuild.tar.gz")
web.download_file(file_url=url, dest_path=file_path)
return file_path
def run_acbuild(args, captureOutput=False, as_root=False, debug=False):
"""Run acbuild with the specified args (use the local copy if acbuild is not found in the $PATH)"""
acbuild_bin = system.which('acbuild', get_workspace_subdir('acbuild'))
cmd = ( ["sudo", "-E", acbuild_bin] if os.geteuid() != 0 and as_root == True else [acbuild_bin] ) + args
if debug:
print(" ".join(cmd))
if captureOutput:
return subprocess.check_output(cmd, stdin=subprocess.PIPE)
else:
return subprocess.check_call(cmd, stdin=subprocess.PIPE)

View File

@ -1,154 +0,0 @@
#!/usr/bin/env bash
HOOKS_DIR="${BASE_DIR}/hooks"
DEFAULT_OPTS_FILE="${BASE_DIR}/tmp/default_opts"
OPT_FILE="${BASE_DIR}/tmp/tamarin/opts"
OPT_PREFIX="tamarin_opt_"
LOCAL_OPTS_FILE=".tamarinrc"
# 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 {
if [ -z "$@" ]; then
while read str; do
log INFO "${str}"
done
else
log INFO "$@"
fi
}
function warn {
if [ -z "$@" ]; then
while read str; do
log WARN "${str}"
done
else
log WARN "$@"
fi
}
function debug {
if [ -z "$@" ]; then
while read str; do
log DEBUG "${str}"
done
else
log DEBUG "$@"
fi
}
function fatal {
if [ -z "$@" ]; then
while read str; do
log FATAL "${str}" >&2
done
else
log FATAL "$@" >&2
fi
exit 1
}
function success {
if [ -z "$@" ]; then
while read str; do
log SUCCESS "${str}"
done
else
log SUCCESS "$@"
fi
}
function log {
local args=( $@ )
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_local_opts {
if [ -e "${LOCAL_OPTS_FILE}" ]; then
info "Loading local opts..."
while read line; do
if [[ ! "${line}" =~ ^\s*# ]]; then
set -- $(echo $line | tr '=' ' ')
local key=$1
local value=$2
debug "Load opt $key=$value"
set_opt $key $value
fi
done < "${LOCAL_OPTS_FILE}"
fi
}
function get_opt {
local opt_name=${OPT_PREFIX}${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}
mkdir -p "$(dirname ${OPT_FILE})"
touch "${OPT_FILE}"
sed -i "s/^${OPT_PREFIX}${opt_name}*$//" "${OPT_FILE}"
echo "local ${OPT_PREFIX}${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 | sort )
for hook_script in ${hook_scripts}; do
info "[>> ${hook}] ${hook_script}"
( cd "${workspace}" && "${hook_script}" ) 2> >(stderr) 1> >(stdout)
# 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
}

33
lib/web.py Normal file
View File

@ -0,0 +1,33 @@
from urllib import request
import math, sys
def print_progress_bar(percent_progress=0, char_size=50, clear_line=True):
bar_progress = math.floor(char_size*(percent_progress/100))
bar = "=" * bar_progress + " " * (char_size - bar_progress)
if clear_line:
sys.stdout.write(u"\u001b[1000D")
sys.stdout.write("[{:s}] {:d}%".format(bar, int(percent_progress)))
sys.stdout.flush()
def download_file(file_url, dest_path, bulk_size = 8192):
req = request.urlopen(file_url)
meta = req.info()
file_size = int(meta.get('Content-Length'))
with open(dest_path, 'wb') as dest_file:
print('Downloading "{:s}". Size: {:d}b'.format(file_url, file_size))
downloaded_size = 0
while True:
buff = req.read(bulk_size)
if not buff:
break
downloaded_size += len(buff)
dest_file.write(buff)
progress = downloaded_size/file_size*100
print_progress_bar(progress)
dest_file.close()
# Add linebreak
print("")

292
package
View File

@ -1,182 +1,184 @@
#!/usr/bin/env bash
#!/usr/bin/env python3
set -e
import argparse, sys, shutil, os, hashlib
TAMARIN_VERSION=0.0.1
TAMARIN_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
sys.path.append(os.path.dirname(__file__) + '/lib')
BASE_DIR="$TAMARIN_DIR" source "$TAMARIN_DIR/lib/util.sh"
DEFAULT_64_IMAGE="debian:jessie"
DEFAULT_32_IMAGE="32bit/debian:jessie"
import tamarin, system, rkt
function show_usage {
echo
echo "Usage: $0 -p project_path [-a arch] [-d destination] [-i image] [-k]"
echo
echo "Parameters: "
echo
echo " -p Path to the project to build"
echo " -v Optional : Show Tamarin version"
echo " -a Optional : Target architecture (default amd64)"
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 " -k Optional : Keep the Docker container after build "
echo " -B Optional : Build branch (for git projects only) (default dist/ubuntu/precise/master)"
echo
exit 2
}
def create_args_parser():
'''Return a new configured ArgumentParser'''
profile_names = tamarin.get_available_profile_names()
# Create a build container based on the $BASE_IMAGE argument
function create_container {
parser = argparse.ArgumentParser(description="Generate packages for various GNU/Linux distributions")
# Escape image name
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
local temp_dir="$(mktemp -d)"
# Define available/required arguments and flags
parser.add_argument("project_directory", help="The path to your project to package")
parser.add_argument("-o", "--output", help="The path to the generated packages destination directory", default=".")
parser.add_argument("-p", "--profile", help="The profile to use to package this project (default: debian)", choices=profile_names, default='debian')
parser.add_argument("-a", "--architecture", help="The target architecture for the package (default: amd64)", default='amd64')
parser.add_argument("-b", "--base", help="Use the specified image instead of the profile's one", default='')
parser.add_argument("--rebuild", help="Ignore cache and rebuild container's image", action="store_true", default=False)
parser.add_argument("--debug", help="Will add extra output and start the container in interactive mode", action="store_true", default=False)
local projectName=${1}
return parser
# Link lib & hooks folders
ln -s $(readlink -f "$TAMARIN_DIR/lib") "$temp_dir/lib"
ln -s $(readlink -f "$TAMARIN_DIR/hooks") "$temp_dir/hooks"
def download_and_extract_rkt(dest_dir, debug=False):
'''Download and extract rkt to the given destination directory'''
rkt_archive_path = tamarin.download_rkt(debug=debug)
system.extract_tar(rkt_archive_path, workspace_tmp, debug=debug)
rkt_archive_dir = tamarin.get_rkt_achive_dest_dir()
shutil.rmtree(local_rkt_dir, ignore_errors=True)
os.rename(rkt_archive_dir, dest_dir)
# Create Dockerfile
cat << EOF > "$temp_dir/Dockerfile"
FROM ${BASE_IMAGE}
def download_and_extract_acbuild(dest_dir, debug=False):
'''Download and extract acbuild to the given destination directory'''
acbuild_archive_path = tamarin.download_acbuild(debug=debug)
system.extract_tar(acbuild_archive_path, workspace_tmp, debug=debug)
acbuild_archive_dir = tamarin.get_acbuild_achive_dest_dir()
shutil.rmtree(local_acbuild_dir, ignore_errors=True)
os.rename(acbuild_archive_dir, dest_dir)
ENV DEBIAN_FRONTEND noninteractive
def get_cached_image_path(profile, debug=False):
'''Compute and return the path for an hypothetic cached image for the given profile'''
containerbuild_hooks = profile['containerbuild']['hooks']
hasher = hashlib.sha1()
hasher.update(base_image.encode())
hasher.update(containerbuild_hooks.encode())
image_hash = hasher.hexdigest()
cache_dir = tamarin.get_workspace_subdir('cache')
return os.path.join(os.sep, cache_dir, '{:s}.aci'.format(image_hash[:12]));
RUN apt-get update &&\
apt-get install --yes --no-install-recommends build-essential devscripts equivs
def build_image(build_workspace, aci_file, base_image, profile, debug=False):
RUN mkdir /root/.tamarin
RUN mkdir /project
acbuild_flags = ["--work-path", build_workspace]
ADD ./lib /root/.tamarin/lib
ADD ./hooks /hooks
RUN chmod +x /root/.tamarin/lib/build.sh
VOLUME /src
VOLUME /dist
EOF
exec_hooks "containerbuild" "$temp_dir"
echo "CMD /root/.tamarin/lib/build.sh ${projectName} ${BUILD_BRANCH} /tmp ${BUILD_TAG}" >> "$temp_dir/Dockerfile"
# Find and export base image from rkt' store
name_pattern = base_image.split('/')[-1] + '$'
image = rkt.find_image_by_name(name_pattern, rkt_flags=rkt_flags)
rkt.export_image(image['id'], aci_file, rkt_flags=rkt_flags, debug=debug);
# Build image
tar -C "$temp_dir" -czh . | docker build -t "$container_tag" - 2> >(stderr) 1> >(stdout)
tamarin.run_acbuild(acbuild_flags+["begin", aci_file], debug=debug)
tamarin.run_acbuild(acbuild_flags+["set-name", "image_{:d}".format(pid)], debug=debug)
tamarin.run_acbuild(acbuild_flags+["mount", "add", "src", "/src", "--read-only"], debug=debug)
tamarin.run_acbuild(acbuild_flags+["mount", "add", "dist", "/dist"], debug=debug)
tamarin.run_acbuild(acbuild_flags+["mount", "add", "tamarin-hooks", "/tamarin/hooks", "--read-only"], debug=debug)
tamarin.run_acbuild(acbuild_flags+["mount", "add", "tamarin-lib", "/tamarin/lib", "--read-only"], debug=debug)
tamarin.run_acbuild(acbuild_flags+["mount", "add", "tamarin-profiles", "/tamarin/profiles", "--read-only"], debug=debug)
# Delete temporary folder
rm -rf "$temp_dir"
# Configure "containerbuild" hooks environment
hooks_env = os.environ.copy()
hooks_env["PATH"] = os.environ['PATH'] + ':' + tamarin.get_workspace_subdir('acbuild')
hooks_env["TAMARIN_ACBUILD"] = " ".join([system.which('acbuild', local_acbuild_dir)]+acbuild_flags)
hooks_env["TAMARIN_ACBUILD_ENGINE"] = "chroot" if not system.which('systemctl') else "systemd-nspawn"
}
# Run hooks
tamarin.run_profile_hooks(profile, 'containerbuild', cwd=build_workspace, env=hooks_env, debug=debug)
# Main function
function main {
tamarin.run_acbuild(acbuild_flags+["write", "--overwrite", aci_file], as_root=True, debug=debug)
tamarin.run_acbuild(acbuild_flags+["end"], as_root=True, debug=debug)
info "Building container from $BASE_IMAGE..."
return aci_file
local project_name=$(basename ${PROJECT_PATH})
def cleanup(build_workspace, rkt_flags, debug=False):
# Create container & "$container_tag" variable
create_container ${project_name}
# Nettoyage des conteneurs
rkt.run([
"gc",
"--grace-period=0"
] + rkt_flags, as_root=True, debug=debug)
local docker_opt="run -e \"DISTRIB=$BASE_IMAGE\" -e \"PROJECT_NAME=$project_name\""
# Nettoyage des images obsolètes du store
rkt.run([
"image",
"gc"
] + rkt_flags, as_root=True, debug=debug)
# Expose host uid & gid
docker_opt="${docker_opt} -e HOST_UID=$(id -u) -e HOST_GID=$(id -g)"
# Suppression de l'espace de travail de build
shutil.rmtree(build_workspace, ignore_errors=True)
# Expose host proxy variables
docker_opt="${docker_opt} -e HTTP_PROXY=${HTTP_PROXY} -e HTTPS_PROXY=${HTTPS_PROXY}"
docker_opt="${docker_opt} -e http_proxy=${http_proxy} -e https_proxy=${https_proxy}"
def validate_args(args):
'''TODO'''
# Target architecture
docker_opt="${docker_opt} -e TARGET_ARCH=${TARGET_ARCH}"
if __name__ == "__main__":
# If running in terminal, set docker to interactive
if [[ -t 1 ]]; then
docker_opt="${docker_opt} -it"
fi
parser = create_args_parser()
args = parser.parse_args()
if [[ ${PERSIST_CONTAINER} -eq 0 ]]
then
docker_opt="${docker_opt} --rm "
else
docker_opt="${docker_opt}"
fi
validate_args(args)
docker_opt="${docker_opt} -v $PROJECT_PATH:/src:ro -v $PROJECT_DEST:/dist:rw $container_tag"
# Verify project directory
project_dir = os.path.abspath(args.project_directory)
output_dir = os.path.abspath(args.output)
info "Switching to container..."
debug "docker ${docker_opt}"
docker ${docker_opt}
res=${?}
# Load build profile
profile = tamarin.load_profile(args.profile, debug=args.debug)
success "Done"
return ${res}
}
workspace = tamarin.get_workspace_dir()
workspace_tmp = tamarin.get_workspace_subdir('tmp')
#
# Parsing options
#
while getopts "vkp:d:i:B:t:a:o:" option
do
case $option in
k)
PERSIST_CONTAINER=1
;;
p)
PROJECT_PATH=$(readlink -f ${OPTARG})
;;
d)
PROJECT_DEST=$(readlink -f ${OPTARG})
;;
i)
BASE_IMAGE="${OPTARG}"
;;
B)
BUILD_BRANCH=${OPTARG}
;;
t)
BUILD_TAG=${OPTARG}
;;
a)
TARGET_ARCH=${OPTARG}
;;
v)
echo "Tamarin v${TAMARIN_VERSION}"
exit
;;
*)
show_usage
;;
esac
done
local_rkt_dir = tamarin.get_workspace_subdir('rkt')
if not system.which('rkt', local_rkt_dir):
download_and_extract_rkt(local_rkt_dir)
[[ -z ${PROJECT_PATH} ]] && show_usage
[[ -z ${PROJECT_DEST} ]] && PROJECT_DEST=$(readlink -f "./packages")
[[ -z ${BUILD_BRANCH} ]] && BUILD_BRANCH="dist/ubuntu/precise/master"
[[ -z ${PERSIST_CONTAINER} ]] && PERSIST_CONTAINER=0
[[ -z ${TARGET_ARCH} ]] && TARGET_ARCH=amd64
local_acbuild_dir = tamarin.get_workspace_subdir('acbuild')
if not system.which('acbuild', local_acbuild_dir):
download_and_extract_acbuild(local_acbuild_dir)
if [[ "${TARGET_ARCH}" =~ ^i[0-9]86$ ]] && [ -z "${BASE_IMAGE}" ]; then
info "32bit architecture specified and no specific image given, switching to default 32bit image..."
BASE_IMAGE=${DEFAULT_32_IMAGE}
fi
pid = os.getpid()
build_workspace = tamarin.get_workspace_subdir('tmp/build_{:d}'.format(pid))
[[ -z ${BASE_IMAGE} ]] && BASE_IMAGE=${DEFAULT_64_IMAGE}
rkt_store = tamarin.get_workspace_subdir('store')
rkt_flags = ["--dir={:s}".format(rkt_store)]
#
# Warn user about "proxy"
#
base_image = args.base if args.base != '' else profile['profile']['default_image']
if [[ -n ${http_proxy} ]]
then
warn "You have a proxy defined please make sure docker deamon is configured to use this proxy"
fi
# If the base image is Docker-based, download it
if base_image.startswith('docker://'):
rkt.run([
"fetch",
"--insecure-options=image",
base_image
] + rkt_flags, debug=args.debug)
main
aci_file = os.path.join(os.sep, build_workspace, 'image.aci')
cached_image_file = get_cached_image_path(profile, debug=args.debug)
if not args.rebuild and os.path.exists(cached_image_file):
# Copy cached image
shutil.copyfile(cached_image_file, aci_file)
else:
build_image(build_workspace, aci_file, base_image, profile, debug=args.debug)
# Cache image
shutil.copyfile(aci_file, cached_image_file)
# rkt run arguments
rkt_args = [
"run",
"--insecure-options=image",
aci_file, "--net=host",
"--volume=src,kind=host,source={:s}".format(project_dir),
"--volume=dist,kind=host,source={:s}".format(output_dir),
"--volume=tamarin-hooks,kind=host,source={:s}".format(tamarin.get_hooks_dir()),
"--volume=tamarin-lib,kind=host,source={:s}".format(tamarin.get_lib_dir()),
"--volume=tamarin-profiles,kind=host,source={:s}".format(tamarin.get_profiles_dir())
]
# Use environment proxy if defined
for proxy_var in ['HTTP_PROXY', 'HTTPS_PROXY', 'http_proxy', 'https_proxy']:
if proxy_var in os.environ:
rkt_args += ["--set-env={:s}={:s}".format(proxy_var, os.environ[proxy_var])]
if args.debug:
rkt_args += ["--interactive", "--exec", "/bin/bash"]
helper_cmd = " ".join(["/usr/bin/python3", "/tamarin/lib/build.py", args.profile, args.architecture])
print("Executer '{:s}' pour lancer la construction du paquet.".format(helper_cmd))
else:
rkt_args += ["--exec", "/usr/bin/python3", "--", "/tamarin/lib/build.py", args.profile, args.architecture]
# Start container
rkt.run(rkt_flags+rkt_args, as_root=True, debug=args.debug)
# Cleanup
cleanup(build_workspace, rkt_flags, debug=args.debug)

27
profiles/debian.conf Normal file
View File

@ -0,0 +1,27 @@
# Configuration générale du profil
[profile]
# Image Docker par défaut
default_image=docker://debian:jessie
# Configuration de l'étape de pré-construction du conteneur
[containerbuild]
hooks=containerbuild/debian/install-build-essential,
containerbuild/debian/install-git
# Configuration de l'étape de pré-construction du paquet
[prebuild]
hooks=prebuild/debian/load-project-db,
prebuild/debian/copy-sources-to-workspace,
prebuild/debian/complete-project-db,
prebuild/debian/create-changelog,
prebuild/debian/create-dummy-changelog,
prebuild/debian/add-package-version-suffix,
prebuild/debian/install-build-depends
# Configuration de l'étape de construction du paquet
[build]
hooks=build/debian/build
# Configuration de l'étape de post-construction du paquet
[postbuild]
hooks=postbuild/debian/export-dist-postbuild

View File

@ -0,0 +1,5 @@
dummy-project (0.0.1) unstable; urgency=low
* Dev release
-- William Petit <wpetit@cadoles.com> Fri, 16 Oct 2015 15:57:03 +0200

View File

@ -0,0 +1 @@
9

View File

@ -0,0 +1,13 @@
Source: dummy-project
Section: unknown
Priority: optional
Maintainer: William Petit <wpetit@cadoles.com>
Build-Depends: debhelper (>= 8.0.0)
Standards-Version: 3.9.4
Homepage:
Vcs-Git: https://forge.cadoles.com/wpetit/tamarin.git
Vcs-Browser: https://forge.cadoles.com/wpetit/tamarin
Package: dummy-project
Architecture: any
Description: Projet test pour la construction de paquets via Tamarin

View File

@ -0,0 +1,8 @@
#!/usr/bin/make -f
# -*- makefile -*-
# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1
%:
dh $@

View File

@ -0,0 +1 @@
3.0 (quilt)

16
test/package_test.py Normal file
View File

@ -0,0 +1,16 @@
import unittest, os
from importlib.machinery import SourceFileLoader
package = SourceFileLoader('package', os.path.dirname(__file__) + '/../package').load_module()
class TestPackage(unittest.TestCase):
def test_args_parser(self):
parser = package.create_args_parser()
def test_download_and_extract_rkt(self):
"""TODO"""
if __name__ == '__main__':
unittest.main()