github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/cli/sawtooth_cli/network_command/peers.py (about)

     1  # Copyright 2018 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 os
    17  import json
    18  
    19  from sawtooth_cli.network_command.parent_parsers import base_multinode_parser
    20  from sawtooth_cli.network_command.parent_parsers import split_comma_append_args
    21  from sawtooth_cli.network_command.parent_parsers import make_rest_apis
    22  
    23  from sawtooth_cli.exceptions import CliException
    24  
    25  
    26  DOT_FILE = 'peers.dot'
    27  
    28  
    29  def add_peers_parser(subparsers, parent_parser):
    30      help_text = 'Shows the peering arrangment of a network'
    31  
    32      parser = subparsers.add_parser(
    33          'peers',
    34          help=help_text,
    35          description='{}.'.format(help_text))
    36  
    37      peers_parsers = parser.add_subparsers(
    38          title='subcommands',
    39          dest='peers_command')
    40      peers_parsers.required = True
    41  
    42      _add_list_parser(peers_parsers, parent_parser)
    43      _add_graph_parser(peers_parsers, parent_parser)
    44  
    45  
    46  def _add_list_parser(parser, parent_parser):
    47      help_text = 'Lists peers for validators with given URLs'
    48  
    49      list_parser = parser.add_parser(
    50          'list',
    51          help=help_text,
    52          description='{}.'.format(help_text),
    53          parents=[parent_parser, base_multinode_parser()])
    54  
    55      list_parser.add_argument(
    56          '--pretty', '-p',
    57          action='store_true',
    58          help='Pretty-print the results')
    59  
    60  
    61  def do_peers(args):
    62      if args.peers_command == 'list':
    63          _do_peers_list(args)
    64      elif args.peers_command == 'graph':
    65          _do_peers_graph(args)
    66      else:
    67          raise CliException('Invalid command: {}'.format(args.subcommand))
    68  
    69  
    70  def _do_peers_list(args):
    71      urls = split_comma_append_args(args.urls)
    72      users = split_comma_append_args(args.users)
    73      clients = make_rest_apis(urls, users)
    74  
    75      print(
    76          json.dumps(
    77              _get_peer_endpoints(clients),
    78              sort_keys=True,
    79              indent=(4 if args.pretty else 0)
    80          )
    81      )
    82  
    83  
    84  def _get_peer_endpoints(clients):
    85      statuses = [client.get_status() for client in clients]
    86  
    87      return {
    88          status['endpoint']: [
    89              peer['endpoint']
    90              for peer in status['peers']
    91          ]
    92          for status in statuses
    93      }
    94  
    95  
    96  def _add_graph_parser(parser, parent_parser):
    97      help_text = "Generates a file to graph a network's peering arrangement"
    98  
    99      graph_parser = parser.add_parser(
   100          'graph',
   101          help=help_text,
   102          description='{}.'.format(help_text),
   103          parents=[parent_parser, base_multinode_parser()])
   104  
   105      graph_parser.add_argument(
   106          '-o', '--output',
   107          help='The path of the dot file to be produced (defaults to peers.dot)')
   108  
   109      graph_parser.add_argument(
   110          '--force',
   111          action='store_true',
   112          help='TODO')
   113  
   114  
   115  def _do_peers_graph(args):
   116      urls = split_comma_append_args(args.urls)
   117      users = split_comma_append_args(args.users)
   118      clients = make_rest_apis(urls, users)
   119  
   120      status_dict = _get_peer_endpoints(clients)
   121  
   122      path = args.output if args.output else DOT_FILE
   123  
   124      if not args.force and os.path.isfile(path):
   125          raise CliException(
   126              '{} already exists; '
   127              'rerun with `--force` to overwrite'.format(path))
   128  
   129      with open(path, 'w') as dot:
   130          print(
   131              'strict graph peers {',
   132              file=dot)
   133  
   134          for node, peers in status_dict.items():
   135              for peer in peers:
   136                  print(
   137                      '    "{}" -- "{}"'.format(node, peer),
   138                      file=dot)
   139  
   140          print(
   141              '}',
   142              file=dot)