xref: /4.6.0/couchbase-cli/couchbase-cli (revision dffc1a76)
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
18import usage
19from timeout import TimeoutException
20
21CB_USERNAME = 'CB_REST_USERNAME'
22CB_PASSWORD = 'CB_REST_PASSWORD'
23
24def main():
25
26    debug = False
27
28    (cluster, user, password, ssl) = ('', '', '', False)
29
30    try:
31        (opts, _args) = getopt.getopt(sys.argv[2:],
32                                     'a:b:c:dse:gdlmo:OPp:r:Ssu:vVhH', [
33                'cluster=',
34                'debug',
35                'storage',
36                'password=',
37                'user=',
38                'mem',
39                'output=',
40                'os',
41                'ports',
42                'stats',
43                'license',
44                'server-add=',
45                'server-add-username=',
46                'server-add-password=',
47                'server-remove=',
48                'server-failover=',
49                'server-recovery=',
50                'cluster-init=',
51                'cluster-init-username=',
52                'cluster-username=',
53                'cluster-init-password=',
54                'cluster-password=',
55                'cluster-init-port=',
56                'cluster-port=',
57                'cluster-init-ramsize=',
58                'cluster-ramsize=',
59                'cluster-name=',
60                'cluster-index-ramsize=',
61                'cluster-fts-ramsize=',
62                'index-storage-setting=',
63                'enable-auto-failover=',
64                'auto-failover-timeout=',
65                'enable-notification=',
66                'enable-email-alert=',
67                'compaction-db-percentage=',
68                'compaction-db-size=',
69                'compaction-view-percentage=',
70                'compaction-view-size=',
71                'compaction-period-from=',
72                'compaction-period-to=',
73                'enable-compaction-abort=',
74                'enable-compaction-parallel=',
75                'gsi-compaction-mode=',
76                'compaction-gsi-percentage=',
77                'compaction-gsi-size=',
78                'compaction-gsi-interval=',
79                'compaction-gsi-period-from=',
80                'compaction-gsi-period-to=',
81                'enable-gsi-compaction-abort=',
82                'metadata-purge-interval=',
83                'node-init=',
84                'node-init-data-path=',
85                'node-init-index-path=',
86                'node-init-hostname=',
87                'bucket=',
88                'bucket-type=',
89                'bucket-port=',
90                'bucket-password=',
91                'bucket-ramsize=',
92                'bucket-replica=',
93                'bucket-priority=',
94                'bucket-eviction-policy=',
95                'enable-flush=',
96                'enable-index-replica=',
97                'enable-timestamps',
98                'wait',
99                'force',
100                'data-only',
101                'view-only',
102                'version',
103                'xdcr-cluster-name=',
104                'xdcr-hostname=',
105                'xdcr-username=',
106                'xdcr-password=',
107                'xdcr-from-bucket=',
108                'xdcr-to-bucket=',
109                'xdcr-replicator=',
110                'xdcr-replication-mode=',
111                'xdcr-demand-encryption=',
112                'xdcr-certificate=',
113                'create',
114                'edit',
115                'delete',
116                'list',
117                'set',
118                'pause',
119                'resume',
120                'settings',
121                'ro-username=',
122                'ro-password=',
123                'checkpoint-interval=',
124                'worker-batch-size='
125                'doc-batch-size=',
126                'failure-restart-interval=',
127                'optimistic-replication-threshold=',
128                'source-nozzle-per-node=',
129                'target-nozzle-per-node=',
130                'log-level=',
131                'stats-interval=',
132                'email-recipients=',
133                'email-sender=',
134                'email-user=',
135                'email-password=',
136                'email-host=',
137                'email-port=',
138                'enable-email-encrypt=',
139                'alert-auto-failover-node',
140                'alert-auto-failover-max-reached',
141                'alert-auto-failover-node-down',
142                'alert-auto-failover-cluster-small',
143                'alert-auto-failover-disabled',
144                'alert-ip-changed',
145                'alert-disk-space',
146                'alert-meta-overhead',
147                'alert-meta-oom',
148                'alert-write-failed',
149                'alert-audit-msg-dropped',
150                'group-name=',
151                'rename=',
152                'add-servers=',
153                'move-servers=',
154                'from-group=',
155                'to-group=',
156                'retrieve-cert=',
157                'regenerate-cert=',
158                'cluster-cert-info',
159                'node-cert-info',
160                'upload-cluster-ca=',
161                'set-node-certificate',
162                'extended',
163                'recovery-type=',
164                'recovery-buckets=',
165                'nodes=',
166                'all-nodes',
167                'upload',
168                'upload-host=',
169                'customer=',
170                'ticket=',
171                'services=',
172                'help',
173                'ssl',
174                'filter-expression=',
175                'audit-enabled=',
176                'audit-log-path=',
177                'audit-log-rotate-interval=',
178                'ldap-enabled=',
179                'ldap-admins=',
180                'ldap-roadmins=',
181                'ldap-default=',
182                'index-max-rollback-points=',
183                'index-stable-snapshot-interval=',
184                'index-memory-snapshot-interval=',
185                'index-threads=',
186                'index-log-level=',
187                'vm=',
188                'get-roles',
189                'roles=',
190                'my-roles',
191                'set-users=',
192                'set-names=',
193                'delete-users=',
194                ])
195    except getopt.GetoptError, err:
196        usage.command_error(err)
197    if len(_args) > 0:
198        err_message = ("Possible missing '--' in front of one of parameters '%s'," +
199                       " please see -h for more help.") % _args
200        usage.command_error(err_message)
201    commands = {
202        'host-list'         : listservers.ListServers,
203        'server-list'       : listservers.ListServers,
204        'server-info'       : info.Info,
205        'server-eshell'     : info.Info,
206        'server-add'        : node.Node,
207        'server-readd'      : node.Node,
208        'group-manage'      : node.Node,
209        'rebalance'         : node.Node,
210        'rebalance-stop'    : node.Node,
211        'rebalance-status'  : node.Node,
212        'eject-server'      : node.Node,
213        'failover'          : node.Node,
214        'recovery'          : node.Node,
215        'cluster-init'      : node.Node,
216        'cluster-edit'      : node.Node,
217        'ssl-manage'        : node.Node,
218        'node-init'         : node.Node,
219        'user-manage'       : node.Node,
220        'collect-logs-start'  : node.Node,
221        'collect-logs-stop'   : node.Node,
222        'collect-logs-status' : node.Node,
223        'bucket-list'       : buckets.Buckets,
224        'bucket-create'     : buckets.Buckets,
225        'bucket-edit'       : buckets.Buckets,
226        'bucket-delete'     : buckets.Buckets,
227        'bucket-flush'      : buckets.Buckets,
228        'bucket-compact'    : buckets.Buckets,
229        'setting-cluster'   : node.Node,
230        'setting-compaction'    : node.Node,
231        'setting-notification'  : node.Node,
232        'setting-autofailover'  : node.Node,
233        'setting-alert'         : node.Node,
234        'setting-audit'         : node.Node,
235        'setting-ldap'          : node.Node,
236        'admin-role-manage'     : node.Node,
237        'setting-xdcr'          : xdcr.XDCR,
238        'setting-index'         : node.Node,
239        'xdcr-setup'            : xdcr.XDCR,
240        'xdcr-replicate'        : xdcr.XDCR,
241        'help'                  : usage.Usage
242        }
243
244    if len(sys.argv) <= 1:
245        usage.short_usage(commands)
246
247    cmd = sys.argv[1]
248
249    if cmd not in commands:
250        # cmd not a command, maybe help is needed.
251        for x in sys.argv:
252            if x in ('-h', '-H', '--help'):
253                usage.all_help(commands) # exits program.
254
255        err_message = ("'%s' is not a couchbase-cli command;" +
256                       " please see -h for more help.") % cmd
257        usage.command_error(err_message) # exits program.
258
259    if cmd == "help":
260        usage.all_help(commands)
261
262    # If a command and -h,-H,--help give context sensitive help by passing cmd
263    for x in sys.argv:
264        if x in ('-h', '-H', '--help'):
265            usage.one_command_usage(cmd, commands)
266
267    for (opt, arg) in opts:
268        if opt in ('-c', '--cluster'):
269            cluster = arg
270        if opt in ('-u', '--user'):
271            user = arg
272        if opt in ('-p', '--password'):
273            password = arg
274        if opt in ('-d', '--debug'):
275            debug = True
276        if opt in ('-s', '--ssl'):
277            ssl = True
278
279    if cmd == "cluster-init" and not cluster:
280        cluster = "127.0.0.1:8091"
281
282    if not cluster:
283        usage.command_error("please provide a CLUSTER, or use -h for more help.")
284
285    if cmd != "cluster-init":
286        if not user:
287            if CB_USERNAME in os.environ:
288                user = os.environ[CB_USERNAME]
289            else:
290                usage.command_error("Username cannot be empty. \
291Please use -u or set environment variable %s."  % CB_USERNAME)
292        if not password:
293            if CB_PASSWORD in os.environ:
294                password = os.environ[CB_PASSWORD]
295            else:
296                usage.command_error("Password cannot be empty. \
297Please use -p or set environment variable %s." % CB_PASSWORD)
298
299    server, port = util.hostport(cluster)
300
301    if debug:
302        print "INFO: running command: %s" % cmd
303
304    c = commands[cmd]()
305    try:
306        c.runCmd(cmd, server, port, user, password, ssl, opts)
307    except TimeoutException, err:
308        print "TIMED OUT: command: %s: %s:%d, %s" % (cmd, server, port, err)
309        sys.exit(1)
310    except KeyboardInterrupt:
311        print "INTERRUPTED: command: %s: %s:%d" % (cmd, server, port)
312        sys.exit(1)
313    except Exception, err:
314        print "ERROR: command: %s: %s:%d, %s" % (cmd, server, port, err)
315        sys.exit(1)
316
317if __name__ == '__main__':
318    main()
319