diff --git a/main.py b/main.py index 1525c4b..8c53776 100755 --- a/main.py +++ b/main.py @@ -405,6 +405,8 @@ if __name__ == "__main__": ("update_service","update_service"), ("update_containers","update_containers"), ("list_stacks","list_stacks"), + ("list_all_stacks", "list_all_stacks"), + ("delete_ophaned_stacks", "delete_ophaned_stacks"), ("update_stack","update_stack"), ("secrets","secrets"), ("print_all_data","print_all_data"), @@ -591,6 +593,21 @@ if __name__ == "__main__": # print(json.dumps(por.all_data, indent=2)) sys.exit() + if args.action == "list_all_stacks": + por.get_stacks_failed() + if args.launcher: + input("Press ENTER to continue...") + # print(json.dumps(por.all_data, indent=2)) + sys.exit() + + if args.action == "delete_ophaned_stacks": + por.delete_failed_stack() + if args.launcher: + input("Press ENTER to continue...") + # print(json.dumps(por.all_data, indent=2)) + sys.exit() + + if args.action == "list_containers": print("Getting containers") args = prompt_missing_args( diff --git a/portainer/api.py b/portainer/api.py index 303155f..748b754 100644 --- a/portainer/api.py +++ b/portainer/api.py @@ -237,6 +237,34 @@ class PortainerApi: self.get_containers(self) return True + def get_stacks_failed(self, timeout=20): + '''Get a list of stacks for a specific endpoint or all endpoints.''' + path = "/stacks" + stcks = [] + stacks = self._api_get(path, timeout=timeout) + self.stacks_all = {} + fail_endponts = [20, 39, 41, 32, 43] + # print(json.dumps(stacks,indent=2)) + for s in stacks: + stcks.append((s["EndpointId"], s["Id"])) + print(f"{s['EndpointId']} : {s['Id']}") + return stcks + + def delete_failed_stack(self, timeout=20): + '''Get a list of stacks for a specific endpoint or all endpoints.''' + path = "/stacks" + stcks = [] + stacks = self._api_get(path, timeout=timeout) + + # print(json.dumps(stacks,indent=2)) + eid = input("Enter endpoint : ") + for s in stacks: + # print(s) + # print(f"{s['EndpointId']} : {s['Id']}") + if int(eid) == s["EndpointId"]: + print(f"deleting stack {s['Id']}") + self._delete_single_stack(s["Id"], "50") + def get_stacks(self, endpoint_id="all", timeout=20): '''Get a list of stacks for a specific endpoint or all endpoints.''' if endpoint_id != "all": @@ -245,7 +273,7 @@ class PortainerApi: stcks = [] stacks = self._api_get(path, timeout=timeout) self.stacks_all = {} - fail_endponts = [20, 39, 41] + fail_endponts = [20, 39, 41, 32, 43] # print(json.dumps(stacks,indent=2)) webhooks = {} for s in stacks: