startup scripts : assume initscripts is not installed, only use systemctl
[myplc.git] / plc.d / functions
1 # -*-Shell-script-*-
2 # Common functions for PLC startup/shutdown scripts
3 #
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
6 #
7
8 export PATH=/sbin:/bin:/usr/bin:/usr/sbin
9
10 # covering for former /etc/init.d/functions
11 function success() {
12     echo success "$@"
13     true
14 }
15
16 function failure() {
17     echo failure "$@"
18     false
19 }
20
21 function action() {
22     local message="$1"; shift
23     echo $message
24     "$@"
25 }
26
27 # Total number of errors
28 ERRORS=0
29
30 # Count the exit status of the last command
31 function check () {
32     ERRORS=$(($ERRORS+$?))
33 }
34
35 function dialog () {
36     echo -n "PLC: $*: " >&3
37 }
38
39 # Print result
40 function result () {
41     if [ $ERRORS -eq 0 ] ; then
42         success "$@" >&3
43     else
44         failure "$@" >&3
45     fi
46     echo >&3
47 }
48
49 # Start up a program with a plc_ prefix
50 function plc_daemon () {
51     base=${1##*/}
52
53     # See if it's already running. Look *only* at the pid file.
54     if [ -f /var/run/plc_${base}.pid ]; then
55         local line p
56         read line < /var/run/plc_${base}.pid
57         for p in $line ; do
58                 [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p"
59         done
60     fi
61
62     [ -n "${pid:-}" -a -z "${force:-}" ] && return
63
64     # And start it up.
65     # Thierry -- June 18 2007
66     # when invoking, e.g. service plc start httpd from an ssh connection
67     # ssh stupidly hangs when everything is done
68     # it turns out the forked ssh daemon exhibits the following stack at that point
69     # (gdb) where
70     # #0  0x001d6402 in __kernel_vsyscall ()
71     # #1  0x003c2e7d in ___newselect_nocancel () from /lib/libc.so.6
72     # #2  0x004387b4 in main () from /usr/sbin/sshd
73     # So I figured the various file descriptors used were not properly closed
74     (exec 3>&- 4>&- ; exec -a plc_${base} $*)
75     ret=$?
76
77     if [ -f /var/run/${base}.pid ] ; then
78         mv /var/run/${base}.pid /var/run/plc_${base}.pid
79     fi
80
81     return $ret
82 }
83
84 # Print IP address of hostname if resolvable
85 function gethostbyname () {
86     python -c 'import socket; import sys; print socket.gethostbyname(sys.argv[1])' $1 2>/dev/null
87 }
88
89 # Forcefully make a symlink
90 function symlink () {
91     mkdir -p $(dirname $2)
92     rm -f $2
93     ln -s $1 $2
94 }
95
96 # Argument(s) or stdin to lowercase stdout
97 function lower () {
98     if [ ${#*} -ge 1 ] ; then
99         tr A-Z a-z <<<$*
100     else
101         tr A-Z a-z
102     fi
103 }
104
105 # Argument(s) or stdin to uppercase stdout
106 function upper () {
107     if [ ${#*} -ge 1 ] ; then
108         tr a-z A-Z <<<$*
109     else
110         tr a-z A-Z
111     fi
112 }
113
114 # Regenerate configuration files
115 function plc_reload () {
116     force=$1
117
118     # Regenerate the main configuration file from default values
119     # overlaid with site-specific and current values.
120     # Thierry -- 2007-07-05 : values in plc_config.xml are *not* taken into account here
121     files=(
122         /etc/planetlab/default_config.xml
123         /etc/planetlab/configs/site.xml
124     )
125
126     for file in "${files[@]}" ; do
127     if [ -n "$force" -o $file -nt /etc/planetlab/plc_config.xml ] ; then
128         tmp=$(mktemp /tmp/plc_config.xml.XXXXXX)
129         plc-config --xml "${files[@]}" >$tmp
130         if [ $? -eq 0 ] ; then
131         mv $tmp /etc/planetlab/plc_config.xml
132         chmod 444 /etc/planetlab/plc_config.xml
133         else
134         echo "PLC: Warning: Invalid configuration file(s) detected"
135         rm -f $tmp
136         fi
137         break
138     fi
139     done
140
141     # Convert configuration to various formats
142     if [ -n "$force" -o /etc/planetlab/plc_config.xml -nt /etc/planetlab/plc_config ] ; then
143     plc-config --shell >/etc/planetlab/plc_config
144     fi
145     if [ -n "$force" -o /etc/planetlab/plc_config.xml -nt /etc/planetlab/plc_config.py ] ; then
146     plc-config --python >/etc/planetlab/plc_config.py
147     fi
148     if [ -n "$force" -o /etc/planetlab/plc_config.xml -nt /etc/planetlab/php/plc_config.php ] ; then
149     mkdir -p /etc/planetlab/php
150     plc-config --php >/etc/planetlab/php/plc_config.php
151     fi
152 }
153
154 #################### feb 2013 for f18
155 # Authorization directives change in apache 2.4
156 # http://httpd.apache.org/docs/2.4/upgrading.html#access
157 #### up to 2.2, this would be e.g.
158 # apachectl -V
159 # Server version: Apache/2.2.22 (Unix)
160 # <Directory "/usr/share/plc_api/apache">
161 # Options       +ExecCGI
162 # Order allow,deny
163 # Allow from all
164 # </Directory>
165 #### starting with 2.4 it becomes
166 # apachectl -V
167 # Server version: Apache/2.4.3 (Fedora)
168 # <Directory "/usr/share/plc_api/apache">
169 # Options       +ExecCGI
170 # Require       all granted
171 # </Directory>
172
173 function apache_newauth () {
174     apache_version=$(apachectl -V 2> /dev/null | grep 'Server version' | cut -d ' ' -f3 | sed -e 's,^.*/,,')
175     apache_major=$(echo $apache_version | cut -d. -f1)
176     apache_minor=$(echo $apache_version | cut -d. -f2)
177     test "$apache_minor" -ge 4
178 }
179 function apache_allow () {
180     if apache_newauth; then
181         echo -e "Require all granted"
182     else
183         echo -e "Order allow,deny\n   Allow from all"
184     fi
185 }
186 function apache_forbid () {
187     if apache_newauth; then
188         echo -e "Require all denied"
189     else
190         echo -e "Order deny,allow\n   Deny from all"
191     fi
192 }
193
194 # Make copies of stdout and stderr. The plc initscript redirects
195 # stdout and stderr to a logfile if -v is not specified.
196 [ ! -e /proc/self/fd/3 ] && exec 3>&1
197 [ ! -e /proc/self/fd/4 ] && exec 4>&2