Skip to content

Commit

Permalink
Modified conf file feature
Browse files Browse the repository at this point in the history
* Modified to ignore default boolean input arguments while merging with conf file values.

* Modified to print warning message if input conf file not exist.

* Fixed to work with command line password entry.
  • Loading branch information
hbpatre committed Mar 2, 2018
1 parent abe9b22 commit 931ee55
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 38 deletions.
15 changes: 1 addition & 14 deletions asadm.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,25 +518,12 @@ def execute_asinfo_commands(commands_arg, seed, user=None, password=None, ssl_co


def main():
try:
import argparse
parser = argparse.ArgumentParser(
add_help=False, conflict_handler='resolve')
conf.add_options(parser.add_argument)
cli_args = parser.parse_args()
except Exception:
import optparse
usage = "usage: %prog [options]"
parser = optparse.OptionParser(usage, add_help_option=False)
conf.add_options(parser.add_option)
(cli_args, args) = parser.parse_args()

cli_args = conf.get_cli_args()

admin_version = get_version()

if cli_args.help:
conf.print_config_help()
#parser.print_help()
exit(0)

if cli_args.version:
Expand Down
80 changes: 56 additions & 24 deletions lib/utils/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,13 @@ def _flatten(conf_dict, instance):
return asadm_conf


def _merge(dct, merge_dct):
def _merge(dct, merge_dct, ignore_false=False):
for k, v in merge_dct.iteritems():
if (k in dct and isinstance(dct[k], dict)
and isinstance(merge_dct[k], collections.Mapping)):
_merge(dct[k], merge_dct[k])
_merge(dct[k], merge_dct[k], ignore_false=ignore_false)
else:
if merge_dct[k] is not None:
if merge_dct[k] is not None and (not ignore_false or merge_dct[k] is not False):
dct[k] = merge_dct[k]


Expand Down Expand Up @@ -257,14 +257,14 @@ def _getseeds(conf):
m = re_ipv6hostnameport.match(host)
if (m and len(m.groups()) == 3):
g = m.groups()
seeds.append((str(g[0]).strip("[]"), str(g[2]),
seeds.append((str(g[0]).strip("[]"), int(g[2]),
tls_name if (tls_name is not None) else str(g[1])))
continue

m = re_ipv6hostport.match(host)
if (m and len(m.groups()) == 2):
g = m.groups()
seeds.append((str(g[0]).strip("[]"), str(g[1]), tls_name))
seeds.append((str(g[0]).strip("[]"), int(g[1]), tls_name))
continue

m = re_ipv6host.match(host)
Expand All @@ -276,14 +276,14 @@ def _getseeds(conf):
m = re_ipv4hostnameport.match(host)
if (m and len(m.groups()) == 3):
g = m.groups()
seeds.append((str(g[0]).strip("[]"), str(g[2]),
seeds.append((str(g[0]).strip("[]"), int(g[2]),
tls_name if (tls_name is not None) else str(g[1])))
continue

m = re_ipv4hostport.match(host)
if (m and len(m.groups()) == 2):
g = m.groups()
seeds.append((str(g[0]).strip("[]"), str(g[1]), tls_name))
seeds.append((str(g[0]).strip("[]"), int(g[1]), tls_name))
continue

# ipv4 host only
Expand Down Expand Up @@ -313,12 +313,17 @@ def loadconfig(cli_args, logger):
# Load only config file.
if cli_args.only_config_file is not None:
f = cli_args.only_config_file
conffiles = [f]
try:
_merge(conf_dict, _loadfile(f, logger))
except Exception as e:
# Bail out of the primary file has parsing error.
logger.critical("Config file parse error: " + str(f) + " " + str(e).split("\n")[0])
conffiles = []

if os.path.exists(f):
try:
_merge(conf_dict, _loadfile(f, logger))
conffiles.append(f)
except Exception as e:
# Bail out of the primary file has parsing error.
logger.critical("Config file parse error: " + str(f) + " " + str(e).split("\n")[0])
else:
logger.warning("Config file read error : " + str(f) + " " + "No such file")

# Read config file if no-config-file is not specified
# is specified
Expand All @@ -328,7 +333,10 @@ def loadconfig(cli_args, logger):
# -> user specified conf file
conffiles = ["/etc/aerospike/astools.conf", ADMIN_HOME + "astools.conf"]
if cli_args.config_file:
conffiles.append(cli_args.config_file)
if os.path.exists(cli_args.config_file):
conffiles.append(cli_args.config_file)
else:
logger.warning("Config file read error : " + str(cli_args.config_file) + " " + "No such file")

for f in conffiles:
try:
Expand All @@ -341,7 +349,8 @@ def loadconfig(cli_args, logger):

# -> Command line
cli_dict = vars(cli_args)
_merge(asadm_dict, cli_dict)
# For boolean arguments, false is default value... so ignore it
_merge(asadm_dict, cli_dict, ignore_false=True)

# Find seed nods
seeds = _getseeds(asadm_dict)
Expand Down Expand Up @@ -412,9 +421,9 @@ def print_config_file_option():
print (" --tls-enable Enable TLS on connections. By default TLS is disabled.")
# Deprecated
# print(" --tls-encrypt-only Disable TLS certificate verification.\n")
print (" --tls-cafile=TLS_CAFILE\n"
print (" --tls-cafile=TLS_CAFILE <path>\n"
" Path to a trusted CA certificate file.")
print (" --tls-capath=TLS_CAPATH.\n"
print (" --tls-capath=TLS_CAPATH <path>\n"
" Path to a directory of trusted CA certificates.")
print (" --tls-protocols=TLS_PROTOCOLS\n"
" Set the TLS protocol selection criteria. This format\n"
Expand All @@ -428,7 +437,7 @@ def print_config_file_option():
" the same as Open_sSL's Cipher List Format documented\n"
" at https://www.openssl.org/docs/man1.0.1/apps/ciphers.\n"
" html")
print (" --tls-keyfile=TLS_KEYFILE\n"
print (" --tls-keyfile=TLS_KEYFILE <path>\n"
" Path to the key for mutual authentication (if\n"
" Aerospike Cluster is supporting it).")
print (" --tls-certfile=TLS_CERTFILE <path>\n"
Expand All @@ -447,7 +456,7 @@ def print_config_file_option():
print (" --tls-crl-check Enable CRL checking for leaf certificate. An error\n"
" occurs if a valid CRL files cannot be found in\n"
" tls_capath.")
print (" --tls-crl-checkall Enable CRL checking for entire certificate chain. An\n"
print (" --tls-crl-check-all Enable CRL checking for entire certificate chain. An\n"
" error occurs if a valid CRL files cannot be found in\n"
" tls_capath.")
print ("")
Expand All @@ -467,21 +476,34 @@ def config_file_help():
print "\n\n"
print ("Default configuration files are read from the following files in the given order:\n"
"/etc/aerospike/astools.conf ~/.aerospike/astools.conf\n"
"The following sections are read: (cluster aql include)\n"
"The following sections are read: (cluster asadm include)\n"
"The following options effect configuration file behavior\n")
print (" --no-config-file\n"
" Do not read any config file. Default: disabled")
print (" --instance <name>\n"
" Section with these instance is read. e.g in case instance \n"
" `a` is specified sections cluster_a, aql_a is read.")
" `a` is specified sections cluster_a, asadm_a is read.")
print (" --config-file <path>\n"
" Read this file after default configuration file.")
print (" --only-config-file <path>\n"
" Read only this configuration file.")
print ("\n")


def add_options(add_fn):
def get_cli_args():
have_argparse = True
try:
import argparse
parser = argparse.ArgumentParser(
add_help=False, conflict_handler='resolve')
add_fn = parser.add_argument
except Exception:
import optparse
have_argparse = False
usage = "usage: %prog [options]"
parser = optparse.OptionParser(usage, add_help_option=False)
add_fn = parser.add_option

add_fn("-V", "--version", action="store_true")
add_fn("-E", "--help", action="store_true")
add_fn("-e", "--execute")
Expand All @@ -497,7 +519,11 @@ def add_options(add_fn):
add_fn("-h", "--host")
add_fn("-p", "--port", type=int)
add_fn("-U", "--user")
add_fn("-P", "--password", action="store_const")
if have_argparse:
add_fn("-P", "--password", nargs="?")
else:
parser.add_option("-P", "--password", dest="password", action="store_const", const="prompt")

add_fn("--tls-enable", action="store_true")
add_fn("--tls-cafile")
add_fn("--tls-capath")
Expand All @@ -512,7 +538,7 @@ def add_options(add_fn):
add_fn("-t", "--tls-name")
add_fn("-s", "--services-alumni", action="store_true")
add_fn("-a", "--services-alternate", action="store_true")
add_fn("--timeout", type=float, default=5)
add_fn("--timeout", type=float)

add_fn("--config-file")
add_fn("--instance")
Expand Down Expand Up @@ -543,3 +569,9 @@ def add_options(add_fn):
add_fn("--tls_cert_blacklist")
add_fn("--tls_crl_check", action="store_true")
add_fn("--tls_crl_check_all", action="store_true")

if have_argparse:
return parser.parse_args()

(cli_args, args) = parser.parse_args()
return cli_args

0 comments on commit 931ee55

Please sign in to comment.