Skip to content

Commit 359d5ac

Browse files
committed
usage: Update init script to use JSVC (Debian)
1 parent a56631b commit 359d5ac

6 files changed

Lines changed: 561 additions & 409 deletions

File tree

usage/distro/centos/SYSCONFDIR/rc.d/init.d/cloud-usage.in

Lines changed: 98 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,132 @@
11
#!/bin/bash
2+
3+
### BEGIN INIT INFO
4+
# Provides: cloud usage
5+
# Required-Start: $network $local_fs
6+
# Required-Stop: $network $local_fs
7+
# Default-Start: 3 4 5
8+
# Default-Stop: 0 1 2 6
9+
# Short-Description: Start/stop Apache CloudStack Usage Monitor
10+
# Description: This scripts Starts/Stops the Apache CloudStack Usage Monitor
11+
## The CloudStack Usage Monitor is a part of the Apache CloudStack project and is used
12+
## for storing usage statistics from instances.
13+
## JSVC (Java daemonizing) is used for starting and stopping the usage monitor.
14+
### END INIT INFO
15+
216
# Licensed to the Apache Software Foundation (ASF) under one
317
# or more contributor license agreements. See the NOTICE file
418
# distributed with this work for additional information
519
# regarding copyright ownership. The ASF licenses this file
620
# to you under the Apache License, Version 2.0 (the
721
# "License"); you may not use this file except in compliance
822
# with the License. You may obtain a copy of the License at
9-
#
23+
#
1024
# http://www.apache.org/licenses/LICENSE-2.0
11-
#
25+
#
1226
# Unless required by applicable law or agreed to in writing,
1327
# software distributed under the License is distributed on an
1428
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1529
# KIND, either express or implied. See the License for the
1630
# specific language governing permissions and limitations
1731
# under the License.
1832

19-
# chkconfig: 35 99 10
20-
# description: CloudStack Usage Monitor
21-
22-
# WARNING: if this script is changed, then all other initscripts MUST BE changed to match it as well
23-
24-
. /etc/rc.d/init.d/functions
33+
. /lib/lsb/init-functions
2534

26-
whatami=cloud-usage
27-
28-
# set environment variables
29-
30-
SHORTNAME="$whatami"
31-
PIDFILE=@PIDDIR@/"$whatami".pid
35+
SHORTNAME="cloud-usage"
36+
PIDFILE=@PIDDIR@/"$SHORTNAME".pid
3237
LOCKFILE=@LOCKDIR@/"$SHORTNAME"
3338
LOGFILE=@USAGELOG@
3439
PROGNAME="CloudStack Usage Monitor"
40+
CLASS="com.cloud.usage.UsageServer"
41+
PROG="jsvc"
42+
DAEMON="/usr/bin/jsvc"
3543
USER=@MSUSER@
3644

3745
unset OPTIONS
38-
[ -r @SYSCONFDIR@/sysconfig/"$SHORTNAME" ] && source @SYSCONFDIR@/sysconfig/"$SHORTNAME"
39-
DAEMONIZE=@BINDIR@/@PACKAGE@-daemonize
40-
PROG=@LIBEXECDIR@/usage-runner
46+
[ -r @SYSCONFDIR@/default/"$SHORTNAME" ] && source @SYSCONFDIR@/default/"$SHORTNAME"
47+
48+
# The first existing directory is used for JAVA_HOME (if JAVA_HOME is not defined in $DEFAULT)
49+
JDK_DIRS="/usr/lib/jvm/java-6-openjdk /usr/lib/jvm/java-6-openjdk-i386 /usr/lib/jvm/java-6-openjdk-amd64 /usr/lib/jvm/java-6-sun /usr/lib/jvm/jre-1.6.0 /usr/lib/j2sdk1.5-sun /usr/lib/jre-openjdk"
50+
51+
for jdir in $JDK_DIRS; do
52+
if [ -r "$jdir/bin/java" -a -z "${JAVA_HOME}" ]; then
53+
JAVA_HOME="$jdir"
54+
fi
55+
done
56+
export JAVA_HOME
57+
58+
SCP="@SYSTEMCLASSPATH@"
59+
DCP="@DEPSCLASSPATH@"
60+
UCP="@USAGECLASSPATH@"
61+
JCP="/usr/share/java/commons-daemon.jar"
62+
63+
# We need to append the JSVC daemon JAR to the classpath
64+
# AgentShell implements the JSVC daemon methods
65+
export CLASSPATH="$SCP:$DCP:$UCP:$JCP:@USAGESYSCONFDIR@"
4166

4267
start() {
43-
echo -n $"Starting $PROGNAME: "
44-
if hostname --fqdn >/dev/null 2>&1 ; then
45-
daemon --check=$SHORTNAME --pidfile=${PIDFILE} "$DAEMONIZE" \
46-
-n "$SHORTNAME" -p "$PIDFILE" -l "$LOGFILE" -u "$USER" "$PROG" $OPTIONS
47-
RETVAL=$?
48-
echo
49-
else
50-
failure
51-
echo
52-
echo The host name does not resolve properly to an IP address. Cannot start "$PROGNAME". > /dev/stderr
53-
RETVAL=9
54-
fi
55-
[ $RETVAL = 0 ] && touch ${LOCKFILE}
56-
return $RETVAL
68+
if [ -s "$PIDFILE" ] && kill -0 $(cat "$PIDFILE") >/dev/null 2>&1; then
69+
log_daemon_msg "$PROGNAME apparently already running"
70+
log_end_msg 0
71+
exit 0
72+
fi
73+
74+
log_daemon_msg "Starting $PROGNAME" "$SHORTNAME"
75+
if hostname --fqdn >/dev/null 2>&1 ; then
76+
true
77+
else
78+
log_failure_msg "The host name does not resolve properly to an IP address. Cannot start $PROGNAME"
79+
log_end_msg 1
80+
exit 1
81+
fi
82+
83+
if start_daemon -p $PIDFILE $DAEMON -cp "$CLASSPATH" -pidfile "$PIDFILE" -user "$USER" -outfile SYSLOG -errfile SYSLOG -Dpid=$$ $CLASS
84+
RETVAL=$?
85+
then
86+
rc=0
87+
sleep 1
88+
if ! kill -0 $(cat "$PIDFILE") >/dev/null 2>&1; then
89+
log_failure_msg "$PROG failed to start"
90+
rc=1
91+
fi
92+
else
93+
rc=1
94+
fi
95+
96+
if [ $rc -eq 0 ]; then
97+
log_end_msg 0
98+
else
99+
log_end_msg 1
100+
rm -f "$PIDFILE"
101+
fi
57102
}
58103

59104
stop() {
60-
echo -n $"Stopping $PROGNAME: "
61-
killproc -p ${PIDFILE} $SHORTNAME # -d 10 $SHORTNAME
62-
RETVAL=$?
63-
echo
64-
[ $RETVAL = 0 ] && rm -f ${LOCKFILE} ${PIDFILE}
105+
log_daemon_msg "Stopping $PROGNAME" "$SHORTNAME"
106+
killproc -p $PIDFILE $DAEMON
107+
log_end_msg $?
108+
rm -f "$PIDFILE"
65109
}
66110

67-
68-
# See how we were called.
69111
case "$1" in
70-
start)
71-
start
72-
;;
73-
stop)
74-
stop
75-
;;
76-
status)
77-
status -p ${PIDFILE} $SHORTNAME
78-
RETVAL=$?
79-
;;
80-
restart)
81-
stop
82-
sleep 3
83-
start
84-
;;
85-
condrestart)
86-
if status -p ${PIDFILE} $SHORTNAME >&/dev/null; then
87-
stop
88-
sleep 3
89-
start
90-
fi
91-
;;
92-
*)
93-
echo $"Usage: $whatami {start|stop|restart|condrestart|status|help}"
94-
RETVAL=3
112+
start)
113+
start
114+
;;
115+
stop)
116+
stop
117+
;;
118+
status)
119+
status_of_proc -p "$PIDFILE" "$PROG" "$SHORTNAME"
120+
RETVAL=$?
121+
;;
122+
restart | force-reload)
123+
stop
124+
sleep 3
125+
start
126+
;;
127+
*)
128+
echo "Usage: $0 {start|stop|restart|force-reload|status}"
129+
RETVAL=3
95130
esac
96131

97132
exit $RETVAL

usage/distro/fedora/SYSCONFDIR/rc.d/init.d/cloud-usage.in

Lines changed: 98 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,132 @@
11
#!/bin/bash
2+
3+
### BEGIN INIT INFO
4+
# Provides: cloud usage
5+
# Required-Start: $network $local_fs
6+
# Required-Stop: $network $local_fs
7+
# Default-Start: 3 4 5
8+
# Default-Stop: 0 1 2 6
9+
# Short-Description: Start/stop Apache CloudStack Usage Monitor
10+
# Description: This scripts Starts/Stops the Apache CloudStack Usage Monitor
11+
## The CloudStack Usage Monitor is a part of the Apache CloudStack project and is used
12+
## for storing usage statistics from instances.
13+
## JSVC (Java daemonizing) is used for starting and stopping the usage monitor.
14+
### END INIT INFO
15+
216
# Licensed to the Apache Software Foundation (ASF) under one
317
# or more contributor license agreements. See the NOTICE file
418
# distributed with this work for additional information
519
# regarding copyright ownership. The ASF licenses this file
620
# to you under the Apache License, Version 2.0 (the
721
# "License"); you may not use this file except in compliance
822
# with the License. You may obtain a copy of the License at
9-
#
23+
#
1024
# http://www.apache.org/licenses/LICENSE-2.0
11-
#
25+
#
1226
# Unless required by applicable law or agreed to in writing,
1327
# software distributed under the License is distributed on an
1428
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1529
# KIND, either express or implied. See the License for the
1630
# specific language governing permissions and limitations
1731
# under the License.
1832

19-
# chkconfig: 35 99 10
20-
# description: CloudStack Usage Monitor
21-
22-
# WARNING: if this script is changed, then all other initscripts MUST BE changed to match it as well
23-
24-
. /etc/rc.d/init.d/functions
33+
. /lib/lsb/init-functions
2534

26-
whatami=cloud-usage
27-
28-
# set environment variables
29-
30-
SHORTNAME="$whatami"
31-
PIDFILE=@PIDDIR@/"$whatami".pid
35+
SHORTNAME="cloud-usage"
36+
PIDFILE=@PIDDIR@/"$SHORTNAME".pid
3237
LOCKFILE=@LOCKDIR@/"$SHORTNAME"
3338
LOGFILE=@USAGELOG@
3439
PROGNAME="CloudStack Usage Monitor"
40+
CLASS="com.cloud.usage.UsageServer"
41+
PROG="jsvc"
42+
DAEMON="/usr/bin/jsvc"
3543
USER=@MSUSER@
3644

3745
unset OPTIONS
38-
[ -r @SYSCONFDIR@/sysconfig/"$SHORTNAME" ] && source @SYSCONFDIR@/sysconfig/"$SHORTNAME"
39-
DAEMONIZE=@BINDIR@/@PACKAGE@-daemonize
40-
PROG=@LIBEXECDIR@/usage-runner
46+
[ -r @SYSCONFDIR@/default/"$SHORTNAME" ] && source @SYSCONFDIR@/default/"$SHORTNAME"
47+
48+
# The first existing directory is used for JAVA_HOME (if JAVA_HOME is not defined in $DEFAULT)
49+
JDK_DIRS="/usr/lib/jvm/java-6-openjdk /usr/lib/jvm/java-6-openjdk-i386 /usr/lib/jvm/java-6-openjdk-amd64 /usr/lib/jvm/java-6-sun /usr/lib/jvm/jre-1.6.0 /usr/lib/j2sdk1.5-sun /usr/lib/jre-openjdk"
50+
51+
for jdir in $JDK_DIRS; do
52+
if [ -r "$jdir/bin/java" -a -z "${JAVA_HOME}" ]; then
53+
JAVA_HOME="$jdir"
54+
fi
55+
done
56+
export JAVA_HOME
57+
58+
SCP="@SYSTEMCLASSPATH@"
59+
DCP="@DEPSCLASSPATH@"
60+
UCP="@USAGECLASSPATH@"
61+
JCP="/usr/share/java/commons-daemon.jar"
62+
63+
# We need to append the JSVC daemon JAR to the classpath
64+
# AgentShell implements the JSVC daemon methods
65+
export CLASSPATH="$SCP:$DCP:$UCP:$JCP:@USAGESYSCONFDIR@"
4166

4267
start() {
43-
echo -n $"Starting $PROGNAME: "
44-
if hostname --fqdn >/dev/null 2>&1 ; then
45-
daemon --check=$SHORTNAME --pidfile=${PIDFILE} "$DAEMONIZE" \
46-
-n "$SHORTNAME" -p "$PIDFILE" -l "$LOGFILE" -u "$USER" "$PROG" $OPTIONS
47-
RETVAL=$?
48-
echo
49-
else
50-
failure
51-
echo
52-
echo The host name does not resolve properly to an IP address. Cannot start "$PROGNAME". > /dev/stderr
53-
RETVAL=9
54-
fi
55-
[ $RETVAL = 0 ] && touch ${LOCKFILE}
56-
return $RETVAL
68+
if [ -s "$PIDFILE" ] && kill -0 $(cat "$PIDFILE") >/dev/null 2>&1; then
69+
log_daemon_msg "$PROGNAME apparently already running"
70+
log_end_msg 0
71+
exit 0
72+
fi
73+
74+
log_daemon_msg "Starting $PROGNAME" "$SHORTNAME"
75+
if hostname --fqdn >/dev/null 2>&1 ; then
76+
true
77+
else
78+
log_failure_msg "The host name does not resolve properly to an IP address. Cannot start $PROGNAME"
79+
log_end_msg 1
80+
exit 1
81+
fi
82+
83+
if start_daemon -p $PIDFILE $DAEMON -cp "$CLASSPATH" -pidfile "$PIDFILE" -user "$USER" -outfile SYSLOG -errfile SYSLOG -Dpid=$$ $CLASS
84+
RETVAL=$?
85+
then
86+
rc=0
87+
sleep 1
88+
if ! kill -0 $(cat "$PIDFILE") >/dev/null 2>&1; then
89+
log_failure_msg "$PROG failed to start"
90+
rc=1
91+
fi
92+
else
93+
rc=1
94+
fi
95+
96+
if [ $rc -eq 0 ]; then
97+
log_end_msg 0
98+
else
99+
log_end_msg 1
100+
rm -f "$PIDFILE"
101+
fi
57102
}
58103

59104
stop() {
60-
echo -n $"Stopping $PROGNAME: "
61-
killproc -p ${PIDFILE} $SHORTNAME # -d 10 $SHORTNAME
62-
RETVAL=$?
63-
echo
64-
[ $RETVAL = 0 ] && rm -f ${LOCKFILE} ${PIDFILE}
105+
log_daemon_msg "Stopping $PROGNAME" "$SHORTNAME"
106+
killproc -p $PIDFILE $DAEMON
107+
log_end_msg $?
108+
rm -f "$PIDFILE"
65109
}
66110

67-
68-
# See how we were called.
69111
case "$1" in
70-
start)
71-
start
72-
;;
73-
stop)
74-
stop
75-
;;
76-
status)
77-
status -p ${PIDFILE} $SHORTNAME
78-
RETVAL=$?
79-
;;
80-
restart)
81-
stop
82-
sleep 3
83-
start
84-
;;
85-
condrestart)
86-
if status -p ${PIDFILE} $SHORTNAME >&/dev/null; then
87-
stop
88-
sleep 3
89-
start
90-
fi
91-
;;
92-
*)
93-
echo $"Usage: $whatami {start|stop|restart|condrestart|status|help}"
94-
RETVAL=3
112+
start)
113+
start
114+
;;
115+
stop)
116+
stop
117+
;;
118+
status)
119+
status_of_proc -p "$PIDFILE" "$PROG" "$SHORTNAME"
120+
RETVAL=$?
121+
;;
122+
restart | force-reload)
123+
stop
124+
sleep 3
125+
start
126+
;;
127+
*)
128+
echo "Usage: $0 {start|stop|restart|force-reload|status}"
129+
RETVAL=3
95130
esac
96131

97132
exit $RETVAL

0 commit comments

Comments
 (0)