Skip to content

Commit

Permalink
Add config to Identity Transaction Processor
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Gunderson <[email protected]>
  • Loading branch information
agunde406 committed Nov 15, 2017
1 parent 61238ee commit 246cf1a
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 8 deletions.
6 changes: 6 additions & 0 deletions families/identity/packaging/identity.toml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#
# Sawtooth -- Identity Transaction Processor Configuration
#

# The url to connect to a running Validator
# connect = "tcp://localhost:4004"
Empty file.
129 changes: 129 additions & 0 deletions families/identity/sawtooth_identity/processor/config/identity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# Copyright 2017 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------------------------------------------------------------------

import collections
import logging
import os

import toml

from sawtooth_sdk.processor.exceptions import LocalConfigurationError

LOGGER = logging.getLogger(__name__)


def load_default_identity_config():
"""
Returns the default IdentityConfig
"""
return IdentityConfig(
connect='tcp://localhost:4004',
)


def load_toml_identity_config(filename):
"""Returns a IdentityConfig created by loading a TOML file from the
filesystem.
Args:
filename (string): The name of the file to load the config from
Returns:
config (IdentityConfig): The IdentityConfig created from the stored
toml file.
Raises:
LocalConfigurationError
"""
if not os.path.exists(filename):
LOGGER.info(
"Skipping transaction proccesor config loading from non-existent"
" config file: %s", filename)
return IdentityConfig()

LOGGER.info("Loading transaction processor information from config: %s",
filename)

try:
with open(filename) as fd:
raw_config = fd.read()
except IOError as e:
raise LocalConfigurationError(
"Unable to load transaction processor configuration file:"
" {}".format(str(e)))

toml_config = toml.loads(raw_config)
invalid_keys = set(toml_config.keys()).difference(
['connect'])
if invalid_keys:
raise LocalConfigurationError(
"Invalid keys in transaction processor config: "
"{}".format(", ".join(sorted(list(invalid_keys)))))

config = IdentityConfig(
connect=toml_config.get("connect", None)
)

return config


def merge_identity_config(configs):
"""
Given a list of IdentityConfig object, merges them into a single
IdentityConfig, giving priority in the order of the configs
(first has highest priority).
Args:
config (list of IdentityConfigs): The list of identity configs that
must be merged together
Returns:
config (IdentityConfig): One IdentityConfig that combines all of the
passed in configs.
"""
connect = None

for config in reversed(configs):
if config.connect is not None:
connect = config.connect

return IdentityConfig(
connect=connect
)


class IdentityConfig:
def __init__(self, connect=None):
self._connect = connect

@property
def connect(self):
return self._connect

def __repr__(self):
# not including password for opentsdb
return \
"{}(connect={})".format(
self.__class__.__name__,
repr(self._connect),
)

def to_dict(self):
return collections.OrderedDict([
('connect', self._connect),
])

def to_toml_string(self):
return str(toml.dumps(self.to_dict())).strip().split('\n')
57 changes: 49 additions & 8 deletions families/identity/sawtooth_identity/processor/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,19 @@
from colorlog import ColoredFormatter

from sawtooth_sdk.processor.core import TransactionProcessor
from sawtooth_sdk.processor.log import init_console_logging
from sawtooth_sdk.processor.log import log_configuration
from sawtooth_sdk.processor.config import get_log_config
from sawtooth_sdk.processor.config import get_log_dir
from sawtooth_sdk.processor.config import get_config_dir
from sawtooth_identity.processor.handler import IdentityTransactionHandler
from sawtooth_identity.processor.config.identity import IdentityConfig
from sawtooth_identity.processor.config.identity import \
load_default_identity_config
from sawtooth_identity.processor.config.identity import \
load_toml_identity_config
from sawtooth_identity.processor.config.identity import \
merge_identity_config


DISTRIBUTION_NAME = 'sawtooth-identity'
Expand Down Expand Up @@ -55,10 +67,23 @@ def create_console_handler(verbose_level):
return clog


def setup_loggers(verbose_level):
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(create_console_handler(verbose_level))
def setup_loggers(verbose_level, processor):
log_config = get_log_config(filename="identity_log_config.toml")

# If no toml, try loading yaml
if log_config is None:
log_config = get_log_config(filename="identity_log_config.yaml")

if log_config is not None:
log_configuration(log_config=log_config)
else:
log_dir = get_log_dir()
# use the transaction processor zmq identity for filename
log_configuration(
log_dir=log_dir,
name="identity-" + str(processor.zmq_id)[2:-1])

init_console_logging(verbose_level=verbose_level)


def create_parser(prog_name):
Expand All @@ -71,7 +96,6 @@ def create_parser(prog_name):

parser.add_argument(
'-C', '--connect',
default='tcp://localhost:4004',
help='Endpoint for the validator connection')

parser.add_argument(
Expand All @@ -95,21 +119,38 @@ def create_parser(prog_name):
return parser


def load_identity_config(first_config):
default_identity_config = \
load_default_identity_config()
conf_file = os.path.join(get_config_dir(), 'identity.toml')

toml_config = load_toml_identity_config(conf_file)

return merge_identity_config(
configs=[first_config, toml_config, default_identity_config])


def create_identity_config(args):
return IdentityConfig(connect=args.connect)


def main(prog_name=os.path.basename(sys.argv[0]), args=None,
with_loggers=True):
if args is None:
args = sys.argv[1:]
parser = create_parser(prog_name)
args = parser.parse_args(args)

arg_config = create_identity_config(args)
identity_config = load_identity_config(arg_config)
processor = TransactionProcessor(url=identity_config.connect)

if with_loggers is True:
if args.verbose is None:
verbose_level = 0
else:
verbose_level = args.verbose
setup_loggers(verbose_level=verbose_level)

processor = TransactionProcessor(url=args.connect)
setup_loggers(verbose_level=verbose_level, processor=processor)

handler = IdentityTransactionHandler()

Expand Down

0 comments on commit 246cf1a

Please sign in to comment.