add jf-detector

This commit is contained in:
ducoterra
2020-10-12 15:56:04 -04:00
parent 96d6e306af
commit 0f849f2e10
8 changed files with 76 additions and 2 deletions

34
jf-detector.py Normal file
View File

@@ -0,0 +1,34 @@
from kubernetes import client, config
from requests.exceptions import ReadTimeout
import requests
import logging
import sys
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
try:
r = requests.get("https://jellyfin.ducoterra.net/users/public", timeout=5)
logging.info('Jellyfin up. Exiting.')
sys.exit(0)
except ReadTimeout as e:
logging.warning(e)
try:
logging.info('Trying incluster config')
config.load_incluster_config()
logging.info('Incluster config loaded')
except:
logging.info('Incluster config failed. Trying standard config')
config.load_kube_config()
logging.info('Standard config loaded')
v1 = client.CoreV1Api()
logging.info('Listing all pods in namespace ducoterra')
ret = v1.list_namespaced_pod(namespace="ducoterra")
jellyfin_pod = list(filter(lambda item: 'jellyfin' in item.metadata.name, ret.items))
logging.info(f'Found {len(jellyfin_pod)} pods')
if len(jellyfin_pod) > 0:
logging.warning('deleting jellyfin pod')
v1.delete_namespaced_pod(jellyfin_pod[0].metadata.name,'ducoterra')
else:
logging.error('no jellyfin pods found, not deleting')
sys.exit(1)