projects
/
mirror
/
dsa-puppet.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
move things from modules/roles/static* to modules/static*
[mirror/dsa-puppet.git]
/
modules
/
roles
/
files
/
mirror_health
/
mirror-health
diff --git
a/modules/roles/files/mirror_health/mirror-health
b/modules/roles/files/mirror_health/mirror-health
index
ed87f56
..
bbd4c27
100755
(executable)
--- a/
modules/roles/files/mirror_health/mirror-health
+++ b/
modules/roles/files/mirror_health/mirror-health
@@
-5,6
+5,7
@@
import requests
import time
import calendar
import logging
import time
import calendar
import logging
+import subprocess
from email.utils import parsedate
logging.basicConfig(level=logging.INFO)
from email.utils import parsedate
logging.basicConfig(level=logging.INFO)
@@
-12,16
+13,16
@@
HOSTS = os.environ['MIRROR_CHECK_HOSTS'].split()
OUTPUT_DIR = "/run/dsa-mirror-health-{}".format(os.environ['MIRROR_CHECK_SERVICE'])
HEALTH_FILE = os.path.join(OUTPUT_DIR, "health")
URL = os.environ['MIRROR_CHECK_URL']
OUTPUT_DIR = "/run/dsa-mirror-health-{}".format(os.environ['MIRROR_CHECK_SERVICE'])
HEALTH_FILE = os.path.join(OUTPUT_DIR, "health")
URL = os.environ['MIRROR_CHECK_URL']
+HEALTH_CHECK_URL = os.environ['MIRROR_CHECK_HEALTH_URL']
INTERVAL = int(os.environ.get('MIRROR_CHECK_INTERVAL', '60'))
INTERVAL = int(os.environ.get('MIRROR_CHECK_INTERVAL', '60'))
-latest_ts = 0
-
def retrieve_from_host(host, url):
proxies = {
'http': 'http://{}:80'.format(host),
'https': 'http://{}:443'.format(host),
}
def retrieve_from_host(host, url):
proxies = {
'http': 'http://{}:80'.format(host),
'https': 'http://{}:443'.format(host),
}
- return requests.get(url, timeout=5, proxies=proxies, allow_redirects=False)
+ headers = {'User-Agent': 'mirror-health'}
+ return requests.get(url, headers=headers, timeout=5, proxies=proxies, allow_redirects=False)
def last_modified(response):
lm = 0
def last_modified(response):
lm = 0
@@
-29,27
+30,47
@@
def last_modified(response):
lm = calendar.timegm(parsedate(response.headers['last-modified']))
return lm
lm = calendar.timegm(parsedate(response.headers['last-modified']))
return lm
-while True:
- start = time.time()
+def healthy(response):
+ if response.status_code == 200:
+ return True
+ return False
+
+def check_shutdown():
+ if subprocess.call(['dsa-is-shutdown-scheduled']) == 0:
+ logging.info("considering myself unhealthy, shutdown scheduled")
+ return False
+ return True
+
+def check_uptodate():
+ latest_ts = 0
for host in HOSTS:
try:
lm = last_modified(retrieve_from_host(host, URL))
logging.debug("lm for host %s: %s", host, lm)
for host in HOSTS:
try:
lm = last_modified(retrieve_from_host(host, URL))
logging.debug("lm for host %s: %s", host, lm)
- if
lm > latest_ts
:
- latest_ts =
lm
- except
requests.exceptions.ProxyError
:
+ if
healthy(retrieve_from_host(host, HEALTH_CHECK_URL))
:
+ latest_ts =
max(latest_ts, lm)
+ except
(requests.exceptions.ProxyError, requests.exceptions.ReadTimeout, requests.exceptions.ConnectTimeout, requests.exceptions.ConnectionError)
:
pass
pass
- local_lm = last_modified(retrieve_from_host('localhost', URL))
+ try:
+ local_lm = last_modified(retrieve_from_host('localhost', URL))
+ except (requests.exceptions.ProxyError, requests.exceptions.ReadTimeout, requests.exceptions.ConnectTimeout, requests.exceptions.ConnectionError):
+ return False
logging.debug("lm for localhost: %s", local_lm)
if local_lm < latest_ts:
logging.debug("lm for localhost: %s", local_lm)
if local_lm < latest_ts:
+ logging.info("considering myself unhealthy my ts=%s latest_ts=%s", local_lm, latest_ts)
+ return False
+ return True
+
+while True:
+ start = time.time()
+ if check_shutdown() and check_uptodate():
+ logging.info("considering myself healthy")
+ open(HEALTH_FILE, 'w').write("OK")
+ else:
try:
try:
- logging.info("considering myself unhealthy my ts=%s latest_ts=%s", local_lm, latest_ts)
os.remove(HEALTH_FILE)
except OSError:
pass
os.remove(HEALTH_FILE)
except OSError:
pass
- else:
- logging.info("considering myself healthy")
- open(HEALTH_FILE, 'w').write("OK")
sleep_time = start + INTERVAL - time.time()
logging.debug("sleeping for %d seconds", sleep_time)
time.sleep(sleep_time)
sleep_time = start + INTERVAL - time.time()
logging.debug("sleeping for %d seconds", sleep_time)
time.sleep(sleep_time)