osquery-1/tools/deployment/make_osx_package.sh

189 lines
5.7 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# Copyright (c) 2014, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.
set -e
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
2014-12-04 17:33:04 +00:00
SOURCE_DIR="$SCRIPT_DIR/../.."
BUILD_DIR="$SOURCE_DIR/build/darwin"
export PATH="$PATH:/usr/local/bin"
2014-12-04 17:33:04 +00:00
source $SCRIPT_DIR/../lib.sh
2014-10-04 01:09:37 +00:00
2014-12-04 17:33:04 +00:00
APP_VERSION=`git describe --tags HEAD`
2014-12-10 01:30:43 +00:00
APP_IDENTIFIER="com.facebook.osquery"
OUTPUT_PKG_PATH="$SOURCE_DIR/osquery-$APP_VERSION.pkg"
2014-10-04 01:09:37 +00:00
LAUNCHD_PATH="$SCRIPT_DIR/$APP_IDENTIFIER.plist"
LAUNCHD_PATH_OVERRIDE=""
2014-10-04 01:09:37 +00:00
LAUNCHD_INSTALL_PATH="/Library/LaunchDaemons/$APP_IDENTIFIER.plist"
2014-11-18 20:54:05 +00:00
INCLUDE_LAUNCHD=true
2014-12-04 17:33:04 +00:00
SIMPLE_INSTALL=false
2014-10-04 01:09:37 +00:00
OSQUERY_LOG_DIR="/var/log/osquery/"
OSQUERY_CONFIG_PATH_DEST="/var/osquery/osquery.conf"
OSQUERY_CONFIG_PATH_SOURCE=""
BREW_PACKAGES=(rocksdb boost gflags glog thrift)
BREW_PREFIX=`brew --prefix`
BREW_CELLAR=`brew --cellar`
2014-10-04 01:09:37 +00:00
WORKING_DIR=/tmp/osquery_packaging
INSTALL_PREFIX=$WORKING_DIR/prefix
SCRIPT_ROOT=$WORKING_DIR/scripts
PREINSTALL=$SCRIPT_ROOT/preinstall
POSTINSTALL=$SCRIPT_ROOT/postinstall
SCRIPT_PREFIX_TEXT="#!/usr/bin/env bash
set -e
"
POSTINSTALL_ADDITIONAL_TEXT="
if launchctl list | grep -qcm1 osquery; then
launchctl unload $LAUNCHD_INSTALL_PATH
fi
launchctl load $LAUNCHD_INSTALL_PATH
"
function usage() {
fatal "Usage: $0 -c path/to/your/osquery.conf"
}
function parse_args() {
while [ "$1" != "" ]; do
case $1 in
-c | --config ) shift
OSQUERY_CONFIG_PATH_SRC=$1
;;
2014-11-18 20:54:05 +00:00
-l | --launchd-path ) shift
LAUNCHD_PATH_OVERRIDE=$1
;;
2014-12-04 17:33:04 +00:00
-s | --simple ) SIMPLE_INSTALL=true
;;
2014-11-18 20:54:05 +00:00
-n | --no-launchd ) INCLUDE_LAUNCHD=false
;;
2014-10-04 01:09:37 +00:00
-h | --help ) usage
;;
* ) usage
esac
shift
done
}
function check_parsed_args() {
if [[ $OSQUERY_CONFIG_PATH_SRC = "" ]]; then
log "no config specified. assuming that you know what you're doing."
2014-10-04 01:09:37 +00:00
fi
2014-11-18 20:54:05 +00:00
if [[ $INCLUDE_LAUNCHD = true ]]; then
if [[ $LAUNCHD_PATH_OVERRIDE = "" ]]; then
log "no custom launchd path was defined. using $LAUNCHD_PATH"
else
LAUNCHD_PATH=$LAUNCHD_PATH_OVERRIDE
log "using $LAUNCHD_PATH as the launchd path"
fi
fi
if [ "$OSQUERY_CONFIG_PATH_SRC" != "" ] && [ ! -f $OSQUERY_CONFIG_PATH_SRC ]; then
2014-10-04 01:09:37 +00:00
log "$OSQUERY_CONFIG_PATH_SRC is not a file"
usage
fi
}
2014-10-03 20:29:46 +00:00
function main() {
2014-10-04 01:09:37 +00:00
parse_args $@
check_parsed_args
platform OS
if [[ ! "$OS" = "darwin" ]]; then
fatal "This script must be ran on OS X"
fi
2014-10-04 01:09:37 +00:00
rm -rf $WORKING_DIR
rm -f $OUTPUT_PKG_PATH
mkdir -p $INSTALL_PREFIX
mkdir -p $SCRIPT_ROOT
# we don't need the preinstall for anything so let's skip it until we do
# echo "$SCRIPT_PREFIX_TEXT" > $PREINSTALL
# chmod +x $PREINSTALL
2014-10-04 01:09:37 +00:00
echo "$SCRIPT_PREFIX_TEXT" > $POSTINSTALL
chmod +x $POSTINSTALL
if [[ $SIMPLE_INSTALL = false ]]; then
log "calculating dependency tree"
dependency_list=("${BREW_PACKAGES[@]}")
for package in ${BREW_PACKAGES[*]}; do
for dep in `brew deps $package`; do
if ! contains_element $dep "${dependency_list[@]}"; then
if [[ "$dep" != "openssl" ]]; then dependency_list+=($dep); fi
fi
done
done
log "calculating library dependencies"
for dep in ${dependency_list[*]}; do
dep_dir=`brew info $dep | grep Cellar | grep '*' | awk '{print $1}'`
brew unlink $dep 2>&1 1>/dev/null
2014-12-04 17:33:04 +00:00
links=`brew link --dry-run $dep | sed 1d`
brew link --overwrite $dep 2>&1 1>/dev/null
echo " - $dep ($dep_dir)"
mkdir -p $INSTALL_PREFIX`dirname $dep_dir`
cp -r $dep_dir $INSTALL_PREFIX`dirname $dep_dir`
mkdir -p "$INSTALL_PREFIX$BREW_PREFIX/Library/Formula"
cp "$BREW_PREFIX/Library/Formula/$dep.rb" "$INSTALL_PREFIX$BREW_PREFIX/Library/Formula/$dep.rb"
for link in $links; do
# Skip if this link was not in the brew prefix.
2014-12-10 01:30:43 +00:00
if [[ ! $link = $BREW_PREFIX* ]]; then continue; fi
target="`dirname $link`/`ls -l $link | awk '{print $11}'`"
echo "if [ ! -e `dirname $link` ]; then rm -f `dirname $link`; fi" >> $POSTINSTALL
echo "mkdir -p `dirname $link`" >> $POSTINSTALL
echo "rm -rf $link" >> $POSTINSTALL
echo "ln -s $target $link" >> $POSTINSTALL
echo "" >> $POSTINSTALL
done
done
fi
2014-10-04 01:09:37 +00:00
log "copying osquery binaries"
BINARY_INSTALL_DIR="$INSTALL_PREFIX/usr/local/bin/"
mkdir -p $BINARY_INSTALL_DIR
2014-12-04 17:33:04 +00:00
cp "$BUILD_DIR/osquery/osqueryi" $BINARY_INSTALL_DIR
cp "$BUILD_DIR/osquery/osqueryd" $BINARY_INSTALL_DIR
2014-10-04 01:09:37 +00:00
mkdir -p $INSTALL_PREFIX/$OSQUERY_LOG_DIR
mkdir -p `dirname $INSTALL_PREFIX$OSQUERY_CONFIG_PATH_DEST`
if [[ "$OSQUERY_CONFIG_PATH_SRC" != "" ]]; then
cp $OSQUERY_CONFIG_PATH_SRC $INSTALL_PREFIX$OSQUERY_CONFIG_PATH_DEST
fi
2014-10-04 01:09:37 +00:00
log "copying osquery configurations"
2014-11-18 20:54:05 +00:00
if [[ $INCLUDE_LAUNCHD = true ]]; then
mkdir -p `dirname $INSTALL_PREFIX$LAUNCHD_INSTALL_PATH`
cp $LAUNCHD_PATH $INSTALL_PREFIX$LAUNCHD_INSTALL_PATH
else
log "skipping LaunchDaemon file"
fi
2014-10-04 01:09:37 +00:00
log "finalizing preinstall and postinstall scripts"
2014-11-18 20:54:05 +00:00
if [[ $INCLUDE_LAUNCHD = true ]]; then
echo "$POSTINSTALL_ADDITIONAL_TEXT" >> $POSTINSTALL
else
log "skipping LaunchDaemon commands"
fi
2014-10-04 01:09:37 +00:00
log "creating package"
pkgbuild --root $INSTALL_PREFIX \
--scripts $SCRIPT_ROOT \
--identifier $APP_IDENTIFIER \
--version $APP_VERSION \
$OUTPUT_PKG_PATH 2>&1 1>/dev/null
log "package created at $OUTPUT_PKG_PATH"
}
2014-10-04 01:09:37 +00:00
main $@