Compare commits

...

19 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
11 changed files with 127 additions and 40 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

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

@ -10,6 +10,7 @@ import requests
from tomllib import load
import os
from AppriseClient import apprise_notify
requests.packages.urllib3.disable_warnings()
class Crn:
def __init__(self):
@ -55,11 +56,14 @@ class Crn:
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:
@ -82,7 +86,8 @@ class Crn:
cont_notify_summary(self, apprise_notify, requests)
if self.use_apprise:
cont_notify_summary(self, apprise_notify, requests)
send_ping(self, requests, self.healthcheck_url)
if self.use_healthcheck:
send_ping(self, requests, self.healthcheck_url)
# Run
if __name__== "__main__":

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,44 +2,52 @@ 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__':

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())