mirror of
https://gitlab.sectorq.eu/jaydee/portainer.git
synced 2026-01-29 04:49:44 +01:00
Compare commits
2 Commits
v1.0.0
...
174aab4faa
| Author | SHA1 | Date | |
|---|---|---|---|
| 174aab4faa | |||
| 9c6445ee03 |
74
port.py
74
port.py
@@ -315,6 +315,7 @@ class Portainer:
|
|||||||
|
|
||||||
def get_endpoint_id(self):
|
def get_endpoint_id(self):
|
||||||
'''Get endpoint ID from either ID or name input.'''
|
'''Get endpoint ID from either ID or name input.'''
|
||||||
|
# input(self.args.endpoint_id)
|
||||||
if self._is_number(self.args.endpoint_id):
|
if self._is_number(self.args.endpoint_id):
|
||||||
self.endpoint_id = self.args.endpoint_id
|
self.endpoint_id = self.args.endpoint_id
|
||||||
self.endpoint_name = self.endpoints["by_id"][self.args.endpoint_id]
|
self.endpoint_name = self.endpoints["by_id"][self.args.endpoint_id]
|
||||||
@@ -394,14 +395,13 @@ class Portainer:
|
|||||||
else:
|
else:
|
||||||
|
|
||||||
eps = [self.get_endpoint_id()]
|
eps = [self.get_endpoint_id()]
|
||||||
#input(eps)
|
|
||||||
for endpoint in eps:
|
for endpoint in eps:
|
||||||
|
|
||||||
# print(s)
|
|
||||||
#print(self.args.stack)
|
#print(self.args.stack)
|
||||||
if self.args.stack in ["all", None]:
|
if self.args.stack in ["all", None]:
|
||||||
# input([id for id in self.all_data["stacks"][endpoint]['by_id'].keys()])
|
# 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"]:
|
# if s not in self.all_data["stacks"]:
|
||||||
# continue
|
# continue
|
||||||
#input(self.all_data)
|
#input(self.all_data)
|
||||||
@@ -409,36 +409,36 @@ class Portainer:
|
|||||||
# print(f"Endpoint {self.all_data["endpoints"]["by_id"][s]} is offline")
|
# print(f"Endpoint {self.all_data["endpoints"]["by_id"][s]} is offline")
|
||||||
continue
|
continue
|
||||||
# input(self.all_data["stacks"][endpoint]["by_name"])
|
# input(self.all_data["stacks"][endpoint]["by_name"])
|
||||||
for e in self.all_data["stacks"][endpoint]["by_name"]:
|
|
||||||
#input(e)
|
#input(e)
|
||||||
path = (
|
path = (
|
||||||
f"/endpoints/{endpoint}/docker/containers/json"
|
f"/endpoints/{endpoint}/docker/containers/json"
|
||||||
f'?all=1&filters={{"label": ["com.docker.compose.project={e}"]}}'
|
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
|
self.all_data["containers"] = data
|
||||||
|
|
||||||
@@ -834,7 +834,7 @@ class Portainer:
|
|||||||
}
|
}
|
||||||
self._api_post_file(path, self.endpoint_id, stack, envs, file)
|
self._api_post_file(path, self.endpoint_id, stack, envs, file)
|
||||||
|
|
||||||
def print_stacks(self, endpoint="all"):
|
def print_stacks(self, args):
|
||||||
"""Print a table of stacks, optionally filtered by endpoint."""
|
"""Print a table of stacks, optionally filtered by endpoint."""
|
||||||
stacks = self.get_stacks()
|
stacks = self.get_stacks()
|
||||||
count = 0
|
count = 0
|
||||||
@@ -842,11 +842,11 @@ class Portainer:
|
|||||||
stack_names = []
|
stack_names = []
|
||||||
for stack in stacks:
|
for stack in stacks:
|
||||||
# print(stack)
|
# print(stack)
|
||||||
if endpoint is not None:
|
if args.endpoint_id is not None:
|
||||||
if not stack["EndpointId"] in self.endpoints["by_id"]:
|
if not stack["EndpointId"] in self.endpoints["by_id"]:
|
||||||
continue
|
continue
|
||||||
if endpoint != "all":
|
if args.endpoint_id != "all":
|
||||||
if self.endpoints["by_name"][endpoint] != stack["EndpointId"]:
|
if self.endpoints["by_name"][args.endpoint_id] != stack["EndpointId"]:
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
stack_names.append(stack["Name"])
|
stack_names.append(stack["Name"])
|
||||||
|
|||||||
20
portainer.py
20
portainer.py
@@ -83,10 +83,6 @@ def load_config(defaults=defaults):
|
|||||||
print("Configuration written to /myapps/portainer.conf")
|
print("Configuration written to /myapps/portainer.conf")
|
||||||
return cur_config
|
return cur_config
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
a = load_config(defaults)
|
a = load_config(defaults)
|
||||||
|
|
||||||
# ENV_VARS = [
|
# ENV_VARS = [
|
||||||
@@ -109,7 +105,11 @@ def update_configs(cur_config):
|
|||||||
print("Configuration written to /myapps/portainer.conf")
|
print("Configuration written to /myapps/portainer.conf")
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description="Portainer helper - use env vars or pass credentials."
|
description=f"""\
|
||||||
|
Portainer helper - use env vars or pass credentials."
|
||||||
|
version: {VERSION}
|
||||||
|
""",
|
||||||
|
formatter_class=argparse.RawTextHelpFormatter,
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--base",
|
"--base",
|
||||||
@@ -579,7 +579,15 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
if args.action == "list_containers":
|
if args.action == "list_containers":
|
||||||
print("Getting 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()
|
sys.exit()
|
||||||
|
|
||||||
if args.action == "update_stack":
|
if args.action == "update_stack":
|
||||||
|
|||||||
Reference in New Issue
Block a user