1#!/usr/bin/env python
2# -*- python -*-
3
4import pump
5import pump_transfer
6import pump_json
7
8import sys
9import time
10import zipfile
11import os
12import os.path
13import shutil
14import simplejson as json
15import tempfile
16
17from optparse import OptionParser
18
19from couchbase import client
20from couchbase.rest_client import RestConnection, RestHelper
21
22class ZipUtil:
23    def __init__(self, zipobj):
24        self.zipobj = zipobj
25
26    def extractall(self, path=None):
27        if path is None:
28            path = os.getcwd()
29        if (path[-1] in (os.path.sep, os.path.altsep)
30            and len(os.path.splitdrive(path)[1]) > 1):
31            path = path[:-1]
32
33        for member in self.zipobj.namelist():
34            if not isinstance(member, zipfile.ZipInfo):
35                member = self.zipobj.getinfo(member)
36
37            # don't include leading "/" from file name if present
38            if member.filename[0] == '/':
39                targetpath = os.path.join(path, member.filename[1:])
40            else:
41                targetpath = os.path.join(path, member.filename)
42
43            targetpath = os.path.normpath(targetpath)
44
45            # Create all parent directories if necessary.
46            upperdirs = os.path.dirname(targetpath)
47            if upperdirs and not os.path.exists(upperdirs):
48                try:
49                    os.makedirs(upperdirs)
50                except:
51                    print "Unexpected error:", sys.exc_info()[0]
52                    return upperdirs
53
54            if member.filename[-1] == '/':
55                if not os.path.isdir(targetpath):
56                    try:
57                        os.mkdir(targetpath)
58                    except:
59                        print "Fail to create directory:", targetpath
60                continue
61
62            target = file(targetpath, "wb")
63            target.write(self.zipobj.read(member.filename))
64            target.close()
65
66        return path
67
68class DocLoader(pump_transfer.Transfer):
69
70    def parse_args(self, argv):
71        usage = "usage: %prog [options] <directory>|zipfile\n\n" + \
72                "Example: %prog -u Administrator -p password -n 127.0.0.1:8091 " + \
73                "-b mybucket -s 100 gamesim-sample.zip"
74
75        parser = OptionParser(usage)
76
77        username = os.environ.get('REST_USERNAME', None)
78        password = os.environ.get('REST_PASSWORD', None)
79
80        parser.add_option('-u', dest='username', default=username,
81                          help='Username', metavar='Administrator')
82        parser.add_option('-p', dest='password', default=password,
83                          help='Password', metavar='password')
84        parser.add_option('-b', dest='bucket',
85                          help='Bucket', metavar='mybucket')
86        parser.add_option('-n', dest='node', default='127.0.0.1:8091',
87                          help='Node address', metavar='127.0.0.1:8091')
88        parser.add_option('-s', dest='ram_quota', default=100, type='int',
89                          help='RAM quota in MB', metavar=100)
90
91        self.options, self.args = parser.parse_args(argv[1:])
92        if not self.args or not self.options.bucket:
93            parser.print_help()
94            sys.exit()
95
96        # check if the uploaded file exists
97        if not os.path.exists(self.args[0]):
98            print "Invalid path: %s" % self.args[0]
99            sys.exit()
100
101    def opt_construct(self, argv):
102        sink_opts = {"node" : "http://"}
103        common_opts = {"bucket" : ["-B", None],
104                       "username" : ["-u", None],
105                       "password" : ["-p", None],
106                      }
107        count_opts = {"verbose" : ["-v", None]}
108
109        # parse options and arguments
110        self.parse_args(argv)
111
112        gen_str = "json://" + self.args[0]
113        sink_str = ""
114        for key in sink_opts.iterkeys():
115            val = getattr(self.options, key, None)
116            if val:
117                sink_str += sink_opts[key] + val
118
119        for key in common_opts.iterkeys():
120            val = getattr(self.options, key, None)
121            if val:
122                common_opts[key][1] = str(val)
123
124        for key in count_opts.iterkeys():
125            val = getattr(self.options, key, None)
126            if val:
127                count_opts[key][1] = int(val)
128
129        return gen_str, sink_str, common_opts, count_opts
130
131    def init_bucket(self):
132        server_info = {'ip': self.options.node.split(':')[0],
133                       'port': self.options.node.split(':')[1],
134                       'username': self.options.username,
135                       'password': self.options.password}
136
137        self.rest = RestConnection(server_info)
138        uri = "http://%s:%s/nodes/self" % (server_info["ip"], server_info["port"])
139        status, content = self.rest._http_request(uri)
140        quotaUnused = -1
141        if status:
142            try:
143                json_parsed = json.loads(content)
144                quotaTotal = json_parsed["storageTotals"]["ram"]["quotaTotal"]
145                quotaUnused = quotaTotal - json_parsed["storageTotals"]["ram"]["quotaUsed"]
146            except:
147                pass
148        quotaUnused = quotaUnused / 1024.0
149        if quotaUnused > 0 and quotaUnused < self.options.ram_quota:
150            print "RAM quota specified is too large to be provisioned into this cluster"
151            print "Available RAM quota: %d, requested: %d" % (quotaUnused, self.options.ram_quota)
152            sys.exit()
153        if not RestHelper(self.rest).bucket_exists(self.options.bucket):
154            self.rest.create_bucket(bucket=self.options.bucket,
155                                    ramQuotaMB=self.options.ram_quota,
156                                    authType='sasl')
157            timeout_in_seconds = 120
158            start = time.time()
159            # Make sure the bucket exists before querying its status
160            bucket_exist = False
161            while (time.time() - start) <= timeout_in_seconds and not bucket_exist:
162                bucket_exist = RestHelper(self.rest).bucket_exists(self.options.bucket)
163                if bucket_exist:
164                    break
165                else:
166                    sys.stderr.write(".")
167                    time.sleep(2)
168
169            if not bucket_exist:
170                print "\nFail to create bucket '%s' within %s seconds" %\
171                      (self.options.bucket, timeout_in_seconds)
172                sys.exit()
173
174            #Query status for all bucket nodes
175            uri = "http://%s:%s/pools/default/buckets/%s" % \
176                    (server_info["ip"], server_info["port"], self.options.bucket)
177            all_node_ready = False
178            while (time.time() - start) <= timeout_in_seconds and not all_node_ready:
179                status, content = self.rest._http_request(uri)
180                try:
181                    json_data = json.loads(content)
182                    all_node_ready = True
183                    for node in json_data["nodes"]:
184                        if node["status"] != "healthy":
185                            all_node_ready = False
186                            break
187                    if not all_node_ready:
188                        sys.stderr.write(".")
189                        time.sleep(2)
190                except Exception, err:
191                    print "Exception:", err
192                    break
193            if not all_node_ready:
194                print "\nNode status is not ready after creating bucket '%s' within %s seconds" %\
195                      (self.options.bucket, timeout_in_seconds)
196                sys.exit()
197            else:
198                print "bucket creation is successful"
199
200    def save_doc(self, dockey, datafile):
201        raw_data = datafile.read()
202        try:
203            doc = json.loads(raw_data)
204            if '_id' in doc:
205                doc['_id'] = doc['_id'].encode('UTF-8')
206                self.bucket.save(doc)
207                for view in doc.get('views', []):
208                    self.views.append(doc['_id'] + '/_view/' + view)
209        except ValueError, error:
210            print error
211
212    def gen_dockey(self, filename):
213        return os.path.splitext(os.path.basename(filename))[0]
214
215    def enumerate_and_save(self, subdir=None):
216        if not subdir:
217            subdir = self.args[0]
218        subdirlist = list()
219        viewdirs = list()
220        for item in os.listdir(subdir):
221            if os.path.isfile(os.path.join(subdir, item)):
222                try:
223                    fp = open(os.path.join(subdir, item), 'r')
224                    dockey = self.gen_dockey(item)
225                    self.save_doc(dockey, fp)
226                    fp.close()
227                except IOError, error:
228                    print error
229            else:
230                if item.find("design_docs") > 0:
231                    viewdirs.append(os.path.join(subdir, item))
232                else:
233                    subdirlist.append(os.path.join(subdir, item))
234        for dir in subdirlist:
235            self.enumerate_and_save(dir)
236        for dir in viewdirs:
237            self.enumerate_and_save(dir)
238
239    def unzip_file_and_upload(self):
240        zfobj = zipfile.ZipFile(self.args[0])
241
242        working_dir = tempfile.mkdtemp()
243        ZipUtil(zfobj).extractall(working_dir)
244
245        self.enumerate_and_save(working_dir)
246        shutil.rmtree(working_dir)
247
248    def populate_docs(self):
249        cb = client.Couchbase(self.options.node,
250                              self.options.username,
251                              self.options.password)
252
253        self.bucket = cb[self.options.bucket]
254
255        #Retrieve and reset couchbase_api_base from server
256        self.bucket.server.couch_api_base = self.retrive_couch_api_base(cb)
257
258        self.views = list()
259
260        if self.args[0].endswith('.zip'):
261            self.unzip_file_and_upload()
262        else:
263            self.enumerate_and_save()
264
265    def retrive_couch_api_base(self, cb):
266        if (':' in self.options.node):
267            ip, port = self.options.node.split(':')
268        else:
269            ip, port = self.options.node, 8091
270
271        server_config_uri = "http://%s:%s/pools/default/buckets/%s" % (ip, port, self.options.bucket)
272        config = client.ServerHelper.parse_server_config(server_config_uri,
273                                                         self.options.username,
274                                                         self.options.password)
275        couch_api_base = config["nodes"][0].get("couchApiBase")
276
277        #Remove bucket suffix because it is added when saving design docs
278        couch_api_base = "/".join(couch_api_base.split("/")[:-1]) + "/"
279
280        return couch_api_base
281
282    def verify_queries(self):
283        for view in self.views:
284            self.bucket.view(view, stale="update_after")
285
286    def find_handlers(self, opts, source, sink):
287        return pump_json.JSONSource, pump.PumpingStation.find_handler(opts, sink, pump_transfer.SINKS)
288
289    def main(self, argv):
290
291        src, sink, common_opts, count_opts = self.opt_construct(argv)
292        local_args = [argv[0]]
293        local_args.append(src)
294        local_args.append(sink)
295        for v in common_opts.itervalues():
296            local_args.append(v[0])
297            local_args.append(v[1])
298
299        for v in count_opts.itervalues():
300            if v[1] is not None:
301                for i in range(v[1]):
302                    local_args.append(v[0])
303        print local_args
304
305        # create new bucket if it doesn't exist
306        self.init_bucket()
307
308        #use cbtransfer to upload documents
309        pump_transfer.Transfer.main(self, local_args)
310
311        #upload documents
312        self.populate_docs()
313
314        # execute views at least once
315        self.verify_queries()
316
317        #sys.stderr.write("done\n")
318
319if __name__ == '__main__':
320    if os.name == 'nt':
321        mydir = os.path.dirname(sys.argv[0])
322        bin_dir = os.path.join(mydir, '..')
323        path = [mydir, bin_dir, os.environ['PATH']]
324        os.environ['PATH'] = ';'.join(path)
325
326    pump_transfer.exit_handler(DocLoader().main(sys.argv))
327