Commit 437fb035 authored by Bram Daams's avatar Bram Daams
Browse files

hc.update_check

parent 887c1a10
Pipeline #34887 failed with stage
in 32 seconds
......@@ -6,6 +6,7 @@ import json
import hashlib
import platform
import re
import tzlocal
class hcCred:
......@@ -56,10 +57,14 @@ class hcRegistry:
def get_id(self, job):
r = self.find_by_hash(job)
if r:
print("lookup match by hash!")
return r['HC_ID']
r = self.find_by_jobid(job)
if r:
print("lookup match by JOB_ID")
# hash has changed, let's update the schedule
self.hc.update_check(r, job)
return r['HC_ID']
return False
......@@ -71,14 +76,14 @@ class hcRegistry:
class hc:
def __init__(self, cred):
self.cred = cred
self.auth_headers = { 'X-Api-Key': self.cred.api_key }
def get_checks(self):
"""Returns a list of checks from the HC API"""
url = "{}checks/".format(self.cred.url)
headers = {'X-Api-Key': self.cred.api_key}
try:
response = requests.get(url, headers=headers)
response = requests.get(url, headers=self.auth_headers)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
print(err)
......@@ -88,6 +93,36 @@ class hc:
raise Exception('fetching cron checks failed')
def update_check(self, registration, job):
url = "{apiurl}checks/{code}".format(
apiurl=self.cred.url,
code=registration['HC_ID']
)
data = {
'schedule': '* * * * *',
'tz': tzlocal.get_localzone().zone,
'tags': 'sch host_{}'.format(platform.node())
}
try:
response = requests.post(
url=url,
headers=self.auth_headers,
json=data
)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
print("ERROR")
print(err)
return False
return True
print(response)
def print_status(self, status_filter=""):
"""Show status of monitored cron jobs"""
checks = self.get_checks()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment