- simplify/correct certificate generation
[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.12 2006/04/04 22:09:47 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     # Load configuration
37     plc-config --shell >/etc/planetlab/plc_config
38     . /etc/planetlab/plc_config
39
40     # Generate various defaults
41     if [ -z "$PLC_DB_PASSWORD" ] ; then
42         PLC_DB_PASSWORD=$(uuidgen)
43         plc-config --category=plc_db --variable=password --value="$PLC_DB_PASSWORD" --save
44     fi
45
46     if [ -z "$PLC_API_MAINTENANCE_PASSWORD" ] ; then
47         PLC_API_MAINTENANCE_PASSWORD=$(uuidgen)
48         plc-config --category=plc_api --variable=maintenance_password --value="$PLC_API_MAINTENANCE_PASSWORD" --save
49     fi
50
51     # Need to configure network before resolving hostnames
52     /etc/plc.d/network start 3>/dev/null 4>/dev/null
53
54     PLC_API_MAINTENANCE_SOURCES=$(
55         for server in API BOOT WWW ; do
56             hostname=PLC_${server}_HOST
57             gethostbyname ${!hostname}
58         done | sort -u
59     )
60     plc-config --category=plc_api --variable=maintenance_sources --value="$PLC_API_MAINTENANCE_SOURCES" --save
61
62     # Save configuration
63     mkdir -p /etc/planetlab/php
64     plc-config --php >/etc/planetlab/php/plc_config.php
65     plc-config --shell >/etc/planetlab/plc_config
66
67     # For backward compatibility, until we can convert all code to use
68     # the now standardized variable names.
69
70     # DB constants are all named the same
71     ln -sf plc_config /etc/planetlab/plc_db
72
73     # API constants
74     cat >/etc/planetlab/plc_api <<EOF
75 PL_API_SERVER='$PLC_API_HOST'
76 PL_API_PATH='$PLC_API_PATH'
77 PL_API_PORT=$PLC_API_PORT
78 PL_API_CAPABILITY_AUTH_METHOD='capability'
79 PL_API_CAPABILITY_PASS='$PLC_API_MAINTENANCE_PASSWORD'
80 PL_API_CAPABILITY_USERNAME='$PLC_API_MAINTENANCE_USER'
81 PL_API_TICKET_KEY_FILE='$PLC_API_SSL_KEY'
82 PLANETLAB_SUPPORT_EMAIL='$PLC_MAIL_SUPPORT_ADDRESS'
83 BOOT_MESSAGES_EMAIL='$PLC_MAIL_BOOT_ADDRESS'
84 WWW_BASE='$PLC_WWW_HOST'
85 BOOT_BASE='$PLC_BOOT_HOST'
86 EOF
87
88     # API expects root SSH public key to be at /etc/planetlab/node_root_key
89     ln -sf "$PLC_ROOT_SSH_KEY_PUB" /etc/planetlab/node_root_key
90
91     # The format is
92     #
93     # ip:max_role_id:organization_id:password
94     #
95     # It is unlikely that we will let federated sites use the
96     # maintenance account to access each others' APIs, so we always
97     # set organization_id to -1.
98     (
99         echo -n "PL_API_CAPABILITY_SOURCES='"
100         first=1
101         for ip in $PLC_API_MAINTENANCE_SOURCES ; do
102             if [ $first -ne 1 ] ; then
103                 echo -n " "
104             fi
105             first=0
106             echo -n "$ip:-1:-1:$PLC_API_MAINTENANCE_PASSWORD"
107         done
108         echo "'"
109     ) >>/etc/planetlab/plc_api
110
111     cat >/etc/planetlab/php/site_constants.php <<"EOF"
112 <?php
113 include('plc_config.php');
114
115 define('PL_API_SERVER', PLC_API_HOST);
116 define('PL_API_PATH', PLC_API_PATH);
117 define('PL_API_PORT', PLC_API_PORT);
118 define('PL_API_CAPABILITY_AUTH_METHOD', 'capability');
119 define('PL_API_CAPABILITY_PASS', PLC_API_MAINTENANCE_PASSWORD);
120 define('PL_API_CAPABILITY_USERNAME', PLC_API_MAINTENANCE_USER);
121 define('WWW_BASE', PLC_WWW_HOST);
122 define('BOOT_BASE', PLC_BOOT_HOST);
123 define('DEBUG', PLC_WWW_DEBUG);
124 define('API_CALL_DEBUG', PLC_API_DEBUG);
125 define('SENDMAIL', PLC_MAIL_ENABLED);
126 define('PLANETLAB_SUPPORT_EMAIL', PLC_NAME . 'Support <' . PLC_MAIL_SUPPORT_ADDRESS . '>');
127 define('PLANETLAB_SUPPORT_EMAIL_ONLY', PLC_MAIL_SUPPORT_ADDRESS);
128 ?>
129 EOF
130 }
131
132 usage()
133 {
134     echo "Usage: $0 [OPTION]... [COMMAND] [STEP]..."
135     echo "      -v              Be verbose"
136     echo "      -h              This message"
137     echo
138     echo "Commands:"
139     echo "      start           Start all PLC subsystems"
140     echo "      stop            Stop all PLC subsystems"
141     echo "      reload          Regenerate configuration files"
142     echo "      restart         Restart all PLC subsystems"
143     echo
144     echo "Steps:"
145     for step in "${steps[@]}" ; do
146         if [ -x /etc/plc.d/$step ] ; then
147             echo "      $(basename $step)"
148         fi
149     done
150     exit 1
151 }
152
153 # Get options
154 while getopts "vh" opt ; do
155     case $opt in
156         v)
157             verbose=1
158             ;;
159         h|*)
160             usage
161             ;;
162     esac
163 done
164
165 # Redirect stdout and stderr of each step to /var/log/boot.log
166 if [ $verbose -eq 0 ] ; then
167     exec 1>>/var/log/boot.log
168     exec 2>>/var/log/boot.log
169 fi
170
171 # Get command
172 shift $(($OPTIND - 1))
173 if [ -z "$1" ] ; then
174     usage
175 fi
176 command=$1
177
178 # Get step(s)
179 shift 1
180 if [ -z "$1" ] ; then
181     # Start or stop everything. Regenerate configuration first.
182     reload
183 else
184     # Start or stop a particular step
185     steps=("$@")
186     nsteps=${#steps[@]}
187 fi
188
189 RETVAL=0
190
191 start ()
192 {
193     for step in "${steps[@]}" ; do
194         if [ -x /etc/plc.d/$step ] ; then
195             /etc/plc.d/$step start
196         else
197             echo "PLC: $step: unrecognized step" >&4
198             exit 1
199         fi
200     done
201 }
202
203 stop ()
204 {
205     for i in $(seq 1 $nsteps) ; do
206         step=${steps[$(($nsteps - $i))]}
207         if [ -x /etc/plc.d/$step ] ; then
208             /etc/plc.d/$step stop
209         else
210             echo "PLC: $step: unrecognized step" >&4
211             exit 1
212         fi
213     done
214 }
215
216 case "$command" in
217     start|stop)
218         $command
219         ;;
220
221     restart)
222         stop
223         start
224         ;;
225
226     reload)
227         ;;
228
229     *)
230         usage >&3
231         ;;
232 esac
233
234 exit $RETVAL