Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the osnadmin CLI to add orderer to channel #638

Merged
merged 1 commit into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions src/api-engine/api/lib/peer/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,33 @@
import subprocess
from api.lib.peer.command import Command
from api.config import FABRIC_TOOL, FABRIC_VERSION
import logging

LOG = logging.getLogger(__name__)

class Channel(Command):
"""Call CMD to perform channel create, join and other related operations"""

def __init__(self, version=FABRIC_VERSION, peer=FABRIC_TOOL, **kwargs):
self.peer = peer + "/peer"
self.osnadmin = peer + "/osnadmin"
super(Channel, self).__init__(version, **kwargs)

def create(self, channel, orderer_url, channel_tx, output_block, time_out="90s"):
def create(self, channel, orderer_url, block_path, time_out="90s"):
try:
res = 0x100
command = ""

if os.getenv("CORE_PEER_TLS_ENABLED") == "false" or os.getenv("CORE_PEER_TLS_ENABLED") is None:
res = os.system("{} channel create -c {} -o {} -f {} --outputBlock {} --timeout {}"
.format(self.peer, channel, orderer_url, channel_tx, output_block, time_out))
command = "{} channel join --channelID {} --config-block {} -o {}".format(self.osnadmin, channel, block_path, orderer_url)
else:
ORDERER_CA = os.getenv("ORDERER_CA")
res = os.system("{} channel create -c {} -o {} -f {} --outputBlock {} --timeout {} --tls --cafile {}"
.format(self.peer, channel, orderer_url, channel_tx, output_block, time_out, ORDERER_CA))
ORDERER_ADMIN_TLS_SIGN_CERT = os.getenv("ORDERER_ADMIN_TLS_SIGN_CERT")
ORDERER_ADMIN_TLS_PRIVATE_KEY = os.getenv("ORDERER_ADMIN_TLS_PRIVATE_KEY")
command = "{} channel join --channelID {} --config-block {} -o {} --ca-file {} --client-cert {} --client-key {}".format(self.osnadmin, channel, block_path, orderer_url, ORDERER_CA, ORDERER_ADMIN_TLS_SIGN_CERT, ORDERER_ADMIN_TLS_PRIVATE_KEY)

LOG.info(f"Running command: {command}")
res = os.system(command)

# The return value of os.system is not the result of executing the program. It is a 16 bit number,
# and its high bit is the return code
Expand Down Expand Up @@ -106,16 +114,19 @@ def signconfigtx(self, channel_tx):
res = res >> 8
return res

def join(self, block_file):
def join(self, block_path):
"""
Joins the peer to a channel.
params:
block_file: Path to file containing genesis block.
block_path: Path to file containing genesis block.
"""
try:
res = os.system(
"{} channel join -b {} ".format(self.peer, block_file)
)
command = "{} channel join -b {} ".format(self.peer, block_path)

LOG.info(f"Running command: {command}")

res = os.system(command)

except Exception as e:
err_msg = "join the peer to a channel failed. {}".format(e)
raise Exception(err_msg)
Expand Down
31 changes: 20 additions & 11 deletions src/api-engine/api/routes/channel/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,17 +386,26 @@ def init_env_vars(node, org):
dir_node = "{}/{}/crypto-config/peerOrganizations".format(
CELLO_HOME, org_name)

envs = {
"CORE_PEER_TLS_ENABLED": "true",
# "Org1.cello.comMSP"
"CORE_PEER_LOCALMSPID": "{}MSP".format(org_name.capitalize()),
"CORE_PEER_TLS_ROOTCERT_FILE": "{}/{}/peers/{}/tls/ca.crt".format(dir_node, org_name, node.name + "." + org_name),
"CORE_PEER_ADDRESS": "{}:{}".format(
node.name + "." + org_name, str(7051)),
"CORE_PEER_MSPCONFIGPATH": "{}/{}/users/Admin@{}/msp".format(dir_node, org_name, org_name),
"FABRIC_CFG_PATH": "{}/{}/peers/{}/".format(dir_node, org_name, node.name + "." + org_name),
"ORDERER_CA": "{}/msp/tlscacerts/tlsca.{}-cert.pem".format(dir_certificate, org_domain)
}
envs = {}

if(node.type == "orderer"):
envs = {
"ORDERER_CA": "{}/msp/tlscacerts/tlsca.{}-cert.pem".format(dir_certificate, org_domain),
"ORDERER_ADMIN_TLS_SIGN_CERT": "{}/orderers/{}/tls/server.crt".format(dir_certificate, node.name + "." + org_domain),
"ORDERER_ADMIN_TLS_PRIVATE_KEY": "{}/orderers/{}/tls/server.key".format(dir_certificate, node.name + "." + org_domain)
}
elif(node.type == "peer"):
envs = {
"CORE_PEER_TLS_ENABLED": "true",
"CORE_PEER_LOCALMSPID": "{}MSP".format(org_name.capitalize()),
"CORE_PEER_TLS_ROOTCERT_FILE": "{}/{}/peers/{}/tls/ca.crt".format(dir_node, org_name, node.name + "." + org_name),
"CORE_PEER_MSPCONFIGPATH": "{}/{}/users/Admin@{}/msp".format(dir_node, org_name, org_name),
"CORE_PEER_ADDRESS": "{}:{}".format(
node.name + "." + org_name, str(7051)),

"FABRIC_CFG_PATH": "{}/{}/peers/{}/".format(dir_node, org_name, node.name + "." + org_name)
}

return envs


Expand Down
Loading