Skip to content

Instantly share code, notes, and snippets.

@DavidAntaramian
Last active September 6, 2017 03:06
Show Gist options
  • Save DavidAntaramian/5375b50b117e106efc3ac1d6e5c6fd9c to your computer and use it in GitHub Desktop.
Save DavidAntaramian/5375b50b117e106efc3ac1d6e5c6fd9c to your computer and use it in GitHub Desktop.
Elasticbeanstalk extension providing Timber Agent 0.3.x with automatic key setting
###
#
# Installation configuration script for Timber.io's log collection agent
#
# * Docs: https://timber.io/docs/platforms/aws-elastic-beanstalk
# * Support: support@timber.io
#
# To use this file:
#
# 1. Set the environment variable "TIMBER_API_KEY" using `eb setenv`
# 2. Replace the `path` values with the actual log files you want to.
# Feel free to add multiple `[[files]]` entries for any additional log
# files you want to track.
# 3. This file should be added to the `.ebextensions` directory in your
# project folder.
#
---
files:
"/etc/timber.toml":
mode: "000640"
owner: root
group: root
encoding: plain
content: |
[[files]]
path = "/var/log/nodejs/nodejs.log"
[[files]]
path = "/var/log/nginx/access.log"
"/tmp/set_timber_api_keys.sh":
mode: "00755"
owner: root
group: root
encoding: plain
content: |
#!/bin/bash
timber_config_file="/etc/timber.toml"
timber_api_key=`{ "Fn::GetOptionSetting" : { "Namespace": "aws:elasticbeanstalk:application:environment", "OptionName": "TIMBER_API_KEY" } }`
sed "1i\
default_api_key = \"${timber_api_key}\"\
\n
" -i $timber_config_file
"/opt/install_timber.sh":
mode: "000755"
owner: root
group: root
encoding: plain
content: |
#!/usr/bin/env bash
#
# Elastic Beanstalk pre-deploy hook
#
# Shuts down the timber-agent service (if present) in order to allow upgrades
# to proceed smoothly; then removes the any current version of the agent and
# installs a fresh copy
#
if [ -f /etc/rc.d/init.d/timber-agent ]; then
/sbin/service timber-agent stop
fi
# Remove any existing version of the agent
rm -rf /opt/timber-agent
# Download the latest patch version of the Timber Agent with a major.minor version of 0.3
curl -o /opt/timber-agent.tar.gz \
https://s3.amazonaws.com/packages.timber.io/agent/0.3.x/linux-amd64/timber-agent-0.3.x-linux-amd64.tar.gz
# Unpack the agent archive at /opt/timber-agent
tar -xzf /opt/timber-agent.tar.gz -C /opt
rm /opt/timber-agent.tar.gz
"/etc/logrotate.d/timber":
mode: "000644"
owner: root
group: root
encoding: plain
content: |
/var/log/timber-agent.log {
missingok
notifempty
size 100K
rotate 2
postrotate
/sbin/service timber-agent condrestart
endscript
}
"/opt/elasticbeanstalk/tasks/bundlelogs.d/timber-agent.conf":
mode: "000755"
owner: root
group: root
encoding: plain
content: |
/var/log/timber-agent.log
"/etc/rc.d/init.d/timber-agent":
mode: "000755"
owner: root
group: root
encoding: plain
content: |
#!/bin/sh
#
# timber-agent Starts and stops the Timber agent as a daemonized service
#
# chkconfig: - 40 90
# description: Starts and stops the Timber agent as a daemonized service \
# to read and upload log files to the Timber hosted service. \
# Configuration of the Timber agent is stored in the file \
# /etc/timber.toml.
### BEGIN INIT INFO
# Provides: timber-agent
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Should-Start: $named $time
# Should-Stop: $named $time
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: Starts and stops the Timber agent as a daemonized service
# Description: Starts and stops the Timber agent as a daemonized service
# to read and upload log files to the Timber hosted service.
# Configuration of the Timber agent is stored in the file
# /etc/timber.toml.
### END INIT INFO
PATH=/sbin:/bin
. /etc/rc.d/init.d/functions
prog="timber-agent"
exec=/opt/timber-agent/bin/timber-agent
pidfile=/var/run/$prog.pid
logfile=/var/log/timber-agent.log
lockfile=/var/lock/subsys/$prog
start() {
[ -x $exec ] || exit 5
echo -n $"Starting $prog: "
daemon $exec --daemonize --pidfile $pidfile --agent-log-file $logfile
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc -p $pidfile $prog
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile && rm -f $pidfile
return $retval
}
restart() {
stop
start
}
reload() {
restart
}
force_reload() {
restart
}
rh_status() {
status -p $pidfile $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
restart
;;
*)
echo "Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
exit 2
;;
esac
exit $?
commands:
00_install_timber:
command: "/opt/install_timber.sh"
01_set_api_key:
command: "/tmp/set_timber_api_keys.sh"
02_chkconfig:
command: "/sbin/chkconfig timber-agent on"
03_start_service:
command: "/sbin/service timber-agent restart"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment