|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +export CMAKE_VERSION="3.15.2" |
| 4 | +export BASE_IMAGE="ubuntu:bionic" |
| 5 | + |
| 6 | +main() { |
| 7 | + if [[ -z "${run_build_script}" ]] ; then |
| 8 | + startDockerContainer || return 1 |
| 9 | + else |
| 10 | + buildOsqueryToolchain || return 1 |
| 11 | + fi |
| 12 | + |
| 13 | + return 0 |
| 14 | +} |
| 15 | + |
| 16 | +startDockerContainer() { |
| 17 | + if [[ -d "build" ]] ; then |
| 18 | + echo "Reusing existing build folder" |
| 19 | + else |
| 20 | + echo "Creating new build folder" |
| 21 | + |
| 22 | + mkdir build |
| 23 | + if [[ $? != 0 ]] ; then |
| 24 | + echo "Failed to create the build folder" |
| 25 | + return 1 |
| 26 | + fi |
| 27 | + fi |
| 28 | + |
| 29 | + local container_name="osquery-toolchain-$(git rev-parse HEAD)" |
| 30 | + docker rm "${container_name}" > /dev/null 2>&1 |
| 31 | + |
| 32 | + docker run --rm -e "run_build_script=1" -v "$(realpath build):/opt/osquery-toolchain" -v "$(pwd):/home/osquery/osquery-toolchain" --name "${container_name}" -it "${BASE_IMAGE}" /bin/bash -c '/home/osquery/osquery-toolchain/build_inside_docker.sh' |
| 33 | + if [[ $? != 0 ]] ; then |
| 34 | + echo "Failed to start the Docker container" |
| 35 | + return 1 |
| 36 | + fi |
| 37 | + |
| 38 | + return 0 |
| 39 | +} |
| 40 | + |
| 41 | +buildOsqueryToolchain() { |
| 42 | + # https://stackoverflow.com/questions/59895/how-to-get-the-source-directory-of-a-bash-script-from-within-the-script-itself |
| 43 | + local home_folder="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" |
| 44 | + cd "${home_folder}" |
| 45 | + |
| 46 | + installSystemDependencies || return 1 |
| 47 | + installCMake || return 1 |
| 48 | + initializeOsqueryUser || return 1 |
| 49 | + |
| 50 | + export KEEP_INTERMEDIATE_STAGES=1 |
| 51 | + sudo -u osquery ./build.sh "/opt/osquery-toolchain" |
| 52 | + if [[ $? != 0 ]] ; then |
| 53 | + echo "The build script has failed" |
| 54 | + return 1 |
| 55 | + fi |
| 56 | + |
| 57 | + return 0 |
| 58 | +} |
| 59 | + |
| 60 | +initializeOsqueryUser() { |
| 61 | + useradd -d "/home/osquery" -M osquery |
| 62 | + if [[ $? != 0 ]] ; then |
| 63 | + echo "Failed to create the osquery user" |
| 64 | + return 1 |
| 65 | + fi |
| 66 | + |
| 67 | + chown -R osquery:osquery "/home/osquery" |
| 68 | + if [[ $? != 0 ]] ; then |
| 69 | + echo "Failed to set the require permissions on the home directory" |
| 70 | + return 1 |
| 71 | + fi |
| 72 | + |
| 73 | + return 0 |
| 74 | +} |
| 75 | + |
| 76 | +installSystemDependencies() { |
| 77 | + apt-get update |
| 78 | + if [[ $? != 0 ]] ; then |
| 79 | + echo "Failed to update the package repositories" |
| 80 | + return 1 |
| 81 | + fi |
| 82 | + |
| 83 | + apt-get install g++-8 gcc-8 automake autoconf gettext bison flex unzip help2man libtool-bin libncurses-dev make ninja-build wget git texinfo xz-utils gawk python sudo -y |
| 84 | + if [[ $? != 0 ]] ; then |
| 85 | + echo "Failed to install the required dependencies" |
| 86 | + return 1 |
| 87 | + fi |
| 88 | + |
| 89 | + update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 20 |
| 90 | + if [ $? != 0 ] ; then |
| 91 | + echo "Failed to set the default gcc binary path" |
| 92 | + return 1 |
| 93 | + fi |
| 94 | + |
| 95 | + update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-8 20 |
| 96 | + if [ $? != 0 ] ; then |
| 97 | + echo "Failed to set the default g++ binary path" |
| 98 | + return 1 |
| 99 | + fi |
| 100 | + |
| 101 | + update-alternatives --install /usr/bin/cpp cpp /usr/bin/cpp-8 20 |
| 102 | + if [ $? != 0 ] ; then |
| 103 | + echo "Failed to set the default cpp binary path" |
| 104 | + return 1 |
| 105 | + fi |
| 106 | + |
| 107 | + return 0 |
| 108 | +} |
| 109 | + |
| 110 | +installCMake() { |
| 111 | + local cmake_archive_path="/tmp/cmake.tar.gz" |
| 112 | + wget "https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/cmake-${CMAKE_VERSION}-Linux-x86_64.tar.gz" -O "${cmake_archive_path}" |
| 113 | + if [[ $? != 0 ]] ; then |
| 114 | + echo "Failed to download the CMake release archive" |
| 115 | + return 1 |
| 116 | + fi |
| 117 | + |
| 118 | + tar xvf "${cmake_archive_path}" -C "/usr/local" --strip 1 |
| 119 | + if [[ $? != 0 ]] ; then |
| 120 | + echo "Failed to extract the CMake release archive" |
| 121 | + return 1 |
| 122 | + fi |
| 123 | + |
| 124 | + return 0 |
| 125 | +} |
| 126 | + |
| 127 | +main $@ |
| 128 | +exit $? |
0 commit comments