Make client report more data, use system config

This commit is contained in:
Ian Gulliver
2016-05-09 15:40:41 -07:00
parent 5c46707ea4
commit f62297b261

View File

@@ -2,6 +2,7 @@
import argparse import argparse
import json import json
import os
import socket import socket
import time import time
from ws4py.client import threadedclient from ws4py.client import threadedclient
@@ -9,10 +10,10 @@ from ws4py.client import threadedclient
parser = argparse.ArgumentParser(description='iconograph fetcher') parser = argparse.ArgumentParser(description='iconograph fetcher')
parser.add_argument( parser.add_argument(
'--server', '--config',
dest='server', dest='config',
action='store', action='store',
required=True) default='/etc/iconograph.json')
parser.add_argument( parser.add_argument(
'--https-ca-cert', '--https-ca-cert',
dest='https_ca_cert', dest='https_ca_cert',
@@ -29,8 +30,8 @@ parser.add_argument(
action='store', action='store',
required=True) required=True)
parser.add_argument( parser.add_argument(
'--image-type', '--server',
dest='image_type', dest='server',
action='store', action='store',
required=True) required=True)
FLAGS = parser.parse_args() FLAGS = parser.parse_args()
@@ -38,17 +39,24 @@ FLAGS = parser.parse_args()
class Client(threadedclient.WebSocketClient): class Client(threadedclient.WebSocketClient):
def __init__(self, config_path, *args, **kwargs):
super().__init__(*args, **kwargs)
with open(config_path, 'r') as fh:
self._config = json.load(fh)
def Loop(self): def Loop(self):
self.daemon = True self.daemon = True
self.connect() self.connect()
while True: while True:
report = {
'hostname': socket.gethostname(),
'uptime_seconds': self._Uptime(),
'next_timestamp': self._NextTimestamp(),
}
report.update(self._config)
self.send(json.dumps({ self.send(json.dumps({
'type': 'report', 'type': 'report',
'data': { 'data': report,
'hostname': socket.gethostname(),
'uptime_seconds': self._Uptime(),
'image_type': FLAGS.image_type,
},
}), False) }), False)
time.sleep(5.0) time.sleep(5.0)
@@ -56,10 +64,14 @@ class Client(threadedclient.WebSocketClient):
with open('/proc/uptime', 'r') as fh: with open('/proc/uptime', 'r') as fh:
return int(float(fh.readline().split(' ', 1)[0])) return int(float(fh.readline().split(' ', 1)[0]))
def _NextTimestamp(self):
next_image = os.path.basename(os.readlink('/isodevice/iconograph/current'))
return int(next_image.split('.', 1)[0])
def received_message(self, msg): def received_message(self, msg):
parsed = json.loads(str(msg)) parsed = json.loads(msg.data.decode('utf8'))
if parsed['type'] == 'image_types': if parsed['type'] == 'image_types':
assert FLAGS.image_type in parsed['data']['image_types'] assert self._config['image_type'] in parsed['data']['image_types']
def main(): def main():
@@ -68,7 +80,11 @@ def main():
'certfile': FLAGS.https_client_cert, 'certfile': FLAGS.https_client_cert,
'ca_certs': FLAGS.https_ca_cert, 'ca_certs': FLAGS.https_ca_cert,
} }
client = Client('wss://%s/ws/slave' % FLAGS.server, protocols=['iconograph-slave'], ssl_options=ssl_options) client = Client(
FLAGS.config,
'wss://%s/ws/slave' % FLAGS.server,
protocols=['iconograph-slave'],
ssl_options=ssl_options)
client.Loop() client.Loop()