creation - updated after v4 deployment
[infrastructure.git] / tunings-myplc-devel / init-d-plc-devel
1 #!/bin/bash
2 #
3 # plc   Manages all PLC services on this machine
4 #
5 # chkconfig: 2345 99 5
6 #
7 # description:  Manages all PLC services on this machine
8 #
9 # $Id: host.init,v 1.7.2.4 2006/08/21 21:21:11 mlhuang Exp $
10 #
11
12 PATH=/sbin:/bin:/usr/bin:/usr/sbin
13
14 # Source function library.
15 if [ -f /etc/init.d/functions ] ; then
16     . /etc/init.d/functions
17 fi
18
19 # If success() or failure() are not defined
20 if ! type -type success >/dev/null || ! type -type failure >/dev/null ; then
21     success() {
22         echo -ne "[  OK  ]\r"
23         return 0
24     }
25     failure() {
26         echo -ne "[FAILED]\r"
27         return 1
28     }
29 fi
30
31 # Source configuration
32 SERVICE=$(basename $0)
33 if [ ! -f /etc/sysconfig/$SERVICE ] ; then
34     SERVICE=plc
35 fi
36 if [ -f /etc/sysconfig/$SERVICE -a -z "${PLC_ROOT}${PLC_DATA}" ] ; then
37     . /etc/sysconfig/$SERVICE
38 fi
39
40 # Total number of errors
41 ERRORS=0
42
43 # Count the exit status of the last command
44 check ()
45 {
46     ERRORS=$(($ERRORS+$?))
47 }
48
49 mounted ()
50 {
51     if cut -d' ' -f2 /proc/mounts | grep -q "$1" ; then
52         return 0
53     else
54         return 1
55     fi
56 }
57
58 mount_plc ()
59 {
60     echo -n $"Mounting PLC: "
61
62     if ! mounted $PLC_ROOT ; then
63         if ! e2fsck -a $PLC_ROOT.img | logger -t "PLC" ; then
64             e2fsck $PLC_ROOT.img
65         fi
66         mount -o loop $PLC_ROOT.img $PLC_ROOT
67         check
68     fi
69     if ! mounted $PLC_ROOT/data ; then
70         mount -t none -o bind,rw $PLC_DATA $PLC_ROOT/data
71         check
72     fi
73     if [ -d /data/fedora ] ; then
74       if ! mounted $PLC_ROOT/data/fedora ; then
75         mount -t none -o bind,ro /data/fedora $PLC_ROOT/data/fedora 
76         check
77       fi
78     if [ -d /svn ] ; then
79       if ! mounted $PLC_ROOT/svn ; then
80         mount -t none -o bind,ro /svn $PLC_ROOT/svn 
81         check
82       fi
83     fi
84     if ! mounted $PLC_ROOT/proc ; then
85         mount -t proc none $PLC_ROOT/proc
86         check
87     fi
88
89     [ $ERRORS -eq 0 ] && success $"PLC unmount" || failure $"PLC unmount"       
90     echo
91
92 }
93
94 start ()
95 {
96     # Starting everything
97     if [ -z "$1" ] ; then
98         mount_plc 
99     fi
100
101     chroot $PLC_ROOT /sbin/service plc $PLC_OPTIONS start $*
102     check
103 }
104
105 umount_plc ()
106 {
107     echo -n $"Unmounting PLC: "
108
109     for dir in $PLC_ROOT/proc $PLC_ROOT/data/fedora $PLC_ROOT/svn $PLC_ROOT/data $PLC_ROOT ; do
110         if mounted $dir ; then
111             umount $dir
112             check
113         fi
114     done
115
116     [ $ERRORS -eq 0 ] && success $"PLC unmount" || failure $"PLC unmount"       
117     echo
118 }
119
120 stop ()
121 {
122     if mounted $PLC_ROOT ; then
123         chroot $PLC_ROOT /sbin/service plc $PLC_OPTIONS stop $*
124         check
125     fi
126
127     # Stopped everything
128     if [ -z "$1" ] ; then
129         umount_plc
130     fi
131 }
132
133 mountstatus_plc ()
134 {
135     for dir in $PLC_ROOT/proc $PLC_ROOT/data/fedora$PLC_ROOT/svn $PLC_ROOT/data $PLC_ROOT ; do
136         if mounted $dir ; then
137             echo $dir
138         fi
139     done
140 }
141
142 # Get command
143 shift $(($OPTIND - 1))
144 command=$1
145
146 # Get step(s)
147 shift 1
148
149 case "$command" in
150     start|stop)
151         $command $*
152         ;;
153
154     restart)
155         stop $*
156         start $*
157         ;;
158
159     reload)
160         chroot $PLC_ROOT /sbin/service plc $PLC_OPTIONS reload $*
161         ;;      
162
163     mount|umount|mountstatus)
164         ${command}_plc $*
165         ;;
166   
167     *)
168         echo "Usage: $0 {start|stop|restart|reload|mount|umount|mountstatus}"
169         RETVAL=1
170         ;;
171 esac
172
173 exit $ERRORS