Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / ceph_common.sh
1 #!/bin/sh
2
3 CCONF="$BINDIR/ceph-conf"
4
5 default_conf=$ETCDIR"/ceph.conf"
6 conf=$default_conf
7
8 hostname=`hostname -s`
9
10 verify_conf() {
11     # fetch conf?
12     if [ -x "$ETCDIR/fetch_config" ] && [ "$conf" = "$default_conf" ]; then
13         conf="/tmp/fetched.ceph.conf.$$"
14         echo "[$ETCDIR/fetch_config $conf]"
15         if $ETCDIR/fetch_config $conf && [ -e $conf ]; then true ; else
16             echo "$0: failed to fetch config with '$ETCDIR/fetch_config $conf'"
17             exit 1
18         fi
19         # yay!
20     else
21         # make sure ceph.conf exists
22         if [ ! -e $conf ]; then
23             if [ "$conf" = "$default_conf" ]; then
24                 echo "$0: ceph conf $conf not found; system is not configured."
25                 exit 0
26             fi
27             echo "$0: ceph conf $conf not found!"
28             usage_exit
29         fi
30     fi
31 }
32
33
34 check_host() {
35     # what host is this daemon assigned to?
36     host=`$CCONF -c $conf -n $type.$id host`
37     if [ "$host" = "localhost" ]; then
38         echo "$0: use a proper short hostname (hostname -s), not 'localhost', in $conf section $type.$id; skipping entry"
39         return 1
40     fi
41     if expr match "$host" '.*\.' > /dev/null 2>&1; then
42         echo "$0: $conf section $type.$id"
43         echo "contains host=$host, which contains dots; this is probably wrong"
44         echo "It must match the result of hostname -s"
45     fi
46     ssh=""
47     rootssh=""
48     sshdir=$PWD
49     get_conf user "" "user"
50
51     #echo host for $name is $host, i am $hostname
52
53     cluster=$1
54     if [ -e "/var/lib/ceph/$type/$cluster-$id/upstart" ]; then
55         return 1
56     fi
57
58     # sysvinit managed instance in standard location?
59     if [ -e "/var/lib/ceph/$type/$cluster-$id/sysvinit" ]; then
60         host="$hostname"
61         echo "=== $type.$id === "
62         return 0
63     fi
64
65     # ignore all sections without 'host' defined
66     if [ -z "$host" ]; then
67         return 1
68     fi
69
70     if [ "$host" != "$hostname" ]; then
71         # skip, unless we're starting remote daemons too
72         if [ $allhosts -eq 0 ]; then
73             return 1
74         fi
75
76         # we'll need to ssh into that host
77         if [ -z "$user" ]; then
78             ssh="ssh $host"
79         else
80             ssh="ssh $user@$host"
81         fi
82         rootssh="ssh root@$host"
83         get_conf sshdir "$sshdir" "ssh path"
84     fi
85
86     echo "=== $type.$id === "
87
88     return 0
89 }
90
91 do_cmd() {
92     if [ -z "$ssh" ]; then
93         [ $verbose -eq 1 ] && echo "--- $host# $1"
94         ulimit -c unlimited
95         whoami=`whoami`
96         if [ "$whoami" = "$user" ] || [ -z "$user" ]; then
97             bash -c "$1" || { [ -z "$3" ] && echo "failed: '$1'" && exit 1; }
98         else
99             sudo su $user -c "$1" || { [ -z "$3" ] && echo "failed: '$1'" && exit 1; }
100         fi
101     else
102         [ $verbose -eq 1 ] && echo "--- $ssh $2 \"if [ ! -d $sshdir ]; then mkdir -p $sshdir; fi; cd $sshdir ; ulimit -c unlimited ; $1\""
103         $ssh $2 "if [ ! -d $sshdir ]; then mkdir -p $sshdir; fi; cd $sshdir ; ulimit -c unlimited ; $1" || { [ -z "$3" ] && echo "failed: '$ssh $1'" && exit 1; }
104     fi
105 }
106
107 do_cmd_okfail() {
108     ERR=0
109     if [ -z "$ssh" ]; then
110         [ $verbose -eq 1 ] && echo "--- $host# $1"
111         ulimit -c unlimited
112         whoami=`whoami`
113         if [ "$whoami" = "$user" ] || [ -z "$user" ]; then
114             bash -c "$1" || { [ -z "$3" ] && echo "failed: '$1'" && ERR=1 && return 1; }
115         else
116             sudo su $user -c "$1" || { [ -z "$3" ] && echo "failed: '$1'" && ERR=1 && return 1; }
117         fi
118     else
119         [ $verbose -eq 1 ] && echo "--- $ssh $2 \"if [ ! -d $sshdir ]; then mkdir -p $sshdir; fi; cd $sshdir ; ulimit -c unlimited ; $1\""
120         $ssh $2 "if [ ! -d $sshdir ]; then mkdir -p $sshdir; fi; cd $sshdir ; ulimit -c unlimited ; $1" || { [ -z "$3" ] && echo "failed: '$ssh $1'" && ERR=1 && return 1; }
121     fi
122     return 0
123 }
124
125 do_root_cmd() {
126     if [ -z "$ssh" ]; then
127         [ $verbose -eq 1 ] && echo "--- $host# $1"
128         ulimit -c unlimited
129         whoami=`whoami`
130         if [ "$whoami" = "root" ]; then
131             bash -c "$1" || { echo "failed: '$1'" ; exit 1; }
132         else
133             sudo bash -c "$1" || { echo "failed: '$1'" ; exit 1; }
134         fi
135     else
136         [ $verbose -eq 1 ] && echo "--- $rootssh $2 \"if [ ! -d $sshdir ]; then mkdir -p $sshdir; fi ; cd $sshdir ; ulimit -c unlimited ; $1\""
137         $rootssh $2 "if [ ! -d $sshdir ]; then mkdir -p $sshdir; fi ; cd $sshdir; ulimit -c unlimited ; $1" || { echo "failed: '$rootssh $1'" ; exit 1; }
138     fi
139 }
140
141 do_root_cmd_okfail() {
142     ERR=0
143     if [ -z "$ssh" ]; then
144         [ $verbose -eq 1 ] && echo "--- $host# $1"
145         ulimit -c unlimited
146         whoami=`whoami`
147         if [ "$whoami" = "root" ]; then
148             bash -c "$1" || { [ -z "$3" ] && echo "failed: '$1'" && ERR=1 && return 1; }
149         else
150             sudo bash -c "$1" || { [ -z "$3" ] && echo "failed: '$1'" && ERR=1 && return 1; }
151         fi
152     else
153         [ $verbose -eq 1 ] && echo "--- $rootssh $2 \"if [ ! -d $sshdir ]; then mkdir -p $sshdir; fi; cd $sshdir ; ulimit -c unlimited ; $1\""
154         $rootssh $2 "if [ ! -d $sshdir ]; then mkdir -p $sshdir; fi; cd $sshdir ; ulimit -c unlimited ; $1" || { [ -z "$3" ] && echo "failed: '$rootssh $1'" && ERR=1 && return 1; }
155     fi
156     return 0
157 }
158
159 get_local_daemon_list() {
160     type=$1
161     if [ -d "/var/lib/ceph/$type" ]; then
162         for p in `find -L /var/lib/ceph/$type -mindepth 1 -maxdepth 1 -type d`; do
163             i=`basename $p` 
164             if [ -e "/var/lib/ceph/$type/$i/sysvinit" ]; then
165                 id=`echo $i | sed 's/[^-]*-//'`
166                 local="$local $type.$id"
167             fi
168         done
169     fi
170 }
171
172 get_local_name_list() {
173     # enumerate local directories
174     local=""
175     get_local_daemon_list "mon"
176     get_local_daemon_list "osd"
177     get_local_daemon_list "mds"
178     get_local_daemon_list "mgr"
179 }
180
181 get_name_list() {
182     orig="$*"
183
184     # extract list of monitors, mdss, osds, mgrs defined in startup.conf
185     allconf="$local "`$CCONF -c $conf -l mon | egrep -v '^mon$' || true ; \
186         $CCONF -c $conf -l mds | egrep -v '^mds$' || true ; \
187         $CCONF -c $conf -l mgr | egrep -v '^mgr$' || true ; \
188         $CCONF -c $conf -l osd | egrep -v '^osd$' || true`
189
190     if [ -z "$orig" ]; then
191         what="$allconf"
192         return
193     fi
194
195     what=""
196     for f in $orig; do
197         type=`echo $f | cut -c 1-3`   # e.g. 'mon', if $item is 'mon1'
198         id=`echo $f | cut -c 4- | sed 's/\\.//'`
199         case $f in
200             mon | osd | mds | mgr)
201                 for d in $allconf; do
202                     if echo $d | grep -q ^$type; then
203                         what="$what $d"
204                     fi
205                 done
206                 ;;
207             *)
208                 if ! echo " " $allconf $local " " | egrep -q "( $type$id | $type.$id )"; then
209                     echo "$0: $type.$id not found ($conf defines" $allconf", /var/lib/ceph defines" $local")"
210                     exit 1
211                 fi
212                 what="$what $f"
213                 ;;
214         esac
215     done
216 }
217
218 get_conf() {
219         var=$1
220         def=$2
221         key=$3
222         shift; shift; shift
223
224         if [ -z "$1" ]; then
225             [ "$verbose" -eq 1 ] && echo "$CCONF -c $conf -n $type.$id \"$key\""
226             eval "$var=\"`$CCONF -c $conf -n $type.$id \"$key\" || printf \"$def\"`\""
227         else
228             [ "$verbose" -eq 1 ] && echo "$CCONF -c $conf -s $1 \"$key\""
229             eval "$var=\"`$CCONF -c $conf -s $1 \"$key\" || eval printf \"$def\"`\""
230         fi
231 }
232
233 get_conf_bool() {
234         get_conf "$@"
235
236         eval "val=$"$1
237         [ "$val" = "0" ] && export $1=0
238         [ "$val" = "false" ] && export $1=0
239         [ "$val" = "1" ] && export $1=1
240         [ "$val" = "true" ] && export $1=1
241 }
242