mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
Use decorator to automatically configure before starting any command (that requires configuration)
This commit is contained in:
parent
df9fd6dc23
commit
54ea18dd2b
@ -16,7 +16,6 @@ from bigchaindb.common.exceptions import (StartupError,
|
|||||||
DatabaseAlreadyExists,
|
DatabaseAlreadyExists,
|
||||||
KeypairNotFoundException)
|
KeypairNotFoundException)
|
||||||
import bigchaindb
|
import bigchaindb
|
||||||
import bigchaindb.config_utils
|
|
||||||
from bigchaindb.models import Transaction
|
from bigchaindb.models import Transaction
|
||||||
from bigchaindb.utils import ProcessGroup
|
from bigchaindb.utils import ProcessGroup
|
||||||
from bigchaindb import backend, processes
|
from bigchaindb import backend, processes
|
||||||
@ -29,7 +28,7 @@ from bigchaindb.commands.messages import (
|
|||||||
CANNOT_START_KEYPAIR_NOT_FOUND,
|
CANNOT_START_KEYPAIR_NOT_FOUND,
|
||||||
RETHINKDB_STARTUP_ERROR,
|
RETHINKDB_STARTUP_ERROR,
|
||||||
)
|
)
|
||||||
from bigchaindb.commands.utils import input_on_stderr
|
from bigchaindb.commands.utils import configure_bigchaindb, input_on_stderr
|
||||||
|
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
@ -42,12 +41,12 @@ logger = logging.getLogger(__name__)
|
|||||||
# should be printed to stderr.
|
# should be printed to stderr.
|
||||||
|
|
||||||
|
|
||||||
|
@configure_bigchaindb
|
||||||
def run_show_config(args):
|
def run_show_config(args):
|
||||||
"""Show the current configuration"""
|
"""Show the current configuration"""
|
||||||
# TODO Proposal: remove the "hidden" configuration. Only show config. If
|
# TODO Proposal: remove the "hidden" configuration. Only show config. If
|
||||||
# the system needs to be configured, then display information on how to
|
# the system needs to be configured, then display information on how to
|
||||||
# configure the system.
|
# configure the system.
|
||||||
bigchaindb.config_utils.autoconfigure(filename=args.config, force=True)
|
|
||||||
config = copy.deepcopy(bigchaindb.config)
|
config = copy.deepcopy(bigchaindb.config)
|
||||||
del config['CONFIGURED']
|
del config['CONFIGURED']
|
||||||
private_key = config['keypair']['private']
|
private_key = config['keypair']['private']
|
||||||
@ -120,10 +119,10 @@ def run_configure(args, skip_if_exists=False):
|
|||||||
print('Ready to go!', file=sys.stderr)
|
print('Ready to go!', file=sys.stderr)
|
||||||
|
|
||||||
|
|
||||||
|
@configure_bigchaindb
|
||||||
def run_export_my_pubkey(args):
|
def run_export_my_pubkey(args):
|
||||||
"""Export this node's public key to standard output
|
"""Export this node's public key to standard output
|
||||||
"""
|
"""
|
||||||
bigchaindb.config_utils.autoconfigure(filename=args.config, force=True)
|
|
||||||
pubkey = bigchaindb.config['keypair']['public']
|
pubkey = bigchaindb.config['keypair']['public']
|
||||||
if pubkey is not None:
|
if pubkey is not None:
|
||||||
print(pubkey)
|
print(pubkey)
|
||||||
@ -145,9 +144,9 @@ def _run_init():
|
|||||||
logger.info('Genesis block created.')
|
logger.info('Genesis block created.')
|
||||||
|
|
||||||
|
|
||||||
|
@configure_bigchaindb
|
||||||
def run_init(args):
|
def run_init(args):
|
||||||
"""Initialize the database"""
|
"""Initialize the database"""
|
||||||
bigchaindb.config_utils.autoconfigure(filename=args.config, force=True)
|
|
||||||
# TODO Provide mechanism to:
|
# TODO Provide mechanism to:
|
||||||
# 1. prompt the user to inquire whether they wish to drop the db
|
# 1. prompt the user to inquire whether they wish to drop the db
|
||||||
# 2. force the init, (e.g., via -f flag)
|
# 2. force the init, (e.g., via -f flag)
|
||||||
@ -158,9 +157,9 @@ def run_init(args):
|
|||||||
print('If you wish to re-initialize it, first drop it.', file=sys.stderr)
|
print('If you wish to re-initialize it, first drop it.', file=sys.stderr)
|
||||||
|
|
||||||
|
|
||||||
|
@configure_bigchaindb
|
||||||
def run_drop(args):
|
def run_drop(args):
|
||||||
"""Drop the database"""
|
"""Drop the database"""
|
||||||
bigchaindb.config_utils.autoconfigure(filename=args.config, force=True)
|
|
||||||
dbname = bigchaindb.config['database']['name']
|
dbname = bigchaindb.config['database']['name']
|
||||||
|
|
||||||
if not args.yes:
|
if not args.yes:
|
||||||
@ -173,10 +172,10 @@ def run_drop(args):
|
|||||||
schema.drop_database(conn, dbname)
|
schema.drop_database(conn, dbname)
|
||||||
|
|
||||||
|
|
||||||
|
@configure_bigchaindb
|
||||||
def run_start(args):
|
def run_start(args):
|
||||||
"""Start the processes to run the node"""
|
"""Start the processes to run the node"""
|
||||||
logger.info('BigchainDB Version %s', bigchaindb.__version__)
|
logger.info('BigchainDB Version %s', bigchaindb.__version__)
|
||||||
bigchaindb.config_utils.autoconfigure(filename=args.config, force=True)
|
|
||||||
|
|
||||||
if args.allow_temp_keypair:
|
if args.allow_temp_keypair:
|
||||||
if not (bigchaindb.config['keypair']['private'] or
|
if not (bigchaindb.config['keypair']['private'] or
|
||||||
@ -224,8 +223,8 @@ def _run_load(tx_left, stats):
|
|||||||
break
|
break
|
||||||
|
|
||||||
|
|
||||||
|
@configure_bigchaindb
|
||||||
def run_load(args):
|
def run_load(args):
|
||||||
bigchaindb.config_utils.autoconfigure(filename=args.config, force=True)
|
|
||||||
logger.info('Starting %s processes', args.multiprocess)
|
logger.info('Starting %s processes', args.multiprocess)
|
||||||
stats = logstats.Logstats()
|
stats = logstats.Logstats()
|
||||||
logstats.thread.start(stats)
|
logstats.thread.start(stats)
|
||||||
@ -240,6 +239,7 @@ def run_load(args):
|
|||||||
workers.start()
|
workers.start()
|
||||||
|
|
||||||
|
|
||||||
|
@configure_bigchaindb
|
||||||
def run_set_shards(args):
|
def run_set_shards(args):
|
||||||
conn = backend.connect()
|
conn = backend.connect()
|
||||||
try:
|
try:
|
||||||
@ -248,6 +248,7 @@ def run_set_shards(args):
|
|||||||
sys.exit(str(e))
|
sys.exit(str(e))
|
||||||
|
|
||||||
|
|
||||||
|
@configure_bigchaindb
|
||||||
def run_set_replicas(args):
|
def run_set_replicas(args):
|
||||||
conn = backend.connect()
|
conn = backend.connect()
|
||||||
try:
|
try:
|
||||||
@ -256,9 +257,9 @@ def run_set_replicas(args):
|
|||||||
sys.exit(str(e))
|
sys.exit(str(e))
|
||||||
|
|
||||||
|
|
||||||
|
@configure_bigchaindb
|
||||||
def run_add_replicas(args):
|
def run_add_replicas(args):
|
||||||
# Note: This command is specific to MongoDB
|
# Note: This command is specific to MongoDB
|
||||||
bigchaindb.config_utils.autoconfigure(filename=args.config, force=True)
|
|
||||||
conn = backend.connect()
|
conn = backend.connect()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -269,9 +270,9 @@ def run_add_replicas(args):
|
|||||||
print('Added {} to the replicaset.'.format(args.replicas))
|
print('Added {} to the replicaset.'.format(args.replicas))
|
||||||
|
|
||||||
|
|
||||||
|
@configure_bigchaindb
|
||||||
def run_remove_replicas(args):
|
def run_remove_replicas(args):
|
||||||
# Note: This command is specific to MongoDB
|
# Note: This command is specific to MongoDB
|
||||||
bigchaindb.config_utils.autoconfigure(filename=args.config, force=True)
|
|
||||||
conn = backend.connect()
|
conn = backend.connect()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -4,6 +4,7 @@ for ``argparse.ArgumentParser``.
|
|||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import builtins
|
import builtins
|
||||||
|
import functools
|
||||||
import multiprocessing as mp
|
import multiprocessing as mp
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
@ -12,11 +13,21 @@ import rethinkdb as r
|
|||||||
from pymongo import uri_parser
|
from pymongo import uri_parser
|
||||||
|
|
||||||
import bigchaindb
|
import bigchaindb
|
||||||
|
import bigchaindb.config_utils
|
||||||
from bigchaindb import backend
|
from bigchaindb import backend
|
||||||
from bigchaindb.common.exceptions import StartupError
|
from bigchaindb.common.exceptions import StartupError
|
||||||
from bigchaindb.version import __version__
|
from bigchaindb.version import __version__
|
||||||
|
|
||||||
|
|
||||||
|
def configure_bigchaindb(command):
|
||||||
|
@functools.wraps(command)
|
||||||
|
def configure(args):
|
||||||
|
bigchaindb.config_utils.autoconfigure(filename=args.config, force=True)
|
||||||
|
command(args)
|
||||||
|
|
||||||
|
return configure
|
||||||
|
|
||||||
|
|
||||||
# We need this because `input` always prints on stdout, while it should print
|
# We need this because `input` always prints on stdout, while it should print
|
||||||
# to stderr. It's a very old bug, check it out here:
|
# to stderr. It's a very old bug, check it out here:
|
||||||
# - https://bugs.python.org/issue1927
|
# - https://bugs.python.org/issue1927
|
||||||
|
@ -45,7 +45,7 @@ def test_set_shards(mock_reconfigure, monkeypatch, b):
|
|||||||
return {'shards': [{'replicas': [1]}]}
|
return {'shards': [{'replicas': [1]}]}
|
||||||
|
|
||||||
monkeypatch.setattr(rethinkdb.RqlQuery, 'run', mockreturn_one_replica)
|
monkeypatch.setattr(rethinkdb.RqlQuery, 'run', mockreturn_one_replica)
|
||||||
args = Namespace(num_shards=3)
|
args = Namespace(num_shards=3, config=None)
|
||||||
run_set_shards(args)
|
run_set_shards(args)
|
||||||
mock_reconfigure.assert_called_with(replicas=1, shards=3, dry_run=False)
|
mock_reconfigure.assert_called_with(replicas=1, shards=3, dry_run=False)
|
||||||
|
|
||||||
@ -72,7 +72,7 @@ def test_set_shards_raises_exception(monkeypatch, b):
|
|||||||
monkeypatch.setattr(rethinkdb.RqlQuery, 'run', mockreturn_one_replica)
|
monkeypatch.setattr(rethinkdb.RqlQuery, 'run', mockreturn_one_replica)
|
||||||
monkeypatch.setattr(rethinkdb.ast.Table, 'reconfigure', mock_raise)
|
monkeypatch.setattr(rethinkdb.ast.Table, 'reconfigure', mock_raise)
|
||||||
|
|
||||||
args = Namespace(num_shards=3)
|
args = Namespace(num_shards=3, config=None)
|
||||||
with pytest.raises(SystemExit) as exc:
|
with pytest.raises(SystemExit) as exc:
|
||||||
run_set_shards(args)
|
run_set_shards(args)
|
||||||
assert exc.value.args == ('Failed to reconfigure tables.',)
|
assert exc.value.args == ('Failed to reconfigure tables.',)
|
||||||
@ -88,7 +88,7 @@ def test_set_replicas(mock_reconfigure, monkeypatch, b):
|
|||||||
return {'shards': [1, 2]}
|
return {'shards': [1, 2]}
|
||||||
|
|
||||||
monkeypatch.setattr(rethinkdb.RqlQuery, 'run', mockreturn_two_shards)
|
monkeypatch.setattr(rethinkdb.RqlQuery, 'run', mockreturn_two_shards)
|
||||||
args = Namespace(num_replicas=2)
|
args = Namespace(num_replicas=2, config=None)
|
||||||
run_set_replicas(args)
|
run_set_replicas(args)
|
||||||
mock_reconfigure.assert_called_with(replicas=2, shards=2, dry_run=False)
|
mock_reconfigure.assert_called_with(replicas=2, shards=2, dry_run=False)
|
||||||
|
|
||||||
@ -115,7 +115,7 @@ def test_set_replicas_raises_exception(monkeypatch, b):
|
|||||||
monkeypatch.setattr(rethinkdb.RqlQuery, 'run', mockreturn_two_shards)
|
monkeypatch.setattr(rethinkdb.RqlQuery, 'run', mockreturn_two_shards)
|
||||||
monkeypatch.setattr(rethinkdb.ast.Table, 'reconfigure', mock_raise)
|
monkeypatch.setattr(rethinkdb.ast.Table, 'reconfigure', mock_raise)
|
||||||
|
|
||||||
args = Namespace(num_replicas=2)
|
args = Namespace(num_replicas=2, config=None)
|
||||||
with pytest.raises(SystemExit) as exc:
|
with pytest.raises(SystemExit) as exc:
|
||||||
run_set_replicas(args)
|
run_set_replicas(args)
|
||||||
assert exc.value.args == ('Failed to reconfigure tables.',)
|
assert exc.value.args == ('Failed to reconfigure tables.',)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user