diff --git a/port.py b/port.py index acd1654..c3a1b8f 100644 --- a/port.py +++ b/port.py @@ -315,6 +315,7 @@ class Portainer: def get_endpoint_id(self): '''Get endpoint ID from either ID or name input.''' + # input(self.args.endpoint_id) if self._is_number(self.args.endpoint_id): self.endpoint_id = self.args.endpoint_id self.endpoint_name = self.endpoints["by_id"][self.args.endpoint_id] @@ -394,14 +395,13 @@ class Portainer: else: eps = [self.get_endpoint_id()] - #input(eps) + for endpoint in eps: - - # print(s) #print(self.args.stack) if self.args.stack in ["all", None]: # input([id for id in self.all_data["stacks"][endpoint]['by_id'].keys()]) - for s in [id for id in self.all_data["stacks"][endpoint]['by_id'].keys()]: + for e in [id for id in self.all_data["stacks"][endpoint]['by_name'].keys()]: + #input(e) # if s not in self.all_data["stacks"]: # continue #input(self.all_data) @@ -409,36 +409,36 @@ class Portainer: # print(f"Endpoint {self.all_data["endpoints"]["by_id"][s]} is offline") continue # input(self.all_data["stacks"][endpoint]["by_name"]) - for e in self.all_data["stacks"][endpoint]["by_name"]: - #input(e) - path = ( - f"/endpoints/{endpoint}/docker/containers/json" - f'?all=1&filters={{"label": ["com.docker.compose.project={e}"]}}' + + #input(e) + path = ( + f"/endpoints/{endpoint}/docker/containers/json" + f'?all=1&filters={{"label": ["com.docker.compose.project={e}"]}}' + ) + logging.info(f"request : {path}") + try: + containers = self._api_get(path) + #input(containers) + except Exception as e: + print(f"failed to get containers from {path}: {e}") + continue + contr = [] + try: + for c in containers: + # input(c) + cont.append(c["Names"][0].replace("/", "")) + contr.append(c["Names"][0].replace("/", "")) + if self.all_data["endpoints"]["by_id"][endpoint] in data: + data[self.all_data["endpoints"]["by_id"][endpoint]][e] = contr + else: + data[self.all_data["endpoints"]["by_id"][endpoint]] = { + e: contr + } + except Exception as e: + logger.debug( + f"Exception while getting containers for stack {e} ", + f"on endpoint {self.all_data['endpoints']['by_id'][endpoint]}: {e}", ) - logging.info(f"request : {path}") - try: - containers = self._api_get(path) - #input(containers) - except Exception as e: - print(f"failed to get containers from {path}: {e}") - continue - contr = [] - try: - for c in containers: - # input(c) - cont.append(c["Names"][0].replace("/", "")) - contr.append(c["Names"][0].replace("/", "")) - if self.all_data["endpoints"]["by_id"][endpoint] in data: - data[self.all_data["endpoints"]["by_id"][endpoint]][e] = contr - else: - data[self.all_data["endpoints"]["by_id"][endpoint]] = { - e: contr - } - except Exception as e: - logger.debug( - f"Exception while getting containers for stack {e} ", - f"on endpoint {self.all_data['endpoints']['by_id'][endpoint]}: {e}", - ) self.all_data["containers"] = data diff --git a/portainer.py b/portainer.py index 4a8fa20..35ff44d 100755 --- a/portainer.py +++ b/portainer.py @@ -579,7 +579,15 @@ if __name__ == "__main__": if args.action == "list_containers": print("Getting containers") - print(por.get_containers()) + args = prompt_missing_args( + args, + cur_config, + [ + ("site", "Site"), + ("endpoint_id", "Endpoint ID"), + ], + ) + print("\n".join(por.get_containers())) sys.exit() if args.action == "update_stack":