aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjwansek <eddie.atten.ea29@gmail.com>2025-02-10 18:13:20 +0000
committerjwansek <eddie.atten.ea29@gmail.com>2025-02-10 18:13:20 +0000
commit464b9fadc814bbd45f4e75376cdd9b6703bfad0b (patch)
tree44c9781549864a1054f87794b8599482aaa47fbd
parenta39208de998519bf0a6a3c968ae5070a43f954cf (diff)
downloadBetterZFSReplication-464b9fadc814bbd45f4e75376cdd9b6703bfad0b.tar.gz
BetterZFSReplication-464b9fadc814bbd45f4e75376cdd9b6703bfad0b.zip
Started on TrueNAS HTTP API
-rw-r--r--.gitignore3
-rw-r--r--.gitmodules3
l---------autoBackup/.dockerignore1
m---------autoBackup/api_client0
-rw-r--r--autoBackup/autoBackup.py53
-rw-r--r--autoBackup/requirements.txt2
6 files changed, 62 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 82f9275..0e8bbbc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,6 @@
+autoBackup/.env
+
+
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 0000000..932c5c8
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "autoBackup/api_client"]
+ path = autoBackup/api_client
+ url = git@github.com:truenas/api_client.git
diff --git a/autoBackup/.dockerignore b/autoBackup/.dockerignore
new file mode 120000
index 0000000..5a19b83
--- /dev/null
+++ b/autoBackup/.dockerignore
@@ -0,0 +1 @@
+../.gitignore \ No newline at end of file
diff --git a/autoBackup/api_client b/autoBackup/api_client
new file mode 160000
+Subproject 3c060d360bfa14dca61350426c79a277a1292e0
diff --git a/autoBackup/autoBackup.py b/autoBackup/autoBackup.py
new file mode 100644
index 0000000..fc5c6a7
--- /dev/null
+++ b/autoBackup/autoBackup.py
@@ -0,0 +1,53 @@
+from truenas_api_client import Client
+import requests
+import dotenv
+import json
+import os
+
+env_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), ".env")
+if os.path.exists(env_path):
+ dotenv.load_dotenv(dotenv_path = env_path)
+
+class TrueNASAPIClient:
+ def __init__(self, host, api_key):
+ self.base_url = base_url = "http://%s/api/v2.0" % host
+ self.headers = {
+ "Authorization": "Bearer " + api_key
+ }
+
+ def base_get(self, endpoint, payload = None):
+ if payload is None:
+ payload = {}
+
+ if not endpoint.startswith("/"):
+ endpoint = "/" + endpoint
+
+ req = requests.get(self.base_url + endpoint, headers = self.headers, data = payload)
+ if not req.status_code == 200:
+ raise ConnectionError("API call failed (%d): '%s'" % (req.status_code, req.content.decode()))
+ return req.json()
+
+ def get_websocket_connections(self):
+ return self.base_get("/core/sessions")
+
+ def get_replication_naming_schemas(self):
+ return self.base_get("/replication/list_naming_schemas")
+
+ def get_jobs(self):
+ return self.base_get("/core/get_jobs")
+
+ def get_replication_jobs(self):
+ return [i for i in self.get_jobs() if i["method"] == "replication.run"]
+
+ def get_running_replication_jobs(self):
+ return [i for i in self.get_jobs() if i["method"] == "replication.run" and i["progress"]["percent"] != 100 and not i["state"] == "FAILED"]
+
+ def get_running_jobs(self):
+ return [i for i in self.get_jobs() if i["progress"]["percent"] != 100]
+
+ def get_replication_tasks(self):
+ return self.base_get("/replication")
+
+if __name__ == "__main__":
+ truenas = TrueNASAPIClient(host = os.environ["SLAVE_HOST"], api_key = os.environ["SLAVE_KEY"])
+ print(json.dumps(truenas.get_replication_tasks(), indent = 4)) \ No newline at end of file
diff --git a/autoBackup/requirements.txt b/autoBackup/requirements.txt
new file mode 100644
index 0000000..323ef8c
--- /dev/null
+++ b/autoBackup/requirements.txt
@@ -0,0 +1,2 @@
+python-dotenv
+requests \ No newline at end of file