mirror of
https://gitlab.sectorq.eu/jaydee/portainer.git
synced 2025-12-14 18:44:53 +01:00
build
This commit is contained in:
4
port.py
4
port.py
@@ -1,6 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
from concurrent.futures import ThreadPoolExecutor
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
import json
|
import json
|
||||||
|
import sys
|
||||||
import uuid
|
import uuid
|
||||||
import shutil
|
import shutil
|
||||||
import time
|
import time
|
||||||
@@ -435,7 +436,8 @@ class Portainer:
|
|||||||
self.stack_ids.append(s.get("Id"))
|
self.stack_ids.append(s.get("Id"))
|
||||||
return s
|
return s
|
||||||
|
|
||||||
raise ValueError(f"Stack not found: {stack}")
|
print(ValueError(f"Stack not found: {stack}"))
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
def create_stack(
|
def create_stack(
|
||||||
self,
|
self,
|
||||||
|
|||||||
64
portainer.py
64
portainer.py
@@ -21,10 +21,54 @@ defaults = {
|
|||||||
"endpoint_id": "vm01",
|
"endpoint_id": "vm01",
|
||||||
"stack": "my_stack",
|
"stack": "my_stack",
|
||||||
"deploy_mode": "git",
|
"deploy_mode": "git",
|
||||||
"autostart": True,
|
"autostart": "True",
|
||||||
"stack_mode": "swarm",
|
"stack_mode": "swarm",
|
||||||
"site": "portainer",
|
"site": "portainer",
|
||||||
}
|
}
|
||||||
|
cur_config = {}
|
||||||
|
if os.path.exists("/myapps/portainer.conf"):
|
||||||
|
with open("/myapps/portainer.conf", "r") as f:
|
||||||
|
conf_data = f.read()
|
||||||
|
for line in conf_data.split("\n"):
|
||||||
|
if line.startswith("#") or line.strip() == "":
|
||||||
|
continue
|
||||||
|
key, value = line.split("=", 1)
|
||||||
|
os.environ[key.strip()] = value.strip()
|
||||||
|
cur_config[key.strip()] = value.strip()
|
||||||
|
else:
|
||||||
|
|
||||||
|
print("No /myapps/portainer.conf file found, proceeding with env vars.")
|
||||||
|
os.makedirs("/myapps", exist_ok=True)
|
||||||
|
|
||||||
|
for field in defaults.keys():
|
||||||
|
value_in = os.getenv(f"PORTAINER_{field.upper()}")
|
||||||
|
if value_in is not None:
|
||||||
|
os.environ[field] = value_in
|
||||||
|
cur_config[field] = value_in
|
||||||
|
else:
|
||||||
|
os.environ[field] = defaults[field]
|
||||||
|
cur_config[field] = defaults[field]
|
||||||
|
|
||||||
|
conf_data = "\n".join(f"PORTAINER_{k.upper()}={v}" for k, v in cur_config.items())
|
||||||
|
# print("Using the following configuration:")
|
||||||
|
# print(conf_data)
|
||||||
|
|
||||||
|
with open("/myapps/portainer.conf", "w") as f:
|
||||||
|
f.write(conf_data)
|
||||||
|
print("Configuration written to /myapps/portainer.conf")
|
||||||
|
|
||||||
|
if os.getenv("PORTAINER_SITE") is not None:
|
||||||
|
defaults["site"] = os.getenv("PORTAINER_SITE")
|
||||||
|
if os.getenv("PORTAINER_ENDPOINT_ID") is not None:
|
||||||
|
defaults["endpoint_id"] = os.getenv("PORTAINER_ENDPOINT_ID")
|
||||||
|
if os.getenv("PORTAINER_STACK") is not None:
|
||||||
|
defaults["stack"] = os.getenv("PORTAINER_STACK")
|
||||||
|
if os.getenv("PORTAINER_DEPLOY_MODE") is not None:
|
||||||
|
defaults["deploy_mode"] = os.getenv("PORTAINER_DEPLOY_MODE")
|
||||||
|
if os.getenv("PORTAINER_STACK_MODE") is not None:
|
||||||
|
defaults["stack_mode"] = os.getenv("PORTAINER_STACK_MODE")
|
||||||
|
|
||||||
|
print(cur_config)
|
||||||
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
@@ -147,27 +191,33 @@ def prompt_missing_args(args_in, defaults_in, fields):
|
|||||||
"""
|
"""
|
||||||
for field, text in fields:
|
for field, text in fields:
|
||||||
value_in = getattr(args_in, field)
|
value_in = getattr(args_in, field)
|
||||||
default = defaults_in.get(field)
|
default = defaults_in.get(f"PORTAINER_{field}".upper())
|
||||||
|
|
||||||
if value_in is None:
|
if value_in is None:
|
||||||
if default is not None:
|
if default is not None:
|
||||||
prompt = f"{text} (default={default}) : "
|
prompt = f"{text} (default={default}) : "
|
||||||
value_in = input(prompt) or default
|
value_in = input(prompt) or default
|
||||||
|
defaults_in[f"PORTAINER_{field}".upper()] = value_in
|
||||||
else:
|
else:
|
||||||
value_in = input(f"{text}: ")
|
value_in = input(f"{text}: ")
|
||||||
|
defaults_in[f"PORTAINER_{field}".upper()] = value_in
|
||||||
setattr(args, field, value_in)
|
setattr(args, field, value_in)
|
||||||
|
os.environ[field] = value_in
|
||||||
|
|
||||||
|
with open("/myapps/portainer.conf", "w") as f:
|
||||||
|
for k in defaults_in.keys():
|
||||||
|
f.write(f"{k}={defaults_in[k]}\n")
|
||||||
|
|
||||||
return args
|
return args
|
||||||
|
|
||||||
|
print(cur_config)
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# Example usage: set PORTAINER_USER and PORTAINER_PASS in env, or pass literals below.
|
# Example usage: set PORTAINER_USER and PORTAINER_PASS in env, or pass literals below.
|
||||||
# token = get_portainer_token(base,"admin","l4c1j4yd33Du5lo") # or get_portainer_token(base, "admin", "secret")
|
# token = get_portainer_token(base,"admin","l4c1j4yd33Du5lo") # or get_portainer_token(base, "admin", "secret")
|
||||||
if args.action is None:
|
if args.action is None:
|
||||||
actions = [
|
actions = [
|
||||||
"delete_stack",
|
|
||||||
"create_stack",
|
"create_stack",
|
||||||
|
"delete_stack",
|
||||||
"stop_stack",
|
"stop_stack",
|
||||||
"start_stack",
|
"start_stack",
|
||||||
"list_stacks",
|
"list_stacks",
|
||||||
@@ -212,7 +262,7 @@ if __name__ == "__main__":
|
|||||||
if args.action == "delete_stack":
|
if args.action == "delete_stack":
|
||||||
args = prompt_missing_args(
|
args = prompt_missing_args(
|
||||||
args,
|
args,
|
||||||
defaults,
|
cur_config,
|
||||||
[
|
[
|
||||||
("site", "Site"),
|
("site", "Site"),
|
||||||
("endpoint_id", "Endpoint ID"),
|
("endpoint_id", "Endpoint ID"),
|
||||||
@@ -228,7 +278,7 @@ if __name__ == "__main__":
|
|||||||
if args.action == "create_stack":
|
if args.action == "create_stack":
|
||||||
args = prompt_missing_args(
|
args = prompt_missing_args(
|
||||||
args,
|
args,
|
||||||
defaults,
|
cur_config,
|
||||||
[
|
[
|
||||||
("site", "Site"),
|
("site", "Site"),
|
||||||
("endpoint_id", "Endpoint ID"),
|
("endpoint_id", "Endpoint ID"),
|
||||||
|
|||||||
Reference in New Issue
Block a user