Compare commits
19 Commits
44b0431490
...
main
Author | SHA1 | Date | |
---|---|---|---|
256488187e | |||
9576004c4b | |||
4b75a8fb32 | |||
9bf1d46ce3 | |||
eca3e3920f | |||
bc325787a3 | |||
5d6a77c108 | |||
f60c3be987 | |||
ff8136bf0c | |||
f66a42a84a | |||
b16875da29 | |||
b4d92128df | |||
6a4d6e3813 | |||
b8d2ab1f15 | |||
0a7462ce9c | |||
634820c50d | |||
e3ee336440 | |||
989c1cd684 | |||
e833f06851 |
@ -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
77
.drone.yml
Normal 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
|
@ -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"]
|
@ -1,2 +1,4 @@
|
||||
# crane
|
||||
App to make sure specified containers are running in portainer
|
||||
|
||||
a
|
10
cclient.py
10
cclient.py
@ -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)
|
||||
|
7
crane.py
7
crane.py
@ -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__":
|
||||
|
@ -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
|
@ -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__':
|
||||
|
@ -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 ---")
|
@ -1,6 +0,0 @@
|
||||
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