summaryrefslogtreecommitdiffstats
path: root/tests/geo-rep/compare-arequal.py
blob: 6d9850337b832a615f1d7a3195cf4ef62cf7bfda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/python

import sys
import os
import re
import tempfile
import subprocess
from multiprocessing import Pool
import time
from optparse import OptionParser

slave_dict = {}
master_res = ''


def get_arequal_checksum(me, mnt):
    global slave_dict
    master_cmd = ['./tests/utils/arequal-checksum', '-p', mnt]
    print "Calculating  "+me+" checksum ..."
    print ""
    p = subprocess.Popen(master_cmd, stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)
    ret = p.wait()
    stdout, stderr = p.communicate()
    if ret:
        print "Failed to get the checksum of " + me + " with following error"
        print stderr
        return 1
    else:
        return stdout


def get_file_count(me, mnt):
    global slave_dict
    master_cmd = ['find ' + mnt + ' |wc -l']
    print "Calculating  " + me + " files ..."
    print ""
    p = subprocess.Popen(master_cmd, stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE, shell=True)
    ret = p.wait()
    stdout, stderr = p.communicate()
    if ret:
        print "Failed to get the count of files in " + me
        + " with following error"
        print stderr
        return 1
    else:
        return stdout.strip()


def compare_checksum(master_mnt, slave_dict):
    proc = len(slave_dict)+1
    pool = Pool(processes=proc)
    master_res = pool.apply_async(get_arequal_checksum, args=("master",
                                                              master_mnt))
    results = [(slave, pool.apply_async(get_arequal_checksum,
                                        args=(slave_dict[slave]["vol"],
                                              slave_dict[slave]["mnt"])))
               for slave in slave_dict]

    pool.close()
    pool.join()
    for slave, result in results:
        slave_dict[slave]["res"] = result.get()
        # exception:  OSError

    master_res = master_res.get()

    print "arequal-checksum of master is : \n %s" % master_res
    for slave in slave_dict:
        print "arequal-checksum of geo_rep_slave %s: \n %s" % (
            slave_dict[slave]["vol"], slave_dict[slave]["res"])

    master_files, master_total = re.findall('Total[\s]+:\s(\w+)', master_res)
    master_reg_meta, master_reg = re.findall('Regular files[\s]+:\s(\w+)',
                                             master_res)[1:]
    master_dir_meta, master_dir = re.findall('Directories[\s]+:\s(\w+)',
                                             master_res)[1:]

    ret = 0
    for slave in slave_dict:
        slave_dict[slave]["files"], slave_dict[slave]["total"] = re.findall(
            'Total[\s]+:\s(\w+)', slave_dict[slave]["res"])
        slave_dict[slave]["reg_meta"], slave_dict[slave]["reg"] = re.findall(
            'Regular files[\s]+:\s(\w+)', slave_dict[slave]["res"])[1:]
        slave_dict[slave]["dir_meta"], slave_dict[slave]["dir"] = re.findall(
            'Directories[\s]+:\s(\w+)', slave_dict[slave]["res"])[1:]

        if master_reg_meta != slave_dict[slave]["reg_meta"]:
            print ("Meta data checksum for regular files doesn't match " +
                   "between master and  "+slave_dict[slave]["vol"])
            ret = 67

        if master_dir_meta != slave_dict[slave]["dir_meta"]:
            print ("Meta data checksum for directories doesn't match " +
                   "between master and "+slave_dict[slave]["vol"])
            ret = 68

        if master_files != slave_dict[slave]["files"]:
            print ("Failed to sync all the files from master to " +
                   slave_dict[slave]["vol"])
            ret = 1

        if master_total != slave_dict[slave]["total"]:
            if master_reg != slave_dict[slave]["reg"]:
                print ("Checksum for regular files doesn't match " +
                       "between master and "+slave_dict[slave]["vol"])
                ret = 1
            elif master_dir != slave_dict[slave]["dir"]:
                print ("Checksum for directories doesn't match between " +
                       "master and "+slave_dict[slave]["vol"])
                ret = 1
            else:
                print ("Checksum for symlinks or others doesn't match " +
                       "between master and "+slave_dict[slave]["vol"])
                ret = 1

        if ret is 0:
            print ("Successfully synced all the files from master " +
                   "to the "+slave_dict[slave]["vol"])

    return ret


def compare_filecount(master_mnt, slave_dict):
    proc = len(slave_dict)+1
    pool = Pool(processes=proc)

    master_res = pool.apply_async(get_file_count, args=("master", master_mnt))
    results = [(slave, pool.apply_async(get_file_count,
                                        args=(slave_dict[slave]["vol"],
                                              slave_dict[slave]["mnt"])))
               for slave in slave_dict]

    pool.close()
    pool.join()
    for slave, result in results:
        slave_dict[slave]["res"] = result.get()

    master_res = master_res.get()
    ret = 0
    for slave in slave_dict:
        if not master_res == slave_dict[slave]["res"]:
            print ("files count between master and " +
                   slave_dict[slave]["vol"]+" doesn't match yet")
            ret = 1

    return ret


def parse_url(url):
    match = re.search(r'([\w - _ @ \.]+)::([\w - _ @ \.]+)', url)
    if match:
        node = match.group(1)
        vol = match.group(2)
    else:
        print 'given url is not a valid.'
        sys.exit(1)
    return node, vol


def cleanup(master_mnt, slave_dict):
    try:
        os.system("umount %s" % (master_mnt))
    except:
        print("Failed to unmount the master volume")

    for slave in slave_dict:

        try:
            os.system("umount %s" % (slave_dict[slave]["mnt"]))
            os.removedirs(slave_dict[slave]["mnt"])
        except:
            print("Failed to unmount the "+slave+" volume")

    os.removedirs(master_mnt)


def main():

    slaves = args[1:]

    masterurl = args[0]
    master_node, mastervol = parse_url(masterurl)
    master_mnt = tempfile.mkdtemp()

    i = 1
    for slave in slaves:
        slave_dict["slave"+str(i)] = {}
        slave_dict["slave"+str(i)]["node"], slave_dict[
            "slave"+str(i)]["vol"] = parse_url(slave)
        slave_dict["slave"+str(i)]["mnt"] = tempfile.mkdtemp()
        i += 1

    try:
        print ("mounting the master volume on "+master_mnt)
        os.system("glusterfs -s  %s --volfile-id %s %s" % (master_node,
                                                           mastervol,
                                                           master_mnt))
        time.sleep(3)
    except:
        print("Failed to mount the master volume")

    for slave in slave_dict:
        print slave
        print slave_dict[slave]
        try:
            print ("mounting the slave volume on "+slave_dict[slave]['mnt'])
            os.system("glusterfs -s %s --volfile-id %s %s" % (
                slave_dict[slave]["node"], slave_dict[slave]["vol"],
                slave_dict[slave]["mnt"]))
            time.sleep(3)
        except:
            print("Failed to mount the "+slave+" volume")

    res = 0
    if option.check == "arequal":
        res = compare_checksum(master_mnt, slave_dict)
    elif option.check == "find":
        res = compare_filecount(master_mnt, slave_dict)
    else:
        print "wrong options given"

    cleanup(master_mnt, slave_dict)

    sys.exit(res)


if __name__ == '__main__':

    usage = "usage: %prog [option] <master-host>::<master-vol> \
    <slave1-host>::<slave1-vol> . . ."
    parser = OptionParser(usage=usage)
    parser.add_option("-c", dest="check", action="store", type="string",
                      default="arequal",
                      help="size of the files to be used [default: %default]")
    (option, args) = parser.parse_args()
    if not args:
        print "usage: <script> [option] <master-host>::<master-vol>\
         <slave1-host>::<slave1-vol> . . ."
        print ""
        sys.exit(1)

    main()