Skip to content

Commit

Permalink
installation script improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Roy Razon committed Apr 25, 2017
1 parent 5f2e55c commit 6689cb1
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 16 deletions.
92 changes: 78 additions & 14 deletions install.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,27 +1,91 @@
#!/bin/sh
#!/bin/bash

# Test for root
if [ "$EUID" -ne 0 ]
then echo "Please run as root (sudo)"
exit
if [[ "$EUID" -ne 0 ]]; then
echo "Please run as root (sudo). Aborting." >&2
exit 1
fi

# Check for systemd
[[ `systemctl` =~ -\.mount ]] || ( echo "systemd is required but it's not running. Aborting." >&2; exit 1 )

# Check for python2.7
command -v python2.7 >/dev/null 2>&1 || { echo "python2.7 is required but it's not installed. Aborting." >&2; exit 1; }

# Check for pip
command -v pip >/dev/null 2>&1 || { echo "pip is required but it's not installed. Aborting." >&2; exit 1; }

set -e

INSTALLATION_LOG=/tmp/jumper_agent_installation.log
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
DEST_DIR=/opt/jumper_logging_agent
FIFO_DIR=/var/run/jumper_logging_agent
SERVICE_USER=jumperagent
SERVICE_NAME=jumper-agent

if ! command -v virtualenv --version >/dev/null 2>&1; then
echo Installing virtualenv...
yes w | python2.7 -m pip --isolated -qq install virtualenv
fi

if id -u ${SERVICE_USER} >/dev/null 2>&1; then
echo Reusing user ${SERVICE_USER}
else
useradd ${SERVICE_USER} -M -s /usr/sbin/nologin -c "Jumper Logging Agent"
fi

echo Creating directories...
rm -rf ${DEST_DIR}
mkdir -p ${DEST_DIR}
mkdir -p ${FIFO_DIR}
chown ${SERVICE_USER}:${SERVICE_USER} ${FIFO_DIR}

# Copying the agent to its final destination
cp -r jumper-logging-agent /opt
COPY_FILES="jumper_logging_agent README.rst setup.py setup.cfg agent_main.py"
for FILE in "${COPY_FILES}"; do
cp -R ${SCRIPT_DIR}/${FILE} ${DEST_DIR}/
done

# Install virtualenv
yes w | pip install virtualenv
chown -R ${SERVICE_USER}:${SERVICE_USER} ${DEST_DIR}
chmod -R u+rw,g+rw ${DEST_DIR}

# Create a virtual environment and install the app
virtualenv /opt/jumper-logging-agent/venv
source /opt/jumper-logging-agentvenv/bin/activate
python setup.py install
deactivate
su -s /bin/bash ${SERVICE_USER} <<EOFSU
cd ${DEST_DIR}
# Create a virtual environment
virtualenv -p python2.7 ./venv >/dev/null
source ./venv/bin/activate
# Install dependent python packages
if ! python2.7 setup.py -q install >${INSTALLATION_LOG} 2>&1; then
echo Installation of dependent python packages failed. See ${INSTALLATION_LOG} for details. >&2
exit 1
fi
EOFSU

# Setup the jumper agent service
cp /opt/jumper-logging-agent/jumper-agent.template /lib/systemd/jumper-agent.service
ln -s /lib/systemd/jumper-agent.service /etc/systemd/system/jumper-agent.service
echo Setting up service ${SERVICE_NAME}...

SERVICE_FILE=/lib/systemd/${SERVICE_NAME}.service

cp ${SCRIPT_DIR}/jumper-agent.template ${SERVICE_FILE}
echo "ExecStart=${DEST_DIR}/venv/bin/python2.7 ${DEST_DIR}/agent_main.py" >> ${SERVICE_FILE}
echo "User=${SERVICE_USER}" >> ${SERVICE_FILE}
ln -fs ${SERVICE_FILE} /etc/systemd/system/${SERVICE_NAME}.service

# Start the jumper agent service
systemctl daemon-reload
systemctl start jumper-agent.service

sleep 1

if [[ "`systemctl is-active ${SERVICE_NAME}`" -ne "active" ]]; then
echo "Error: Service ${SERVICE_NAME} is not running. Status information: " >&2
echo "" >&2
systemctl status ${SERVICE_NAME} >&2
exit 1
fi

echo Success! Jumper logging agent is now installed and running.
5 changes: 4 additions & 1 deletion jumper-agent.template
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ WantedBy=multi-user.target

[Service]
Type=simple
ExecStart=/opt/jumper-logging-agent/venv/bin/python2.7 /opt/jumper-logging-agent/agent_main.py
Restart=always
RestartSec=5
StartLimitInterval=200
StartLimitBurst=5
2 changes: 1 addition & 1 deletion jumper_logging_agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from future.builtins import *
standard_library.install_aliases()

DEFAULT_INPUT_FILENAME = '/var/run/jumper_logging_agent'
DEFAULT_INPUT_FILENAME = '/var/run/jumper_logging_agent/events'
DEFAULT_FLUSH_THRESHOLD = 100
DEFAULT_FLUSH_PRIORITY = 2
DEFAULT_FLUSH_INTERVAL = 15.0
Expand Down

0 comments on commit 6689cb1

Please sign in to comment.