#4 updated to use a toml file
This commit is contained in:
		
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -1,6 +1,7 @@ | |||||||
| *.log | *.log | ||||||
| *.json | *.json | ||||||
| *.csv | *.csv | ||||||
|  | *.toml | ||||||
|  |  | ||||||
| # Byte-compiled / optimized / DLL files | # Byte-compiled / optimized / DLL files | ||||||
| __pycache__/ | __pycache__/ | ||||||
|   | |||||||
							
								
								
									
										20
									
								
								config.toml.example
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								config.toml.example
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | |||||||
|  | [portainer] | ||||||
|  | host = "192.168.1.11" | ||||||
|  | port = 9443 | ||||||
|  | username = "a" | ||||||
|  | password = "a" | ||||||
|  | endpoint = 1 | ||||||
|  | start_containers = true | ||||||
|  |  | ||||||
|  | [logging] | ||||||
|  | use_log = true | ||||||
|  | log_level = "DEBUG" | ||||||
|  | log_path = "./crn.log" | ||||||
|  |  | ||||||
|  | [pushover] | ||||||
|  | use_pushover = false | ||||||
|  | po_key = "" | ||||||
|  | po_token = "" | ||||||
|  |  | ||||||
|  | [containers] | ||||||
|  | qbit = "qbittorrent" | ||||||
							
								
								
									
										40
									
								
								crane.py
									
									
									
									
									
								
							
							
						
						
									
										40
									
								
								crane.py
									
									
									
									
									
								
							| @@ -1,5 +1,4 @@ | |||||||
| import pushover | import pushover | ||||||
| from json import load |  | ||||||
| from cclient import * | from cclient import * | ||||||
| from clogging import * | from clogging import * | ||||||
| from cprocess import * | from cprocess import * | ||||||
| @@ -7,36 +6,39 @@ import time | |||||||
| import datetime | import datetime | ||||||
| import logging | import logging | ||||||
| import requests | import requests | ||||||
|  | from tomllib import load | ||||||
|  |  | ||||||
| class Crn: | class Crn: | ||||||
|     def __init__(self): |     def __init__(self): | ||||||
|         """Main object, should be calling functions from qlist.py, qlogging.py and qprocess.py""" |         """Main object, should be calling functions from qlist.py, qlogging.py and qprocess.py""" | ||||||
|         # Open the config. Needs a json file with the data in config.json.example |         # Open the config. Needs a json file with the data in config.json.example | ||||||
|         self.st = datetime.datetime.now() |         self.st = datetime.datetime.now() | ||||||
|         with open('./config.json') as c: |         with open('./config.toml', 'rb') as c: | ||||||
|             self.config = load(c) |             self.config = load(c) | ||||||
|         with open('./containers.json') as cts: |  | ||||||
|             self.containers = load(cts) |  | ||||||
|         self.observed_containers = self.containers.values() |  | ||||||
|         # Create the api object |         # Create the api object | ||||||
|         self.cc = requests |         self.cc = requests | ||||||
|         # Create the logging and pushover objects |         # Create the logging and pushover objects | ||||||
|         self.tl = logging |         self.tl = logging | ||||||
|         self.po = pushover |         self.po = pushover | ||||||
|          |         #Load settings from config.toml | ||||||
|         # Init config.json |         #portainer | ||||||
|         self.use_pushover = self.config["use_pushover"] |         self.host = self.config["portainer"]["host"] | ||||||
|         self.use_log = self.config["use_log"] |         self.port = self.config["portainer"]["port"] | ||||||
|         self.po_key = self.config["po_key"] |         self.username = self.config["portainer"]["username"] | ||||||
|         self.po_token = self.config["po_token"] |         self.password = self.config["portainer"]["password"] | ||||||
|         self.log_path = self.config["log_path"] |         self.endpoint = self.config["portainer"]["endpoint"] | ||||||
|         self.log_level = self.config["log_level"] |         self.start_containers = self.config["portainer"]["start_containers"] | ||||||
|         self.host = self.config["host"] |         #logging | ||||||
|         self.port = self.config["port"] |         self.use_log = self.config["logging"]["use_log"] | ||||||
|         self.username = self.config["username"] |         self.log_path = self.config["logging"]["log_path"] | ||||||
|         self.password = self.config["password"] |         self.log_level = self.config["logging"]["log_level"] | ||||||
|         self.endpoint = self.config["endpoint"] |         #pushover | ||||||
|         self.start_containers = self.config["start_containers"] |         self.use_pushover = self.config["pushover"]["use_pushover"] | ||||||
|  |         self.po_key = self.config["pushover"]["po_key"] | ||||||
|  |         self.po_token = self.config["pushover"]["po_token"] | ||||||
|  |         #containers | ||||||
|  |         self.observed_containers = self.config["containers"].values() | ||||||
|  |  | ||||||
|         cont_log(self) |         cont_log(self) | ||||||
|         cont_notify(self) |         cont_notify(self) | ||||||
|         self.t = time |         self.t = time | ||||||
|   | |||||||
| @@ -1,19 +1,19 @@ | |||||||
| import unittest | import unittest | ||||||
| import requests | import requests | ||||||
| from json import load | from tomllib import load | ||||||
| from cclient import c_auth, c_get_containers, c_start_container, c_stop_container | from cclient import c_auth, c_get_containers, c_start_container, c_stop_container | ||||||
| from cprocess import build_cont_list, process_cont_list, build_full_cont_list, process_cont_status | from cprocess import build_cont_list, process_cont_list, build_full_cont_list, process_cont_status | ||||||
| unittest.TestLoader.sortTestMethodsUsing = None | unittest.TestLoader.sortTestMethodsUsing = None | ||||||
|  |  | ||||||
| class TestCrane(unittest.TestCase): | class TestCrane(unittest.TestCase): | ||||||
|     def setUp(self): |     def setUp(self): | ||||||
|         with open('./config.json') as c: |         with open('./config.toml', 'rb') as c: | ||||||
|             self.config = load(c) |             self.config = load(c) | ||||||
|         self.host = self.config["host"] |         self.host = self.config["portainer"]["host"] | ||||||
|         self.port = self.config["port"] |         self.port = self.config["portainer"]["port"] | ||||||
|         self.username = self.config["username"] |         self.username = self.config["portainer"]["username"] | ||||||
|         self.password = self.config["password"] |         self.password = self.config["portainer"]["password"] | ||||||
|         self.endpoint = self.config["endpoint"] |         self.endpoint = self.config["portainer"]["endpoint"] | ||||||
|         self.cid = 'aa5b217ca6217fd9d268396039da69ea9e4a5aff381b3dceb71edb5a1f4d429d' |         self.cid = 'aa5b217ca6217fd9d268396039da69ea9e4a5aff381b3dceb71edb5a1f4d429d' | ||||||
|         self.req_obj = requests |         self.req_obj = requests | ||||||
|         self.hypercare_containers = ['hello-world'] |         self.hypercare_containers = ['hello-world'] | ||||||
|   | |||||||
							
								
								
									
										6
									
								
								test_toml.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								test_toml.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,6 @@ | |||||||
|  | from tomllib import load | ||||||
|  |  | ||||||
|  | with open('./config.toml', mode="rb") as c: | ||||||
|  |     config = load(c) | ||||||
|  |  | ||||||
|  | print(config["containers"].values()) | ||||||
		Reference in New Issue
	
	Block a user