crane/crane.py

94 lines
3.9 KiB
Python
Raw Normal View History

2022-10-17 17:18:17 -05:00
import pushover
2023-04-10 22:51:37 -05:00
import json
2022-10-21 12:48:19 -05:00
from cclient import *
2022-10-17 17:18:17 -05:00
from clogging import *
2022-10-21 12:48:19 -05:00
from cprocess import *
2022-10-17 17:18:17 -05:00
import time
import datetime
import logging
import requests
2022-10-28 01:44:29 -05:00
from tomllib import load
2023-06-05 18:05:17 -05:00
import os
2023-06-05 22:30:53 -05:00
from AppriseClient import apprise_notify
2023-06-11 14:44:27 -05:00
requests.packages.urllib3.disable_warnings()
2022-10-17 17:18:17 -05:00
class Crn:
def __init__(self):
"""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
self.st = datetime.datetime.now()
2023-06-05 22:30:53 -05:00
if os.getenv("toml_path"):
config_file_path=os.getenv("toml_path")
with open(config_file_path, 'rb') as c:
self.config = load(c)
2023-06-05 18:05:17 -05:00
if os.path.exists('./config.toml'):
config_file_path = './config.toml'
with open(config_file_path, 'rb') as c:
self.config = load(c)
2022-10-17 17:18:17 -05:00
# Create the api object
self.cc = requests
# Create the logging and pushover objects
self.tl = logging
self.po = pushover
2023-04-10 22:51:37 -05:00
self.jn = json
2022-10-28 01:44:29 -05:00
#Load settings from config.toml
#portainer
self.host = self.config["portainer"]["host"]
self.port = self.config["portainer"]["port"]
2023-04-10 22:51:37 -05:00
self.access_token = self.config["portainer"]["access_token"]
2022-10-28 01:44:29 -05:00
self.endpoint = self.config["portainer"]["endpoint"]
self.start_containers = self.config["portainer"]["start_containers"]
#logging
self.use_log = self.config["logging"]["use_log"]
self.log_path = self.config["logging"]["log_path"]
self.log_level = self.config["logging"]["log_level"]
#pushover
self.use_pushover = self.config["pushover"]["use_pushover"]
self.po_key = self.config["pushover"]["po_key"]
self.po_token = self.config["pushover"]["po_token"]
2023-06-05 22:30:53 -05:00
#apprise
self.use_apprise = self.config["apprise"]["use_apprise"]
self.apprise_host = self.config["apprise"]["host"]
self.apprise_port = self.config["apprise"]["port"]
self.apprise_aurls = self.config["apprise"]["aurls"]
2022-10-28 01:44:29 -05:00
#containers
2023-04-10 22:51:37 -05:00
self.observed_containers = list(self.config["containers"].values())
self.container_statuses = ["paused","dead","created","exited","removing","restarting","created"]
2023-06-05 23:10:33 -05:00
#healthcheck
2023-06-06 00:48:21 -05:00
self.use_healthcheck = self.config["healthcheck"]["use_healthcheck"]
2023-06-05 23:10:33 -05:00
self.healthcheck_url = self.config["healthcheck"]["healthcheck_url"]
2022-10-28 01:44:29 -05:00
2022-10-17 17:18:17 -05:00
cont_log(self)
cont_notify(self)
self.t = time
if self.use_healthcheck:
send_ping(self, requests, self.healthcheck_url.rstrip("/") + "/start")
2022-10-17 17:18:17 -05:00
#logging in
try:
2023-04-10 22:51:37 -05:00
# c_get_filtered_containers(req_obj, j_obj, host, port, access_token, endpoint, containers, statuses)
self.cont_obj = c_get_filtered_containers(self.cc, self.jn, self.host, self.port, self.access_token, self.endpoint,self.observed_containers,self.container_statuses)
# print(self.cont_obj)
2022-11-01 16:05:57 -05:00
self.tl.debug('Collected container data.')
2023-04-10 22:51:37 -05:00
self.process_cont_list_response = process_cont_list(self.cont_obj, c_start_container, self.cc, self.host, self.port, self.access_token, self.endpoint)
2022-11-01 16:05:57 -05:00
if self.process_cont_list_response:
2023-04-10 22:51:37 -05:00
self.tl.warning(f'Started: {self.process_cont_list_response}')
2022-10-21 12:48:19 -05:00
2022-10-17 17:18:17 -05:00
except requests.exceptions.RequestException as e:
self.tl.exception(e)
2022-11-02 11:29:01 -05:00
self.poc.message(self.po_key, e, title="crane API ERROR")
2022-10-17 17:18:17 -05:00
#Main process block
self.et = datetime.datetime.now()
get_script_runtime(self)
if self.use_pushover:
2023-06-05 22:30:53 -05:00
cont_notify_summary(self, apprise_notify, requests)
if self.use_apprise:
cont_notify_summary(self, apprise_notify, requests)
2023-06-06 00:48:21 -05:00
if self.use_healthcheck:
send_ping(self, requests, self.healthcheck_url)
2023-06-05 23:10:33 -05:00
2022-10-17 17:18:17 -05:00
# Run
if __name__== "__main__":
Crn()