1c26a305882d096242ee4afabba0e14b784f27cb
[sahlberg/ctdb.git] / tests / eventscripts / stubs / ctdb
1 #!/bin/sh
2
3 prog="ctdb"
4
5 not_implemented_exit_code=1
6
7 usage ()
8 {
9     cat >&2 <<EOF
10 Usage: $prog [-Y] cmd
11
12 A fake CTDB stub that prints items depending on the variables
13 FAKE_CTDB_PNN (default 0) depending on command-line options.
14
15 Note that -Y is ignored.
16
17 EOF
18     exit $not_implemented_exit_code
19 }
20
21 # Don't set $POSIXLY_CORRECT here.
22 _temp=$(getopt -n "$prog" -o "Yvh" -l help -- "$@") || \
23     usage
24
25 eval set -- "$_temp"
26
27 verbose=false
28
29 while true ; do
30     case "$1" in
31         -Y) shift ;;
32         -v) verbose=true ; shift ;;
33         --) shift ; break ;;
34         -h|--help|*) usage ;; # * shouldn't happen, so this is reasonable.
35     esac
36 done
37
38 [ $# -ge 1 ] || usage
39
40 setup_tickles ()
41 {
42     # Make sure tickles file exists.
43     tickles_file="$CTDB_VARDIR/fake-ctdb/tickles"
44     mkdir -p $(dirname "$tickles_file")
45     touch "$tickles_file"
46 }
47
48 setup_pstore ()
49 {
50     pstore_dir="$CTDB_VARDIR/fake-ctdb/pstore/$1"
51     mkdir -p "$pstore_dir"
52 }
53
54 # For testing backward compatibility...
55 for i in $CTDB_NOT_IMPLEMENTED ; do
56     if [ "$i" = "$1" ] ; then
57         echo "${prog}: command \"$i\" not implemented" >&2
58         usage
59     fi
60 done
61
62 case "$1" in
63     ip)
64         # NOTE: all nodes share the same public addresses file.
65
66         # This is completely stateless and IPs move unnecessarily.
67         _f="${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}"
68         if [ -f "$_f" ] ; then
69             if $verbose ; then
70                 echo ":Public IP:Node:ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:"
71
72             else
73                 echo ":Public IP:Node:"
74             fi
75             # Here IPs are distributed across nodes in a stupid way...
76             _n=0
77             while read _ip _ifaces ; do
78                 case "_$ip" in
79                     \#*) : ;;
80                     *)
81                         # Find a node that is up... but don't loop forever.
82                         _orig=$_n
83                         while [ -f "${FAKE_CTDB_NODES_DOWN}/${_n}" ] ; do
84                             _n=$(($_n + 1))
85                             if [ _n -eq $FAKE_CTDB_NUMNODES ] ; then
86                                 _n=0
87                             fi
88                             if [ $_n -eq $_orig ] ; then
89                                 _n=-1 # Never down!  :-)
90                             fi
91                         done
92                         if $verbose ; then
93                             # If more than 1 interface, assume all
94                             # addresses are on the 1st.
95                             _first_iface="${_ifaces%%,*}"
96                             # Only show interface if address is on
97                             # this node.
98                             _my_iface=""
99                             if [ "PNN:$_n" = $(ctdb -Y pnn) ]; then
100                                 _my_iface="$_first_iface"
101                             fi
102                             echo ":${_ip}:${_n}:${_my_iface}:${_first_iface}:${_ifaces}:"
103                         else
104                             echo ":${_ip}:${_n}:"
105                         fi
106                 esac
107             done <"$_f"
108         fi
109         ;;
110     pnn|xpnn)
111         # Defaults to 0
112         echo "PNN:${FAKE_CTDB_PNN:-0}"
113         ;;
114     gettickles)
115         setup_tickles
116         echo ":source ip:port:destination ip:port:"
117         while read src dst ; do
118             echo ":${src}:${dst}:"
119         done <"$tickles_file"
120         ;;
121     addtickle)
122         setup_tickles
123         echo "$2 $3" >>"$tickles_file"
124         ;;
125     deltickle)
126         setup_tickles
127         _t=$(grep -F -v "$2 $3" "$tickles_file")
128         echo "$_t" >"$tickles_file"
129         ;;
130     pstore)
131         setup_pstore "$2"
132         cat "$4" >"${pstore_dir}/$3"
133         ;;
134     pfetch)
135         setup_pstore "$2"
136         cat "${pstore_dir}/$3" >"$4" 2>/dev/null
137         ;;
138     ifaces)
139         # Assume -Y.
140         echo ":Name:LinkStatus:References:"
141         _f="${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}"
142         if [ -r "$_f" ] ; then
143             while read _ip _iface ; do
144                 case "_$ip" in
145                     \#*) : ;;
146                     *)
147                         _status=1
148                         # For now assume _iface contains only 1.
149                         if [ -f "{FAKE_CTDB_IFACES_DOWN}/${_iface}" ] ; then
150                             _status=0
151                         fi
152                         # Nobody looks at references
153                         echo ":${_iface}:${_status}:0"
154                 esac
155             done <"$_f" |
156             sort -u
157         fi
158         ;;
159     setifacelink)
160         # Existence of file means CTDB thinks interface is down.
161         _f="${FAKE_CTDB_IFACES_DOWN}/$2"
162         case "$3" in
163             up)   rm -f "$_f" ;;
164             down) touch "$_f" ;;
165             *)
166                 echo "ctdb setifacelink: unsupported interface status $3"
167                 exit 1
168         esac
169         ;;
170     getdebug)
171         case "$CTDB_DEBUGLEVEL" in
172             -3) _t="EMERG"  ;;
173             -2) _t="ALERT"  ;;
174             -1) _t="CRIT"   ;;
175             0) _t="ERR"     ;;
176             1) _t="WARNING" ;;
177             2) _t="NOTICE"  ;;
178             3) _t="INFO"    ;;
179             4) _t="DEBUG"   ;;
180             *) _t="ERR"     ;;
181         esac
182
183         cat<<EOF
184 :Name:Level:
185 :${_t}:${CTDB_DEBUGLEVEL}:
186 EOF
187         ;;
188     checktcpport)
189         for _i in $FAKE_TCP_LISTEN ; do
190             if [ "$2" = "${_i##*:}" ] ; then
191                 exit 98
192             fi
193         done
194
195         exit 0
196         ;;
197     *)
198         echo "${prog}: command \"$1\" not implemented in stub"
199         usage
200 esac
201