This commit is contained in:
2025-12-15 13:18:45 +01:00
parent d7f202e2b8
commit 1dde729887
2 changed files with 67 additions and 35 deletions

81
port.py
View File

@@ -335,7 +335,6 @@ class Portainer:
return endpoint
def refresh_in_containers(self):
print("Refreshing containers")
'''Get a list of containers for a specific endpoint and stack.'''
# print(json.dumps(self.all_data,indent=2))
# print(endpoint)
@@ -474,38 +473,62 @@ class Portainer:
with ThreadPoolExecutor(max_workers=10) as exe:
exe.map(stop, containers)
def update_stack(self, endpoint, stack, autostart, timeout=130):
def update_stack(self, args):
'''Update one stack or all stacks on an endpoint.'''
stcs = []
if stack == "all":
for s in self.all_data["webhooks"][endpoint]:
stcs.append([s, self.all_data["webhooks"][endpoint][s]])
else:
#print("Updating stacks")
stacks = self.get_stacks(endpoint_id=args.endpoint_id)
stacks_tuples = []
for s in stacks:
#print(s)
try:
stcs.append([stack, self.all_data["webhooks"][endpoint][stack]])
except Exception as e:
print(f"Error: Stack {stack} not found on endpoint {endpoint}: {e}")
stacks_tuples.append((s['AutoUpdate']['Webhook'],s['Name']))
# print(s['Name'], " : ", s['AutoUpdate']['Webhook'])
except:
stacks_tuples.append((s['Webhook'],s['Name']))
# print(s['Name'], " : ", s['Webhook'])
stacks_dict = dict(stacks_tuples)
print(stacks_dict)
#input(stacks_tuples)
# stacks_tuples = [(s['AutoUpdate']['Webhook'], s['Name']) for s in stacks if "Webhook" in s['AutoUpdate'] ]
# input(stcs)
def update(c):
print(f" > Updating {c[0]} on {endpoint}")
ans = self._api_post_no_body(f"/stacks/webhooks/{c[1]}")
print(f" > Updating {c[1]} ")
ans = self._api_post_no_body(f"/stacks/webhooks/{c[0]}")
logger.debug(
f"Update response for stack {c[0]} on endpoint {endpoint}: {ans}"
f"Update response for stack {c[0]} on endpoint {ans}"
)
# input(stacks_tuples)
if args.debug:
input(args)
stacks_tuples = sorted(stacks_tuples, key=lambda x: x[1])
stack_dict = dict(stacks_tuples)
# input(service_tuples)
if self.args.service_id is None:
#services = [(s["Id"], s["Name"]) for s in self.get_stacks(endpoint_id)]
stacks_tuples.insert(0, ("__ALL__", "[Select ALL]"))
stack_ids = checkboxlist_dialog(
title="Select one stack to update",
text="Choose a service:",
values=stacks_tuples
).run()
stcs = []
input(stack_ids)
def stop():
cont = []
for c in self.all_data["containers"][endpoint]:
if stack == c or stack == "all":
cont += self.all_data["containers"][endpoint][c]
self.stop_containers(endpoint, cont)
if args.stack == "all":
for s in stack_dict:
stcs.append([s, stack_dict[s]])
else:
for s in stack_dict:
if s in stack_ids:
stcs.append([s, stack_dict[s]])
print(stcs)
with ThreadPoolExecutor(max_workers=10) as exe:
exe.map(update, stcs)
list(exe.map(update, stcs))
if not autostart:
input('UPDATED')
if not args.autostart:
time.sleep(120)
cont = []
for c in self.all_data["containers"][endpoint]:
@@ -943,12 +966,16 @@ class Portainer:
service_ids = [s[0] for s in service_tuples if s[0] != "__ALL__" and s[0] != "__ONLY_CHECK__"]
else:
service_ids = [self.args.service_id]
if "__ONLY_CHECK__" in service_ids or self.args.update is False:
pull = False
print("Checking for updates only...")
if self.args.update is False:
if "__ONLY_CHECK__" in service_ids:
pull = False
print("Checking for updates only...")
else:
pull = True
print("Checking for updates and pulling updates...")
else:
print("Checking for updates and pulling updates...")
pull = True
print("Checking for updates and pulling updates...")
if "__ALL__" in service_ids:
service_ids = [s[0] for s in service_tuples if s[0] != "__ALL__" and s[0] != "__ONLY_CHECK__"]

View File

@@ -475,8 +475,8 @@ if __name__ == "__main__":
if args.action == "create_stack":
por.action = "create_stack"
print(cur_config)
print(args)
#print(cur_config)
#print(args)
args = prompt_missing_args(
args,
cur_config,
@@ -538,9 +538,6 @@ if __name__ == "__main__":
sys.exit()
if args.action == "update_service":
args = prompt_missing_args(
args,
cur_config,
@@ -585,11 +582,19 @@ if __name__ == "__main__":
print(por.get_containers())
sys.exit()
if args.action == "update_stack":
print("Updating stacks")
por.update_stack(args.endpoint_id, args.stack, args.autostart)
args = prompt_missing_args(
args,
cur_config,
[
("site", "Site"),
("endpoint_id", "Endpoint ID")
],
)
por.update_stack(args)
sys.exit()
if args.action == "print_all_data":
print(json.dumps(por.all_data, indent=2))
sys.exit()