Compare commits

...

24 Commits

Author SHA1 Message Date
256488187e updated healthchecks to have a run time
All checks were successful
continuous-integration/drone/push Build is passing
2024-06-03 22:47:11 -05:00
9576004c4b test
All checks were successful
continuous-integration/drone/push Build is passing
2023-08-09 10:53:35 -05:00
4b75a8fb32 changed drone 2023-08-06 23:33:17 -05:00
9bf1d46ce3 updated drone events
All checks were successful
continuous-integration/drone/push Build is passing
2023-06-27 12:18:38 -05:00
eca3e3920f Merge pull request 'dev-update-testing' (#24) from dev-update-testing into main
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: https://git.jbranan.com/jblu/crane/pulls/24
2023-06-27 12:03:05 -05:00
bc325787a3 updated testing for ci
Some checks failed
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is failing
2023-06-27 11:59:19 -05:00
5d6a77c108 updated testing
All checks were successful
continuous-integration/drone/push Build is passing
2023-06-25 23:46:10 -05:00
f60c3be987 Merge pull request 'dev-custom-crontab' (#23) from dev-custom-crontab into main
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: https://git.jbranan.com/jblu/crane/pulls/23
2023-06-23 03:52:15 -05:00
ff8136bf0c updated test
Some checks failed
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is failing
2023-06-23 03:49:55 -05:00
f66a42a84a removed crontab from dockerfile
All checks were successful
continuous-integration/drone/push Build is passing
2023-06-23 03:08:36 -05:00
b16875da29 included supercronic
Some checks failed
continuous-integration/drone/push Build is failing
2023-06-23 03:06:59 -05:00
b4d92128df added supercronic
All checks were successful
continuous-integration/drone/push Build is passing
2023-06-19 18:46:37 -05:00
6a4d6e3813 disabled warning
All checks were successful
continuous-integration/drone/push Build is passing
2023-06-11 14:44:27 -05:00
b8d2ab1f15 tag only latest
All checks were successful
continuous-integration/drone/push Build is passing
2023-06-06 15:20:48 -05:00
0a7462ce9c fixing push repo
All checks were successful
continuous-integration/drone/push Build is passing
2023-06-06 15:12:36 -05:00
634820c50d enabled drone
Some checks failed
continuous-integration/drone/push Build is failing
2023-06-06 15:09:59 -05:00
e3ee336440 added drone support
All checks were successful
continuous-integration/drone/push Build is passing
2023-06-06 15:03:21 -05:00
989c1cd684 opt in to healthcheck 2023-06-06 00:48:21 -05:00
e833f06851 updated cron 2023-06-06 00:24:39 -05:00
44b0431490 Merge pull request 'add-healthcheck-support' (#17) from add-healthcheck-support into main
Reviewed-on: https://git.jbranan.com/jblu/crane/pulls/17
2023-06-05 23:18:23 -05:00
43c79b13ca changed apprise client 2023-06-05 23:16:19 -05:00
8509269085 #14 2023-06-05 23:10:33 -05:00
4a5060c7ae Merge pull request 'added apprise support #15' (#16) from add-apprise-support into main
Reviewed-on: https://git.jbranan.com/jblu/crane/pulls/16
2023-06-05 22:34:15 -05:00
8908315665 added apprise support #15 2023-06-05 22:30:53 -05:00
13 changed files with 196 additions and 48 deletions

View File

@ -3,7 +3,6 @@ LICENSE
*.log
README.md
requirements.txt
test_*
Dockerfile
*docker-test*
*.log
@ -11,6 +10,11 @@ Dockerfile
*.csv
*.toml
*.git*
.DS_Store
.vscode/*
thunder-tests/*
.drone.yml
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

77
.drone.yml Normal file
View File

@ -0,0 +1,77 @@
kind: pipeline
name: default
steps:
- name: docker
image: plugins/docker
settings:
registry: git.jonb.io
dry_run: false
username: jblu
password:
from_secret: gittea_drone
repo: git.jonb.io/jblu/crane
tags:
- latest
when:
branch:
- main
event:
- push
- pull_request
- name: docker-test
image: plugins/docker
settings:
registry: git.jonb.io
dry_run: false
username: jblu
password:
from_secret: gittea_drone
repo: git.jonb.io/jblu/crane
tags:
- dev
when:
branch:
- dev*
event:
- push
- pull_request
- name: test-main
image: git.jonb.io/jblu/crane:latest
environment:
CRANE_HOST:
from_secret: CRANE_HOST
CRANE_PORT:
from_secret: CRANE_PORT
CRANE_ENDPOINT:
from_secret: CRANE_ENDPOINT
commands:
- echo $CRANE_HOST
- echo $CRANE_PORT
- echo $CRANE_ENDPOINT
- python test_crane.py
when:
branch:
- main
event:
- push
- pull_request
- name: test-dev
image: git.jonb.io/jblu/crane:dev
environment:
CRANE_HOST:
from_secret: CRANE_HOST
CRANE_PORT:
from_secret: CRANE_PORT
CRANE_ENDPOINT:
from_secret: CRANE_ENDPOINT
commands:
- echo $CRANE_HOST
- echo $CRANE_PORT
- echo $CRANE_ENDPOINT
- python test_crane.py
when:
branch:
- dev*
event:
- push
- pull_request

37
AppriseClient.py Normal file
View File

@ -0,0 +1,37 @@
import requests as r
from tomllib import load
import os
def apprise_notify(req_obj, host, port, aurls, title, body):
payload = {'urls': aurls,'title': title,'body': body,}
url = f'http://{host}:{port}/notify/'
apprise_response = req_obj.post(url, json = payload ,verify=False)
return apprise_response
class AppriseClient:
def __init__(self):
if os.getenv("DOCKER"):
self.host = os.getenv("host")
self.port = os.getenv("port")
self.aurls = os.getenv("aurls")
self.title = os.getenv("title")
self.body = os.getenv("body")
if os.getenv("toml_path"):
config_file_path=os.getenv("toml_path")
with open(config_file_path, 'rb') as c:
self.config = load(c)
if os.path.exists('./config.toml'):
config_file_path = './config.toml'
with open(config_file_path, 'rb') as c:
self.config = load(c)
if self.config:
self.host = self.config["apprise"]["host"]
self.port = self.config["apprise"]["port"]
self.aurls = self.config["apprise"]["aurls"]
self.title = self.config["apprise"]["title"]
self.body = self.config["apprise"]["body"]
self.apprise_response = apprise_notify(r,self.host,self.port,self.aurls,self.title,self.body)
if __name__ == "__main__":
AppriseClient()

View File

@ -1,7 +1,7 @@
FROM python:alpine3.18
WORKDIR /
COPY . opt
RUN apk add --no-cache supercronic
RUN pip install requests
RUN crontab /opt/crontab
RUN chmod +x /opt/entrypoint.sh
CMD ["/opt/entrypoint.sh"]

View File

@ -1,2 +1,4 @@
# crane
App to make sure specified containers are running in portainer
a

View File

@ -5,11 +5,19 @@ def c_get_containers(req_obj, host, port, access_token, endpoint):
def c_get_filtered_containers(req_obj, j_obj, host, port, access_token, endpoint, containers, statuses):
filter_string = j_obj.dumps({"name":containers,"status":statuses})
# url = 'https://192.168.4.11:9443/api/endpoints/1/docker/containers/json?filters={"name": ["restic","qbittorrent"],"status": ["paused","dead","created","exited","removing","restarting","created"]}'
url = f'https://{host}:{port}/api/endpoints/{endpoint}/docker/containers/json?filters={filter_string}'
c_get_containers_response = req_obj.get(url, headers={"X-API-Key": access_token}, verify=False)
return c_get_containers_response.json()
def c_get_container_id(req_obj, j_obj, host, port, access_token, endpoint, containers, statuses):
statuses.append("running")
filter_string = j_obj.dumps({"name":containers,"status":statuses})
url = f'https://{host}:{port}/api/endpoints/{endpoint}/docker/containers/json?filters={filter_string}'
c_get_container_id_response = req_obj.get(url, headers={"X-API-Key": access_token}, verify=False)
id = c_get_container_id_response.json()
id = id[0]["Id"]
return id
def c_start_container(req_obj, host, port, access_token, endpoint, cid):
url = f'https://{host}:{port}/api/endpoints/{endpoint}/docker/containers/{cid}/start'
c_start_container_response = req_obj.post(url, headers={"X-API-Key": access_token},verify=False)

View File

@ -16,11 +16,15 @@ def cont_notify(self):
if self.use_pushover:
self.poc = self.po.Pushover(self.po_token)
def cont_notify_summary(self):
def cont_notify_summary(self, app_obj, req_obj):
"""Main notification method when the app is used in an automated fashion"""
title = "--- crane summary ---"
body = f'{self.extm}'
if self.use_pushover:
self.poc.message(self.po_key,f" \
{self.extm}", title="--- crane summary ---")
#TODO figure out why the flip it thinks its getting 4 pos args
self.poc.message(self.po_key, message=body, title=title)
if self.use_apprise:
app_obj(req_obj, self.apprise_host, self.apprise_port, self.apprise_aurls, title, body)
def list_first_cont(self, index=0):
"""Only lists the first torrent"""
@ -36,3 +40,11 @@ def get_script_runtime(self):
self.tl.info(f'Execution time: [{elapsed_time}]')
if self.use_pushover:
self.extm = f"Execution time: [{elapsed_time}]"
if self.use_apprise:
self.extm = f"Execution time: [{elapsed_time}]"
def send_ping(self, req_obj, healthcheck_url):
try:
req_obj.get(healthcheck_url, timeout=10)
except req_obj.RequestException as e:
self.tl.info(f"Ping failed: {e}")

View File

@ -9,16 +9,18 @@ import logging
import requests
from tomllib import load
import os
from AppriseClient import apprise_notify
requests.packages.urllib3.disable_warnings()
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()
config_file_path=os.environ["toml_path"]
with open(config_file_path, 'rb') as c:
self.config = load(c)
if os.getenv("toml_path"):
config_file_path=os.getenv("toml_path")
with open(config_file_path, 'rb') as c:
self.config = load(c)
if os.path.exists('./config.toml'):
config_file_path = './config.toml'
with open(config_file_path, 'rb') as c:
@ -45,13 +47,23 @@ class Crn:
self.use_pushover = self.config["pushover"]["use_pushover"]
self.po_key = self.config["pushover"]["po_key"]
self.po_token = self.config["pushover"]["po_token"]
#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"]
#containers
self.observed_containers = list(self.config["containers"].values())
self.container_statuses = ["paused","dead","created","exited","removing","restarting","created"]
#healthcheck
self.use_healthcheck = self.config["healthcheck"]["use_healthcheck"]
self.healthcheck_url = self.config["healthcheck"]["healthcheck_url"]
cont_log(self)
cont_notify(self)
self.t = time
if self.use_healthcheck:
send_ping(self, requests, self.healthcheck_url.rstrip("/") + "/start")
#logging in
try:
@ -71,7 +83,12 @@ class Crn:
self.et = datetime.datetime.now()
get_script_runtime(self)
if self.use_pushover:
cont_notify_summary(self)
cont_notify_summary(self, apprise_notify, requests)
if self.use_apprise:
cont_notify_summary(self, apprise_notify, requests)
if self.use_healthcheck:
send_ping(self, requests, self.healthcheck_url)
# Run
if __name__== "__main__":
Crn()

View File

@ -1 +0,0 @@
0 1 * * * . /etc/environment; python /opt/crane.py >> /logfile

View File

@ -1,5 +1,7 @@
#!/bin/sh
printenv | grep -v "no_proxy" >> /etc/environment
CRON_CONFIG_FILE="/opt/crontab"
crond -f
echo "${CRON} python /opt/crane.py" > $CRON_CONFIG_FILE
exec supercronic -passthrough-logs -quiet $CRON_CONFIG_FILE

View File

@ -2,45 +2,53 @@ import unittest
import requests
import json
from tomllib import load
from cclient import c_get_filtered_containers, c_start_container, c_stop_container, c_get_filtered_containers
from cclient import c_get_filtered_containers, c_start_container, c_stop_container, c_get_container_id
from cprocess import process_cont_list
# unittest.TestLoader.sortTestMethodsUsing = None
import os
class TestCrane(unittest.TestCase):
def setUp(self):
with open('./config.toml', 'rb') as c:
self.config = load(c)
self.host = self.config["portainer"]["host"]
self.port = self.config["portainer"]["port"]
self.access_token = "ptr_ufS1nADXmrU3QSN3bvITLMQ7oOH9yo3ECb/QNwtIYJ4="
self.endpoint = self.config["portainer"]["endpoint"]
self.cid = 'ef8fee86e02b2b82acbddf6f0da1ff023f60bfe52c0b4087cac29c1686ccbac4'
self.req_obj = requests
self.j_obj = json
self.hypercare_containers = ['restic','qbittorrent']
self.req_obj.packages.urllib3.disable_warnings()
config_file_path = './docker/config.toml'
if os.getenv("toml_path", os.path.exists(config_file_path)):
with open(config_file_path, 'rb') as c:
self.config = load(c)
self.host = self.config["portainer"]["host"]
self.port = self.config["portainer"]["port"]
self.endpoint = self.config["portainer"]["endpoint"]
else:
self.host = os.getenv("CRANE_HOST", "192.168.4.11")
self.port = os.getenv("CRANE_PORT", 9443)
self.endpoint = os.getenv("CRANE_ENDPOINT", 1)
self.access_token = "ptr_ufS1nADXmrU3QSN3bvITLMQ7oOH9yo3ECb/QNwtIYJ4="
self.hypercare_containers = ['hottub']
self.status_filters = ["paused","dead","created","exited","removing","restarting","created"]
self.cont_obj = c_get_filtered_containers(self.req_obj,self.j_obj, self.host, self.port, self.access_token, self.endpoint,self.hypercare_containers,self.status_filters)
def test_c_get_filtered_containers(self):
self.cid = c_get_container_id(self.req_obj,self.j_obj, self.host, self.port, self.access_token, self.endpoint, self.hypercare_containers,self.status_filters)
self.c_stop_container_response = c_stop_container(self.req_obj, self.host, self.port, self.access_token, self.endpoint, self.cid)
self.cont_obj = c_get_filtered_containers(self.req_obj,self.j_obj, self.host, self.port, self.access_token, self.endpoint,self.hypercare_containers,self.status_filters)
self.assertTrue(self.cont_obj, "No cont object returned by c_get_filtered_containers.")
def test_c_start_container(self):
self.cid = c_get_container_id(self.req_obj,self.j_obj, self.host, self.port, self.access_token, self.endpoint, self.hypercare_containers,self.status_filters)
c_stop_container(self.req_obj, self.host, self.port, self.access_token, self.endpoint, self.cid)
self.c_start_container_response = c_start_container(self.req_obj, self.host, self.port, self.access_token, self.endpoint, self.cid)
# print(self.c_start_container_response)
self.assertTrue(self.c_start_container_response, "No c_start_container_resonse returned by c_start_container.")
# 204 success 304 already on
def test_c_process_cont_list(self):
self.cont_obj = c_get_filtered_containers(self.req_obj,self.j_obj, self.host, self.port, self.access_token, self.endpoint,self.hypercare_containers,self.status_filters)
self.assertTrue(process_cont_list(self.cont_obj, c_start_container, self.req_obj, self.host, self.port, self.access_token, self.endpoint))
def test_c_stop_container(self):
self.cid = c_get_container_id(self.req_obj,self.j_obj, self.host, self.port, self.access_token, self.endpoint, self.hypercare_containers,self.status_filters)
self.c_stop_container_response = c_stop_container(self.req_obj, self.host, self.port, self.access_token, self.endpoint, self.cid)
# print(self.c_stop_container_response)
self.assertTrue(self.c_stop_container_response, "No c_start_container_resonse returned by c_start_container.")
def test_z_tear_down(self):
c_stop_container(self.req_obj, self.host, self.port, self.access_token, self.endpoint, self.cid)
if __name__ == '__main__':
unittest.main()

View File

@ -1,12 +0,0 @@
import pushover
from tomllib import load
with open('./config.toml', mode="rb") as c:
config = load(c)
po_key = config["pushover"]["po_key"]
po_token = config["pushover"]["po_token"]
message = "hello"
# pushover.Pushover(po_token).message(po_key, message, title="--- crane summary ---")

View File

@ -1,6 +0,0 @@
from tomllib import load
with open('./config.toml', mode="rb") as c:
config = load(c)
print(config["containers"].values())