Repository navigation
Expand file tree
/
Copy pathmongoctl.py
More file actions
199 lines (156 loc) · 6.92 KB
/
Copy pathmongoctl.py
File metadata and controls
199 lines (156 loc) · 6.92 KB
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
#!/usr/bin/env python
# The MIT License
# Copyright (c) 2012 ObjectLabs Corporation
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
__author__ = 'abdul'
###############################################################################
# Imports
###############################################################################
import sys
import traceback
import os
im
D101
port config
import objects.server
import repository
from dargparse import dargparse
from mongoctl_logging import (
log_error, log_info, turn_logging_verbose_on, log_verbose, log_exception
)
from mongoctl_command_config import MONGOCTL_PARSER_DEF
from errors import MongoctlException
from prompt import (
is_interactive_mode, set_interactive_mode, say_yes_to_everything,
say_no_to_everything
)
from utils import namespace_get_property
from users import parse_global_login_user_arg
from mongoctl_signal import init_mongoctl_signal_handler
###############################################################################
# Constants
###############################################################################
CONF_ROOT_ENV_VAR = "MONGOCTL_CONF"
SERVER_ID_PARAM = "server"
###############################################################################
# MAIN
###############################################################################
def main(args):
try:
do_main(args)
except MongoctlException,e:
log_error("mongoctl error: %s" % e)
log_exception(e)
exit(1)
except Exception, e:
log_exception(e)
raise
###############################################################################
def do_main(args):
init_mongoctl_signal_handler()
header = """
-------------------------------------------------------------------------------------------
__ _ ___ ___ ___ ____ ____/ /_/ /
/ ' \/ _ \/ _ \/ _ `/ _ \/ __/ __/ /
/_/_/_/\___/_//_/\_, /\___/\__/\__/_/
/___/
-------------------------------------------------------------------------------------------
"""
# Parse options
parser = get_mongoctl_cmd_parser()
if len(args) < 1:
print(header)
parser.print_help()
return
# Parse the arguments and call the function of the selected cmd
parsed_args = parser.parse_args(args)
# turn on verbose if specified
if namespace_get_property(parsed_args,"mongoctlVerbose"):
turn_logging_verbose_on()
# set interactive mode
non_interactive = namespace_get_property(parsed_args,'noninteractive')
non_interactive = False if non_interactive is None else non_interactive
set_interactive_mode(not non_interactive)
if not is_interactive_mode():
log_verbose("Running with noninteractive mode")
# set global prompt value
yes_all = parsed_args.yesToEverything
no_all = parsed_args.noToEverything
if yes_all and no_all:
raise MongoctlException("Cannot have --yes and --no at the same time. "
"Please choose either --yes or --no")
elif yes_all:
say_yes_to_everything()
elif no_all:
say_no_to_everything()
# set the global SSL_OFF flag
if parsed_args.clientSslMode:
objects.server.set_client_ssl_mode(parsed_args.clientSslMode)
# set the global USE_ALT_ADDRESS field
if parsed_args.useAltAddress:
use_alt_address = parsed_args.useAltAddress
log_info("Using alternative address '%s'..." % use_alt_address)
objects.server.USE_ALT_ADDRESS = use_alt_address
# set conf root if specified
if parsed_args.configRoot is not None:
config.set_config_root(parsed_args.configRoot)
elif os.getenv(CONF_ROOT_ENV_VAR) is not None:
config.set_config_root(os.getenv(CONF_ROOT_ENV_VAR))
# set cmd arg servers/clusters
if parsed_args.servers or parsed_args.clusters:
repository.set_commandline_servers_and_clusters(parsed_args.servers, parsed_args.clusters)
# get the function to call from the parser framework
command_function = parsed_args.func
# parse global login if present
username = namespace_get_property(parsed_args, "username")
password = namespace_get_property(parsed_args, "password")
server_id = namespace_get_property(parsed_args, SERVER_ID_PARAM)
parse_global_login_user_arg(username, password, server_id)
if server_id is not None:
# check if assumeLocal was specified
assume_local = namespace_get_property(parsed_args,"assumeLocal")
if assume_local:
objects.server.assume_local_server(server_id)
# execute command
log_info("")
return command_function(parsed_args)
###############################################################################
######################## #################################
######################## Commandline parsing #################################
######################## #################################
###############################################################################
###############################################################################
# Mongoctl main parser
###############################################################################
###############################################################################
def get_mongoctl_cmd_parser():
parser = dargparse.build_parser(MONGOCTL_PARSER_DEF)
return parser
###############################################################################
######################## ####################################
######################## BOOTSTRAP ####################################
######################## ####################################
###############################################################################
if __name__ == '__main__':
try:
main(sys.argv[1:])
except (SystemExit, KeyboardInterrupt) , e:
if e.code == 0:
pass
else:
raise
except:
traceback.print_exc()