Typo in namepsace

This commit is contained in:
nolash 2021-04-19 13:41:53 +02:00
parent 3eaca2577e
commit 2524d5ccb4
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
6 changed files with 65 additions and 28 deletions

View File

@ -142,7 +142,7 @@ RPCConnection.register_location(config.get('SIGNER_SOCKET_PATH'), chain_spec, 's
Otx.tracing = config.true('TASKS_TRACE_QUEUE_STATUS') Otx.tracing = config.true('TASKS_TRACE_QUEUE_STATUS')
liveness.linux.load('cic-eth', health_modules) liveness.linux.load(health_modules)
def main(): def main():
argv = ['worker'] argv = ['worker']
@ -178,9 +178,9 @@ def main():
connect_declarator(rpc, chain_spec, trusted_addresses) connect_declarator(rpc, chain_spec, trusted_addresses)
connect_token_registry(rpc, chain_spec) connect_token_registry(rpc, chain_spec)
liveness.linux.set('cic-eth') liveness.linux.set()
current_app.worker_main(argv) current_app.worker_main(argv)
liveness.linux.reset('cic-eth') liveness.linux.reset()
@celery.signals.eventlet_pool_postshutdown.connect @celery.signals.eventlet_pool_postshutdown.connect

View File

@ -53,3 +53,5 @@ COPY cic-eth/crypto_dev_signer_config/ /usr/local/etc/crypto-dev-signer/
RUN git clone https://gitlab.com/grassrootseconomics/cic-contracts.git && \ RUN git clone https://gitlab.com/grassrootseconomics/cic-contracts.git && \
mkdir -p /usr/local/share/cic/solidity && \ mkdir -p /usr/local/share/cic/solidity && \
cp -R cic-contracts/abis /usr/local/share/cic/solidity/abi cp -R cic-contracts/abis /usr/local/share/cic/solidity/abi
COPY util/liveness/health.sh /usr/local/bin/health.sh

View File

@ -1,33 +1,34 @@
#!/bin/bash #!/bin/bash
rundir=${CIC_RUNDIR:-/run} rundir=${CIC_RUNDIR:-/run}
unit=${CIC_UNIT:-$HOSTNAME}
read p < $rundir/$CIC_UNIT/pid read p < $rundir/$unit/pid
if [ -z $p ]; then if [ -z $p ]; then
>&2 echo unit $CIC_UNIT has no pid >&2 echo unit $unit has no pid
exit 1 exit 1
fi fi
if [ ! -d /proc/$p ]; then if [ ! -d /proc/$p ]; then
>&2 echo unit $CIC_UNIT reports non-existent pid $p >&2 echo unit $unit reports non-existent pid $p
exit 1 exit 1
fi fi
>&2 echo unit $CIC_UNIT has pid $p >&2 echo unit $unit has pid $p
if [ ! -f $rundir/$CIC_UNIT/error ]; then if [ ! -f $rundir/$unit/error ]; then
>&2 echo unit $CIC_UNIT has unspecified state >&2 echo unit $unit has unspecified state
exit 1 exit 1
fi fi
read e 2> /dev/null < $rundir/$CIC_UNIT/error read e 2> /dev/null < $rundir/$unit/error
if [ -z $e ]; then if [ -z $e ]; then
>&2 echo unit $CIC_UNIT has unspecified state >&2 echo unit $unit has unspecified state
exit 1 exit 1
fi fi
>&2 echo unit $CIC_UNIT has error $e >&2 echo unit $unit has error $e
if [ $e -gt 0 ]; then if [ $e -gt 0 ]; then
exit 1; exit 1;

View File

@ -8,8 +8,18 @@ logg = logging.getLogger().getChild(__name__)
pid = os.getpid() pid = os.getpid()
default_namespace = os.environ.get('LIVENESS_UNIT_NAME')
if default_namespace == None:
import socket
default_namespace = socket.gethostname()
def load(check_strs, namespace=default_namespace, rundir='/run'):
if namespace == None:
import socket
namespace = socket.gethostname()
def load(namespace, check_strs, rundir='/run'):
logg.info('pid ' + str(pid)) logg.info('pid ' + str(pid))
checks = [] checks = []
@ -31,13 +41,13 @@ def load(namespace, check_strs, rundir='/run'):
f.close() f.close()
def set(namespace, error=0, rundir='/run'): def set(error=0, namespace=default_namespace, rundir='/run'):
app_rundir = os.path.join(rundir, namespace) app_rundir = os.path.join(rundir, namespace)
f = open(os.path.join(app_rundir, 'error'), 'w') f = open(os.path.join(app_rundir, 'error'), 'w')
f.write(str(error)) f.write(str(error))
f.close() f.close()
def reset(namespace, rundir='/run'): def reset(namespace=default_namespace, rundir='/run'):
app_rundir = os.path.join(rundir, namespace) app_rundir = os.path.join(rundir, namespace)
os.unlink(os.path.join(app_rundir, 'error')) os.unlink(os.path.join(app_rundir, 'error'))

View File

@ -1,7 +1,7 @@
from setuptools import setup from setuptools import setup
setup( setup(
name='liveness', name='liveness',
version='0.0.1a2', version='0.0.1a4',
packages=['liveness'], packages=['liveness'],
include_package_data=True, include_package_data=True,
) )

View File

@ -3,6 +3,7 @@ import os
import unittest import unittest
import logging import logging
import tempfile import tempfile
import socket
# local imports # local imports
import liveness.linux import liveness.linux
@ -18,27 +19,37 @@ data_dir = os.path.join(script_dir, 'testdata')
run_base_dir = os.path.join(data_dir, 'run') run_base_dir = os.path.join(data_dir, 'run')
class TestImports(unittest.TestCase): class TestImports(unittest.TestCase):
def setUp(self): def setUp(self):
os.makedirs(run_base_dir, exist_ok=True) os.makedirs(run_base_dir, exist_ok=True)
self.run_dir = tempfile.mkdtemp(dir=run_base_dir) self.run_dir = tempfile.mkdtemp(dir=run_base_dir)
self.unit_dir = os.path.join(self.run_dir, 'unittest') self.unit = 'unittest'
self.unit_dir = os.path.join(self.run_dir, self.unit)
self.pid_path = os.path.join(self.unit_dir, 'pid') self.pid_path = os.path.join(self.unit_dir, 'pid')
self.error_path = os.path.join(self.unit_dir, 'error') self.error_path = os.path.join(self.unit_dir, 'error')
self.host_path = os.path.join(self.run_dir, socket.gethostname())
def test_no_import(self): def test_no_import(self):
liveness.linux.load('unittest', [], rundir=self.run_dir) liveness.linux.load([], namespace=self.unit, rundir=self.run_dir)
f = open(self.pid_path, 'r') f = open(self.pid_path, 'r')
r = f.read() r = f.read()
f.close() f.close()
self.assertEqual(str(os.getpid()), r) self.assertEqual(str(os.getpid()), r)
def test_hostname(self):
liveness.linux.load([], rundir=self.run_dir)
f = open(os.path.join(self.host_path, 'pid'), 'r')
r = f.read()
f.close()
self.assertEqual(str(os.getpid()), r)
def test_import_single_true(self): def test_import_single_true(self):
checks = ['tests.imports.import_true'] checks = ['tests.imports.import_true']
liveness.linux.load('unittest', checks, rundir=self.run_dir) liveness.linux.load(checks, namespace=self.unit, rundir=self.run_dir)
f = open(self.pid_path, 'r') f = open(self.pid_path, 'r')
r = f.read() r = f.read()
f.close() f.close()
@ -48,7 +59,7 @@ class TestImports(unittest.TestCase):
def test_import_single_false(self): def test_import_single_false(self):
checks = ['tests.imports.import_false'] checks = ['tests.imports.import_false']
with self.assertRaises(RuntimeError): with self.assertRaises(RuntimeError):
liveness.linux.load('unittest', checks, rundir=self.run_dir) liveness.linux.load(checks, namespace=self.unit, rundir=self.run_dir)
with self.assertRaises(FileNotFoundError): with self.assertRaises(FileNotFoundError):
os.stat(self.pid_path) os.stat(self.pid_path)
@ -56,14 +67,14 @@ class TestImports(unittest.TestCase):
def test_import_false_then_true(self): def test_import_false_then_true(self):
checks = ['tests.imports.import_false', 'tests.imports.import_true'] checks = ['tests.imports.import_false', 'tests.imports.import_true']
with self.assertRaises(RuntimeError): with self.assertRaises(RuntimeError):
liveness.linux.load('unittest', checks, rundir=self.run_dir) liveness.linux.load(checks, namespace=self.unit, rundir=self.run_dir)
with self.assertRaises(FileNotFoundError): with self.assertRaises(FileNotFoundError):
os.stat(self.pid_path) os.stat(self.pid_path)
def test_import_multiple_true(self): def test_import_multiple_true(self):
checks = ['tests.imports.import_true', 'tests.imports.import_true'] checks = ['tests.imports.import_true', 'tests.imports.import_true']
liveness.linux.load('unittest', checks, rundir=self.run_dir) liveness.linux.load(checks, namespace=self.unit, rundir=self.run_dir)
f = open(self.pid_path, 'r') f = open(self.pid_path, 'r')
r = f.read() r = f.read()
f.close() f.close()
@ -71,24 +82,37 @@ class TestImports(unittest.TestCase):
def test_set(self): def test_set(self):
liveness.linux.load('unittest', [], rundir=self.run_dir) liveness.linux.load([], namespace='unittest', rundir=self.run_dir)
liveness.linux.set('unittest', rundir=self.run_dir) liveness.linux.set(namespace='unittest', rundir=self.run_dir)
f = open(self.error_path, 'r') f = open(self.error_path, 'r')
r = f.read() r = f.read()
f.close() f.close()
self.assertEqual('0', r) self.assertEqual('0', r)
liveness.linux.set('unittest', 42, rundir=self.run_dir) liveness.linux.set(error=42, namespace='unittest', rundir=self.run_dir)
f = open(self.error_path, 'r') f = open(self.error_path, 'r')
r = f.read() r = f.read()
f.close() f.close()
self.assertEqual('42', r) self.assertEqual('42', r)
liveness.linux.reset('unittest', rundir=self.run_dir) liveness.linux.reset(namespace='unittest', rundir=self.run_dir)
with self.assertRaises(FileNotFoundError): with self.assertRaises(FileNotFoundError):
os.stat(self.error_path) os.stat(self.error_path)
def test_set_hostname(self):
liveness.linux.load([], rundir=self.run_dir)
liveness.linux.set(rundir=self.run_dir)
error_path = os.path.join(self.host_path, 'error')
f = open(error_path, 'r')
r = f.read()
f.close()
self.assertEqual('0', r)
liveness.linux.reset(rundir=self.run_dir)
with self.assertRaises(FileNotFoundError):
os.stat(error_path)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()