b4726061aeaa62340eedad1d1413205dea195eae
[invirt/packages/invirt-console.git] / debian / sipb-xen-console.init
1 #!/bin/bash
2 ### BEGIN INIT INFO
3 # Provides:          sipb-xen-console
4 # Required-Start:    $local_fs $remote_fs
5 # Required-Stop:     $local_fs $remote_fs
6 # Default-Start:     2 3 4 5
7 # Default-Stop:      0 1 6
8 # Short-Description: sipb-xen Console Server homedir filesystem
9 # Description:       
10 ### END INIT INFO
11
12 # Author: SIPB Xen Project <sipb-xen@mit.edu>
13
14 # Do NOT "set -e"
15
16 # PATH should only include /usr/* if it runs after the mountnfs.sh script
17 PATH=/sbin:/usr/sbin:/bin:/usr/bin
18 DESC="The sipb-xen console server"
19 NAME=sipb-xen-console
20 DAEMON=/usr/bin/sipb-xen-consolefs
21 DAEMON_ARGS="/consolefs"
22 PIDFILE=/var/run/$NAME.pid
23 SCRIPTNAME=/etc/init.d/$NAME
24
25 # Exit if the package is not installed
26 [ -x "$DAEMON" ] || exit 0
27
28 # Read configuration variable file if it is present
29 [ -r /etc/default/$NAME ] && . /etc/default/$NAME
30
31 # Load the VERBOSE setting and other rcS variables
32 . /lib/init/vars.sh
33
34 # Define LSB log_* functions.
35 # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
36 . /lib/lsb/init-functions
37
38 gen_config()
39 {
40     {
41         for i in $(invirt-getconf --ls hosts); do
42             local hostname=$(invirt-getconf hosts.$i.hostname)
43             echo "#include /etc/conserver/conf.d/$hostname"
44         done
45     } > /etc/conserver/invirt-hosts.cf
46     local realm=$(invirt-getconf authn.0.realm)
47     {
48         for i in $(invirt-getconf --ls hosts); do
49             local hostname=$(invirt-getconf hosts.$i.hostname)
50             echo "host/$hostname@$realm"
51         done
52     } > /etc/remctl/acl/invirt-console
53
54     cat > /etc/nss-pgsql.conf << EOF
55 host            = $(invirt-getconf db.host)
56 port            = $(invirt-getconf db.port)
57 database        = $(invirt-getconf db.dbname)
58 login           = $(invirt-getconf db.user)
59
60 querypasswd = SELECT name, NULL, machine_id + 1000 as uid, machine_id + 1000 as gid, '', '/consolefs/'|| name, '/usr/bin/sipb-xen-consolesh' FROM machines
61 querygroup = SELECT name, NULL, machine_id + 1000 as gid FROM machines
62 querymembers = SELECT name FROM machines WHERE 1000 + machine_id = %d
63 queryids = SELECT 1000 + machine_id AS gid FROM machines LIMIT 0
64
65 passwd_name = name
66 passwd_uid = 1000 + machine_id
67
68 group_name = name
69 group_gid = 1000 + machine_id
70 EOF
71 }
72
73 #
74 # Function that starts the daemon/service
75 #
76 do_start()
77 {
78         # Return
79         #   0 if daemon has been started
80         #   1 if daemon was already running
81         #   2 if daemon could not be started
82         modprobe fuse
83         gen_config
84         daemon --running -n $NAME && return 1
85         daemon -r -O daemon.info -E daemon.err -n $NAME -U $DAEMON $DAEMON_ARGS || return 2
86 }
87
88 #
89 # Function that stops the daemon/service
90 #
91 do_stop()
92 {
93         # Return
94         #   0 if daemon has been stopped
95         #   1 if daemon was already stopped
96         #   2 if daemon could not be stopped
97         #   other if a failure occurred
98         daemon --stop -n $NAME
99         RETVAL="$?"
100         [ "$RETVAL" = 2 ] && return 2
101         # Many daemons don't delete their pidfiles when they exit.
102         rm -f $PIDFILE
103         umount "$DAEMON_ARGS"
104         return "$RETVAL"
105 }
106
107 do_reload()
108 {
109         gen_config
110         /etc/init.d/conserver-server reload
111 }
112
113 case "$1" in
114   start)
115         [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
116         do_start
117         case "$?" in
118                 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
119                 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
120         esac
121         ;;
122   stop)
123         [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
124         do_stop
125         case "$?" in
126                 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
127                 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
128         esac
129         ;;
130   reload|force-reload)
131         log_daemon_msg "Reloading $DESC" "$NAME"
132         do_reload
133         log_end_msg $?
134         ;;
135   restart)
136         log_daemon_msg "Restarting $DESC" "$NAME"
137         do_stop
138         case "$?" in
139           0|1)
140                 do_start
141                 case "$?" in
142                         0) log_end_msg 0 ;;
143                         1) log_end_msg 1 ;; # Old process is still running
144                         *) log_end_msg 1 ;; # Failed to start
145                 esac
146                 ;;
147           *)
148                 # Failed to stop
149                 log_end_msg 1
150                 ;;
151         esac
152         ;;
153   *)
154         echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
155         exit 3
156         ;;
157 esac
158
159 :