POC fonctionnel

This commit is contained in:
2017-02-09 21:53:24 +01:00
parent 38d236e09b
commit 2375dd6571
25 changed files with 279 additions and 470 deletions

View File

@ -1,3 +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__':
print('Test')
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,13 +1,20 @@
import os, glob, subprocess, configparser
import web, system
import codecs
def run_profile_hooks(profile, step, cwd=None, env=None):
hooks_dir = get_hooks_dir()
step_hooks = profile[step]["hooks"].split(",")
for hook_name in step_hooks:
hook_path = os.path.join(hooks_dir, hook_name)
print(hook_path)
subprocess.call(hook_path, cwd=cwd, stdin=subprocess.PIPE, env=env)
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.call(hook_path, cwd=cwd, stdin=subprocess.PIPE, env=env)
if code != 0:
raise Exception("Hook '{:s}' exited with a non zero code ({:d}) !".format(trimmed_hook_name, code))
def get_hooks_dir():
return os.path.realpath(os.path.dirname(os.path.abspath(__file__)) + "/../hooks")
@ -20,8 +27,9 @@ def load_profile(profile_name):
for profile_file in get_available_profiles():
if profile_filename == os.path.basename(profile_file):
config = configparser.ConfigParser()
config.read(profile_file)
return config
with codecs.open(profile_file, encoding = 'utf-8', mode = 'r') as handle:
config.read_file(handle)
return config
return None
def get_profiles_dir():

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
}