1#!/bin/bash
2
3function check_libs {
4    local elf_path=$1
5
6    for elf in $(find $elf_path -maxdepth 1 -exec file {} \; | grep 'ELF ' | cut -d':' -f1); do
7        echo "$elf"
8        ldd "$elf"
9    done
10    return
11}
12
13function prepare {
14    CURDIR=$(pwd)
15    TMP_DIR="$CURDIR/temp"
16
17    mkdir -p "$CURDIR"/temp
18    mkdir -p "$TMP_DIR"/dbdeployer/tarball "$TMP_DIR"/dbdeployer/deployment
19
20    TARBALLS=""
21    for tarball in $(find . -name "*.tar.gz"); do
22        TARBALLS+=" $(basename $tarball)"
23    done
24
25    DIRLIST="bin lib lib/private lib/mysql/plugin"
26}
27
28function install_deps {
29    if [ -f /etc/redhat-release ]; then
30        yum install -y epel-release
31        yum install -y wget perl-Time-HiRes perl numactl numactl-libs libaio libidn libcurl-devel || true
32    else
33        apt install -y wget perl numactl libaio-dev libidn11 libcurl4-openssl-dev || true
34    fi
35    wget -c https://github.com/datacharmer/dbdeployer/releases/download/v1.52.0/dbdeployer-1.52.0.linux.tar.gz -O - | tar -xz
36    mv dbdeployer*.linux /usr/bin/dbdeployer
37}
38
39main () {
40    prepare
41    for tarfile in $TARBALLS; do
42        echo "Unpacking tarball: $tarfile"
43        cd "$TMP_DIR"
44        tar xf "$CURDIR/$tarfile"
45        cd "${tarfile%.tar.gz}"
46
47        echo "Building ELFs ldd output list"
48        for DIR in $DIRLIST; do
49            if ! check_libs "$DIR" >> "$TMP_DIR"/libs_err.log; then
50                echo "There is an error with libs linkage"
51                echo "Displaying log: "
52                cat "$TMP_DIR"/libs_err.log
53                exit 1
54            fi
55        done
56
57         echo "Checking for missing libraries"
58         if [[ ! -z $(grep "not found" $TMP_DIR/libs_err.log) ]]; then
59            echo "ERROR: There are missing libraries: "
60            grep "not found" "$TMP_DIR"/libs_err.log
61            echo "Log: "
62            cat "$TMP_DIR"/libs_err.log
63            exit 1
64        fi
65
66        echo "Invoking dbdeployer to make a test run"
67        dbdeployer unpack --sandbox-binary="$TMP_DIR"/dbdeployer/tarball --prefix=ps "$CURDIR/$tarfile"
68        dbdeployer deploy single --sandbox-home="$TMP_DIR"/dbdeployer/deployment --sandbox-binary="$TMP_DIR"/dbdeployer/tarball "$(ls $TMP_DIR/dbdeployer/tarball)"
69        if [[ $? -eq 0 ]]; then
70            SANDBOX="$(dbdeployer sandboxes --sandbox-home=$TMP_DIR/dbdeployer/deployment | awk '{print $1}')"
71            if ! "$TMP_DIR"/dbdeployer/deployment/"$SANDBOX"/test_sb; then
72                exit 1
73            else
74                dbdeployer delete --sandbox-home="$TMP_DIR"/dbdeployer/deployment --sandbox-binary="$TMP_DIR"/dbdeployer/tarball "$SANDBOX"
75                dbdeployer delete-binaries --skip-confirm --sandbox-home="$TMP_DIR"/dbdeployer/deployment --sandbox-binary="$TMP_DIR"/dbdeployer/tarball "$(ls $TMP_DIR/dbdeployer/tarball)"
76            fi
77        fi
78        rm -rf "${TMP_DIR:?}/*"
79    done
80}
81
82case "$1" in
83    --install_deps) install_deps ;;
84    --test) main ;;
85    --help|*)
86    cat <<EOF
87Usage: $0 [OPTIONS]
88    The following options may be given :
89        --install_deps
90        --test
91        --help) usage ;;
92Example $0 --install_deps 
93EOF
94    ;;
95esac
96