From 5453dadffe7cd44917b9e121b2a4e7a1d1c7ca4f Mon Sep 17 00:00:00 2001 From: Adar Nimrod <nimrod@shore.co.il> Date: Fri, 30 Apr 2021 19:52:22 +0300 Subject: [PATCH] Check the GitLab instance. --- functions.tf | 1 + requirements.txt | 1 + src/gitlab.py | 22 ++++++++++++++++++++++ src/utils.py | 12 ++++++++++++ 4 files changed, 36 insertions(+) create mode 100644 src/gitlab.py diff --git a/functions.tf b/functions.tf index 50d0920..3bc9e1c 100644 --- a/functions.tf +++ b/functions.tf @@ -2,6 +2,7 @@ locals { function_name_prefix = local.Name functions = [ "_dns", + "gitlab", ] } diff --git a/requirements.txt b/requirements.txt index 2f73596..5c7639c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ dnspython +requests diff --git a/src/gitlab.py b/src/gitlab.py new file mode 100644 index 0000000..d012691 --- /dev/null +++ b/src/gitlab.py @@ -0,0 +1,22 @@ +from utils import check_url, publish + + +def handler(event, context): # pylint: disable=unused-argument + """Lambda event handler.""" + checks = [ + {"url": "http://git.shore.co.il/", "codes": [301, 302]}, + {"url": "https://git.shore.co.il/", "codes": [301, 302]}, + {"url": "https://git.shore.co.il/explore/", "codes": [200]}, + ] + + for check in checks: + if not check_url(check["url"], valid_codes=check["codes"]): + message = f"Failed check for {check['url']}." + print(message) + publish(message) + else: + print(f"{check['url']} is OK.") + + +if __name__ == "__main__": + handler("event", "context") diff --git a/src/utils.py b/src/utils.py index 0dbc3cc..4ff0952 100644 --- a/src/utils.py +++ b/src/utils.py @@ -1,6 +1,7 @@ # pylint: disable=import-error import os import boto3 +import requests TOPIC_ARN = os.getenv("TOPIC_ARN") @@ -10,3 +11,14 @@ def publish(message): """Publish an SNS message.""" client = boto3.client("sns") client.publish(TopicArn=TOPIC_ARN, Message=message) + + +def check_url(url, method="GET", valid_codes=(200)): + """Checks URL for validity. + + Allows specifying the HTTP method and a list of valid codes.""" + try: + response = requests.request(method, url, allow_redirects=False) + return response.status_code in valid_codes + except Exception: # pylint: disable=broad-except + return False -- GitLab