Skip to content

Commit

Permalink
Merge pull request ksdme#25 from kx-chen/detect-gitlab-down
Browse files Browse the repository at this point in the history
Detect if GitLab is down and CLI support for host checking/skipping
  • Loading branch information
ksdme authored Nov 5, 2018
2 parents 96eb2fc + 9bb5f80 commit d832331
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
12 changes: 12 additions & 0 deletions org_status/org_hosts/gitlab.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import json

import requests
from IGitt.GitLab.GitLab import GitLabPrivateToken
from IGitt.GitLab.GitLabOrganization import GitLabOrganization

Expand All @@ -9,6 +12,9 @@ class GitLabOrg(OrgHost):
HostName = 'gitlab'
StatusProvider = GitLabCIStatus

HOST_STATUS_URL = ('https://api.status.io/1.0/status'
'/5b36dc6502d06804c08349f7')

def __init__(self, token, group, **kargs):
super().__init__(**kargs)

Expand All @@ -18,6 +24,12 @@ def __init__(self, token, group, **kargs):

self._status_provider = self.StatusProvider(self._group)

@classmethod
def get_host_status(cls):
status = requests.get(cls.HOST_STATUS_URL)
status = json.loads(status.text)
return status['result']['status_overall']['status'] == 'Operational'

def process_repository(self, repo, branch='master'):
self.print_status(repo.web_url)

Expand Down
36 changes: 27 additions & 9 deletions org_status/org_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ def present_status(statuses, no_color):

def get_argument_parser():
parser = ArgumentParser()
parser.add_argument('orgs', nargs='+', help='host:org')
parser.add_argument('orgs', nargs='*', help='host:org')
parser.add_argument('--threads', type=int, default=2)
parser.add_argument('--no-color', action='store_true')
parser.add_argument('--verbose', '-v', action='store_true')
parser.add_argument('--hosts-only', '-o', action='store_true')
parser.add_argument('--skip-host-checks', action='store_true')

return parser

Expand All @@ -84,17 +86,33 @@ def main():
styled = (lambda l, *_: l) if args.no_color else colored
verbose = print if args.verbose else (lambda *_: None)

hosts_only_print = print if args.hosts_only else verbose

if len(args.orgs) == 0 and args.hosts_only:
args.orgs = ['coala']
elif len(args.orgs) == 0:
print(styled('no organizations to check', 'red'))
return

for Host, org in generate_fetch_jobs(args.orgs):
token = None

try:
if not Host.get_host_status():
print(styled(f'{Host.HostName} is currently down', 'red'))
continue
else:
verbose(f'{Host.HostName} is up')
except NotImplementedError:
verbose(f'{Host.HostName} does not support checking host status')
if not args.skip_host_checks:
try:
if not Host.get_host_status():
print(styled(f'{Host.HostName} is currently down', 'red'))
continue
else:
hosts_only_print(f'{Host.HostName} is up')
except NotImplementedError:
verbose(
f'{Host.HostName} does not support checking host status')

if args.hosts_only and args.skip_host_checks:
verbose('nothing to do')
return
elif args.hosts_only:
continue

if (args.verbose):
print(f'processing org {Host.HostName}:{org}')
Expand Down

0 comments on commit d832331

Please sign in to comment.