diff options
author | Lars Wirzenius <liw@liw.fi> | 2019-01-24 09:04:15 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2019-01-24 09:04:15 +0200 |
commit | 9570f628ec485cb2fe52685555103dfe2a89e596 (patch) | |
tree | 8645424e602769008edeb064675515a84b41e234 | |
parent | 376fe411eb45fbf4c2d38b75b440e281336bcc27 (diff) | |
download | effitool-9570f628ec485cb2fe52685555103dfe2a89e596.tar.gz |
Add: status subcommand
-rw-r--r-- | README | 4 | ||||
-rwxr-xr-x | effitool | 38 |
2 files changed, 40 insertions, 2 deletions
@@ -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 ----------------------------------------------------------------------------- @@ -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() |