2022-10-17 17:18:17 -05:00
|
|
|
import pushover
|
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
|
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()
|
2022-10-28 01:44:29 -05:00
|
|
|
with open('./config.toml', 'rb') as c:
|
2022-10-17 17:18:17 -05:00
|
|
|
self.config = load(c)
|
|
|
|
# Create the api object
|
|
|
|
self.cc = requests
|
|
|
|
# Create the logging and pushover objects
|
|
|
|
self.tl = logging
|
|
|
|
self.po = pushover
|
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"]
|
|
|
|
self.username = self.config["portainer"]["username"]
|
|
|
|
self.password = self.config["portainer"]["password"]
|
|
|
|
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"]
|
|
|
|
#containers
|
|
|
|
self.observed_containers = self.config["containers"].values()
|
|
|
|
|
2022-10-17 17:18:17 -05:00
|
|
|
cont_log(self)
|
|
|
|
cont_notify(self)
|
|
|
|
self.t = time
|
|
|
|
|
|
|
|
#logging in
|
|
|
|
try:
|
2022-11-01 16:05:57 -05:00
|
|
|
self.tl.debug('Authenticating.')
|
2022-10-17 17:18:17 -05:00
|
|
|
self.jwt = c_auth(self.cc, self.host, self.port, self.username, self.password)
|
|
|
|
self.tl.info('Authenticated successfully.')
|
2022-10-21 12:48:19 -05:00
|
|
|
self.cont_obj = c_get_containers(self.cc, self.host, self.port, self.jwt, self.endpoint)
|
2022-11-01 16:05:57 -05:00
|
|
|
self.tl.debug('Collected container data.')
|
2022-10-21 12:48:19 -05:00
|
|
|
self.cont_list = build_cont_list(self.cont_obj, self.observed_containers)
|
2022-11-01 16:05:57 -05:00
|
|
|
self.tl.debug('Building container list.')
|
2022-10-21 12:48:19 -05:00
|
|
|
self.process_cont_list_response = process_cont_list(self.cont_list, c_start_container, self.cc, self.host, self.port, self.jwt, self.endpoint)
|
2022-11-01 16:05:57 -05:00
|
|
|
if self.process_cont_list_response:
|
2022-11-01 16:10:22 -05:00
|
|
|
self.tl.warn(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)
|
|
|
|
self.po.send_message(e, title="crane API ERROR")
|
|
|
|
|
|
|
|
#Main process block
|
|
|
|
self.et = datetime.datetime.now()
|
|
|
|
get_script_runtime(self)
|
|
|
|
if self.use_pushover:
|
|
|
|
cont_notify_summary(self)
|
|
|
|
# Run
|
|
|
|
if __name__== "__main__":
|
|
|
|
Crn()
|