Exemplo n.º 1
0
def setup_nodes(opts):
    """Setup identities for nodes.

    Args:
        opts (dict): Nephos options dict.
    """
    for msp in get_msps(opts=opts):
        for peer in get_peers(opts=opts, msp=msp):
            setup_id(opts, msp, peer, "peer")

    for msp in get_msps(opts=opts):
        for orderer in get_orderers(opts=opts, msp=msp):
            setup_id(opts, msp, orderer, "orderer")
Exemplo n.º 2
0
def get_namespace(opts, msp=None, ca=None):
    """Get relevant namespace where MSP or CA should be located.

    Args:
        opts (dict): Nephos options dict.
        msp (str): Name of Membership Service Provider (MSP).
        ca (str): Name of Certificate Authority (CA).

    Returns:
        str: Namespace relating to either an MSP or a CA.
    """
    if msp is not None:
        if msp in list(get_msps(opts=opts)):
            if "namespace" in opts["msps"][msp]:
                # Specific MSP-based namespace
                return opts["msps"][msp]["namespace"]
        else:
            raise KeyError(f'Settings dict does not contain MSP "{msp}"')
    elif ca is not None:
        if "cas" in opts and ca in opts["cas"]:
            ca_values = opts["cas"][ca]
        else:
            raise KeyError(f'Settings dict does not contain CA "{ca}"')
        if "namespace" in ca_values:
            # Specific MSP-based namespace
            return ca_values["namespace"]
    # Default case is to return core namespace
    return opts["core"]["namespace"]
Exemplo n.º 3
0
def channel_tx(opts):
    """Create and save Channel Transaction to K8S.

    Args:
        opts (dict): Nephos options dict.
    """
    # Change to blockchain materials directory
    chdir(opts["core"]["dir_config"])
    # Create Channel Tx
    for channel in get_channels(opts):
        channel_key = f"{opts['channels'][channel]['channel_name']}.tx"
        channel_file = join(opts["core"]["dir_crypto"], channel_key)
        if not exists(channel_file):
            # Channel transaction creation and storage
            execute(
                f"configtxgen -profile {opts['channels'][channel]['channel_profile']} -channelID {opts['channels'][channel]['channel_name']} -outputCreateChannelTx {channel_file}"
            )
        else:
            logging.info(f"{channel_file} already exists")
        # Create the channel transaction secret
        for msp in get_msps(opts=opts):
            if msp not in opts["channels"][channel]["msps"]:
                continue
            peer_namespace = get_namespace(opts, msp=msp)
            secret_from_file(
                secret=opts["channels"][channel]["secret_channel"],
                namespace=peer_namespace,
                key=channel_key,
                filename=channel_file,
            )
            # Return to original directory
    chdir(PWD)
Exemplo n.º 4
0
def genesis_block(opts):
    """Create and save Genesis Block to K8S.

    Args:
        opts (dict): Nephos options dict.
    """
    # Change to blockchain materials directory
    chdir(opts["core"]["dir_config"])
    # Create the genesis block
    genesis_key = "genesis.block"
    genesis_file = join(opts["core"]["dir_crypto"], genesis_key)
    if not exists(genesis_file):
        # Genesis block creation and storage
        execute(
            f"configtxgen -profile OrdererGenesis -outputBlock {genesis_file}")
    else:
        logging.info(f"{genesis_file} already exists")

    for msp in get_msps(opts=opts):
        if not is_orderer_msp(opts=opts, msp=msp):
            continue
        ord_namespace = get_namespace(opts, msp=msp)
        # Create the genesis block secret
        secret_from_file(
            secret=get_secret_genesis(opts=opts),
            namespace=ord_namespace,
            key=genesis_key,
            filename=genesis_file,
        )
        # Return to original directory
    chdir(PWD)
Exemplo n.º 5
0
def setup_ord(opts, upgrade=False):
    """Setup Orderer on K8S.

    Args:
        opts (dict): Nephos options dict.
        upgrade (bool): Do we upgrade the deployment? False by default.
    """
    # Kafka
    if "kafka" in opts["ordering"]:
        # Kafka upgrade is risky, so we disallow it by default
        version = get_version(opts, "kafka")
        kafka_config = get_kafka_configs(opts=opts)
        ord_namespace = get_namespace(opts, msp=kafka_config["msp"])
        config_yaml = f"{opts['core']['dir_values']}/{kafka_config['msp']}/kafka/{kafka_config['name']}.yaml"
        extra_vars = helm_extra_vars(version=version, config_yaml=config_yaml)
        helm_install(
            "incubator",
            "kafka",
            kafka_config["name"],
            ord_namespace,
            extra_vars=extra_vars,
        )
        helm_check(
            "kafka",
            kafka_config["name"],
            ord_namespace,
            pod_num=kafka_config["pod_num"],
        )

    for msp in get_msps(opts=opts):
        if not is_orderer_msp(opts=opts, msp=msp):
            continue
        ord_namespace = get_namespace(opts, msp=msp)
        version = get_version(opts, "hlf-ord")
        for release in get_orderers(opts=opts, msp=msp):
            # HL-Ord
            config_yaml = f'{opts["core"]["dir_values"]}/{msp}/hlf-ord/{release}.yaml'
            extra_vars = helm_extra_vars(version=version, config_yaml=config_yaml)
            if not upgrade:
                helm_install(
                    opts["core"]["chart_repo"],
                    "hlf-ord",
                    release,
                    ord_namespace,
                    extra_vars=extra_vars,
                )
            else:
                helm_upgrade(
                    opts["core"]["chart_repo"],
                    "hlf-ord",
                    release,
                    extra_vars=extra_vars,
                )
            helm_check("hlf-ord", release, ord_namespace)
            # Check that Orderer is running
            check_ord(ord_namespace, release)
Exemplo n.º 6
0
def create_channel(opts):
    """Create Channel for Peer.

    Args:
        opts (dict): Nephos options dict.
        
    """
    ord_msp = get_an_orderer_msp(opts=opts)
    ord_namespace = get_namespace(opts, msp=ord_msp)
    ord_name = random.choice(list(get_orderers(opts=opts, msp=ord_msp)))
    cmd_suffix = peer_channel_suffix(opts, ord_msp, ord_name)

    for msp in get_msps(opts=opts):
        peer_namespace = get_namespace(opts, msp=msp)

        for channel in get_channels(opts=opts):
            channel_name = opts["channels"][channel]["channel_name"]
            secret_channel = opts["channels"][channel]["secret_channel"]
            if msp not in opts["channels"][channel]["msps"]:
                continue
            for index, release in enumerate(get_peers(opts=opts, msp=msp)):
                # Get peer pod
                pod_ex = get_helm_pod(peer_namespace, release, "hlf-peer")

                # Check if the file exists
                has_channel = False
                while not has_channel:
                    has_channel = get_channel_block(
                        pod_ex, ord_name, ord_namespace, channel_name, cmd_suffix
                    )
                    if not has_channel:
                        pod_ex.execute(
                            (
                                "bash -c 'peer channel create "
                                + f"-o {ord_name}-hlf-ord.{ord_namespace}.svc.cluster.local:7050 "
                                + f"-c {channel_name} -f /hl_config/channel/{secret_channel}/{channel_name}.tx {cmd_suffix}'"
                            )
                        )
                res, _ = pod_ex.execute("peer channel list")
                channels = (res.split("Channels peers has joined: ")[1]).split()
                if channel_name not in channels:
                    pod_ex.execute(
                        (
                            "bash -c "
                            + "'CORE_PEER_MSPCONFIGPATH=$ADMIN_MSP_PATH "
                            + f"peer channel join -b /var/hyperledger/{channel_name}.block {cmd_suffix}'"
                        )
                    )
Exemplo n.º 7
0
def runner_crypto(opts):
    """Create Crypto-material by either using CAs or save Cryptogen material.

    Args:
        opts (dict): Nephos options dict.
        
    """
    # Set up Admin MSPs
    for msp in get_msps(opts=opts):
        admin_msp(opts, msp)
    # Genesis & Channel
    genesis_block(opts)
    # TODO: We currently only support a single channel
    channel_tx(opts)
    # Setup node MSPs
    setup_nodes(opts)
Exemplo n.º 8
0
 def test_get_msps(self):
     assert {"BetaMSP", "AlphaMSP"} == get_msps(opts=self.OPTS)
Exemplo n.º 9
0
def setup_peer(opts, upgrade=False):
    """Setup Peer on K8S.

    Args:
        opts (dict): Nephos options dict.
        upgrade (bool): Do we upgrade the deployment? False by default.
        
    """
    for msp in get_msps(opts=opts):
        peer_namespace = get_namespace(opts, msp=msp)
        for release in get_peers(opts=opts, msp=msp):
            # Deploy the CouchDB instances
            version = get_version(opts, "hlf-couchdb")
            config_yaml = (
                f'{opts["core"]["dir_values"]}/{msp}/hlf-couchdb/cdb-{release}.yaml'
            )
            if not upgrade:
                extra_vars = helm_extra_vars(version=version,
                                             config_yaml=config_yaml)
                helm_install(
                    opts["core"]["chart_repo"],
                    "hlf-couchdb",
                    f"cdb-{release}",
                    peer_namespace,
                    extra_vars=extra_vars,
                )
            else:
                preserve = (
                    HelmPreserve(
                        peer_namespace,
                        f"cdb-{release}-hlf-couchdb",
                        "COUCHDB_USERNAME",
                        "couchdbUsername",
                    ),
                    HelmPreserve(
                        peer_namespace,
                        f"cdb-{release}-hlf-couchdb",
                        "COUCHDB_PASSWORD",
                        "couchdbPassword",
                    ),
                )
                extra_vars = helm_extra_vars(version=version,
                                             config_yaml=config_yaml,
                                             preserve=preserve)
                helm_upgrade(
                    opts["core"]["chart_repo"],
                    "hlf-couchdb",
                    f"cdb-{release}",
                    extra_vars=extra_vars,
                )
            helm_check("hlf-couchdb", f"cdb-{release}", peer_namespace)

            # Deploy the HL-Peer charts
            version = get_version(opts, "hlf-peer")
            config_yaml = f"{opts['core']['dir_values']}/{msp}/hlf-peer/{release}.yaml"
            extra_vars = helm_extra_vars(version=version,
                                         config_yaml=config_yaml)
            if not upgrade:
                helm_install(
                    opts["core"]["chart_repo"],
                    "hlf-peer",
                    release,
                    peer_namespace,
                    extra_vars=extra_vars,
                )
            else:
                helm_upgrade(
                    opts["core"]["chart_repo"],
                    "hlf-peer",
                    release,
                    extra_vars=extra_vars,
                )
            helm_check("hlf-peer", release, peer_namespace)
            # Check that peer is running
            check_peer(peer_namespace, release)