- Regenerate the main configuration file from default values overlaid
[myplc.git] / guest.init
1 #!/bin/bash
2 #
3 # plc   Manages all PLC services on this machine
4 #
5 # chkconfig: 2345 5 99
6 #
7 # description:  Manages all PLC services on this machine
8 #
9 # $Id: guest.init,v 1.13 2006/04/06 21:51:59 mlhuang Exp $
10 #
11
12 # Source function library and configuration
13 . /etc/plc.d/functions
14
15 # Verbosity
16 verbose=0
17
18 # All steps should be idempotent. This means that you should be able
19 # to run them multiple times without depending on anything previously
20 # being run. The idea is that when the configuration changes, "service
21 # plc restart" is called, all dependencies are fixed up, and
22 # everything just works.
23 steps=($(
24 for step in /etc/plc.d/* ; do
25     if [ -x $step ] ; then
26         priority=$(sed -ne 's/# priority: \(.*\)/\1/p' $step)
27         echo $priority $(basename $step)
28     fi
29 done | sort -n | cut -d' ' -f2
30 ))
31 nsteps=${#steps[@]}
32
33 # Regenerate configuration files
34 reload ()
35 {
36     # Regenerate the main configuration file from default values
37     # overlaid with site-specific values.
38     plc-config --xml \
39         /etc/planetlab/default_config.xml \
40         /etc/planetlab/configs/* \
41         >/etc/planetlab/plc_config.xml
42
43     # Shell constants
44     plc-config --shell >/etc/planetlab/plc_config
45     . /etc/planetlab/plc_config
46
47     # Generate various defaults
48     if [ -z "$PLC_DB_PASSWORD" ] ; then
49         PLC_DB_PASSWORD=$(uuidgen)
50         plc-config --category=plc_db --variable=password --value="$PLC_DB_PASSWORD" --save
51     fi
52
53     if [ -z "$PLC_API_MAINTENANCE_PASSWORD" ] ; then
54         PLC_API_MAINTENANCE_PASSWORD=$(uuidgen)
55         plc-config --category=plc_api --variable=maintenance_password --value="$PLC_API_MAINTENANCE_PASSWORD" --save
56     fi
57
58     # Need to configure network before resolving hostnames
59     /etc/plc.d/network start 3>/dev/null 4>/dev/null
60
61     PLC_API_MAINTENANCE_SOURCES=$(
62         for server in API BOOT WWW ; do
63             hostname=PLC_${server}_HOST
64             gethostbyname ${!hostname}
65         done | sort -u
66     )
67     plc-config --category=plc_api --variable=maintenance_sources --value="$PLC_API_MAINTENANCE_SOURCES" --save
68
69     # Save configuration
70     mkdir -p /etc/planetlab/php
71     plc-config --php >/etc/planetlab/php/plc_config.php
72     plc-config --shell >/etc/planetlab/plc_config
73
74     # For backward compatibility, until we can convert all code to use
75     # the now standardized variable names.
76
77     # DB constants are all named the same
78     ln -sf plc_config /etc/planetlab/plc_db
79
80     # API constants
81     cat >/etc/planetlab/plc_api <<EOF
82 PL_API_SERVER='$PLC_API_HOST'
83 PL_API_PATH='$PLC_API_PATH'
84 PL_API_PORT=$PLC_API_PORT
85 PL_API_CAPABILITY_AUTH_METHOD='capability'
86 PL_API_CAPABILITY_PASS='$PLC_API_MAINTENANCE_PASSWORD'
87 PL_API_CAPABILITY_USERNAME='$PLC_API_MAINTENANCE_USER'
88 PL_API_TICKET_KEY_FILE='$PLC_API_SSL_KEY'
89 PLANETLAB_SUPPORT_EMAIL='$PLC_MAIL_SUPPORT_ADDRESS'
90 BOOT_MESSAGES_EMAIL='$PLC_MAIL_BOOT_ADDRESS'
91 WWW_BASE='$PLC_WWW_HOST'
92 BOOT_BASE='$PLC_BOOT_HOST'
93 EOF
94
95     # API expects root SSH public key to be at /etc/planetlab/node_root_key
96     ln -sf "$PLC_ROOT_SSH_KEY_PUB" /etc/planetlab/node_root_key
97
98     # The format is
99     #
100     # ip:max_role_id:organization_id:password
101     #
102     # It is unlikely that we will let federated sites use the
103     # maintenance account to access each others' APIs, so we always
104     # set organization_id to -1.
105     (
106         echo -n "PL_API_CAPABILITY_SOURCES='"
107         first=1
108         for ip in $PLC_API_MAINTENANCE_SOURCES ; do
109             if [ $first -ne 1 ] ; then
110                 echo -n " "
111             fi
112             first=0
113             echo -n "$ip:-1:-1:$PLC_API_MAINTENANCE_PASSWORD"
114         done
115         echo "'"
116     ) >>/etc/planetlab/plc_api
117
118     cat >/etc/planetlab/php/site_constants.php <<"EOF"
119 <?php
120 include('plc_config.php');
121
122 define('PL_API_SERVER', PLC_API_HOST);
123 define('PL_API_PATH', PLC_API_PATH);
124 define('PL_API_PORT', PLC_API_PORT);
125 define('PL_API_CAPABILITY_AUTH_METHOD', 'capability');
126 define('PL_API_CAPABILITY_PASS', PLC_API_MAINTENANCE_PASSWORD);
127 define('PL_API_CAPABILITY_USERNAME', PLC_API_MAINTENANCE_USER);
128 define('WWW_BASE', PLC_WWW_HOST);
129 define('BOOT_BASE', PLC_BOOT_HOST);
130 define('DEBUG', PLC_WWW_DEBUG);
131 define('API_CALL_DEBUG', PLC_API_DEBUG);
132 define('SENDMAIL', PLC_MAIL_ENABLED);
133 define('PLANETLAB_SUPPORT_EMAIL', PLC_NAME . 'Support <' . PLC_MAIL_SUPPORT_ADDRESS . '>');
134 define('PLANETLAB_SUPPORT_EMAIL_ONLY', PLC_MAIL_SUPPORT_ADDRESS);
135 ?>
136 EOF
137 }
138
139 usage()
140 {
141     echo "Usage: $0 [OPTION]... [COMMAND] [STEP]..."
142     echo "      -v              Be verbose"
143     echo "      -h              This message"
144     echo
145     echo "Commands:"
146     echo "      start           Start all PLC subsystems"
147     echo "      stop            Stop all PLC subsystems"
148     echo "      reload          Regenerate configuration files"
149     echo "      restart         Restart all PLC subsystems"
150     echo
151     echo "Steps:"
152     for step in "${steps[@]}" ; do
153         if [ -x /etc/plc.d/$step ] ; then
154             echo "      $(basename $step)"
155         fi
156     done
157     exit 1
158 }
159
160 # Get options
161 while getopts "vh" opt ; do
162     case $opt in
163         v)
164             verbose=1
165             ;;
166         h|*)
167             usage
168             ;;
169     esac
170 done
171
172 # Redirect stdout and stderr of each step to /var/log/boot.log
173 if [ $verbose -eq 0 ] ; then
174     exec 1>>/var/log/boot.log
175     exec 2>>/var/log/boot.log
176 fi
177
178 # Get command
179 shift $(($OPTIND - 1))
180 if [ -z "$1" ] ; then
181     usage
182 fi
183 command=$1
184
185 # Get step(s)
186 shift 1
187 if [ -z "$1" ] ; then
188     # Start or stop everything. Regenerate configuration first.
189     reload
190 else
191     # Start or stop a particular step
192     steps=("$@")
193     nsteps=${#steps[@]}
194 fi
195
196 RETVAL=0
197
198 start ()
199 {
200     for step in "${steps[@]}" ; do
201         if [ -x /etc/plc.d/$step ] ; then
202             /etc/plc.d/$step start
203         else
204             echo "PLC: $step: unrecognized step" >&4
205             exit 1
206         fi
207     done
208 }
209
210 stop ()
211 {
212     for i in $(seq 1 $nsteps) ; do
213         step=${steps[$(($nsteps - $i))]}
214         if [ -x /etc/plc.d/$step ] ; then
215             /etc/plc.d/$step stop
216         else
217             echo "PLC: $step: unrecognized step" >&4
218             exit 1
219         fi
220     done
221 }
222
223 case "$command" in
224     start|stop)
225         $command
226         ;;
227
228     restart)
229         stop
230         start
231         ;;
232
233     reload)
234         ;;
235
236     *)
237         usage >&3
238         ;;
239 esac
240
241 exit $RETVAL