diff --git a/port.py b/port.py index 9e74499..5aac7d7 100644 --- a/port.py +++ b/port.py @@ -1,6 +1,7 @@ import os from concurrent.futures import ThreadPoolExecutor import json +import sys import uuid import shutil import time @@ -435,7 +436,8 @@ class Portainer: self.stack_ids.append(s.get("Id")) return s - raise ValueError(f"Stack not found: {stack}") + print(ValueError(f"Stack not found: {stack}")) + sys.exit(1) def create_stack( self, diff --git a/portainer.py b/portainer.py index c917b8a..6ee7623 100755 --- a/portainer.py +++ b/portainer.py @@ -21,10 +21,54 @@ defaults = { "endpoint_id": "vm01", "stack": "my_stack", "deploy_mode": "git", - "autostart": True, + "autostart": "True", "stack_mode": "swarm", "site": "portainer", } +cur_config = {} +if os.path.exists("/myapps/portainer.conf"): + with open("/myapps/portainer.conf", "r") as f: + conf_data = f.read() + for line in conf_data.split("\n"): + if line.startswith("#") or line.strip() == "": + continue + key, value = line.split("=", 1) + os.environ[key.strip()] = value.strip() + cur_config[key.strip()] = value.strip() +else: + + print("No /myapps/portainer.conf file found, proceeding with env vars.") + os.makedirs("/myapps", exist_ok=True) + + for field in defaults.keys(): + value_in = os.getenv(f"PORTAINER_{field.upper()}") + if value_in is not None: + os.environ[field] = value_in + cur_config[field] = value_in + else: + os.environ[field] = defaults[field] + cur_config[field] = defaults[field] + + conf_data = "\n".join(f"PORTAINER_{k.upper()}={v}" for k, v in cur_config.items()) + # print("Using the following configuration:") + # print(conf_data) + + with open("/myapps/portainer.conf", "w") as f: + f.write(conf_data) + print("Configuration written to /myapps/portainer.conf") + +if os.getenv("PORTAINER_SITE") is not None: + defaults["site"] = os.getenv("PORTAINER_SITE") +if os.getenv("PORTAINER_ENDPOINT_ID") is not None: + defaults["endpoint_id"] = os.getenv("PORTAINER_ENDPOINT_ID") +if os.getenv("PORTAINER_STACK") is not None: + defaults["stack"] = os.getenv("PORTAINER_STACK") +if os.getenv("PORTAINER_DEPLOY_MODE") is not None: + defaults["deploy_mode"] = os.getenv("PORTAINER_DEPLOY_MODE") +if os.getenv("PORTAINER_STACK_MODE") is not None: + defaults["stack_mode"] = os.getenv("PORTAINER_STACK_MODE") + +print(cur_config) parser = argparse.ArgumentParser( @@ -147,27 +191,33 @@ def prompt_missing_args(args_in, defaults_in, fields): """ for field, text in fields: value_in = getattr(args_in, field) - default = defaults_in.get(field) + default = defaults_in.get(f"PORTAINER_{field}".upper()) if value_in is None: if default is not None: prompt = f"{text} (default={default}) : " value_in = input(prompt) or default + defaults_in[f"PORTAINER_{field}".upper()] = value_in else: value_in = input(f"{text}: ") - + defaults_in[f"PORTAINER_{field}".upper()] = value_in setattr(args, field, value_in) - + os.environ[field] = value_in + + with open("/myapps/portainer.conf", "w") as f: + for k in defaults_in.keys(): + f.write(f"{k}={defaults_in[k]}\n") + return args - +print(cur_config) if __name__ == "__main__": # Example usage: set PORTAINER_USER and PORTAINER_PASS in env, or pass literals below. # token = get_portainer_token(base,"admin","l4c1j4yd33Du5lo") # or get_portainer_token(base, "admin", "secret") if args.action is None: actions = [ - "delete_stack", "create_stack", + "delete_stack", "stop_stack", "start_stack", "list_stacks", @@ -212,7 +262,7 @@ if __name__ == "__main__": if args.action == "delete_stack": args = prompt_missing_args( args, - defaults, + cur_config, [ ("site", "Site"), ("endpoint_id", "Endpoint ID"), @@ -228,7 +278,7 @@ if __name__ == "__main__": if args.action == "create_stack": args = prompt_missing_args( args, - defaults, + cur_config, [ ("site", "Site"), ("endpoint_id", "Endpoint ID"),