2014-09-25 08:45:13 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2014-12-18 18:50:47 +00:00
|
|
|
# Copyright (c) 2014, Facebook, Inc.
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# This source code is licensed under the BSD-style license found in the
|
2015-01-18 01:23:41 +00:00
|
|
|
# LICENSE file in the root directory of this source tree. An additional grant
|
2014-12-18 18:50:47 +00:00
|
|
|
# of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
|
2015-04-30 22:47:33 +00:00
|
|
|
ORACLE_RELEASE=/etc/oracle-release
|
2015-05-11 00:10:30 +00:00
|
|
|
SYSTEM_RELEASE=/etc/system-release
|
2015-07-25 11:52:59 +00:00
|
|
|
LSB_RELEASE=/etc/lsb-release
|
2015-08-14 20:23:08 +00:00
|
|
|
DEBIAN_VERSION=/etc/debian_version
|
2015-04-03 07:44:27 +00:00
|
|
|
|
2014-09-25 08:45:13 +00:00
|
|
|
function platform() {
|
2015-04-03 07:44:27 +00:00
|
|
|
local __out=$1
|
2015-04-30 22:47:33 +00:00
|
|
|
if [[ -f "$ORACLE_RELEASE" ]]; then
|
|
|
|
FAMILY="redhat"
|
|
|
|
eval $__out="oracle"
|
|
|
|
elif [[ -n `grep -o "CentOS" $SYSTEM_RELEASE 2>/dev/null` ]]; then
|
|
|
|
FAMILY="redhat"
|
2015-04-03 07:44:27 +00:00
|
|
|
eval $__out="centos"
|
2015-05-11 00:10:30 +00:00
|
|
|
elif [[ -n `grep -o "Red Hat Enterprise" $SYSTEM_RELEASE 2>/dev/null` ]]; then
|
2015-04-30 22:47:33 +00:00
|
|
|
FAMILY="redhat"
|
2015-04-03 07:44:27 +00:00
|
|
|
eval $__out="rhel"
|
2015-05-11 00:10:30 +00:00
|
|
|
elif [[ -n `grep -o "Amazon Linux" $SYSTEM_RELEASE 2>/dev/null` ]]; then
|
2015-04-30 22:47:33 +00:00
|
|
|
FAMILY="redhat"
|
2015-05-10 18:42:30 +00:00
|
|
|
eval $__out="amazon"
|
2015-07-25 11:52:59 +00:00
|
|
|
elif [[ -f "$LSB_RELEASE" ]] && grep -q 'DISTRIB_ID=Ubuntu' $LSB_RELEASE; then
|
2015-04-30 22:47:33 +00:00
|
|
|
FAMILY="debian"
|
2015-04-03 07:44:27 +00:00
|
|
|
eval $__out="ubuntu"
|
2015-08-19 04:50:30 +00:00
|
|
|
elif [[ -n `grep -o "Fedora" $SYSTEM_RELEASE 2>/dev/null` ]]; then
|
|
|
|
FAMILY="redhat"
|
|
|
|
eval $__out="fedora"
|
2015-08-14 20:23:08 +00:00
|
|
|
elif [[ -f "$DEBIAN_VERSION" ]]; then
|
|
|
|
FAMILY="debian"
|
|
|
|
eval $__out="debian"
|
2014-11-13 06:44:04 +00:00
|
|
|
else
|
2015-04-03 07:44:27 +00:00
|
|
|
eval $__out=`uname -s | tr '[:upper:]' '[:lower:]'`
|
2014-09-25 08:45:13 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2015-04-03 07:44:27 +00:00
|
|
|
function _platform() {
|
|
|
|
platform PLATFORM
|
|
|
|
echo $PLATFORM
|
|
|
|
}
|
|
|
|
|
2014-11-12 05:34:59 +00:00
|
|
|
function distro() {
|
2015-04-03 07:44:27 +00:00
|
|
|
local __out=$2
|
2015-04-30 22:47:33 +00:00
|
|
|
if [[ $1 = "oracle" ]]; then
|
|
|
|
eval $__out=`grep -o "release [5-7]" $SYSTEM_RELEASE | sed 's/release /oracle/g'`
|
|
|
|
elif [[ $1 = "centos" ]]; then
|
2015-05-11 00:10:30 +00:00
|
|
|
eval $__out=`grep -o "release [6-7]" $SYSTEM_RELEASE | sed 's/release /centos/g'`
|
2015-04-03 23:53:02 +00:00
|
|
|
elif [[ $1 = "rhel" ]]; then
|
2015-05-11 00:10:30 +00:00
|
|
|
eval $__out=`grep -o "release [6-7]" $SYSTEM_RELEASE | sed 's/release /rhel/g'`
|
2015-05-10 18:42:30 +00:00
|
|
|
elif [[ $1 = "amazon" ]]; then
|
2015-05-11 00:10:30 +00:00
|
|
|
eval $__out=`grep -o "release 20[12][0-9]\.[0-9][0-9]" $SYSTEM_RELEASE | sed 's/release /amazon/g'`
|
2014-11-12 05:34:59 +00:00
|
|
|
elif [[ $1 = "ubuntu" ]]; then
|
2015-07-25 11:52:59 +00:00
|
|
|
eval $__out=`awk -F= '/DISTRIB_CODENAME/ { print $2 }' $LSB_RELEASE`
|
2014-11-12 05:34:59 +00:00
|
|
|
elif [[ $1 = "darwin" ]]; then
|
2015-04-03 07:44:27 +00:00
|
|
|
eval $__out=`sw_vers -productVersion | awk -F '.' '{print $1 "." $2}'`
|
2015-08-19 04:50:30 +00:00
|
|
|
elif [[ $1 = "fedora" ]]; then
|
|
|
|
eval $__out=`cat /etc/system-release | cut -d" " -f3`
|
2015-08-14 20:23:08 +00:00
|
|
|
elif [[ $1 = "debian" ]]; then
|
|
|
|
eval $__out="`lsb_release -cs`"
|
2015-08-19 04:50:30 +00:00
|
|
|
elif [[ $1 = "freebsd" ]]; then
|
|
|
|
eval $__out=`uname -r | awk -F '-' '{print $1}'`
|
2014-11-12 05:34:59 +00:00
|
|
|
else
|
2015-04-03 07:44:27 +00:00
|
|
|
eval $__out="unknown_version"
|
2014-11-12 05:34:59 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2015-04-03 07:44:27 +00:00
|
|
|
function _distro() {
|
|
|
|
distro $1 DISTRO
|
|
|
|
echo $DISTRO
|
|
|
|
}
|
|
|
|
|
2014-09-25 08:45:13 +00:00
|
|
|
function threads() {
|
2015-04-03 07:44:27 +00:00
|
|
|
local __out=$1
|
2014-09-25 08:45:13 +00:00
|
|
|
platform OS
|
2015-05-13 06:46:02 +00:00
|
|
|
if [[ $FAMILY = "redhat" ]] || [[ $FAMILY = "debian" ]]; then
|
2015-04-03 07:44:27 +00:00
|
|
|
eval $__out=`cat /proc/cpuinfo | grep processor | wc -l`
|
2014-09-25 08:45:13 +00:00
|
|
|
elif [[ $OS = "darwin" ]]; then
|
2015-04-03 07:44:27 +00:00
|
|
|
eval $__out=`sysctl hw.ncpu | awk '{print $2}'`
|
2014-11-12 19:15:17 +00:00
|
|
|
elif [[ $OS = "freebsd" ]]; then
|
2015-04-03 07:44:27 +00:00
|
|
|
eval $__out=`sysctl -n kern.smp.cpus`
|
2015-04-30 22:47:33 +00:00
|
|
|
else
|
|
|
|
eval $__out=1
|
2014-09-25 08:45:13 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function log() {
|
|
|
|
echo "[+] $1"
|
|
|
|
}
|
|
|
|
|
|
|
|
function fatal() {
|
|
|
|
echo "[!] $1"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
function set_cxx() {
|
|
|
|
export CXX=$1
|
|
|
|
export CMAKE_CXX_COMPILER=$1
|
|
|
|
}
|
|
|
|
|
|
|
|
function add_cxx_flag() {
|
|
|
|
export CXXFLAGS="$CXXFLAGS $1"
|
|
|
|
export CMAKE_CXX_FLAGS="$CMAKE_CXX_FLAGS $1"
|
|
|
|
}
|
|
|
|
|
|
|
|
function set_cc() {
|
|
|
|
export CC=$1
|
|
|
|
export CMAKE_C_COMPILER=$1
|
|
|
|
}
|
2014-10-03 08:02:58 +00:00
|
|
|
|
|
|
|
function contains_element() {
|
|
|
|
local e
|
|
|
|
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
|
|
|
|
return 1
|
|
|
|
}
|
2015-05-15 00:38:54 +00:00
|
|
|
|
|
|
|
function in_ec2() {
|
|
|
|
if [[ -d /home/ec2-user ]]; then
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
2016-01-12 03:32:33 +00:00
|
|
|
|
|
|
|
function build_kernel_cleanup() {
|
|
|
|
# Cleanup kernel
|
|
|
|
$MAKE kernel-unload || sudo reboot
|
|
|
|
}
|
|
|
|
|
|
|
|
function build() {
|
|
|
|
threads THREADS
|
|
|
|
platform PLATFORM
|
|
|
|
distro $PLATFORM DISTRO
|
|
|
|
|
|
|
|
# Build kernel extension/module and tests.
|
|
|
|
BUILD_KERNEL=0
|
|
|
|
if [[ "$PLATFORM" = "darwin" ]]; then
|
|
|
|
if [[ "$DISTRO" = "10.10" ]]; then
|
|
|
|
BUILD_KERNEL=1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
MAKE=make
|
|
|
|
if [[ "$PLATFORM" = "freebsd" ]]; then
|
|
|
|
MAKE=gmake
|
|
|
|
fi
|
|
|
|
|
|
|
|
RUN_TESTS=$1
|
|
|
|
|
|
|
|
cd $SCRIPT_DIR/../
|
|
|
|
|
|
|
|
# Run build host provisions and install library dependencies.
|
|
|
|
$MAKE deps
|
|
|
|
|
|
|
|
# Clean previous build artifacts.
|
|
|
|
$MAKE clean
|
|
|
|
|
|
|
|
# Build osquery.
|
|
|
|
$MAKE -j$THREADS
|
|
|
|
|
|
|
|
if [[ $BUILD_KERNEL = 1 ]]; then
|
|
|
|
# Build osquery kernel (optional).
|
|
|
|
$MAKE kernel-build
|
|
|
|
|
|
|
|
# Setup cleanup code for catastrophic test failures.
|
|
|
|
trap build_kernel_cleanup EXIT INT TERM
|
|
|
|
|
|
|
|
# Load osquery kernel (optional).
|
|
|
|
$MAKE kernel-load
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ $RUN_TESTS = true ]]; then
|
|
|
|
# Request that tests include addition 'release' or 'package' units.
|
|
|
|
export RUN_RELEASE_TESTS=1
|
|
|
|
|
|
|
|
# Run code unit and integration tests.
|
|
|
|
$MAKE test/fast
|
|
|
|
|
|
|
|
if [[ $BUILD_KERNEL = 1 ]]; then
|
|
|
|
# Run kernel unit and integration tests (optional).
|
|
|
|
$MAKE kernel-test/fast
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|