Skip to content

Commit 4e9d07a

Browse files
Make shared func for parsing config bools
1 parent 0a3eb0c commit 4e9d07a

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

mongodb_consistent_backup/Backup/Mongodump/MongodumpThread.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from signal import signal, SIGINT, SIGTERM, SIG_IGN
99
from subprocess import Popen, PIPE
1010

11-
from mongodb_consistent_backup.Common import is_datetime
11+
from mongodb_consistent_backup.Common import is_datetime, parse_config_bool
1212
from mongodb_consistent_backup.Oplog import Oplog
1313

1414

@@ -58,11 +58,10 @@ def close(self, exit_code=None, frame=None):
5858
sys.exit(self.exit_code)
5959

6060
def do_ssl(self):
61-
if isinstance(self.ssl_enabled, bool):
62-
return self.ssl_enabled
63-
elif isinstance(self.ssl_enabled, str) and self.ssl_enabled.rstrip().lower() is "true":
64-
return True
65-
return False
61+
return parse_config_bool(self.ssl_enabled)
62+
63+
def do_ssl_insecure(self):
64+
return parse_config_bool(self.ssl_insecure)
6665

6766
def parse_mongodump_line(self, line):
6867
try:
@@ -153,7 +152,7 @@ def mongodump_cmd(self):
153152
mongodump_flags.extend(["--sslCRLFile", self.ssl_crl_file])
154153
if self.client_cert_file:
155154
mongodump_flags.extend(["--sslPEMKeyFile", self.ssl_cert_file])
156-
if self.ssl_insecure:
155+
if self.do_ssl_insecure():
157156
mongodump_flags.extend(["--sslAllowInvalidCertificates", "--sslAllowInvalidHostnames"])
158157
mongodump_cmd.extend(mongodump_flags)
159158
return mongodump_cmd

mongodb_consistent_backup/Common/Config.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88
from yconf.util import NestedDict
99

1010

11+
def parse_config_bool(item):
12+
try:
13+
if isinstance(item, bool):
14+
return item
15+
elif isinstance(item, str):
16+
if item.rstrip().lower() is "true":
17+
return True
18+
except:
19+
return False
20+
return False
21+
22+
1123
class PrintVersions(Action):
1224
def __init__(self, option_strings, dest, nargs=0, **kwargs):
1325
super(PrintVersions, self).__init__(option_strings=option_strings, dest=dest, nargs=nargs, **kwargs)

mongodb_consistent_backup/Common/DB.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from ssl import CERT_REQUIRED, CERT_NONE
88
from time import sleep
99

10+
from mongodb_consistent_backup.Common import parse_config_bool
1011
from mongodb_consistent_backup.Errors import DBAuthenticationError, DBConnectionError, DBOperationError, Error
1112

1213

@@ -37,11 +38,10 @@ def __init__(self, uri, config, do_replset=False, read_pref='primaryPreferred',
3738
self.auth_if_required()
3839

3940
def do_ssl(self):
40-
if isinstance(self.ssl_enabled, bool):
41-
return self.ssl_enabled
42-
elif isinstance(self.ssl_enabled, str) and self.ssl_enabled.rstrip().lower() is "true":
43-
return True
44-
return False
41+
return parse_config_bool(self.ssl_enabled)
42+
43+
def do_ssl_insecure(self):
44+
return parse_config_bool(self.ssl_insecure)
4545

4646
def client_opts(self):
4747
opts = {
@@ -67,7 +67,7 @@ def client_opts(self):
6767
"ssl_certfile": self.ssl_client_cert_file,
6868
"ssl_cert_reqs": CERT_REQUIRED,
6969
})
70-
if self.ssl_insecure:
70+
if self.do_ssl_insecure():
7171
opts["ssl_cert_reqs"] = CERT_NONE
7272
return opts
7373

mongodb_consistent_backup/Common/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from Config import Config # NOQA
1+
from Config import Config, parse_config_bool # NOQA
22
from DB import DB # NOQA
33
from LocalCommand import LocalCommand # NOQA
44
from Lock import Lock # NOQA

0 commit comments

Comments
 (0)