-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.sh
executable file
·133 lines (101 loc) · 3.59 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/bash
# Test for root
if [[ "$EUID" -ne 0 ]]; then
echo "Please run as root (sudo). Aborting." >&2
exit 1
fi
OS="debian"
if [ -f /etc/lsb-release ]; then
OS=$(awk '/DISTRIB_ID=/' /etc/*-release | sed 's/DISTRIB_ID=//' | tr '[:upper:]' '[:lower:]')
fi
if [ "$(pidof systemd)" != '' ]; then
init_ststem='systemd'
elif [ "$(pidof /sbin/init)" != '' ]; then
init_system='init'
else
echo "Could not detect neither systemd or init. Aborting" >&2
exit 1
fi
# 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
CONFIG_DIR=/etc/jumper_logging_agent
SERVICE_USER=jumperagent
SERVICE_NAME=jumper-agent
CONFIG_FILE=config.json
if ! command -v virtualenv --version >/dev/null 2>&1; then
echo Installing virtualenv...
yes w | python2.7 -m pip -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}
if [ ! -d ${CONFIG_DIR} ]; then
mkdir -p ${CONFIG_DIR}
cp ${CONFIG_FILE} ${CONFIG_DIR}
fi
chown ${SERVICE_USER}:${SERVICE_USER} ${FIFO_DIR}
echo Copying files...
# Copying the agent to its final destination
COPY_FILES="jumper_logging_agent README.md setup.py setup.cfg agent_main.py"
for FILE in ${COPY_FILES}; do
cp -R ${SCRIPT_DIR}/${FILE} ${DEST_DIR}/
done
chown -R ${SERVICE_USER}:${SERVICE_USER} ${DEST_DIR}
chmod -R u+rw,g+rw ${DEST_DIR}
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
echo Setting up service ${SERVICE_NAME}...
if [ $init_system = "init" ]; then
SERVICE_FILE=/etc/init.d/${SERVICE_NAME}
cp ${SCRIPT_DIR}/jumper.template ${SERVICE_FILE}
chmod 755 ${SERVICE_FILE}
# Start the jumper agent service
update-rc.d ${SERVICE_NAME} defaults
update-rc.d ${SERVICE_NAME} enable
service ${SERVICE_NAME} start
sleep 1
if [[ "`service ${SERVICE_NAME} status`" -ne "Running" ]]; then
echo "Error: Service ${SERVICE_NAME} is not running. Status information: " >&2
exit 1
fi
else
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
fi
echo Success! Jumper logging agent is now installed and running.