Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 223 lines (178 sloc) 5.97 KB
#!/bin/bash
#
# sth Start/Stop the Short Time Historic (STH) component
#
# chkconfig: 2345 99 60
# description: Short Time Historic (STH) component for Telefonica's IoT Platform
### BEGIN INIT INFO
# Provides: sth
# Required-Start: $local_fs $syslog
# Required-Stop: $local_fs $syslog
# Default-Start: 345
# Default-Stop: 90
# Short-Description: run sth
# Description: The Short Time Historic (STH, aka. Comet) is a component of the
# FIWARE ecosystem in charge of providing aggregated time series information
# about the evolution in time of entity attribute values registered using the
# Orion Context Broker, an implementation of the publish/subscribe context
# management system exposing NGSI9 and NGSI10 interfaces.
### END INIT INFO
# HISTORY CHANGES
# CJMM-DevOps 2016/03/21
# Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U
#
# This file is part of the Short Time Historic (STH) component
#
# STH is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
#
# STH is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with STH.
# If not, seehttp://www.gnu.org/licenses/.
#
# For those usages not covered by the GNU Affero General Public License
# please contact with: [german.torodelvalle@telefonica.com]
. /etc/rc.d/init.d/functions
PARAM=$1
STH_INSTANCE=${2}
COMPONENT_NAME=sth
PREFIX=/opt
LOG_DIR=/var/log/sth
COMPONENT_DIR=${PREFIX}/${COMPONENT_NAME}
# CONFIG_FILE=${PREFIX}/${COMPONENT_NAME}/conf/sth.conf
COMPONENT_USER=sth
NODE_EXEC="/usr/bin/node"
component_start()
{
local result=0
local sth_instance=${1}
if [[ ! -f ${COMPONENT_DIR}/bin/sth ]]; then
printf "%s\n" "Fail - ${COMPONENT_DIR}/bin/sth application file not exists."
exit 1
fi
if [[ -z ${sth_instance} ]]; then
list_instances="${COMPONENT_DIR}/conf/sth_*.conf"
else
list_instances="${COMPONENT_DIR}/conf/sth_${sth_instance}.conf"
fi
if [[ $(ls -l ${list_instances} 2> /dev/null | wc -l) -eq 0 ]]; then
if [[ ${sth_instance} == "" ]]; then
printf "%s\n" "There aren't any instance of STH configured"
else
printf "%s\n" "There aren't any instance of STH configured with the name ${sth_instance}"
fi
return 1
fi
for instance in ${list_instances}
do
local NAME
NAME=${instance%.conf}
# OLD for 0.3.x
# NAME=${NAME#*sth_}
NAME=$(basename ${NAME})
source ${instance}
local STH_PID_FILE="/var/run/sth/${NAME}.pid"
printf "%s" "Starting STH ${NAME}... "
status -p ${STH_PID_FILE} ${NODE_EXEC} &> /dev/null
if [[ ${?} -eq 0 ]]; then
printf "%s\n" " Already running, skipping $(success)"
continue
fi
su ${COMPONENT_USER} -p -c "${NODE_EXEC} ${COMPONENT_DIR}/bin/sth ${instance} 2>&1 >> ${LOG_DIR}/${NAME}.log & echo \$! > ${STH_PID_FILE}"
sleep 2 # some cortesy time to process startup or die
local PID=$(cat ${STH_PID_FILE})
local STH_PID=$(ps -ef | grep -v "grep" | grep "${PID:-not_found}")
if [[ -z ${STH_PID} ]]; then
printf "%s\n" "$(failure)"
result=$((${result}+1))
rm -f ${STH_PID_FILE} &> /dev/null
else
chown ${COMPONENT_USER}:${COMPONENT_USER} ${STH_PID_FILE}
printf "%s\n" "$(success)"
fi
done
return ${result}
}
component_stop()
{
local result=0
local sth_instance=${1}
if [[ -z ${sth_instance} ]]; then
list_run_instances="/var/run/sth/sth_*.pid"
else
list_run_instances="/var/run/sth/sth_${sth_instance}.pid"
fi
if [[ $(ls -l ${list_run_instances} 2> /dev/null | wc -l) -eq 0 ]]; then
printf "%s\n" "There aren't any instance of STH ${sth_instance} running $(success)"
return 0
fi
for run_instance in ${list_run_instances}
do
local NAME
NAME=${run_instance%.pid}
# OLD for 0.3.x
# NAME=${NAME#*sth_}
NAME=$(basename ${NAME})
printf "%s" "Stopping STH ${NAME}..."
local RUN_PID=$(cat ${run_instance})
kill ${RUN_PID} &> /dev/null
sleep 2 # time to die properly
local KILLED_PID=$(ps -ef | grep ${RUN_PID} | grep -v grep | awk '{print $2}')
if [[ -z ${KILLED_PID} ]]; then
printf "%s\n" "$(success)"
else
printf "%s\n" "$(failure)"
result=$((${result}+1))
fi
rm -f ${run_instance} &> /dev/null
done
return ${result}
}
component_status()
{
local result=0
local sth_instance=${1}
if [[ -z ${sth_instance} ]]; then
list_run_instances="/var/run/sth/sth_*.pid"
else
list_run_instances="/var/run/sth/sth_${sth_instance}.pid"
fi
if [[ $(ls -l ${list_run_instances} 2> /dev/null | wc -l) -eq 0 ]]; then
printf "%s\n" "There aren't any instance of STH ${sth_instance} running."
return 1
fi
for run_instance in ${list_run_instances}
do
local NAME
NAME=${run_instance%.pid}
# OLD for 0.3.x
# NAME=${NAME#*sth_}
NAME=$(basename ${NAME})
printf "%s\n" "STH ${NAME} status..."
status -p ${run_instance} ${NODE_EXEC}
result=$((${result}+${?}))
done
return ${result}
}
case ${PARAM} in
'start')
component_start ${STH_INSTANCE}
;;
'stop')
component_stop ${STH_INSTANCE}
;;
'restart')
component_stop ${STH_INSTANCE}
component_start ${STH_INSTANCE}
;;
'status')
component_status ${STH_INSTANCE}
;;
esac
You can’t perform that action at this time.