From 5e1658c2de4c2ea48de6bbc0738b9ea795647c23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Fra=C3=9F?= Date: Fri, 31 May 2024 14:06:58 +0200 Subject: [PATCH] [mod] revise args and conf --- source/macros.py | 49 ++++++++++++---- source/main.py | 147 +++++++++++++++++++++-------------------------- 2 files changed, 106 insertions(+), 90 deletions(-) diff --git a/source/macros.py b/source/macros.py index 68ceaaf..a4c836c 100644 --- a/source/macros.py +++ b/source/macros.py @@ -95,8 +95,8 @@ def api_macro_save( environment : str, username : str, password : str, - domain : str, - name : str, + domain_base : str, + domain_path, type_ : str, content : str ): @@ -107,12 +107,28 @@ def api_macro_save( "nameserver", "info", { - "domain": domain, + "domain": domain_base, } ) matching = list( 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"] ) ) @@ -124,8 +140,8 @@ def api_macro_save( "nameserver", "createRecord", { - "domain": domain, - "name": name, + "domain": domain_base, + "name": domain_path, "type": type_, "content": content, } @@ -159,8 +175,8 @@ def api_macro_delete( environment : str, username : str, password : str, - domain : str, - name : str, + domain_base : str, + domain_path, type_ ): accesstoken = api_macro_login(environment, username, password) @@ -170,13 +186,25 @@ def api_macro_delete( "nameserver", "info", { - "domain": domain, + "domain": domain_base, } ) matching = list( filter( 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 ( (type_ is None) @@ -200,3 +228,4 @@ def api_macro_delete( ) api_macro_logout(environment, accesstoken) + diff --git a/source/main.py b/source/main.py index d132237..bfd5a80 100644 --- a/source/main.py +++ b/source/main.py @@ -1,10 +1,10 @@ def main( ): ## args - argumentparser = _argparse.ArgumentParser( + argument_parser = _argparse.ArgumentParser( description = "INWX CLI Frontend" ) - argumentparser.add_argument( + argument_parser.add_argument( "-c", "--conf", type = str, @@ -13,16 +13,16 @@ def main( metavar = "", help = "path to configuration file", ) - argumentparser.add_argument( + argument_parser.add_argument( "-e", "--environment", type = str, dest = "environment", metavar = "", 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", "--username", type = str, @@ -31,7 +31,7 @@ def main( default = None, help = "username; overwrites the configuration value", ) - argumentparser.add_argument( + argument_parser.add_argument( "-p", "--password", type = str, @@ -40,25 +40,16 @@ def main( default = None, help = "password; overwrites the configuration value", ) - argumentparser.add_argument( - "-b", - "--domain-base", + argument_parser.add_argument( + "-d", + "--domain", type = str, - dest = "domain_base", + dest = "domain", default = None, - metavar = "", - help = "the domain, which holds the records" + metavar = "", + help = "the domain to work with" ) - argumentparser.add_argument( - "-n", - "--domain-path", - type = str, - dest = "domain_path", - default = None, - metavar = "", - help = "the record name/sub domain to work with" - ) - argumentparser.add_argument( + argument_parser.add_argument( "-t", "--type", type = str, @@ -67,7 +58,7 @@ def main( metavar = "", help = "the record type (A, AAAA, TXT, …)" ) - argumentparser.add_argument( + argument_parser.add_argument( "-v", "--value", type = str, @@ -76,7 +67,7 @@ def main( metavar = "", help = "value for the record" ) - argumentparser.add_argument( + argument_parser.add_argument( "-x", "--challenge-prefix", type = str, @@ -85,7 +76,7 @@ def main( default = "_acme-challenge", help = "which subdomain to use for ACME challanges", ) - argumentparser.add_argument( + argument_parser.add_argument( "-w", "--delay", type = float, @@ -94,7 +85,7 @@ def main( metavar = "", help = "seconds to wait at end of certbot auth hook", ) - argumentparser.add_argument( + argument_parser.add_argument( type = str, dest = "action", choices = [ @@ -113,9 +104,9 @@ def main( [ {"name": "conf-schema", "requirements": []}, {"name": "info", "requirements": []}, - {"name": "list", "requirements": [""]}, - {"name": "save", "requirements": ["", "", "", ""]}, - {"name": "delete", "requirements": ["", ""]}, + {"name": "list", "requirements": [""]}, + {"name": "save", "requirements": ["", "", ""]}, + {"name": "delete", "requirements": [""]}, {"name": "certbot-hook", "requirements": []}, ], [ @@ -144,20 +135,23 @@ def main( } ), ) - arguments = argumentparser.parse_args() + args = argument_parser.parse_args() ## conf - conf_load(arguments.conf) + conf_load(args.conf) ## vars - environment = (arguments.environment or conf_get("environment")) - account_username = (arguments.username or conf_get("account.username")) - account_password = (arguments.password or conf_get("account.password")) + environment = (args.environment or conf_get("environment")) + account_username = (args.username or conf_get("account.username")) + 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 - if (arguments.action == "conf-schema"): + if (args.action == "conf-schema"): print(_json.dumps(conf_schema(), indent = "\t")) - elif (arguments.action == "info"): + elif (args.action == "info"): if (account_username is None): raise ValueError("account username required") else: @@ -170,73 +164,67 @@ def main( account_password ) print(_json.dumps(result, indent = "\t")) - elif (arguments.action == "list"): + elif (args.action == "list"): if (account_username is None): raise ValueError("account username required") else: if (account_password is None): raise ValueError("account password required") else: - if (arguments.domain_base is None): + if (args.domain_base is None): raise ValueError("domain base required") else: result = api_macro_list( environment, account_username, account_password, - arguments.domain_base + domain_base ) print(_json.dumps(result, indent = "\t")) - elif (arguments.action == "save"): + elif (args.action == "save"): if (account_username is None): raise ValueError("account username required") else: if (account_password is None): raise ValueError("account password required") else: - if (arguments.domain_base is None): - raise ValueError("domain base required") + if (args.domain is None): + raise ValueError("domain required") else: - if (arguments.domain_base is None): - raise ValueError("domain path required") + if (args.type is None): + raise ValueError("type required") else: - if (arguments.type is None): - raise ValueError("type required") + if (args.value is None): + raise ValueError("value required") else: - if (arguments.value is None): - raise ValueError("value required") - else: - api_macro_save( - environment, - account_username, - account_password, - arguments.domain_base, - arguments.domain_path, - arguments.type, - arguments.value + api_macro_save( + environment, + account_username, + account_password, + domain_base, + domain_path, + args.type, + args.value ) - elif (arguments.action == "delete"): + elif (args.action == "delete"): if (account_username is None): raise ValueError("account username required") else: if (account_password is None): raise ValueError("account password required") else: - if (arguments.domain_base is None): - raise ValueError("domain base required") + if (args.domain is None): + raise ValueError("domain required") else: - if (arguments.domain_base is None): - raise ValueError("domain path required") - else: - api_macro_delete( - environment, - account_username, - account_password, - arguments.domain_base, - arguments.domain_path, - arguments.type - ) - elif (arguments.action == "certbot-hook"): + api_macro_delete( + environment, + account_username, + account_password, + domain_base, + domain_path, + args.type + ) + elif (args.action == "certbot-hook"): if (account_username is None): raise ValueError("account username required") else: @@ -244,25 +232,24 @@ def main( raise ValueError("account password required") else: domain_full_parts = _os.environ["CERTBOT_DOMAIN"].split(".") - account = ".".join(domain_full_parts[-2:]) - concern = ".".join(domain_full_parts[:-2]) - domain = account - name = (arguments.challenge_prefix + "." + concern) + domain_base = ".".join(domain_full_parts[-2:]) + domain_path_stripped = ".".join(domain_full_parts[:-2]) + domain_path = (args.challenge_prefix + "." + domain_path_stripped) type_ = "TXT" content = _os.environ["CERTBOT_VALIDATION"] api_macro_save( environment, account_username, account_password, - domain, - name, + domain_base, + domain_path, type_, content ) - _time.sleep(arguments.delay) + _time.sleep(args.delay) # print(_json.dumps(result, indent = "\t")) else: - log("unhandled action '%s'" % (arguments.action, )) + log("unhandled action '%s'" % (args.action, )) try: