Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / rgw / test-rgw-common.sh
1 #!/bin/bash
2
3 rgw_flags="--debug-rgw=20 --debug-ms=1"
4
5 function _assert {
6   src=$1; shift
7   lineno=$1; shift
8   [ "$@" ] || echo "$src: $lineno: assert failed: $@" || exit 1
9 }
10
11 assert="eval _assert \$BASH_SOURCE \$LINENO"
12
13 function var_to_python_json_index {
14   echo "['$1']" | sed "s/\./'\]\['/g"
15 }
16
17 function json_extract {
18 var=""
19 [ "$1" != "" ] && var=$(var_to_python_json_index $1)
20 shift
21 python - <<END
22 import json
23 s='$@'
24 data = json.loads(s) 
25 print data$var
26 END
27 }
28
29 function python_array_len {
30 python - <<END
31 arr=$@
32 print len(arr)
33 END
34 }
35
36 function project_python_array_field {
37 var=$(var_to_python_json_index $1)
38 shift
39 python - <<END
40 arr=$@
41 s='( '
42 for x in arr:
43     s += '"' + str(x$var) + '" '
44 s += ')'
45 print s
46 END
47 }
48
49
50 x() {
51   # echo "x " "$@" >&2
52   eval "$@"
53 }
54
55
56 script_dir=`dirname $0`
57 root_path=`(cd $script_dir/../..; pwd)`
58
59 mstart=$root_path/mstart.sh
60 mstop=$root_path/mstop.sh
61 mrun=$root_path/mrun
62 mrgw=$root_path/mrgw.sh
63
64 function start_ceph_cluster {
65   [ $# -ne 1 ] && echo "start_ceph_cluster() needs 1 param" && exit 1
66
67   echo "$mstart $1"
68 }
69
70 function rgw_admin {
71   [ $# -lt 1 ] && echo "rgw_admin() needs 1 param" && exit 1
72
73   echo "$mrun $1 radosgw-admin"
74 }
75
76 function rgw {
77   [ $# -ne 2 ] && echo "rgw() needs 2 params" && exit 1
78
79   echo "$mrgw $1 $2 $rgw_flags"
80 }
81
82 function init_first_zone {
83   [ $# -ne 7 ] && echo "init_first_zone() needs 7 params" && exit 1
84
85   cid=$1
86   realm=$2
87   zg=$3
88   zone=$4
89   endpoints=$5
90
91   access_key=$6
92   secret=$7
93
94 # initialize realm
95   x $(rgw_admin $cid) realm create --rgw-realm=$realm
96
97 # create zonegroup, zone
98   x $(rgw_admin $cid) zonegroup create --rgw-zonegroup=$zg --master --default
99   x $(rgw_admin $cid) zone create --rgw-zonegroup=$zg --rgw-zone=$zone --access-key=${access_key} --secret=${secret} --endpoints=$endpoints --default
100   x $(rgw_admin $cid) user create --uid=zone.user --display-name="Zone User" --access-key=${access_key} --secret=${secret} --system
101
102   x $(rgw_admin $cid) period update --commit
103 }
104
105 function init_zone_in_existing_zg {
106   [ $# -ne 8 ] && echo "init_zone_in_existing_zg() needs 8 params" && exit 1
107
108   cid=$1
109   realm=$2
110   zg=$3
111   zone=$4
112   master_zg_zone1_port=$5
113   endpoints=$6
114
115   access_key=$7
116   secret=$8
117
118   x $(rgw_admin $cid) realm pull --url=http://localhost:$master_zg_zone1_port --access-key=${access_key} --secret=${secret} --default
119   x $(rgw_admin $cid) zonegroup default --rgw-zonegroup=$zg
120   x $(rgw_admin $cid) zone create --rgw-zonegroup=$zg --rgw-zone=$zone --access-key=${access_key} --secret=${secret} --endpoints=$endpoints
121   x $(rgw_admin $cid) period update --commit --url=http://localhost:$master_zg_zone1_port --access-key=${access_key} --secret=${secret}
122 }
123
124 function init_first_zone_in_slave_zg {
125   [ $# -ne 8 ] && echo "init_first_zone_in_slave_zg() needs 8 params" && exit 1
126
127   cid=$1
128   realm=$2
129   zg=$3
130   zone=$4
131   master_zg_zone1_port=$5
132   endpoints=$6
133
134   access_key=$7
135   secret=$8
136
137 # create zonegroup, zone
138   x $(rgw_admin $cid) realm pull --url=http://localhost:$master_zg_zone1_port --access-key=${access_key} --secret=${secret}
139   x $(rgw_admin $cid) realm default --rgw-realm=$realm
140   x $(rgw_admin $cid) zonegroup create --rgw-realm=$realm --rgw-zonegroup=$zg --endpoints=$endpoints --default
141   x $(rgw_admin $cid) zonegroup default --rgw-zonegroup=$zg
142
143   x $(rgw_admin $cid) zone create --rgw-zonegroup=$zg --rgw-zone=$zone --access-key=${access_key} --secret=${secret} --endpoints=$endpoints
144   x $(rgw_admin $cid) zone default --rgw-zone=$zone
145   x $(rgw_admin $cid) zonegroup add --rgw-zonegroup=$zg --rgw-zone=$zone
146
147   x $(rgw_admin $cid) user create --uid=zone.user --display-name="Zone User" --access-key=${access_key} --secret=${secret} --system
148   x $(rgw_admin $cid) period update --commit --url=localhost:$master_zg_zone1_port --access-key=${access_key} --secret=${secret}
149
150 }
151
152 function call_rgw_admin {
153   cid=$1
154   shift 1
155   x $(rgw_admin $cid) "$@"
156 }