Add read service

This commit is contained in:
nolash 2021-09-29 06:34:42 +02:00
parent 8a9d2ee0be
commit 58b890b971
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
4 changed files with 106 additions and 0 deletions

View File

@ -0,0 +1,3 @@
[httpd]
host = localhost
port = 8000

View File

@ -0,0 +1,2 @@
[state]
base_dir = /run/cic_init

View File

@ -0,0 +1 @@
confini>=0.3.6rc4,<0.5.0

100
apps/init_server/server.py Normal file
View File

@ -0,0 +1,100 @@
# standard import
import json
import os
import logging
import argparse
import sys
from http.server import (
HTTPServer,
BaseHTTPRequestHandler,
)
# external imports
import confini
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
default_config_dir = os.environ.get('CONFINI_DIR', './config')
argparser = argparse.ArgumentParser()
argparser.add_argument('-c', '--config', dest='c', default=default_config_dir, type=str, help='rpc provider')
argparser.add_argument('--host', type=str, help='httpd host')
argparser.add_argument('--port', type=str, help='httpd port')
argparser.add_argument('--state-dir', dest='state_dir', type=str, help='directory to read state from')
argparser.add_argument('--env-prefix', default=os.environ.get('CONFINI_ENV_PREFIX'), dest='env_prefix', type=str, help='environment prefix for variables to overwrite configuration')
argparser.add_argument('-v', action='store_true', help='be verbose')
argparser.add_argument('-vv', action='store_true', help='be more verbose')
args = argparser.parse_args()
if args.vv:
logging.getLogger().setLevel(logging.DEBUG)
elif args.v:
logging.getLogger().setLevel(logging.INFO)
config = confini.Config(args.c, args.env_prefix)
config.process()
# override args
args_override = {
'HTTPD_HOST': getattr(args, 'host'),
'HTTPD_PORT': getattr(args, 'port'),
'STATE_BASE_DIR': getattr(args, 'state_dir'),
}
config.dict_override(args_override, 'cli flag')
logg.debug('loaded config: {}\n'.format(config))
class StateRequestHandler(BaseHTTPRequestHandler):
state_store_dir = None
def do_GET(self):
init_path = os.path.join(self.state_store_dir, 'init')
init_level = 0
registry_address = None
try:
f = open(init_path, 'r')
init_level = f.read()
init_level = init_level.rstrip()
f.close()
except FileNotFoundError:
pass
registry_path = os.path.join(self.state_store_dir, 'registry')
try:
f = open(registry_path, 'r')
registry_address = f.read()
registry_address = registry_address.rstrip()
f.close()
except FileNotFoundError:
pass
o = {
'init': init_level,
'registry': registry_address,
}
logg.debug('headers {}'.format(self.headers))
logg.debug('i {}'.format(o))
self.send_response(200, 'yarr')
self.end_headers()
self.wfile.write(json.dumps(o).encode('utf-8'))
def run(store, host=None, port=None):
port = int(port, 10)
server_address = (host, port)
httpd = HTTPServer(server_address, StateRequestHandler)
httpd.serve_forever()
if __name__ == '__main__':
try:
os.stat(config.get('STATE_BASE_DIR'))
except FileNotFoundError:
os.makedirs(config.get('STATE_BASE_DIR'))
store = StateRequestHandler.state_store_dir=config.get('STATE_BASE_DIR')
run(store, host=config.get('HTTPD_HOST'), port=config.get('HTTPD_PORT'))