Skip to content

Commit

Permalink
Move sawtooth admin to sawadm
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Drozd <[email protected]>
  • Loading branch information
nick-drozd committed Nov 14, 2017
1 parent 6de5f8f commit 55c8246
Show file tree
Hide file tree
Showing 47 changed files with 339 additions and 186 deletions.
8 changes: 4 additions & 4 deletions bin/generate_cli_output
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ mkdir -p source/cli/output

save_usage sawtooth

save_usage sawtooth admin
save_usage sawtooth admin genesis
save_usage sawtooth admin keygen

save_usage sawtooth batch
save_usage sawtooth batch list
save_usage sawtooth batch show
Expand Down Expand Up @@ -62,6 +58,10 @@ save_usage sawset proposal create
save_usage sawset proposal list
save_usage sawset proposal vote

save_usage sawadm
save_usage sawadm genesis
save_usage sawadm keygen

save_usage sawtooth-validator
save_usage sawtooth-rest-api
save_usage settings-tp
Expand Down
36 changes: 36 additions & 0 deletions bin/sawadm
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python3

# 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 os
import sys
import sysconfig

build_str = "lib.{}-{}.{}".format(
sysconfig.get_platform(),
sys.version_info.major, sys.version_info.minor)

sys.path.insert(0, os.path.join(
os.path.dirname(os.path.dirname(os.path.realpath(__file__))),
'cli'))
sys.path.insert(0, os.path.join(
os.path.dirname(os.path.dirname(os.path.realpath(__file__))),
'signing'))

from sawtooth_cli.admin import main_wrapper

if __name__ == '__main__':
main_wrapper()
128 changes: 115 additions & 13 deletions cli/sawtooth_cli/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,132 @@
# limitations under the License.
# ------------------------------------------------------------------------------

import argparse
import logging
import os
import traceback
import sys
import pkg_resources

from colorlog import ColoredFormatter

from sawtooth_cli.exceptions import CliException
from sawtooth_cli.admin_command.genesis import add_genesis_parser
from sawtooth_cli.admin_command.genesis import do_genesis
from sawtooth_cli.admin_command.keygen import add_keygen_parser
from sawtooth_cli.admin_command.keygen import do_keygen


def do_admin(args):
DISTRIBUTION_NAME = 'sawadm'


def create_parser(prog_name):
parent_parser = create_parent_parser(prog_name)

parser = argparse.ArgumentParser(
description='Provides subcommands to create validator keys and '
'create the genesis block',
parents=[parent_parser],)

subparsers = parser.add_subparsers(title='subcommands', dest='subcommand')
subparsers.required = True

add_genesis_parser(subparsers, parent_parser)
add_keygen_parser(subparsers, parent_parser)

return parser


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

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

if args.subcommand == 'genesis':
do_genesis(args)
elif args.subcommand == 'keygen':
do_keygen(args)
else:
raise AssertionError("invalid command: {}".format(args.subcommand))
raise CliException('Invalid command: {}'.format(args.subcommand))


def add_admin_parser(subparsers, parent_parser):
parser = subparsers.add_parser(
'admin',
help='Create validator keys and help create the genesis block',
description='Provides subcommands to create validator keys and '
'help create the genesis block',
parents=[parent_parser])
admin_sub = parser.add_subparsers(title='subcommands', dest='subcommand')
admin_sub.required = True
add_genesis_parser(admin_sub, parent_parser)
add_keygen_parser(admin_sub, parent_parser)
def main_wrapper():
# pylint: disable=bare-except
try:
main()
except CliException as e:
print("Error: {}".format(e), file=sys.stderr)
sys.exit(1)
except KeyboardInterrupt:
pass
except BrokenPipeError:
sys.stderr.close()
except SystemExit as e:
raise e
except:
traceback.print_exc(file=sys.stderr)
sys.exit(1)


def create_console_handler(verbose_level):
clog = logging.StreamHandler()
formatter = ColoredFormatter(
"%(log_color)s[%(asctime)s %(levelname)-8s%(module)s]%(reset)s "
"%(white)s%(message)s",
datefmt="%H:%M:%S",
reset=True,
log_colors={
'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red',
})

clog.setFormatter(formatter)

if verbose_level == 0:
clog.setLevel(logging.WARN)
elif verbose_level == 1:
clog.setLevel(logging.INFO)
else:
clog.setLevel(logging.DEBUG)

return clog


def setup_loggers(verbose_level):
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(create_console_handler(verbose_level))


def create_parent_parser(prog_name):
parent_parser = argparse.ArgumentParser(prog=prog_name, add_help=False)
parent_parser.add_argument(
'-v', '--verbose',
action='count',
help='enable more verbose output')

try:
version = pkg_resources.get_distribution(DISTRIBUTION_NAME).version
except pkg_resources.DistributionNotFound:
version = 'UNKNOWN'

parent_parser.add_argument(
'-V', '--version',
action='version',
version=(DISTRIBUTION_NAME + ' (Hyperledger Sawtooth) version {}')
.format(version),
help='print version information')

return parent_parser
2 changes: 1 addition & 1 deletion cli/sawtooth_cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ def create_parser(prog_name):
'genesis',
help='Create a genesis batch file of settings transactions',
description='Creates a Batch of settings proposals that can be '
'consumed by "sawtooth admin genesis" and used '
'consumed by "sawadm genesis" and used '
'during genesis block construction.'
)
genesis_parser.add_argument(
Expand Down
7 changes: 1 addition & 6 deletions cli/sawtooth_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

from sawtooth_cli.exceptions import CliException

from sawtooth_cli.admin import add_admin_parser
from sawtooth_cli.admin import do_admin
from sawtooth_cli.keygen import add_keygen_parser
from sawtooth_cli.keygen import do_keygen
from sawtooth_cli.block import add_block_parser
Expand Down Expand Up @@ -113,7 +111,6 @@ def create_parser(prog_name):
subparsers = parser.add_subparsers(title='subcommands', dest='command')
subparsers.required = True

add_admin_parser(subparsers, parent_parser)
add_batch_parser(subparsers, parent_parser)
add_block_parser(subparsers, parent_parser)
add_identity_parser(subparsers, parent_parser)
Expand All @@ -139,9 +136,7 @@ def main(prog_name=os.path.basename(sys.argv[0]), args=None,
verbose_level = args.verbose
setup_loggers(verbose_level=verbose_level)

if args.command == 'admin':
do_admin(args)
elif args.command == 'keygen':
if args.command == 'keygen':
do_keygen(args)
elif args.command == 'block':
do_block(args)
Expand Down
1 change: 1 addition & 0 deletions cli/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
],
entry_points={
'console_scripts': [
'sawadm = sawtooth_cli.admin:main_wrapper',
'sawset = sawtooth_cli.config:main_wrapper',
'sawtooth = sawtooth_cli.main:main_wrapper'
]
Expand Down
2 changes: 1 addition & 1 deletion docs/source/app_developers_guide/docker.rst
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ To see which components are running, use this command from the container:
$ ps --pid 1 fw
PID TTY STAT TIME COMMAND
1 ? Ss 0:00 bash -c sawtooth admin keygen && sawtooth keygen my_key && sawset genesis -k /root/.sawtooth/keys/my_key.priv && sawtooth admin genesis config-genesis.batch && sawtooth-validator -vv --endpoint
1 ? Ss 0:00 bash -c sawadm keygen && sawtooth keygen my_key && sawset genesis -k /root/.sawtooth/keys/my_key.priv && sawadm genesis config-genesis.batch && sawtooth-validator -vv --endpoint
.. note::

Expand Down
4 changes: 2 additions & 2 deletions docs/source/app_developers_guide/ubuntu.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ and run the following commands:
$ sawtooth keygen
$ sawset genesis
$ sudo -u sawtooth sawtooth admin genesis config-genesis.batch
$ sudo -u sawtooth sawadm genesis config-genesis.batch
The following output appears:

Expand All @@ -133,7 +133,7 @@ following commands:

.. code-block:: console
$ sudo sawtooth admin keygen
$ sudo sawadm keygen
$ sudo -u sawtooth sawtooth-validator -vv
.. note::
Expand Down
2 changes: 1 addition & 1 deletion docs/source/architecture/journal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ and specifies the appropriate settings:
sawtooth.poet.target_wait_time=x \
sawtooth.poet.minimum_wait_time=x \
sawtooth.poet.population_estimate_sample_size=x \
sawtooth admin genesis \
sawadm genesis \
sawset.batch
A genesis.batch file will written to the validator's data directory.
Expand Down
1 change: 1 addition & 0 deletions docs/source/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ CLI Reference
:maxdepth: 2

cli/sawtooth.rst
cli/sawadm.rst
cli/sawset.rst
cli/poet.rst
cli/validator.rst
Expand Down
100 changes: 100 additions & 0 deletions docs/source/cli/sawadm.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
..
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.

.. _sawadm-reference-label:

******************
Sawtooth Admin CLI
******************

sawadm
======

The ``sawadm`` subcommands create validator keys during
initial configuration and help create the genesis block when
initializing a validator.

.. literalinclude:: output/sawadm_usage.out
:language: console
:linenos:

sawadm genesis
==============

Overview
--------

The genesis CLI tool produces a file for use during initialization of
a validator. A network requires an initial block (known as the genesis
block) whose signature will determine the block chain id. This initial
block is produced from a list of batches, which will be applied at
genesis time. The input to the command is a set of zero or more files
containing serialized ``BatchList`` protobuf messages. The output is a
file containing a serialized ``GenesisData`` protobuf message. This
file, when placed at ``<sawtooth_data>/genesis.batch``, will trigger
the genesis process.

The location ``sawtooth_data`` depends on whether or not the
environment variable ``SAWTOOTH_HOME`` is set. If it is, then
``sawtooth_data`` is located at ``<SAWTOOTH_HOME>/data``. If it is
not, then ``sawtooth_data`` is located at ``/var/lib/sawtooth``.

Usage
-----

.. literalinclude:: output/sawadm_genesis_usage.out
:language: console
:linenos:

Arguments
^^^^^^^^^

- ``input_batch_file`` - a repeated list of files containing a
serialized ``BatchList`` message. This may be empty, which will
produce an empty genesis block.

- ``--output <filename>`` - a target file where the serialized
``GenesisData`` will be written. Defaults to
``<sawtooth_data>/genesis.batch``.

Output
^^^^^^

The output of the command displays a message where the output
``GenesisData`` is written.

Example
^^^^^^^

.. code-block:: console
> sawadm genesis config.batch mktplace.batch
Generating /var/lib/sawtooth/genesis.batch
sawadm keygen
=============

The ``sawadm keygen`` command generates keys that the validator uses to
sign blocks. This system-wide key must be created during Sawtooth
configuration.

Validator keys are stored in the directory ``/etc/sawtooth/keys/``. By
default, the public-private key files are named ``validator.priv`` and
validator.pub. Use the <key-name> argument to specify a different file
name.

.. literalinclude:: output/sawadm_keygen_usage.out
:language: console
:linenos:
Loading

0 comments on commit 55c8246

Please sign in to comment.