[mod] revise args and conf

This commit is contained in:
Christian Fraß 2024-05-31 14:06:58 +02:00
parent e8bf8073ca
commit 5e1658c2de
2 changed files with 106 additions and 90 deletions

View file

@ -95,8 +95,8 @@ def api_macro_save(
environment : str, environment : str,
username : str, username : str,
password : str, password : str,
domain : str, domain_base : str,
name : str, domain_path,
type_ : str, type_ : str,
content : str content : str
): ):
@ -107,12 +107,28 @@ def api_macro_save(
"nameserver", "nameserver",
"info", "info",
{ {
"domain": domain, "domain": domain_base,
} }
) )
matching = list( matching = list(
filter( filter(
lambda record: ((record["name"] == (name + "." + domain)) and (record["type"] == type_)), lambda record: (
(
(
(domain_path is None)
and
(record["name"] == domain)
)
or
(
(domain_path is not None)
and
(record["name"] == (domain_path + "." + domain_base))
)
)
and
(record["type"] == type_)
),
info["record"] info["record"]
) )
) )
@ -124,8 +140,8 @@ def api_macro_save(
"nameserver", "nameserver",
"createRecord", "createRecord",
{ {
"domain": domain, "domain": domain_base,
"name": name, "name": domain_path,
"type": type_, "type": type_,
"content": content, "content": content,
} }
@ -159,8 +175,8 @@ def api_macro_delete(
environment : str, environment : str,
username : str, username : str,
password : str, password : str,
domain : str, domain_base : str,
name : str, domain_path,
type_ type_
): ):
accesstoken = api_macro_login(environment, username, password) accesstoken = api_macro_login(environment, username, password)
@ -170,13 +186,25 @@ def api_macro_delete(
"nameserver", "nameserver",
"info", "info",
{ {
"domain": domain, "domain": domain_base,
} }
) )
matching = list( matching = list(
filter( filter(
lambda record: ( lambda record: (
(record["name"] == (name + "." + domain)) (
(
(domain_path is None)
and
(record["name"] == domain_base)
)
or
(
(domain_path is not None)
and
(record["name"] == (domain_path + "." + domain_base))
)
)
and and
( (
(type_ is None) (type_ is None)
@ -200,3 +228,4 @@ def api_macro_delete(
) )
api_macro_logout(environment, accesstoken) api_macro_logout(environment, accesstoken)

View file

@ -1,10 +1,10 @@
def main( def main(
): ):
## args ## args
argumentparser = _argparse.ArgumentParser( argument_parser = _argparse.ArgumentParser(
description = "INWX CLI Frontend" description = "INWX CLI Frontend"
) )
argumentparser.add_argument( argument_parser.add_argument(
"-c", "-c",
"--conf", "--conf",
type = str, type = str,
@ -13,16 +13,16 @@ def main(
metavar = "<conf>", metavar = "<conf>",
help = "path to configuration file", help = "path to configuration file",
) )
argumentparser.add_argument( argument_parser.add_argument(
"-e", "-e",
"--environment", "--environment",
type = str, type = str,
dest = "environment", dest = "environment",
metavar = "<environment>", metavar = "<environment>",
default = None, default = None,
help = "environment to use; one of the keys in the 'url' file of the configuration; overwrites the configuration value", help = "environment to use; one of the keys in the 'url' node of the configuration; overwrites the configuration value",
) )
argumentparser.add_argument( argument_parser.add_argument(
"-u", "-u",
"--username", "--username",
type = str, type = str,
@ -31,7 +31,7 @@ def main(
default = None, default = None,
help = "username; overwrites the configuration value", help = "username; overwrites the configuration value",
) )
argumentparser.add_argument( argument_parser.add_argument(
"-p", "-p",
"--password", "--password",
type = str, type = str,
@ -40,25 +40,16 @@ def main(
default = None, default = None,
help = "password; overwrites the configuration value", help = "password; overwrites the configuration value",
) )
argumentparser.add_argument( argument_parser.add_argument(
"-b", "-d",
"--domain-base", "--domain",
type = str, type = str,
dest = "domain_base", dest = "domain",
default = None, default = None,
metavar = "<domain-base>", metavar = "<domain>",
help = "the domain, which holds the records" help = "the domain to work with"
) )
argumentparser.add_argument( argument_parser.add_argument(
"-n",
"--domain-path",
type = str,
dest = "domain_path",
default = None,
metavar = "<domain-path>",
help = "the record name/sub domain to work with"
)
argumentparser.add_argument(
"-t", "-t",
"--type", "--type",
type = str, type = str,
@ -67,7 +58,7 @@ def main(
metavar = "<type>", metavar = "<type>",
help = "the record type (A, AAAA, TXT, …)" help = "the record type (A, AAAA, TXT, …)"
) )
argumentparser.add_argument( argument_parser.add_argument(
"-v", "-v",
"--value", "--value",
type = str, type = str,
@ -76,7 +67,7 @@ def main(
metavar = "<value>", metavar = "<value>",
help = "value for the record" help = "value for the record"
) )
argumentparser.add_argument( argument_parser.add_argument(
"-x", "-x",
"--challenge-prefix", "--challenge-prefix",
type = str, type = str,
@ -85,7 +76,7 @@ def main(
default = "_acme-challenge", default = "_acme-challenge",
help = "which subdomain to use for ACME challanges", help = "which subdomain to use for ACME challanges",
) )
argumentparser.add_argument( argument_parser.add_argument(
"-w", "-w",
"--delay", "--delay",
type = float, type = float,
@ -94,7 +85,7 @@ def main(
metavar = "<delay>", metavar = "<delay>",
help = "seconds to wait at end of certbot auth hook", help = "seconds to wait at end of certbot auth hook",
) )
argumentparser.add_argument( argument_parser.add_argument(
type = str, type = str,
dest = "action", dest = "action",
choices = [ choices = [
@ -113,9 +104,9 @@ def main(
[ [
{"name": "conf-schema", "requirements": []}, {"name": "conf-schema", "requirements": []},
{"name": "info", "requirements": []}, {"name": "info", "requirements": []},
{"name": "list", "requirements": ["<domain-base>"]}, {"name": "list", "requirements": ["<domain>"]},
{"name": "save", "requirements": ["<domain-base>", "<domain-path>", "<type>", "<value>"]}, {"name": "save", "requirements": ["<domain>", "<type>", "<value>"]},
{"name": "delete", "requirements": ["<domain-base>", "<domain-path>"]}, {"name": "delete", "requirements": ["<domain>"]},
{"name": "certbot-hook", "requirements": []}, {"name": "certbot-hook", "requirements": []},
], ],
[ [
@ -144,20 +135,23 @@ def main(
} }
), ),
) )
arguments = argumentparser.parse_args() args = argument_parser.parse_args()
## conf ## conf
conf_load(arguments.conf) conf_load(args.conf)
## vars ## vars
environment = (arguments.environment or conf_get("environment")) environment = (args.environment or conf_get("environment"))
account_username = (arguments.username or conf_get("account.username")) account_username = (args.username or conf_get("account.username"))
account_password = (arguments.password or conf_get("account.password")) account_password = (args.password or conf_get("account.password"))
domain_parts = (None if (args.domain is None) else args.domain.split("."))
domain_base = (None if (domain_parts is None) else ".".join(domain_parts[-2:]))
domain_path = (None if ((domain_parts is None) or (len(domain_parts[:-2]) <= 0)) else ".".join(domain_parts[:-2]))
## exec ## exec
if (arguments.action == "conf-schema"): if (args.action == "conf-schema"):
print(_json.dumps(conf_schema(), indent = "\t")) print(_json.dumps(conf_schema(), indent = "\t"))
elif (arguments.action == "info"): elif (args.action == "info"):
if (account_username is None): if (account_username is None):
raise ValueError("account username required") raise ValueError("account username required")
else: else:
@ -170,73 +164,67 @@ def main(
account_password account_password
) )
print(_json.dumps(result, indent = "\t")) print(_json.dumps(result, indent = "\t"))
elif (arguments.action == "list"): elif (args.action == "list"):
if (account_username is None): if (account_username is None):
raise ValueError("account username required") raise ValueError("account username required")
else: else:
if (account_password is None): if (account_password is None):
raise ValueError("account password required") raise ValueError("account password required")
else: else:
if (arguments.domain_base is None): if (args.domain_base is None):
raise ValueError("domain base required") raise ValueError("domain base required")
else: else:
result = api_macro_list( result = api_macro_list(
environment, environment,
account_username, account_username,
account_password, account_password,
arguments.domain_base domain_base
) )
print(_json.dumps(result, indent = "\t")) print(_json.dumps(result, indent = "\t"))
elif (arguments.action == "save"): elif (args.action == "save"):
if (account_username is None): if (account_username is None):
raise ValueError("account username required") raise ValueError("account username required")
else: else:
if (account_password is None): if (account_password is None):
raise ValueError("account password required") raise ValueError("account password required")
else: else:
if (arguments.domain_base is None): if (args.domain is None):
raise ValueError("domain base required") raise ValueError("domain required")
else: else:
if (arguments.domain_base is None): if (args.type is None):
raise ValueError("domain path required") raise ValueError("type required")
else: else:
if (arguments.type is None): if (args.value is None):
raise ValueError("type required") raise ValueError("value required")
else: else:
if (arguments.value is None): api_macro_save(
raise ValueError("value required") environment,
else: account_username,
api_macro_save( account_password,
environment, domain_base,
account_username, domain_path,
account_password, args.type,
arguments.domain_base, args.value
arguments.domain_path,
arguments.type,
arguments.value
) )
elif (arguments.action == "delete"): elif (args.action == "delete"):
if (account_username is None): if (account_username is None):
raise ValueError("account username required") raise ValueError("account username required")
else: else:
if (account_password is None): if (account_password is None):
raise ValueError("account password required") raise ValueError("account password required")
else: else:
if (arguments.domain_base is None): if (args.domain is None):
raise ValueError("domain base required") raise ValueError("domain required")
else: else:
if (arguments.domain_base is None): api_macro_delete(
raise ValueError("domain path required") environment,
else: account_username,
api_macro_delete( account_password,
environment, domain_base,
account_username, domain_path,
account_password, args.type
arguments.domain_base, )
arguments.domain_path, elif (args.action == "certbot-hook"):
arguments.type
)
elif (arguments.action == "certbot-hook"):
if (account_username is None): if (account_username is None):
raise ValueError("account username required") raise ValueError("account username required")
else: else:
@ -244,25 +232,24 @@ def main(
raise ValueError("account password required") raise ValueError("account password required")
else: else:
domain_full_parts = _os.environ["CERTBOT_DOMAIN"].split(".") domain_full_parts = _os.environ["CERTBOT_DOMAIN"].split(".")
account = ".".join(domain_full_parts[-2:]) domain_base = ".".join(domain_full_parts[-2:])
concern = ".".join(domain_full_parts[:-2]) domain_path_stripped = ".".join(domain_full_parts[:-2])
domain = account domain_path = (args.challenge_prefix + "." + domain_path_stripped)
name = (arguments.challenge_prefix + "." + concern)
type_ = "TXT" type_ = "TXT"
content = _os.environ["CERTBOT_VALIDATION"] content = _os.environ["CERTBOT_VALIDATION"]
api_macro_save( api_macro_save(
environment, environment,
account_username, account_username,
account_password, account_password,
domain, domain_base,
name, domain_path,
type_, type_,
content content
) )
_time.sleep(arguments.delay) _time.sleep(args.delay)
# print(_json.dumps(result, indent = "\t")) # print(_json.dumps(result, indent = "\t"))
else: else:
log("unhandled action '%s'" % (arguments.action, )) log("unhandled action '%s'" % (args.action, ))
try: try: