ctdb-scripts: Translate old style options into new configuration file
[metze/samba/wip.git] / ctdb / config / ctdbd_wrapper
1 #!/bin/sh
2
3 # ctdbd wrapper - start or stop CTDB
4
5 usage ()
6 {
7     echo "usage: ctdbd_wrapper { start | stop }"
8     exit 1
9 }
10
11 [ $# -eq 1 ] || usage
12
13 action="$1"
14
15 ############################################################
16
17 if [ -z "$CTDB_BASE" ] ; then
18     export CTDB_BASE="/usr/local/etc/ctdb"
19 fi
20
21 . "${CTDB_BASE}/functions"
22
23 loadconfig
24
25 ctdbd="${CTDBD:-/usr/local/sbin/ctdbd}"
26
27 ############################################################
28
29 # Only the nested function references its arguments
30 # shellcheck disable=SC2120
31 build_ctdb_options ()
32 {
33         maybe_set ()
34         {
35                 _section="$1"
36                 _key="$2"
37                 _val="$3"
38                 _check="$4"
39
40                 # If the given variable isn't set then do nothing
41                 [ -n "$_val" ] || return
42                 # If a value is required for the variable and it doesn't
43                 # match, then do nothing
44                 if [ -n "$_check" -a "$_check" != "$_val" ] ; then
45                         return
46                 fi
47
48                 # Configuration file handling needs true/false, not yes/no
49                 case "$_val" in
50                 yes) _val="true"  ;;
51                 no)  _val="false" ;;
52                 esac
53
54                 if grep -q "^\\[${_section}\\]\\$" "$_conf_file" ; then
55                         # Section already exists...
56                         # Must escape leading TAB or sed eats it
57                         sed -i -e "/\\[${_section}\\]/a\
58 \\      ${_key} = ${_val}
59 " "${_conf_file}"
60                 else
61                         # Section does not exist and needs to be created...
62                         # Note literal TAB for indentation in here document
63                         cat >>"${_conf_file}" <<EOF
64
65 [${_section}]
66         ${_key} = ${_val}
67 EOF
68                 fi
69         }
70
71         # Only write a new style configuration file if it doesn't already
72         # exist
73         _conf_file="${CTDB_BASE}/ctdb.conf"
74         if [ -f "$_conf_file" ] ; then
75                 return
76         fi
77
78         touch "$_conf_file"
79
80         # Create configuration file from old style options
81         maybe_set "cluster" "recovery lock"      "$CTDB_RECOVERY_LOCK"
82         maybe_set "logging" "location"           "$CTDB_LOGGING"
83         maybe_set "cluster" "node address"       "$CTDB_NODE_ADDRESS"
84         maybe_set "database" "volatile database directory" \
85                   "$CTDB_DBDIR"
86         maybe_set "database" "persistent database directory" \
87                   "$CTDB_DBDIR_PERSISTENT"
88         maybe_set "database" "state database directory" \
89                   "$CTDB_DBDIR_STATE"
90         maybe_set "cluster" "transport"          "$CTDB_TRANSPORT"
91         maybe_set "logging" "log level"          "$CTDB_DEBUGLEVEL"
92         maybe_set "legacy" "start as disabled"   "$CTDB_START_AS_DISABLED" "yes"
93         maybe_set "legacy" "start as stopped"   "$CTDB_START_AS_STOPPED" "yes"
94         maybe_set "legacy" "recmaster capability" \
95                   "$CTDB_CAPABILITY_RECMASTER" "no"
96         maybe_set "legacy" "lmaster capability" \
97                   "$CTDB_CAPABILITY_LMASTER"   "no"
98         maybe_set "legacy" "no realtime"         "$CTDB_NOSETSCHED" "yes"
99         maybe_set "legacy" "script log level"    "$CTDB_SCRIPT_LOG_LEVEL"
100         maybe_set "database" "volatile uses tmpfs" \
101                   "$CTDB_DBDIR_USES_TMPFS" "yes"
102 }
103
104 export_debug_variables ()
105 {
106     [ -n "$CTDB_DEBUG_HUNG_SCRIPT" ] && export CTDB_DEBUG_HUNG_SCRIPT
107     [ -n "$CTDB_DEBUG_LOCKS" ] && export CTDB_DEBUG_LOCKS
108 }
109
110 ############################################################
111
112 start()
113 {
114     # build_ctdb_options() takes no arguments
115     # shellcheck disable=SC2119
116     build_ctdb_options
117
118     export_debug_variables
119
120     eval "$ctdbd" || return 1
121
122     # Wait until ctdbd has started and is ready to respond to clients.
123     _timeout="${CTDB_STARTUP_TIMEOUT:-10}"
124     _count=0
125     while [ "$_count" -lt "$_timeout" ] ; do
126         if $CTDB runstate first_recovery startup running >/dev/null 2>&1 ; then
127             return 0
128         fi
129
130         _count=$((_count + 1))
131         sleep 1
132     done
133
134     echo "Timed out waiting for initialisation - check logs"
135     # Attempt a shutdown just in case things are still running
136     $CTDB shutdown >/dev/null 2>&1
137     drop_all_public_ips >/dev/null 2>&1
138     return 1
139 }
140
141 stop()
142 {
143         $CTDB shutdown
144
145         # The above command is important and needs to stand out, so
146         # post-check exit status
147         # shellcheck disable=SC2181
148         if [ $? -ne 0 ] ; then
149                 echo "Error while shutting down CTDB"
150                 drop_all_public_ips >/dev/null 2>&1
151                 return 1
152         fi
153
154         return 0
155 }
156
157 ############################################################
158
159 # Allow notifications for start/stop.
160 if [ -x "$CTDB_BASE/rc.ctdb" ] ; then
161     "$CTDB_BASE/rc.ctdb" "$action"
162 fi
163
164 case "$action" in
165     start) start ;;
166     stop)  stop  ;;
167     *)
168         echo "usage: $0 {start|stop}"
169         exit 1
170 esac