Skip to content

Commit

Permalink
[osx] split GUI and agent
Browse files Browse the repository at this point in the history
See DataDog/dd-agent#1361 for more details about the
agent.
Consequences for dd-agent-omnibus:
- software definition of `datadog-agent`:
  * ship `datadog-agent` command line tool in install_dir/bin
  * delete GUI-only packages after app creation
  * do not delete anymore install_dir/{agent,embedded}
  * create and fill install_dir/etc conf directory
- preinst script:
  * use new conf dir `/opt/datadog-agent/etc`
  * delete agent pyc files
- postinst script:
  * some more logs for easier debug (logs are only visible in
  /var/log/datadog/preinstall.log)
  * use new conf dir
  • Loading branch information
degemer committed Jun 22, 2015
1 parent e512889 commit caae886
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 85 deletions.
53 changes: 32 additions & 21 deletions config/software/datadog-agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
always_build true

env = {
"PATH" => "#{install_dir}/embedded/bin/:#{ENV['PATH']}"
'PATH' => "#{install_dir}/embedded/bin/:#{ENV['PATH']}"
}

app_temp_dir = "#{install_dir}/agent/dist/Datadog\\ Agent.app/Contents"
pyside_build_dir = "#{install_dir}/agent/build/bdist.macosx-10.5-intel/python2.7-standalone/app/collect/PySide"
command_fix_shiboken = "install_name_tool -change @rpath/libshiboken-python2.7.1.2.dylib"\
" @executable_path/../Frameworks/libshiboken-python2.7.1.2.dylib "
command_fix_pyside = "install_name_tool -change @rpath/libpyside-python2.7.1.2.dylib"\
" @executable_path/../Frameworks/libpyside-python2.7.1.2.dylib "\
command_fix_shiboken = 'install_name_tool -change @rpath/libshiboken-python2.7.1.2.dylib'\
' @executable_path/../Frameworks/libshiboken-python2.7.1.2.dylib '
command_fix_pyside = 'install_name_tool -change @rpath/libpyside-python2.7.1.2.dylib'\
' @executable_path/../Frameworks/libpyside-python2.7.1.2.dylib '\

build do
license 'https://raw.githubusercontent.com/DataDog/dd-agent/master/LICENSE'
Expand Down Expand Up @@ -74,30 +74,29 @@

# Mac
else
# Command line tool
command "cp packaging/osx/datadog-agent #{install_dir}/bin"
command "chmod 755 #{install_dir}/bin/datadog-agent"

# GUI
command "cp -R packaging/datadog-agent/win32/install_files/guidata/images #{install_dir}/agent"
command "cp win32/gui.py #{install_dir}/agent"
command "cp win32/status.html #{install_dir}/agent"
command "mkdir -p #{install_dir}/agent/packaging"
command "cp packaging/osx/app/* #{install_dir}/agent/packaging"

# Shipping supervisor
command "cp #{install_dir}/embedded/lib/python2.7/site-packages/supervisor-*/supervisor/supervisor{d,ctl}.py"\
" #{install_dir}/agent"
command "cd #{install_dir}/agent && "\
"#{install_dir}/embedded/bin/python #{install_dir}/agent/setup.py py2app"\
" && cd -", :env => env
command "cp #{install_dir}/bin/gohai #{app_temp_dir}/MacOS"
command "cp packaging/osx/datadog-agent #{app_temp_dir}/MacOS"
command "chmod a+x #{app_temp_dir}/MacOS/datadog-agent"
' && cd -', env: env

# Time to patch the install, see py2app bug: (dependencies to system PySide)
# https://bitbucket.org/ronaldoussoren/py2app/issue/143/resulting-app-mistakenly-looks-for-pyside
command "cp #{pyside_build_dir}/libshiboken-python2.7.1.2.dylib #{app_temp_dir}/Frameworks"
command "cp #{pyside_build_dir}/libpyside-python2.7.1.2.dylib #{app_temp_dir}/Frameworks"
command "chmod a+x #{app_temp_dir}/Frameworks/{libpyside,libshiboken}-python2.7.1.2.dylib"
command "#{command_fix_shiboken} #{app_temp_dir}/Frameworks/libpyside-python2.7.1.2.dylib"
command "install_name_tool -change /usr/local/lib/QtCore.framework/Versions/4/QtCore "\
"@executable_path/../Frameworks/QtCore.framework/Versions/4/QtCore "\
command 'install_name_tool -change /usr/local/lib/QtCore.framework/Versions/4/QtCore '\
'@executable_path/../Frameworks/QtCore.framework/Versions/4/QtCore '\
"#{app_temp_dir}/Frameworks/libpyside-python2.7.1.2.dylib"
command "#{command_fix_shiboken} #{app_temp_dir}/Resources/lib/python2.7/lib-dynload/PySide/QtCore.so"
command "#{command_fix_shiboken} #{app_temp_dir}/Resources/lib/python2.7/lib-dynload/PySide/QtGui.so"
Expand All @@ -107,12 +106,24 @@
# And finally
command "mv #{install_dir}/agent/dist/Datadog\\ Agent.app #{install_dir}"

command "cp packaging/osx/supervisor.conf #{install_dir}/Datadog\\ Agent.app/Contents/Resources"
command "cp datadog.conf.example #{install_dir}/Datadog\\ Agent.app/Contents/Resources/datadog.conf.example"
command "cp -R conf.d #{install_dir}/Datadog\\ Agent.app/Contents/Resources/"
command "cp packaging/osx/com.datadoghq.Agent.plist.example #{install_dir}/Datadog\\ Agent.app/Contents/Resources/"
command "mv #{install_dir}/licenses #{install_dir}/Datadog\\ Agent.app/Contents/Resources/"
command "mv #{install_dir}/sources #{install_dir}/Datadog\\ Agent.app/Contents/Resources/"
command "rm -rf #{install_dir}/agent #{install_dir}/embedded #{install_dir}/bin"
# Clean GUI related things
%w(build dist images gui.py status.html packaging Datadog_Agent.egg-info).each do |file|
command "rm -rf #{install_dir}/agent/#{file}"
end
%w(py2app macholib modulegraph altgraph).each do |package|
command "yes | #{install_dir}/embedded/bin/pip uninstall #{package}"
end
%w(pyside guidata spyderlib).each do |dependency_name|
# Installed with `python setup.py install`, needs to be uninstalled manually
command "cat #{install_dir}/embedded/#{dependency_name}-files.txt | xargs rm -rf \"{}\""
command "rm -f #{install_dir}/embedded/#{dependency_name}-files.txt"
end

# conf
command "mkdir -p #{install_dir}/etc"
command "grep -v 'user=dd-agent' packaging/supervisor.conf > #{install_dir}/etc/supervisor.conf"
command "cp datadog.conf.example #{install_dir}/etc/datadog.conf.example"
command "cp -R conf.d #{install_dir}/etc/"
command "cp packaging/osx/com.datadoghq.Agent.plist.example #{install_dir}/etc/"
end
end
121 changes: 74 additions & 47 deletions package-scripts/datadog-agent/postinstall
Original file line number Diff line number Diff line change
Expand Up @@ -3,96 +3,123 @@
LOG_DIR=/var/log/datadog
INSTALL_DIR=/opt/datadog-agent
OPT_APP_DIR="$INSTALL_DIR/Datadog Agent.app"
APP_DIRECTORY="/Applications/Datadog Agent.app"
APP_CONF_DIR="$APP_DIRECTORY/Contents/Resources"
RUN_DIR="$APP_CONF_DIR/run"
APP_DIR="/Applications/Datadog Agent.app"
CONF_DIR=$INSTALL_DIR/etc
RUN_DIR=$INSTALL_DIR/run

# We log stdout & stderr of this script
LOG_FILE=$LOG_DIR/postinstall.log
mkdir -p $LOG_DIR
mkdir -vp $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
echo "# State at the beginning"
echo "## Agent version"
grep AGENT_VERSION $INSTALL_DIR/agent/config.py || echo "No config file"
echo "## $INSTALL_DIR"
ls -al $INSTALL_DIR || echo "No agent installed"
echo "## $APP_DIR/Contents/Resources"
ls -al "$APP_DIR/Contents/Resources" || echo "No app installed"

echo "# Determining install user"
# Determine current user if he is using the Graphical installer
INSTALL_USER=$(ps aux | grep "CoreServices/Installer" | grep -v grep | awk '{print $1;}')

# Otherwise, we hope he is using the install script and try to this user
# If it fails, no choice but to use root :'(
if [ -z "$INSTALL_USER" ] || [ "$INSTALL_USER" == "root" ]; then
SCRIPT_INSTALL="yes"
INSTALL_USER=`cat /tmp/datadog-install-user || echo 'root'`
rm /tmp/datadog-install-user || true
rm -v /tmp/datadog-install-user || true
fi
echo "INSTALL_USER: $INSTALL_USER"

if [ -e "$OPT_APP_DIR" ]; then
# Prepare log dir
chown -R $INSTALL_USER:admin $LOG_DIR
chmod 755 $LOG_DIR
echo "# Preparing log dir"
# Prepare log dir
chown -vR $INSTALL_USER:admin $LOG_DIR
chmod -v 755 $LOG_DIR

# Installing the app
mv $INSTALL_DIR/Datadog\ Agent.app /Applications
fi
echo "# Installing the app"
# Installing the app
mv -v "$OPT_APP_DIR" /Applications || echo "App already installed"

if [ "$INSTALL_USER" == "root" ]; then
echo 'INSTALL_USER is set to root, Datadog Agent app has been installed'
echo 'but is not configured. Running Datadog Agent as root is not advised!'
exit 1
fi
echo "# Preparing the run directory"
# Set the run directory for the agent
mkdir -vp "$RUN_DIR"
chown -vR $INSTALL_USER:admin "$RUN_DIR"
chmod -v 755 "$RUN_DIR"

# Set the run directory for supervisord
mkdir -p "$RUN_DIR"
chown -R $INSTALL_USER:admin "$RUN_DIR"
chmod 755 "$RUN_DIR"
echo "# Creating default plist"
# Prepare plist
sed "s|USER_NAME|$INSTALL_USER|" $CONF_DIR/com.datadoghq.agent.plist.example > $CONF_DIR/com.datadoghq.agent.plist

echo "# Copying conf"
# Create directory for custom checks
mkdir -vp $CONF_DIR/checks.d

sed "s|USER_NAME|$INSTALL_USER|" "$APP_CONF_DIR/com.datadoghq.Agent.plist.example" > "$APP_CONF_DIR/com.datadoghq.Agent.plist"

# Copying existing conf is one exists
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
mv -vf /tmp/datadog.conf $CONF_DIR
# We only saved *.yaml files
mv -vf /tmp/conf.d/* $CONF_DIR/conf.d
cp -vn /tmp/checks.d/* $CONF_DIR/checks.d
rm -vrf /tmp/datadog.conf /tmp/conf.d /tmp/checks.d
# Or copying default
else
sed -E 's/^api_key:$/api_key: APIKEY/' "$APP_CONF_DIR/datadog.conf.example" > "$APP_CONF_DIR/datadog.conf"
sed -E 's/^api_key:$/api_key: APIKEY/' $CONF_DIR/datadog.conf.example > $CONF_DIR/datadog.conf
fi

echo "# Setting correct rights on conf"
# Correct rights
chown $INSTALL_USER:admin "$APP_CONF_DIR/datadog.conf"
chown -R $INSTALL_USER:admin "$APP_CONF_DIR/conf.d" "$APP_CONF_DIR/checks.d"
chown -v $INSTALL_USER:admin $CONF_DIR/datadog.conf
chown -vR $INSTALL_USER:admin $CONF_DIR/conf.d $CONF_DIR/checks.d

echo "# add link to datadog-agent for user"
# `datadog-agent` command line
mkdir -p /usr/local/bin
ln -s "$APP_CONF_DIR/../MacOS/datadog-agent" /usr/local/bin/datadog-agent
mkdir -vp /usr/local/bin
ln -vs $INSTALL_DIR/bin/datadog-agent /usr/local/bin/datadog-agent

echo "# link to ~/.datadog-agent for the user"
# Link for conf files (let's ease the user's life)
USER_HOME=`sudo -Hu $INSTALL_USER sh -c 'echo $HOME'`
sudo -Hu $INSTALL_USER mkdir -p "$USER_HOME/.datadog-agent"
rm -f "$USER_HOME/.datadog-agent/conf.d" "$USER_HOME/.datadog-agent/datadog.conf" "$USER_HOME/.datadog-agent/checks.d"
sudo -Hu $INSTALL_USER ln -s "$APP_CONF_DIR/conf.d" "$USER_HOME/.datadog-agent/conf.d"
sudo -Hu $INSTALL_USER ln -s "$APP_CONF_DIR/datadog.conf" "$USER_HOME/.datadog-agent/datadog.conf"
sudo -Hu $INSTALL_USER ln -s "$APP_CONF_DIR/checks.d" "$USER_HOME/.datadog-agent/checks.d"

# Error if app not properly installed
if [ ! -e "$APP_CONF_DIR/datadog.conf" ]; then
sudo -Hu $INSTALL_USER mkdir -vp "$USER_HOME/.datadog-agent"
rm -vf "$USER_HOME/.datadog-agent/conf.d" "$USER_HOME/.datadog-agent/datadog.conf" "$USER_HOME/.datadog-agent/checks.d"
sudo -Hu $INSTALL_USER ln -vs $CONF_DIR/conf.d "$USER_HOME/.datadog-agent/conf.d"
sudo -Hu $INSTALL_USER ln -vs $CONF_DIR/datadog.conf "$USER_HOME/.datadog-agent/datadog.conf"
sudo -Hu $INSTALL_USER ln -vs $CONF_DIR/checks.d "$USER_HOME/.datadog-agent/checks.d"

# Error if app not properly installed or root
if [ "$INSTALL_USER" == "root" ]; then
echo 'INSTALL_USER is set to root, Datadog Agent app has been installed'
echo 'but is not configured. Running Datadog Agent as root is not advised!'
exit 1
fi

if [ ! -e $CONF_DIR/datadog.conf ]; then
exit 1
fi

# Start the app only if it's not a script install
if [ -z "$SCRIPT_INSTALL" ]; then
# Trick to get the right temp dir (for status pickles)
export TMPDIR=`sudo -u $INSTALL_USER getconf DARWIN_USER_TEMP_DIR`
sudo -Eu $INSTALL_USER open -a 'Datadog Agent.app'
echo "# Starting the app"
# -a for application, -F for fresh, do not restore old app
sudo -u $INSTALL_USER open -Fa 'Datadog Agent'
fi

echo "# Configuring the login launch of the app"
# And finally configure the app to be launched at login
sudo -u $INSTALL_USER osascript -e 'tell application "System Events" to delete every login item whose name is "Datadog Agent"'
# It uses the hardcoded path instead of $APP_DIRECTORY because
# osascript wants '' (bash doesn't interpolate $APP_DIRECTORY) and no ""
sudo -u $INSTALL_USER osascript -e 'tell application "System Events" to make login item at end with properties {path:"/Applications/Datadog Agent.app", name:"Datadog Agent", hidden:false}'

echo "# State at the end"
echo "## Agent version"
grep AGENT_VERSION $INSTALL_DIR/agent/config.py || echo "No config file"
echo "## $INSTALL_DIR"
ls -al $INSTALL_DIR || echo "No agent installed"
echo "## $APP_DIR/Contents/Resources"
ls -al "$APP_DIR/Contents/Resources" || echo "No app installed"
fi
exit 0

53 changes: 36 additions & 17 deletions package-scripts/datadog-agent/preinstall
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
INSTALL_DIR=/opt/datadog-agent
LOG_DIR=/var/log/datadog
APP_DIR="/Applications/Datadog Agent.app"
APP_CONF_DIR="$APP_DIR/Contents/Resources"
DD_COMMAND="/opt/datadog-agent/bin/datadog-agent"
CONF_DIR="/opt/datadog-agent/etc"

# We log stdout & stderr of this script
LOG_FILE=$LOG_DIR/preinstall.log
Expand All @@ -14,24 +15,42 @@ exec > $LOG_FILE 2>&1
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
kill `ps aux | grep 'Datadog Agent.app' | grep -v grep | cut -d ' ' -f 4` || true

# Save old conf
mkdir -p /tmp/conf.d
rm -rf /tmp/checks.d
rm -f /tmp/datadog.conf /tmp/conf.d/*
cp -f "$APP_CONF_DIR/datadog.conf" /tmp
cp -f "$APP_CONF_DIR/conf.d/"*.yaml /tmp/conf.d
cp -fR "$APP_CONF_DIR/checks.d" /tmp/
if [ -e "$CONF_DIR/datadog.conf" ]; then
echo "# State at the beginning"
echo "## Agent version"
grep AGENT_VERSION $INSTALL_DIR/agent/config.py || echo "No config file"
echo "## $INSTALL_DIR"
ls -al $INSTALL_DIR || echo "No agent installed"
echo "## $APP_DIR/Contents/Resources"
ls -al "$APP_DIR/Contents/Resources" || echo "No app installed"

echo '# Stop old agent'
$DD_COMMAND stop || true

echo '# Stop old GUI'
kill `ps aux | grep 'Datadog Agent.app' | grep -v grep | awk '{ print $2 }'` || true

echo '# Save old conf'
mkdir -vp /tmp/{conf,checks}.d
rm -vf /tmp/{conf,checks}.d/* /tmp/datadog.conf
cp -vf $CONF_DIR/datadog.conf /tmp
cp -vf $CONF_DIR/conf.d/*.yaml /tmp/conf.d
cp -vf $CONF_DIR/checks.d/* /tmp/checks.d
fi

# Clean before install
rm -rf $INSTALL_DIR
rm -f /usr/local/bin/datadog-agent
echo '# Deleting old datadog-agent link'
rm -vf /usr/local/bin/datadog-agent

# Clean .pyc files
echo '# Deleting pyc files'
find /opt/datadog-agent/agent -name '*.py[co]' -type f -delete || true

# So sometimes the upgrade process works, sometimes not, so let's kill it with fire to be sure
rm -rf "$APP_DIR"
echo "# State at the end"
echo "## Agent version"
grep AGENT_VERSION $INSTALL_DIR/agent/config.py || echo "No config file"
echo "## $INSTALL_DIR"
ls -al $INSTALL_DIR || echo "No agent installed"
echo "## $APP_DIR/Contents/Resources"
ls -al "$APP_DIR/Contents/Resources" || echo "No app installed"
fi

0 comments on commit caae886

Please sign in to comment.