f37 -> f39
[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$
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     fi
79     if [ -d /svn ] ; then
80       if ! mounted $PLC_ROOT/svn ; then
81         mount -t none -o bind,ro /svn $PLC_ROOT/svn 
82         check
83       fi
84     fi
85     if ! mounted $PLC_ROOT/proc ; then
86         mount -t proc none $PLC_ROOT/proc
87         check
88     fi
89
90     [ $ERRORS -eq 0 ] && success $"PLC unmount" || failure $"PLC unmount"       
91     echo
92
93 }
94
95 start ()
96 {
97     # Starting everything
98     if [ -z "$1" ] ; then
99         mount_plc 
100     fi
101
102     chroot $PLC_ROOT /sbin/service plc $PLC_OPTIONS start $*
103     check
104 }
105
106 umount_plc ()
107 {
108     echo -n $"Unmounting PLC: "
109
110     for dir in $PLC_ROOT/proc $PLC_ROOT/svn $PLC_ROOT/data/fedora $PLC_ROOT/data $PLC_ROOT ; do
111         if mounted $dir ; then
112             umount $dir
113             check
114         fi
115     done
116
117     [ $ERRORS -eq 0 ] && success $"PLC unmount" || failure $"PLC unmount"       
118     echo
119 }
120
121 stop ()
122 {
123     if mounted $PLC_ROOT ; then
124         chroot $PLC_ROOT /sbin/service plc $PLC_OPTIONS stop $*
125         check
126     fi
127
128     # Stopped everything
129     if [ -z "$1" ] ; then
130         umount_plc
131     fi
132 }
133
134 mountstatus_plc ()
135 {
136     for dir in $PLC_ROOT/proc $PLC_ROOT/svn $PLC_ROOT/data/fedora $PLC_ROOT/data $PLC_ROOT ; do
137         if mounted $dir ; then
138             echo $dir
139         fi
140     done
141 }
142
143 # Get command
144 shift $(($OPTIND - 1))
145 command=$1
146
147 # Get step(s)
148 shift 1
149
150 case "$command" in
151     start|stop)
152         $command $*
153         ;;
154
155     restart)
156         stop $*
157         start $*
158         ;;
159
160     reload)
161         chroot $PLC_ROOT /sbin/service plc $PLC_OPTIONS reload $*
162         ;;      
163
164     mount|umount|mountstatus)
165         ${command}_plc $*
166         ;;
167   
168     *)
169         echo "Usage: $0 {start|stop|restart|reload|mount|umount|mountstatus}"
170         RETVAL=1
171         ;;
172 esac
173
174 exit $ERRORS