Blame docker-builder-data/build/script/common/manager

1b5f3d
#!/bin/bash
1b5f3d
1b5f3d
# root
1b5f3d
1b5f3d
ROOT_DIR=`realpath "$0"`
1b5f3d
ROOT_DIR=`dirname "$ROOT_DIR"`
1b5f3d
ROOT_DIR=`dirname "$ROOT_DIR"`
1b5f3d
ROOT_DIR=`dirname "$ROOT_DIR"`
1b5f3d
1b5f3d
# dirs
1b5f3d
1b5f3d
SCRIPT_DIR=$ROOT_DIR/script
1b5f3d
COMMON_SCRIPT_DIR=$SCRIPT_DIR/common
1b5f3d
PACKET_SCRIPT_DIR=$SCRIPT_DIR/packet
1b5f3d
PACKET_DIR=$ROOT_DIR/packet
1b5f3d
ENV_DIR=$ROOT_DIR/env
1b5f3d
ENVFLAGS_DIR=$ROOT_DIR/envflags
1b5f3d
1b5f3d
# vars
1b5f3d
1b5f3d
INITIAL_LD_LIBRARY_PATH=$LD_LIBRARY_PATH
1b5f3d
INITIAL_PATH=$PATH
1b5f3d
INITIAL_LDFLAGS=$LDFLAGS
1b5f3d
INITIAL_CPPFLAGS=$CPPFLAGS
1b5f3d
1b5f3d
1b5f3d
clean_packet_directory() {
1b5f3d
    if [ ! -z "$DRY_RUN" ]; then
1b5f3d
        return 0
1b5f3d
    fi
1b5f3d
    rm -f -r "$PACKET_DIR/$1/$2" || true
1b5f3d
    rm -f "$PACKET_DIR/$1/$2.done" || true
1b5f3d
}
1b5f3d
1b5f3d
check_packet_function() {
1b5f3d
    if [ ! -f "$PACKET_DIR/$1/$2.done" ]; then
1b5f3d
        return 1
1b5f3d
    fi
1b5f3d
}
1b5f3d
1b5f3d
prepare_build() {
1b5f3d
    if ! cp -r $2/* "$1/"; then
1b5f3d
        return 1
1b5f3d
    fi
1b5f3d
}
1b5f3d
1b5f3d
call_packet_function() {
1b5f3d
    local NAME=$1
1b5f3d
    local FUNC=$2
1b5f3d
    local PREV_FUNC=$3
1b5f3d
    local NEXT_FUNC=$4
1b5f3d
    local PREPARE_FUNC=$5
1b5f3d
    local FINALIZE_FUNC=$6
1b5f3d
1b5f3d
    local FUNC_NAME=pk$FUNC
1b5f3d
    local CURRENT_PACKET_DIR=$PACKET_DIR/$NAME
1b5f3d
    local DONE_FILE=$CURRENT_PACKET_DIR/$FUNC.done
1b5f3d
    local FUNC_CURRENT_PACKET_DIR=$CURRENT_PACKET_DIR/$FUNC
1b5f3d
    local PREV_FUNC_CURRENT_PACKET_DIR=$CURRENT_PACKET_DIR/$PREV_FUNC
1b5f3d
    local NEXT_FUNC_CURRENT_PACKET_DIR=$CURRENT_PACKET_DIR/$NEXT_FUNC
1b5f3d
1b5f3d
    echo "$FUNC $NAME"
1b5f3d
    if [ ! -z "$DRY_RUN" ]; then
1b5f3d
        return 0
1b5f3d
    fi
1b5f3d
1b5f3d
    rm -f "$DONE_FILE" || true
1b5f3d
1b5f3d
    mkdir -p $FUNC_CURRENT_PACKET_DIR
1b5f3d
    cd $FUNC_CURRENT_PACKET_DIR
1b5f3d
    source "$PACKET_SCRIPT_DIR/$NAME.sh"
1b5f3d
1b5f3d
    if [ ! -z "$PREPARE_FUNC" ]; then
1b5f3d
        if ! "$PREPARE_FUNC" "$FUNC_CURRENT_PACKET_DIR" "$PREV_FUNC_CURRENT_PACKET_DIR" "$NEXT_FUNC_CURRENT_PACKET_DIR"; then
1b5f3d
            return 1
1b5f3d
        fi
1b5f3d
    fi
1b5f3d
1b5f3d
    if ! "$FUNC_NAME" "$FUNC_CURRENT_PACKET_DIR" "$PREV_FUNC_CURRENT_PACKET_DIR" "$NEXT_FUNC_CURRENT_PACKET_DIR"; then
1b5f3d
        return 1
1b5f3d
    fi
1b5f3d
1b5f3d
    if [ ! -z "$FINALIZE_FUNC" ]; then
1b5f3d
        if ! "$FINALIZE_FUNC" "$FUNC_CURRENT_PACKET_DIR" "$PREV_FUNC_CURRENT_PACKET_DIR" "$NEXT_FUNC_CURRENT_PACKET_DIR"; then
1b5f3d
            return 1
1b5f3d
        fi
1b5f3d
    fi
1b5f3d
1b5f3d
    echo 1 > "$DONE_FILE"
1b5f3d
}
1b5f3d
1b5f3d
clean_download() {
1b5f3d
    clean_packet_directory $1 download
1b5f3d
}
1b5f3d
1b5f3d
clean_unpack() {
1b5f3d
    clean_packet_directory $1 download
1b5f3d
}
1b5f3d
1b5f3d
clean_build() {
1b5f3d
    clean_packet_directory $1 build
1b5f3d
}
1b5f3d
1b5f3d
clean_install() {
1b5f3d
    clean_packet_directory $1 install
1b5f3d
}
1b5f3d
1b5f3d
clean() {
1b5f3d
    clean_download $1
1b5f3d
    clean_unpack $1
1b5f3d
    clean_build $1
1b5f3d
    clean_install $1
1b5f3d
}
1b5f3d
1b5f3d
download() {
1b5f3d
    if ! (check_packet_function $1 download || call_packet_function $1 download "" unpack); then
1b5f3d
        return 1
1b5f3d
    fi
1b5f3d
}
1b5f3d
1b5f3d
unpack() {
1b5f3d
    if ! (check_packet_function $1 unpack || (download $1 && call_packet_function $1 unpack download build)); then
1b5f3d
        return 1
1b5f3d
    fi
1b5f3d
}
1b5f3d
1b5f3d
build() {
1b5f3d
    if check_packet_function $1 build; then
1b5f3d
        return 0
1b5f3d
    fi
1b5f3d
1b5f3d
    if ! unpack $1; then
1b5f3d
        return 1
1b5f3d
    fi
1b5f3d
1b5f3d
    set_environment_deps $1
1b5f3d
    if [ ! "$?" -eq "0" ]; then
1b5f3d
        return 1
1b5f3d
    fi
1b5f3d
1b5f3d
    if ! call_packet_function $1 build unpack install prepare_build; then
1b5f3d
        return 1
1b5f3d
    fi
1b5f3d
}
1b5f3d
1b5f3d
install() {
1b5f3d
    if ! (install_deps $1 && (check_packet_function $1 install || (build $1 && call_packet_function $1 install build))); then
1b5f3d
        return 1
1b5f3d
    fi
1b5f3d
}
1b5f3d
1b5f3d
download_with_deps() {
1b5f3d
    source $PACKET_SCRIPT_DIR/$1.sh
1b5f3d
    local CURRENT_DEPS=$DEPS
1b5f3d
    for DEP in $CURRENT_DEPS; do
1b5f3d
        if [ ! -z "$DEP"]; then
1b5f3d
            if ! download $DEP; then
1b5f3d
                return 1
1b5f3d
            fi
1b5f3d
        fi
1b5f3d
    done
1b5f3d
    if ! download $1; then
1b5f3d
        return 1
1b5f3d
    fi
1b5f3d
}
1b5f3d
1b5f3d
unpack_with_deps() {
1b5f3d
    source $PACKET_SCRIPT_DIR/$1.sh
1b5f3d
    local CURRENT_DEPS=$DEPS
1b5f3d
    for DEP in $CURRENT_DEPS; do
1b5f3d
        if [ ! -z "$DEP"]; then
1b5f3d
            if ! unpack $DEP; then
1b5f3d
                return 1
1b5f3d
            fi
1b5f3d
        fi
1b5f3d
    done
1b5f3d
    if ! unpack $1; then
1b5f3d
        return 1
1b5f3d
    fi
1b5f3d
}
1b5f3d
1b5f3d
install_deps() {
1b5f3d
    source $PACKET_SCRIPT_DIR/$1.sh
1b5f3d
    local CURRENT_DEPS=$DEPS
1b5f3d
    for DEP in $CURRENT_DEPS; do
1b5f3d
        if [ ! -z "$DEP"]; then
1b5f3d
            if ! install $DEP; then
1b5f3d
                return 1
1b5f3d
            fi
1b5f3d
        fi
1b5f3d
    done
1b5f3d
}
1b5f3d
1b5f3d
clear_environment() {
1b5f3d
    if [ ! -z "$DRY_RUN" ]; then
1b5f3d
        return 0
1b5f3d
    fi
1b5f3d
    rm -r -f "$ENVFLAGS_DIR"
1b5f3d
    rm -r -f "$ENV_DIR"
1b5f3d
}
1b5f3d
1b5f3d
add_environment_deps() {
1b5f3d
    if ! install_deps $1; then
1b5f3d
        return 1
1b5f3d
    fi
1b5f3d
1b5f3d
    export LD_LIBRARY_PATH="$ENV_DIR/lib:$INITIAL_LD_LIBRARY_PATH"
1b5f3d
    export PATH="$ENV_DIR/bin:$INITIAL_PATH"
1b5f3d
    export LDFLAGS="-L$ENV_DIR/lib $INITIAL_LDFLAGS"
1b5f3d
    export CPPFLAGS="-I$ENV_DIR/include $INITIAL_CPPFLAGS -fpermissive" 
1b5f3d
1b5f3d
    source $PACKET_SCRIPT_DIR/$1.sh
1b5f3d
    local CURRENT_DEPS=$DEPS
1b5f3d
    for DEP in $CURRENT_DEPS; do
1b5f3d
        if [ ! -z "$DEP"]; then
1b5f3d
            if ! add_environment $DEP; then
1b5f3d
                return 1
1b5f3d
            fi
1b5f3d
        fi
1b5f3d
    done
1b5f3d
}
1b5f3d
1b5f3d
add_environment() {
1b5f3d
    local NAME=$1
1b5f3d
    local DONE_FILE=$ENVFLAGS_DIR/$NAME.done
1b5f3d
1b5f3d
    if [ -f "$DONE_FILE" ]; then
1b5f3d
        return 0
1b5f3d
    fi
1b5f3d
1b5f3d
    if ! install $NAME; then
1b5f3d
        return 1
1b5f3d
    fi
1b5f3d
1b5f3d
    add_environment_deps $NAME
1b5f3d
    if [ ! "$?" -eq "0" ]; then
1b5f3d
        return 1
1b5f3d
    fi
1b5f3d
1b5f3d
    if [ ! -z "$DRY_RUN" ]; then
1b5f3d
        return 0
1b5f3d
    fi
1b5f3d
1b5f3d
    mkdir -p $ENV_DIR
1b5f3d
    if ! cp -r "$PACKET_DIR/$NAME/install/*" "$WORK_ENV_DIR/"; then
1b5f3d
        return 1
1b5f3d
    fi
1b5f3d
1b5f3d
    mkdir -p "$ENVFLAGS_DIR"
1b5f3d
    echo 1 > "$DONE_FILE"
1b5f3d
}
1b5f3d
1b5f3d
set_environment_deps() {
1b5f3d
    clear_environment
1b5f3d
    add_environment_deps $1
1b5f3d
    if [ ! "$?" -eq "0" ]; then
1b5f3d
        return 1
1b5f3d
    fi
1b5f3d
1b5f3d
    if [ "$2" = "shell" ]; then
1b5f3d
        cd $PACKET_DIR/$1/build
1b5f3d
        /bin/bash -i
1b5f3d
    fi
1b5f3d
}
1b5f3d
1b5f3d
set_environment() {
1b5f3d
    clear_environment
1b5f3d
    add_environment $1
1b5f3d
    if [ ! "$?" -eq "0" ]; then
1b5f3d
        return 1
1b5f3d
    fi
1b5f3d
1b5f3d
    if [ "$2" = "shell" ]; then
1b5f3d
        cd $PACKET_DIR/$1/build
1b5f3d
        /bin/bash -i
1b5f3d
    fi
1b5f3d
}
1b5f3d
1b5f3d
dry_run() {
1b5f3d
    DRY_RUN=1
1b5f3d
    "$@"
1b5f3d
}
1b5f3d
1b5f3d
"$@"