This commit is contained in:
2025-12-04 20:06:22 +01:00
parent cce4c5eb9b
commit f8b0acdfcc
2 changed files with 42 additions and 22 deletions

32
port.py
View File

@@ -23,9 +23,10 @@ class Portainer:
to perform API operations.
"""
def __init__(self, base_url, token, timeout=10):
self.base_url = base_url.rstrip("/")
self.token = token
def __init__(self, site, timeout=10):
self.base_url = None
self.token = None
self._debug = False
self.timeout = timeout
self.git_url = "git@gitlab.sectorq.eu:home/docker-compose.git"
self.stack_name = None
@@ -103,9 +104,22 @@ class Portainer:
self.log_mode = False
self.hw_mode = False
self.all_data = {"containers": {}, "stacks": {}, "endpoints": {}}
self.get_site(site)
self.get_endpoints()
self.get_stacks()
self.get_containers()
def get_site(self, site):
if site == "portainer":
self.base_url = os.getenv("PORTAINER_URL", "https://portainer.sectorq.eu/api")
self.token = "ptr_GCNUoFcTOaXm7k8ZxPdQGmrFIamxZPTydbserYofMHc="
elif site == "port":
self.base_url = os.getenv("PORTAINER_URL", "https://port.sectorq.eu/api")
self.token = "ptr_/5RkMCT/j3BTaL32vMSDtXFi76yOXRKVFOrUtzMsl5Y="
else:
self.base_url = os.getenv("PORTAINER_URL", "https://portainer.sectorq.eu/api")
self.token = "ptr_GCNUoFcTOaXm7k8ZxPdQGmrFIamxZPTydbserYofMHc="
def _is_number(self, s):
"""Check if the input string is a number."""
@@ -445,9 +459,9 @@ class Portainer:
stack=None,
mode="git",
autostart=False,
swarm=False,
stack_mode='swarm',
):
if swarm:
if stack_mode == 'swarm':
swarm_id = self.get_swarm_id(endpoint)
p = "swarm"
env_path = f"{self.repo_dir}/__swarm/{stack}/.env"
@@ -463,6 +477,7 @@ class Portainer:
Repo.clone_from(self.git_url, self.repo_dir)
if mode == "git":
path = f"/stacks/create/{p}/repository"
print(p)
if self.endpoint_id is not None:
path += f"?endpointId={self.endpoint_id}"
@@ -556,7 +571,7 @@ class Portainer:
"method": "repository",
"swarmID": None,
}
if swarm:
if stack_mode == 'swarm':
req["type"] = "swarm"
req["swarmID"] = swarm_id
req["composeFile"] = f"__swarm/{stack}/{stack}-swarm.yml"
@@ -749,8 +764,11 @@ class Portainer:
def _resolve_endpoint(self, endpoint_id):
self.get_endpoints()
if self._debug:
print(endpoint_id)
print(self.endpoints)
if self._is_number(endpoint_id):
self.endpoint_id = int(endpoint_id)
self.endpoint_name = self.endpoints["by_id"][self.endpoint_id]