From 464b9fadc814bbd45f4e75376cdd9b6703bfad0b Mon Sep 17 00:00:00 2001 From: jwansek Date: Mon, 10 Feb 2025 18:13:20 +0000 Subject: Started on TrueNAS HTTP API --- .gitignore | 3 +++ .gitmodules | 3 +++ autoBackup/.dockerignore | 1 + autoBackup/api_client | 1 + autoBackup/autoBackup.py | 53 +++++++++++++++++++++++++++++++++++++++++++++ autoBackup/requirements.txt | 2 ++ 6 files changed, 63 insertions(+) create mode 100644 .gitmodules create mode 120000 autoBackup/.dockerignore create mode 160000 autoBackup/api_client create mode 100644 autoBackup/autoBackup.py create mode 100644 autoBackup/requirements.txt 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 index 0000000..3c060d3 --- /dev/null +++ b/autoBackup/api_client @@ -0,0 +1 @@ +Subproject commit 3c060d360bfa14dca61350426c79a277a1292e0b 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 -- cgit v1.2.3