inwx/source/main.py

273 lines
6.7 KiB
Python
Raw Normal View History

2024-05-31 11:24:53 +02:00
def main(
):
2024-05-31 11:24:53 +02:00
## args
argumentparser = _argparse.ArgumentParser(
description = "INWX CLI Frontend"
)
argumentparser.add_argument(
2023-05-29 12:03:38 +02:00
"-c",
"--conf",
2024-05-31 11:24:53 +02:00
type = str,
dest = "conf",
2023-05-29 12:03:38 +02:00
default = _os.path.join(str(_pathlib.Path.home()), ".inwx-conf.json"),
metavar = "<conf>",
help = "path to configuration file",
)
argumentparser.add_argument(
2023-05-29 12:03:38 +02:00
"-e",
"--environment",
2024-05-31 11:24:53 +02:00
type = str,
dest = "environment",
2023-05-29 12:03:38 +02:00
metavar = "<environment>",
default = None,
2024-05-31 11:24:53 +02:00
help = "environment to use; one of the keys in the 'url' file of the configuration; overwrites the configuration value",
)
argumentparser.add_argument(
2023-05-29 12:03:38 +02:00
"-u",
"--username",
2024-05-31 11:24:53 +02:00
type = str,
dest = "username",
2023-05-29 12:03:38 +02:00
metavar = "<username>",
default = None,
help = "username; overwrites the configuration value",
)
argumentparser.add_argument(
2023-05-29 12:03:38 +02:00
"-p",
"--password",
2024-05-31 11:24:53 +02:00
type = str,
dest = "password",
2023-05-29 12:03:38 +02:00
metavar = "<password>",
default = None,
help = "password; overwrites the configuration value",
)
argumentparser.add_argument(
2024-05-31 11:24:53 +02:00
"-b",
"--domain-base",
type = str,
dest = "domain_base",
default = None,
metavar = "<domain-base>",
help = "the domain, which holds the records"
)
argumentparser.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",
"--type",
type = str,
dest = "type",
default = None,
metavar = "<type>",
help = "the record type (A, AAAA, TXT, …)"
)
argumentparser.add_argument(
"-v",
"--value",
type = str,
dest = "value",
2023-05-29 12:03:38 +02:00
default = None,
2024-05-31 11:24:53 +02:00
metavar = "<value>",
help = "value for the record"
)
2023-05-29 11:39:33 +02:00
argumentparser.add_argument(
2023-05-29 12:03:38 +02:00
"-x",
"--challenge-prefix",
2024-05-31 11:24:53 +02:00
type = str,
2023-05-29 12:03:38 +02:00
dest = "challenge_prefix",
metavar = "<challenge-prefix>",
default = "_acme-challenge",
help = "which subdomain to use for ACME challanges",
)
argumentparser.add_argument(
"-w",
"--delay",
2023-05-29 11:39:33 +02:00
type = float,
2024-05-31 11:24:53 +02:00
dest = "delay",
2023-05-29 11:39:33 +02:00
default = 60.0,
2023-05-29 12:03:38 +02:00
metavar = "<delay>",
2023-05-29 11:39:33 +02:00
help = "seconds to wait at end of certbot auth hook",
)
argumentparser.add_argument(
2023-05-29 12:03:38 +02:00
type = str,
2024-05-31 11:24:53 +02:00
dest = "action",
choices = [
"conf-schema",
"info",
"list",
"save",
"delete",
"certbot-hook",
],
2023-05-29 12:03:38 +02:00
metavar = "<action>",
2024-05-31 11:24:53 +02:00
help = string_coin(
"action to execute; options:\n{{options}}",
{
"options": convey(
[
{"name": "conf-schema", "requirements": []},
{"name": "info", "requirements": []},
{"name": "list", "requirements": ["<domain-base>"]},
{"name": "save", "requirements": ["<domain-base>", "<domain-path>", "<type>", "<value>"]},
{"name": "delete", "requirements": ["<domain-base>", "<domain-path>"]},
{"name": "certbot-hook", "requirements": []},
],
[
lambda x: map(
lambda entry: string_coin(
"{{name}}{{macro_requirements}}",
{
"name": entry["name"],
"macro_requirements": (
""
if (len(entry["requirements"]) <= 0) else
string_coin(
" (requires: {{requirements}})",
{
"requirements": ",".join(entry["requirements"]),
}
)
),
}
),
x
),
" | ".join,
]
)
}
),
)
arguments = argumentparser.parse_args()
2024-05-31 11:24:53 +02:00
## conf
conf_load(arguments.conf)
2024-05-31 11:24:53 +02:00
## 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"))
## exec
if (arguments.action == "conf-schema"):
print(_json.dumps(conf_schema(), indent = "\t"))
elif (arguments.action == "info"):
if (account_username is None):
raise ValueError("account username required")
else:
if (account_password is None):
raise ValueError("account password required")
else:
result = api_macro_info(
environment,
account_username,
account_password
)
print(_json.dumps(result, indent = "\t"))
2023-05-29 12:03:38 +02:00
elif (arguments.action == "list"):
2024-05-31 11:24:53 +02:00
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")
else:
result = api_macro_list(
environment,
account_username,
account_password,
arguments.domain_base
)
print(_json.dumps(result, indent = "\t"))
2023-05-29 12:03:38 +02:00
elif (arguments.action == "save"):
2024-05-31 11:24:53 +02:00
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")
else:
if (arguments.domain_base is None):
raise ValueError("domain path required")
else:
if (arguments.type is None):
raise ValueError("type 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
)
2024-05-31 09:28:32 +02:00
elif (arguments.action == "delete"):
2024-05-31 11:24:53 +02:00
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")
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
)
2023-05-29 12:03:38 +02:00
elif (arguments.action == "certbot-hook"):
2024-05-31 11:24:53 +02:00
if (account_username is None):
raise ValueError("account username required")
else:
if (account_password is None):
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)
type_ = "TXT"
content = _os.environ["CERTBOT_VALIDATION"]
api_macro_save(
environment,
account_username,
account_password,
domain,
name,
type_,
content
)
_time.sleep(arguments.delay)
# print(_json.dumps(result, indent = "\t"))
else:
2023-05-29 12:03:38 +02:00
log("unhandled action '%s'" % (arguments.action, ))
try:
main()
except ValueError as error:
2024-05-31 11:24:53 +02:00
_sys.stderr.write("-- %s\n" % str(error))