From 9570f628ec485cb2fe52685555103dfe2a89e596 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Thu, 24 Jan 2019 09:04:15 +0200 Subject: Add: status subcommand --- README | 4 ++-- effitool | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/README b/README index de4a787..ccc3425 100644 --- a/README +++ b/README @@ -39,9 +39,9 @@ The list of known servers can be listed: Usage: ----------------------------------------------------------------------------- -To check the version of the membership register: +To check the status of the membership register: - ./effitool version + ./effitool status Legalese ----------------------------------------------------------------------------- diff --git a/effitool b/effitool index 35c03c0..1032da5 100755 --- a/effitool +++ b/effitool @@ -18,11 +18,15 @@ import argparse import configparser +import http +import json import os import sys +import urllib.request CONFIG_FILENAME = os.path.expanduser('~/.config/effitool/credentials.conf') +JSON = 'application/json' class Config: @@ -49,6 +53,30 @@ class Config: } +class HTTPAPI: + + def __init__(self, url): + self._url = url + + def url(self, path): + return '{}{}'.format(self._url, path) + + def get(self, path): + url = self.url(path) + with urllib.request.urlopen(url) as r: + status = r.getcode() + if status != http.HTTPStatus.OK: + raise Exception('Got HTTP status {}'.format(status)) + + info = r.info() + ct = info.get_content_type() + if ct != JSON: + raise Exception('Response is not JSON: {}'.ct) + + body = r.read() + return json.loads(body) + + def list_servers(args, config): for name in config.servers(): print('server', name) @@ -57,9 +85,19 @@ def list_servers(args, config): print(' ', key, server[key]) +def status(args, config): + for name in config.servers(): + server = config.get(name) + url = server['url'] + api = HTTPAPI(url) + obj = api.get('/status') + print('server', name, obj['resources']) + + def process_args(config): subcommands = [ ('list-servers', list_servers), + ('status', status), ] p = argparse.ArgumentParser() -- cgit v1.2.1