github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/families/identity/sawtooth_identity/processor/main.py (about) 1 # Copyright 2017 Intel Corporation 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # ------------------------------------------------------------------------------ 15 16 import argparse 17 import logging 18 import os 19 import sys 20 import pkg_resources 21 22 from colorlog import ColoredFormatter 23 24 from sawtooth_sdk.processor.core import TransactionProcessor 25 from sawtooth_sdk.processor.log import init_console_logging 26 from sawtooth_sdk.processor.log import log_configuration 27 from sawtooth_sdk.processor.config import get_log_config 28 from sawtooth_sdk.processor.config import get_log_dir 29 from sawtooth_sdk.processor.config import get_config_dir 30 from sawtooth_identity.processor.handler import IdentityTransactionHandler 31 from sawtooth_identity.processor.config.identity import IdentityConfig 32 from sawtooth_identity.processor.config.identity import \ 33 load_default_identity_config 34 from sawtooth_identity.processor.config.identity import \ 35 load_toml_identity_config 36 from sawtooth_identity.processor.config.identity import \ 37 merge_identity_config 38 39 40 DISTRIBUTION_NAME = 'sawtooth-identity' 41 42 43 def create_console_handler(verbose_level): 44 clog = logging.StreamHandler() 45 formatter = ColoredFormatter( 46 "%(log_color)s[%(asctime)s %(levelname)-8s%(module)s]%(reset)s " 47 "%(white)s%(message)s", 48 datefmt="%H:%M:%S", 49 reset=True, 50 log_colors={ 51 'DEBUG': 'cyan', 52 'INFO': 'green', 53 'WARNING': 'yellow', 54 'ERROR': 'red', 55 'CRITICAL': 'red', 56 }) 57 58 clog.setFormatter(formatter) 59 60 if verbose_level == 0: 61 clog.setLevel(logging.WARN) 62 elif verbose_level == 1: 63 clog.setLevel(logging.INFO) 64 else: 65 clog.setLevel(logging.DEBUG) 66 67 return clog 68 69 70 def setup_loggers(verbose_level, processor): 71 log_config = get_log_config(filename="identity_log_config.toml") 72 73 # If no toml, try loading yaml 74 if log_config is None: 75 log_config = get_log_config(filename="identity_log_config.yaml") 76 77 if log_config is not None: 78 log_configuration(log_config=log_config) 79 else: 80 log_dir = get_log_dir() 81 # use the transaction processor zmq identity for filename 82 log_configuration( 83 log_dir=log_dir, 84 name="identity-" + str(processor.zmq_id)[2:-1]) 85 86 init_console_logging(verbose_level=verbose_level) 87 88 89 def create_parser(prog_name): 90 parser = argparse.ArgumentParser( 91 description='Starts an Identity transaction processor.', 92 epilog='This process is required to apply any changes to on-chain ' 93 'permissions used by the Sawtooth platform.', 94 prog=prog_name, 95 formatter_class=argparse.RawDescriptionHelpFormatter) 96 97 parser.add_argument( 98 '-C', '--connect', 99 help='specify the endpoint for the validator connection') 100 101 parser.add_argument( 102 '-v', '--verbose', 103 action='count', 104 default=0, 105 help='enable more verbose output to stderr') 106 107 try: 108 version = pkg_resources.get_distribution(DISTRIBUTION_NAME).version 109 except pkg_resources.DistributionNotFound: 110 version = 'UNKNOWN' 111 112 parser.add_argument( 113 '-V', '--version', 114 action='version', 115 version=(DISTRIBUTION_NAME + ' (Hyperledger Sawtooth) version {}') 116 .format(version), 117 help='display version information') 118 119 return parser 120 121 122 def load_identity_config(first_config): 123 default_identity_config = \ 124 load_default_identity_config() 125 conf_file = os.path.join(get_config_dir(), 'identity.toml') 126 127 toml_config = load_toml_identity_config(conf_file) 128 129 return merge_identity_config( 130 configs=[first_config, toml_config, default_identity_config]) 131 132 133 def create_identity_config(args): 134 return IdentityConfig(connect=args.connect) 135 136 137 def main(prog_name=os.path.basename(sys.argv[0]), args=None, 138 with_loggers=True): 139 if args is None: 140 args = sys.argv[1:] 141 parser = create_parser(prog_name) 142 args = parser.parse_args(args) 143 144 arg_config = create_identity_config(args) 145 identity_config = load_identity_config(arg_config) 146 processor = TransactionProcessor(url=identity_config.connect) 147 148 if with_loggers is True: 149 if args.verbose is None: 150 verbose_level = 0 151 else: 152 verbose_level = args.verbose 153 setup_loggers(verbose_level=verbose_level, processor=processor) 154 155 handler = IdentityTransactionHandler() 156 157 processor.add_handler(handler) 158 159 try: 160 processor.start() 161 except KeyboardInterrupt: 162 pass 163 finally: 164 processor.stop()