Skip to content

Commit

Permalink
[osx] Log to file pre/postinstall scripts
Browse files Browse the repository at this point in the history
- set rights to current user
- launch the app at the end of the install
- doesn't install if smae version and already installed
  • Loading branch information
degemer committed Mar 4, 2015
1 parent b3b7a81 commit 28f34e5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 31 deletions.
66 changes: 37 additions & 29 deletions package-scripts/datadog-agent/postinstall
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,57 @@

LOG_DIR=/var/log/datadog
INSTALL_DIR=/opt/datadog-agent
CONFIG_DIR=$INSTALL_DIR/Datadog\ Agent.app/Contents/Resources
OPT_APP_DIR="$INSTALL_DIR/Datadog Agent.app"
APP_CONF_DIR="/Applications/Datadog Agent.app/Contents/Resources"

# OS/Distro Detection
DISTRIBUTION=$(grep -Eo "(Debian|Ubuntu|RedHat|CentOS|openSUSE|Amazon)" /etc/issue 2>/dev/null || uname -s)
# We log stdout & stderr of this script
LOG_FILE=$LOG_DIR/postinstall.log
mkdir -p $LOG_DIR
exec > $LOG_FILE 2>&1

error_exit()
{
echo "${PROGNAME}: ${1:-"Unknown Error"}" 1>&2
exit 1
}
# Determine current user
CURRENT_USER=$(ps aux | grep "CoreServices/Installer" | grep -v grep | awk '{print $1;}')

# OS/Distro Detection
DISTRIBUTION=$(grep -Eo "(Debian|Ubuntu|RedHat|CentOS|openSUSE|Amazon)" /etc/issue 2>/dev/null || uname -s)

if [ $DISTRIBUTION = "Darwin" ]; then
# Prepare log dir
mkdir -p ${LOG_DIR} || error_exit "Cannot create ${LOG_DIR}!"
chown -R root:admin $LOG_DIR
chmod 775 $LOG_DIR
touch $LOG_DIR/collector.log $LOG_DIR/forwarder.log $LOG_DIR/dogstatsd.log $LOG_DIR/supervisord.log
chown root:admin $LOG_DIR/collector.log $LOG_DIR/forwarder.log $LOG_DIR/dogstatsd.log $LOG_DIR/supervisord.log
chmod 664 $LOG_DIR/collector.log $LOG_DIR/forwarder.log $LOG_DIR/dogstatsd.log $LOG_DIR/supervisord.log

sed "s|AGENT_BASE|$INSTALL_DIR|; s|USER_NAME|$(whoami)|" $INSTALL_DIR/Datadog\ Agent.app/Contents/Resources/com.datadoghq.Agent.plist.example > $INSTALL_DIR/Datadog\ Agent.app/Contents/Resources/com.datadoghq.Agent.plist

# Reinstall is really different:
# the app is extracted directly where the old version is
if [ -e "$OPT_APP_DIR" ]; then
# Prepare log dir
chown -R $CURRENT_USER:admin $LOG_DIR
chmod 755 $LOG_DIR

# Installing the app
rm -rf /Applications/Datadog\ Agent.app
mv $INSTALL_DIR/Datadog\ Agent.app /Applications
fi

sed "s|AGENT_BASE|$INSTALL_DIR|; s|USER_NAME|$CURRENT_USER|" $INSTALL_DIR/Datadog\ Agent.app/Contents/Resources/com.datadoghq.Agent.plist.example > $INSTALL_DIR/Datadog\ Agent.app/Contents/Resources/com.datadoghq.Agent.plist

# Copying existing conf is one exists
if [ -e "$APP_CONF_DIR/datadog.conf" ]; then
cp "$APP_CONF_DIR/datadog.conf" "$CONFIG_DIR/"
if [ -e "/tmp/datadog.conf" ]; then
mv -f "/tmp/datadog.conf" "$APP_CONF_DIR/"
mv -f "/tmp/conf.d/*" "$APP_CONF_DIR/conf.d"
cp -n "/tmp/checks.d/*" "$APP_CONF_DIR/checks.d"
rm -rf /tmp/datadog.conf /tmp/conf.d /tmp/checks.d
# Or copying default
else
cp "$CONFIG_DIR/datadog.conf.example" "$CONFIG_DIR/datadog.conf"
cp "$APP_CONF_DIR/datadog.conf.example" "$APP_CONF_DIR/datadog.conf"
fi

# Correct rights
chown root:admin "$CONFIG_DIR/datadog.conf"
chown -R root:admin "$CONFIG_DIR/datadog.conf" "$CONFIG_DIR/conf.d" "$CONFIG_DIR/checks.d"
chmod 775 "$CONFIG_DIR/conf.d" "$CONFIG_DIR/checks.d"
chmod 664 "$CONFIG_DIR/datadog.conf"
chmod 664 "$CONFIG_DIR/datadog.conf.example" "$CONFIG_DIR/supervisor.conf" "$CONFIG_DIR/conf.d/"*
chown $CURRENT_USER:admin "$APP_CONF_DIR/datadog.conf"
chown -R $CURRENT_USER:admin "$APP_CONF_DIR/conf.d" "$APP_CONF_DIR/checks.d"

# Error if app not properly installed
if [ ! -e "$APP_CONF_DIR/datadog.conf" ]; then
exit 1
fi

# Installing the app
rm -rf /Applications/Datadog\ Agent.app
mv $INSTALL_DIR/Datadog\ Agent.app /Applications
# Launch the app
sudo -u $CURRENT_USER "$APP_CONF_DIR/../MacOS/datadog-agent" start || true
sudo -u $CURRENT_USER open -a 'Datadog Agent.app'
fi
exit 0
21 changes: 19 additions & 2 deletions package-scripts/datadog-agent/preinstall
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
#!/bin/sh

INSTALL_DIR=/opt/datadog-agent
LOG_DIR=/var/log/datadog
APP_CONF_DIR="/Applications/Datadog Agent.app/Contents/Resources"

# We log stdout & stderr of this script
LOG_FILE=$LOG_DIR/preinstall.log
mkdir -p $LOG_DIR
exec > $LOG_FILE 2>&1

# OS/Distro Detection
DISTRIBUTION=$(grep -Eo "(Debian|Ubuntu|RedHat|CentOS|openSUSE|Amazon)" /etc/issue 2>/dev/null || uname -s)


if [ $DISTRIBUTION = "Darwin" ]; then
if [ -e "$APP_CONF_DIR/datadog.conf" ]; then
# Stop old version
"$APP_CONF_DIR/../MacOS/datadog-agent" stop || true
osascript -e 'quit app "Datadog Agent"' || true

# Save old conf
cp -f "$APP_CONF_DIR/datadog.conf" /tmp
cp -fR "$APP_CONF_DIR/checks.d" /tmp
cp -fR "$APP_CONF_DIR/conf.d" /tmp
fi

# Clean before install
rm -rf $INSTALL_DIR
fi
exit 0

0 comments on commit 28f34e5

Please sign in to comment.