inwx/source/main.py

164 lines
4 KiB
Python
Raw Normal View History

def args(
):
argumentparser = _argparse.ArgumentParser(
description = "INWX CLI Frontend"
)
argumentparser.add_argument(
2023-05-29 12:03:38 +02:00
"-c",
"--conf",
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",
dest = "environment",
2023-05-29 12:03:38 +02:00
metavar = "<environment>",
default = None,
help = "environment to use; one of the keys in the 'url' filed of the configuration; overwrites the configuration value",
)
argumentparser.add_argument(
2023-05-29 12:03:38 +02:00
"-u",
"--username",
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",
dest = "password",
2023-05-29 12:03:38 +02:00
metavar = "<password>",
default = None,
help = "password; overwrites the configuration value",
)
'''
argumentparser.add_argument(
2023-05-29 12:03:38 +02:00
"-d",
"--domain",
dest = "domain",
2023-05-29 12:03:38 +02:00
default = None,
metavar = "<domain>",
help = "the domain to work with"
)
'''
2023-05-29 11:39:33 +02:00
argumentparser.add_argument(
2023-05-29 12:03:38 +02:00
"-x",
"--challenge-prefix",
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
dest = "delay",
type = float,
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
"action",
type = str,
2024-05-31 09:28:32 +02:00
choices = ["info", "list", "save", "delete", "certbot-hook"],
2023-05-29 12:03:38 +02:00
metavar = "<action>",
2024-05-31 09:28:32 +02:00
help = "action to execute; options: info,list,save,delete,certbot-hook",
)
argumentparser.add_argument(
"parameter",
nargs = "*",
2023-05-29 12:03:38 +02:00
type = str,
metavar = "<parameters>",
help = "action specific parameters",
)
arguments = argumentparser.parse_args()
return arguments
def main(
):
arguments = args()
conf_load(arguments.conf)
if (not (arguments.environment is None)): conf_set("environment", arguments.environment)
if (not (arguments.username is None)): conf_set("account.username", arguments.username)
if (not (arguments.password is None)): conf_set("account.password", arguments.password)
2023-05-29 12:03:38 +02:00
if (arguments.action == "info"):
result = api_macro_info(
conf_get("environment"),
conf_get("account.username"),
conf_get("account.password")
)
print(_json.dumps(result, indent = "\t"))
2023-05-29 12:03:38 +02:00
elif (arguments.action == "list"):
domain = arguments.parameter[0]
result = api_macro_list(
conf_get("environment"),
conf_get("account.username"),
conf_get("account.password"),
domain
)
print(_json.dumps(result, indent = "\t"))
2023-05-29 12:03:38 +02:00
elif (arguments.action == "save"):
domain = arguments.parameter[0]
name = arguments.parameter[1]
type_ = arguments.parameter[2]
content = arguments.parameter[3]
api_macro_save(
conf_get("environment"),
conf_get("account.username"),
conf_get("account.password"),
domain,
name,
type_,
content
)
# print(_json.dumps(result, indent = "\t"))
2024-05-31 09:28:32 +02:00
elif (arguments.action == "delete"):
domain = arguments.parameter[0]
name = arguments.parameter[1]
type_ = (arguments.parameter[2] if (len(arguments.parameter) >= 2) else None)
api_macro_delete(
conf_get("environment"),
conf_get("account.username"),
conf_get("account.password"),
domain,
name,
type_
)
2023-05-29 12:03:38 +02:00
elif (arguments.action == "certbot-hook"):
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)
2023-05-29 11:39:33 +02:00
type_ = "TXT"
content = _os.environ["CERTBOT_VALIDATION"]
api_macro_save(
conf_get("environment"),
conf_get("account.username"),
conf_get("account.password"),
domain,
name,
type_,
content
)
2023-05-29 12:03:38 +02:00
_time.sleep(arguments.delay)
2023-05-29 11:39:33 +02:00
# 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:
_sys.stderr.write(str(error) + "\n")