Compare commits

...

8 Commits

Author SHA1 Message Date
nolash e251612487
make -c switch config override 2021-09-29 07:31:13 +02:00
nolash 86592b837a
Move get state function to main module 2021-09-29 07:27:46 +02:00
nolash fae3258c72
Add missing module init file 2021-09-29 07:24:33 +02:00
nolash 9cfb97eb35
Add config schema setup, runnable 2021-09-29 07:21:42 +02:00
nolash b6b6e36da9
Rename init to runlevel 2021-09-29 07:11:26 +02:00
nolash a5cc173143
Move init server to initness under util, add test 2021-09-29 07:08:31 +02:00
nolash d5392ff160
Add state dir outputs for contract migration 2021-09-29 06:39:39 +02:00
nolash 58b890b971
Add read service 2021-09-29 06:34:42 +02:00
12 changed files with 187 additions and 1 deletions

View File

@ -5,7 +5,13 @@ set -a
if [ -z $DEV_DATA_DIR ]; then
export DEV_DATA_DIR=`mktemp -d`
else
mkdir -p $DEV_DATA_DIR
mkdir -vp $DEV_DATA_DIR
fi
if [ -z $STATE_DIR ]; then
export STATE_DIR=/run/cic_init
mkdir -vp $STATE_DIR
echo 0 > $STATE_DIR/init
fi
if [ -z $DEV_CONFIG_RESET ]; then

View File

@ -117,6 +117,8 @@ export CIC_DEFAULT_TOKEN_SYMBOL=$CIC_DEFAULT_TOKEN_SYMBOL
export TOKEN_NAME=$TOKEN_NAME
" >> "${DEV_DATA_DIR}"/env_reset
echo -n $CIC_REGISTRY_ADDRESS > $STATE_DIR/registry
set +a
set +e

View File

@ -16,6 +16,7 @@ then
>&2 echo -e "\033[;31mFAILED\033[;39m RUN_MASK 1 - contract deployment"
exit 1;
fi
echo -n 1 > $STATE_DIR/init
>&2 echo -e "\033[;32mSUCCEEDED\033[;39m RUN_MASK 1 - contract deployment"
fi
@ -27,5 +28,6 @@ then
>&2 echo -e "\033[;31mFAILED\033[;39m RUN_MASK 2 - custodial service initialization"
exit 1;
fi
echo -n 2 > $STATE_DIR/init
>&2 echo -e "\033[;32mSUCCEEDED\033[;39m RUN_MASK 2 - custodial service initialization"
fi

View File

@ -0,0 +1 @@
include requirements.txt initness/data/config/**

View File

@ -0,0 +1,3 @@
"""Initness reports the initialization state and entry-point variables for the contract migrations process of the CIC services deployment.
"""
from .state import *

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,87 @@
# standard import
import json
import os
import logging
import argparse
import sys
from http.server import (
HTTPServer,
BaseHTTPRequestHandler,
)
# external imports
import confini
# local imports
from initness import get_state
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
script_dir = os.path.dirname(os.path.realpath(__file__))
data_dir = os.path.join(script_dir, '..', 'data')
default_config_dir = os.path.join(data_dir, 'config')
config_dir = os.environ.get('CONFINI_DIR', default_config_dir)
argparser = argparse.ArgumentParser()
argparser.add_argument('-c', '--config', dest='c', type=str, help='configuration override directory')
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)
override_dirs = []
if args.c:
override_dirs = [args.c]
config = confini.Config(config_dir, args.env_prefix, override_dirs=override_dirs)
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):
o = get_state(self.state_store_dir)
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()
def 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'))
if __name__ == '__main__':
main()

View File

@ -0,0 +1,31 @@
# standard imports
import os
def get_state(state_store_dir):
init_path = os.path.join(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(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 = {
'runlevel': init_level,
'registry': registry_address,
}
return o

View File

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

View File

@ -0,0 +1,21 @@
from setuptools import setup
f = open('requirements.txt', 'r')
requirements = f.read()
f.close()
setup(
name='initness',
version='0.0.1a1',
install_requires=requirements,
packages=[
'initness',
'initness.runnable',
],
include_package_data=True,
entry_points = {
'console_scripts': [
'cic-init-server=initness.runnable.server:main',
],
},
)

View File

@ -0,0 +1,27 @@
# standard imports
import unittest
import tempfile
import os
# local imports
from initness.runnable.server import get_state
class TestInitness(unittest.TestCase):
def setUp(self):
self.dir = tempfile.mkdtemp()
f = open(os.path.join(self.dir, 'init'), 'w')
f.write('42')
f.close()
f = open(os.path.join(self.dir, 'registry'), 'w')
f.write('0xdeadbeef')
f.close()
def test_state(self):
o = get_state(self.dir)
self.assertEqual(o['runlevel'], '42')
self.assertEqual(o['registry'], '0xdeadbeef')
if __name__ == '__main__':
unittest.main()