Compare commits
8 Commits
master
...
lash/init-
Author | SHA1 | Date | |
---|---|---|---|
|
e251612487 | ||
|
86592b837a | ||
|
fae3258c72 | ||
|
9cfb97eb35 | ||
|
b6b6e36da9 | ||
|
a5cc173143 | ||
|
d5392ff160 | ||
|
58b890b971 |
@ -5,7 +5,13 @@ set -a
|
|||||||
if [ -z $DEV_DATA_DIR ]; then
|
if [ -z $DEV_DATA_DIR ]; then
|
||||||
export DEV_DATA_DIR=`mktemp -d`
|
export DEV_DATA_DIR=`mktemp -d`
|
||||||
else
|
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
|
fi
|
||||||
|
|
||||||
if [ -z $DEV_CONFIG_RESET ]; then
|
if [ -z $DEV_CONFIG_RESET ]; then
|
||||||
|
@ -117,6 +117,8 @@ export CIC_DEFAULT_TOKEN_SYMBOL=$CIC_DEFAULT_TOKEN_SYMBOL
|
|||||||
export TOKEN_NAME=$TOKEN_NAME
|
export TOKEN_NAME=$TOKEN_NAME
|
||||||
" >> "${DEV_DATA_DIR}"/env_reset
|
" >> "${DEV_DATA_DIR}"/env_reset
|
||||||
|
|
||||||
|
echo -n $CIC_REGISTRY_ADDRESS > $STATE_DIR/registry
|
||||||
|
|
||||||
set +a
|
set +a
|
||||||
set +e
|
set +e
|
||||||
|
|
||||||
|
@ -16,6 +16,7 @@ then
|
|||||||
>&2 echo -e "\033[;31mFAILED\033[;39m RUN_MASK 1 - contract deployment"
|
>&2 echo -e "\033[;31mFAILED\033[;39m RUN_MASK 1 - contract deployment"
|
||||||
exit 1;
|
exit 1;
|
||||||
fi
|
fi
|
||||||
|
echo -n 1 > $STATE_DIR/init
|
||||||
>&2 echo -e "\033[;32mSUCCEEDED\033[;39m RUN_MASK 1 - contract deployment"
|
>&2 echo -e "\033[;32mSUCCEEDED\033[;39m RUN_MASK 1 - contract deployment"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@ -27,5 +28,6 @@ then
|
|||||||
>&2 echo -e "\033[;31mFAILED\033[;39m RUN_MASK 2 - custodial service initialization"
|
>&2 echo -e "\033[;31mFAILED\033[;39m RUN_MASK 2 - custodial service initialization"
|
||||||
exit 1;
|
exit 1;
|
||||||
fi
|
fi
|
||||||
|
echo -n 2 > $STATE_DIR/init
|
||||||
>&2 echo -e "\033[;32mSUCCEEDED\033[;39m RUN_MASK 2 - custodial service initialization"
|
>&2 echo -e "\033[;32mSUCCEEDED\033[;39m RUN_MASK 2 - custodial service initialization"
|
||||||
fi
|
fi
|
||||||
|
1
apps/util/initness/MANIFEST.in
Normal file
1
apps/util/initness/MANIFEST.in
Normal file
@ -0,0 +1 @@
|
|||||||
|
include requirements.txt initness/data/config/**
|
3
apps/util/initness/initness/__init__.py
Normal file
3
apps/util/initness/initness/__init__.py
Normal 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 *
|
3
apps/util/initness/initness/data/config/httpd.ini
Normal file
3
apps/util/initness/initness/data/config/httpd.ini
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
[httpd]
|
||||||
|
host = localhost
|
||||||
|
port = 8000
|
2
apps/util/initness/initness/data/config/state.ini
Normal file
2
apps/util/initness/initness/data/config/state.ini
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
[state]
|
||||||
|
base_dir = /run/cic_init
|
87
apps/util/initness/initness/runnable/server.py
Normal file
87
apps/util/initness/initness/runnable/server.py
Normal 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()
|
31
apps/util/initness/initness/state.py
Normal file
31
apps/util/initness/initness/state.py
Normal 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
|
1
apps/util/initness/requirements.txt
Normal file
1
apps/util/initness/requirements.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
confini>=0.3.6rc4,<0.5.0
|
21
apps/util/initness/setup.py
Normal file
21
apps/util/initness/setup.py
Normal 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',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
)
|
27
apps/util/initness/tests/test_basic.py
Normal file
27
apps/util/initness/tests/test_basic.py
Normal 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()
|
Loading…
Reference in New Issue
Block a user