xref: /3.0.3-GA/couchbase-cli/couchbase-cli (revision 73c1b301)
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4"""
5couchbase-cli - command-line cluster administration tool
6"""
7
8import getopt
9import sys
10import os
11
12import listservers
13import buckets
14import node
15import xdcr
16import info
17import util_cli as util
18from usage import usage, short_usage
19from timeout import TimeoutException
20
21CB_USERNAME = 'CB_REST_USERNAME'
22CB_PASSWORD = 'CB_REST_PASSWORD'
23
24def main():
25    for x in sys.argv:
26        if x in ('-h', '-H', '--help', 'help'):
27            usage()
28
29    if len(sys.argv) <= 2:
30        short_usage()
31
32    debug = False
33
34    (cluster, user, password) = ('', '', '')
35
36    try:
37        (opts, _args) = getopt.getopt(sys.argv[2:],
38                                     'a:b:c:dse:gdlmo:OPp:r:Ssu:vV', [
39                'cluster=',
40                'debug',
41                'storage',
42                'password=',
43                'user=',
44                'mem',
45                'output=',
46                'os',
47                'ports',
48                'stats',
49                'license',
50                'server-add=',
51                'server-add-username=',
52                'server-add-password=',
53                'server-remove=',
54                'server-failover=',
55                'server-recovery=',
56                'cluster-init=',
57                'cluster-init-username=',
58                'cluster-username=',
59                'cluster-init-password=',
60                'cluster-password=',
61                'cluster-init-port=',
62                'cluster-port=',
63                'cluster-init-ramsize=',
64                'cluster-ramsize=',
65                'enable-auto-failover=',
66                'auto-failover-timeout=',
67                'enable-notification=',
68                'enable-email-alert=',
69                'compaction-db-percentage=',
70                'compaction-db-size=',
71                'compaction-view-percentage=',
72                'compaction-view-size=',
73                'compaction-period-from=',
74                'compaction-period-to=',
75                'enable-compaction-abort=',
76                'enable-compaction-parallel=',
77                'metadata-purge-interval=',
78                'node-init=',
79                'node-init-data-path=',
80                'node-init-index-path=',
81                'node-init-hostname=',
82                'bucket=',
83                'bucket-type=',
84                'bucket-port=',
85                'bucket-password=',
86                'bucket-ramsize=',
87                'bucket-replica=',
88                'bucket-priority=',
89                'bucket-eviction-policy=',
90                'enable-flush=',
91                'enable-index-replica=',
92                'wait',
93                'force',
94                'data-only',
95                'view-only',
96                'version',
97                'xdcr-cluster-name=',
98                'xdcr-hostname=',
99                'xdcr-username=',
100                'xdcr-password=',
101                'xdcr-from-bucket=',
102                'xdcr-to-bucket=',
103                'xdcr-replicator=',
104                'xdcr-replication-mode=',
105                'xdcr-demand-encryption=',
106                'xdcr-certificate=',
107                'create',
108                'edit',
109                'delete',
110                'list',
111                'set',
112                'pause',
113                'resume',
114                'settings',
115                'ro-username=',
116                'ro-password=',
117                'max-concurrent-reps=',
118                'checkpoint-interval=',
119                'worker-batch-size='
120                'doc-batch-size=',
121                'failure-restart-interval=',
122                'optimistic-replication-threshold=',
123                'email-recipients=',
124                'email-sender=',
125                'email-user=',
126                'email-password=',
127                'email-host=',
128                'email-port=',
129                'enable-email-encrypt=',
130                'alert-auto-failover-node',
131                'alert-auto-failover-max-reached',
132                'alert-auto-failover-node-down',
133                'alert-auto-failover-cluster-small',
134                'alert-ip-changed',
135                'alert-disk-space',
136                'alert-meta-overhead',
137                'alert-meta-oom',
138                'alert-write-failed',
139                'group-name=',
140                'rename=',
141                'add-servers=',
142                'move-servers=',
143                'from-group=',
144                'to-group=',
145                'retrieve-cert=',
146                'regenerate-cert=',
147                'recovery-type=',
148                'recovery-buckets=',
149                'nodes=',
150                'all-nodes',
151                'upload',
152                'upload-host=',
153                'customer=',
154                'ticket=',
155                ])
156    except getopt.GetoptError, err:
157        usage(err)
158    if len(_args) > 0:
159        err_message = ("Possible missing '--' in front of one of parameters '%s'," +
160                       " please see -h for more help.") % _args
161        usage(err_message)
162    commands = {
163        'host-list'         : listservers.ListServers,
164        'server-list'       : listservers.ListServers,
165        'server-info'       : info.Info,
166        'server-eshell'     : info.Info,
167        'server-add'        : node.Node,
168        'server-readd'      : node.Node,
169        'group-manage'      : node.Node,
170        'rebalance'         : node.Node,
171        'rebalance-stop'    : node.Node,
172        'rebalance-status'  : node.Node,
173        'eject-server'      : node.Node,
174        'failover'          : node.Node,
175        'recovery'          : node.Node,
176        'cluster-init'      : node.Node,
177        'cluster-edit'      : node.Node,
178        'ssl-manage'        : node.Node,
179        'node-init'         : node.Node,
180        'user-manage'       : node.Node,
181        'collect-logs-start'  : node.Node,
182        'collect-logs-stop'   : node.Node,
183        'collect-logs-status' : node.Node,
184        'bucket-list'       : buckets.Buckets,
185        'bucket-create'     : buckets.Buckets,
186        'bucket-edit'       : buckets.Buckets,
187        'bucket-delete'     : buckets.Buckets,
188        'bucket-flush'      : buckets.Buckets,
189        'bucket-compact'    : buckets.Buckets,
190        'setting-compaction'    : node.Node,
191        'setting-notification'  : node.Node,
192        'setting-autofailover'  : node.Node,
193        'setting-alert'         : node.Node,
194        'setting-xdcr'          : xdcr.XDCR,
195        'xdcr-setup'            : xdcr.XDCR,
196        'xdcr-replicate'        : xdcr.XDCR,
197        }
198
199    cmd = sys.argv[1]
200
201    if cmd not in commands:
202        err_message = ("'%s' is not a couchbase-cli command;" +
203                       " please see -h for more help.") % cmd
204        usage(err_message)
205
206    for (opt, arg) in opts:
207        if opt in ('-c', '--cluster'):
208            cluster = arg
209        if opt in ('-u', '--user'):
210            user = arg
211        if opt in ('-p', '--password'):
212            password = arg
213        if opt in ('-d', '--debug'):
214            debug = True
215
216    if not cluster:
217        usage("please provide a CLUSTER, or use -h for more help.")
218
219    if cmd != "cluster-init":
220        if not user:
221            if CB_USERNAME in os.environ:
222                user = os.environ[CB_USERNAME]
223            else:
224                usage("Username cannot be empty. \
225Please use -u or set environment variable %s."  % CB_USERNAME)
226        if not password:
227            if CB_PASSWORD in os.environ:
228                password = os.environ[CB_PASSWORD]
229            else:
230                usage("Password cannot be empty. \
231Please use -p or set environment variable %s." % CB_PASSWORD)
232
233    server, port = util.hostport(cluster)
234
235    if debug:
236        print "INFO: running command: %s" % cmd
237
238    c = commands[cmd]()
239    try:
240        c.runCmd(cmd, server, port, user, password, opts)
241    except TimeoutException, err:
242        print "TIMED OUT: command: %s: %s:%d, %s" % (cmd, server, port, err)
243        sys.exit(1)
244    except KeyboardInterrupt:
245        print "INTERRUPTED: command: %s: %s:%d" % (cmd, server, port)
246        sys.exit(1)
247    except Exception, err:
248        print "ERROR: command: %s: %s:%d, %s" % (cmd, server, port, err)
249        sys.exit(1)
250
251if __name__ == '__main__':
252    main()
253