From 790cbcb65c1dc239e5d2b289b92859f6d3bbf907 Mon Sep 17 00:00:00 2001 From: Yuanmao Zhu <32859592+zhuyuanmao@users.noreply.github.com> Date: Mon, 28 Aug 2023 21:06:25 -0400 Subject: [PATCH] [ISSUE-533]Fix demo admin user creation (#544) --- .gitignore | 2 +- README.md | 2 +- .../docker-compose.dev.yml | 7 ++- .../docker/common/api-engine/entrypoint.sh | 8 +-- .../latest/common/api-engine/entrypoint.sh | 8 +-- docs/reference/configuration/server.md | 2 +- src/api-engine/api/lib/peer/chaincode.py | 56 ++++++++++--------- src/api-engine/api/lib/peer/channel.py | 6 +- .../api/lib/peer/{basicEnv.py => command.py} | 3 +- 9 files changed, 49 insertions(+), 45 deletions(-) rename src/api-engine/api/lib/peer/{basicEnv.py => command.py} (89%) diff --git a/.gitignore b/.gitignore index ef4715786..aee9befeb 100644 --- a/.gitignore +++ b/.gitignore @@ -245,4 +245,4 @@ src/dashboard/lambda/mock/index.js .config.old # Cello local storage -/opt/cello +/**/opt/* diff --git a/README.md b/README.md index 416394cae..a4db534c7 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ If environment is prepared, then we can start cello service. * Set local storage environment variable, e.g. Use current path as storage path ```bash - $ export CELLO_PATH=$(pwd)/opt/cello + $ export CELLO_STORAGE_PATH=$(pwd)/cello ``` diff --git a/bootup/docker-compose-files/docker-compose.dev.yml b/bootup/docker-compose-files/docker-compose.dev.yml index 19d1e2297..e590fb42b 100644 --- a/bootup/docker-compose-files/docker-compose.dev.yml +++ b/bootup/docker-compose-files/docker-compose.dev.yml @@ -32,7 +32,7 @@ services: ports: - "5432:5432" volumes: - - /opt/cello/pgdata:/var/lib/postgresql/data + - ${CELLO_STORAGE_PATH:-/opt/cello}/pgdata:/var/lib/postgresql/data networks: - cello-net @@ -54,10 +54,11 @@ services: - DEBUG=True - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock - FABRIC_LOGGING_SPEC=INFO + - API_ENGINE_ADMIN_EMAIL=admin@cello.com ports: - "8080:8080" volumes: - - /opt/cello:/opt/cello + - ${CELLO_STORAGE_PATH:-/opt/cello}:/opt/cello networks: - cello-net @@ -74,7 +75,7 @@ services: - /var/run/docker.sock:/var/run/docker.sock environment: - DOCKER_URL=unix://var/run/docker.sock - - STORAGE_PATH=/opt/hyperledger + - STORAGE_PATH=${CELLO_STORAGE_PATH:-/opt/cello}/hyperledger networks: - cello-net diff --git a/build_image/docker/common/api-engine/entrypoint.sh b/build_image/docker/common/api-engine/entrypoint.sh index 684d972d1..3faa04b2a 100755 --- a/build_image/docker/common/api-engine/entrypoint.sh +++ b/build_image/docker/common/api-engine/entrypoint.sh @@ -12,11 +12,11 @@ holdup -t 120 tcp://${DB_HOST}:${DB_PORT}; if [[ "$RUN_MODE" == "server" ]]; then python manage.py makemigrations && python manage.py migrate; python manage.py create_user \ - --username ${ADMIN_USERNAME:-admin} \ - --password ${ADMIN_PASSWORD:-pass} \ - --email ${ADMIN_EMAIL:-admin@cello} \ + --username ${API_ENGINE_ADMIN_USERNAME:-admin} \ + --password ${API_ENGINE_ADMIN_PASSWORD:-pass} \ + --email ${API_ENGINE_ADMIN_EMAIL:-admin@cello.com} \ --is_superuser \ - --role operator + --role admin if [[ "$DEBUG" == "True" ]]; then # For dev, use pure Django directly python manage.py runserver 0.0.0.0:8080; else # For production, use uwsgi in front diff --git a/build_image/dockerhub/latest/common/api-engine/entrypoint.sh b/build_image/dockerhub/latest/common/api-engine/entrypoint.sh index 684d972d1..3faa04b2a 100755 --- a/build_image/dockerhub/latest/common/api-engine/entrypoint.sh +++ b/build_image/dockerhub/latest/common/api-engine/entrypoint.sh @@ -12,11 +12,11 @@ holdup -t 120 tcp://${DB_HOST}:${DB_PORT}; if [[ "$RUN_MODE" == "server" ]]; then python manage.py makemigrations && python manage.py migrate; python manage.py create_user \ - --username ${ADMIN_USERNAME:-admin} \ - --password ${ADMIN_PASSWORD:-pass} \ - --email ${ADMIN_EMAIL:-admin@cello} \ + --username ${API_ENGINE_ADMIN_USERNAME:-admin} \ + --password ${API_ENGINE_ADMIN_PASSWORD:-pass} \ + --email ${API_ENGINE_ADMIN_EMAIL:-admin@cello.com} \ --is_superuser \ - --role operator + --role admin if [[ "$DEBUG" == "True" ]]; then # For dev, use pure Django directly python manage.py runserver 0.0.0.0:8080; else # For production, use uwsgi in front diff --git a/docs/reference/configuration/server.md b/docs/reference/configuration/server.md index c0828bf6e..4356c25b5 100644 --- a/docs/reference/configuration/server.md +++ b/docs/reference/configuration/server.md @@ -68,7 +68,7 @@ all related configuration variable is written in ==.makerc/api-engine==. **default**: `admin@cello.com` - **description**: {>>Default operator email of api engine, who have the maximum rights.<<} + **description**: {>>Default admin email of api engine, who have the maximum rights.<<} **usage**: ```bash diff --git a/src/api-engine/api/lib/peer/chaincode.py b/src/api-engine/api/lib/peer/chaincode.py index 5bcfbe4e5..97ca76df8 100644 --- a/src/api-engine/api/lib/peer/chaincode.py +++ b/src/api-engine/api/lib/peer/chaincode.py @@ -4,11 +4,11 @@ import os import json import subprocess -from api.lib.peer.basicEnv import BasicEnv +from api.lib.peer.command import Command from api.config import FABRIC_TOOL, FABRIC_CFG -class ChainCode(BasicEnv): +class ChainCode(Command): def __init__(self, version="2.2.0", peer=FABRIC_TOOL, **kwargs): self.peer = peer + "/peer" super(ChainCode, self).__init__(version, **kwargs) @@ -39,7 +39,8 @@ def lifecycle_install(self, cc_targz): :return: 0 means success. """ try: - res = os.system("{} lifecycle chaincode install {}".format(self.peer, cc_targz)) + res = os.system( + "{} lifecycle chaincode install {}".format(self.peer, cc_targz)) res = res >> 8 except Exception as e: err_msg = "install chaincode failed for {}!".format(e) @@ -158,7 +159,8 @@ def lifecycle_query_approved(self, channel_name, cc_name): try: res = subprocess.Popen("{} lifecycle chaincode queryapproved --output json --channelID {}" - " --name {}".format(self.peer, channel_name, cc_name), + " --name {}".format(self.peer, + channel_name, cc_name), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = res.communicate() return_code = res.returncode @@ -220,7 +222,8 @@ def lifecycle_check_commit_readiness(self, orderer_url, orderer_tls_rootcert, ch stderr = str(stderr, encoding="utf-8") return return_code, stderr except Exception as e: - err_msg = "lifecycle_check_commit_readiness failed for {}!".format(e) + err_msg = "lifecycle_check_commit_readiness failed for {}!".format( + e) raise Exception(err_msg) def lifecycle_commit(self, orderer_url, orderer_tls_rootcert, channel_name, cc_name, chaincode_version, @@ -279,7 +282,8 @@ def lifecycle_query_committed(self, channel_name, cc_name): """ try: res = subprocess.Popen("{} lifecycle chaincode querycommitted --channelID {} " - "--output json --name {}".format(self.peer, channel_name, cc_name), + "--output json --name {}".format( + self.peer, channel_name, cc_name), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = res.communicate() return_code = res.returncode @@ -315,28 +319,26 @@ def invoke(self, orderer_url, orderer_tls_rootcert, channel_name, cc_name, args, invoke_command_tls = "{} chaincode invoke -o {} --tls --cafile {} --channelID {} --name {} -c '{}'" if os.getenv("CORE_PEER_TLS_ENABLED") == "false" or os.getenv("CORE_PEER_TLS_ENABLED") is None: - if self.version in BasicEnv.binary_versions_v2: - res = subprocess.Popen(invoke_command.format(self.peer, orderer_url, channel_name, cc_name, args), - shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - stdout, stderr = res.communicate() - return_code = res.returncode - if return_code == 0: - return return_code, '' - else: - stderr = str(stderr, encoding="utf-8") - return return_code, stderr + res = subprocess.Popen(invoke_command.format(self.peer, orderer_url, channel_name, cc_name, args), + shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + _, stderr = res.communicate() + return_code = res.returncode + if return_code == 0: + return return_code, '' + else: + stderr = str(stderr, encoding="utf-8") + return return_code, stderr else: - if self.version in BasicEnv.binary_versions_v2: - res = subprocess.Popen(invoke_command_tls.format(self.peer, orderer_url, orderer_tls_rootcert, - channel_name, cc_name, args), - shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - stdout, stderr = res.communicate() - return_code = res.returncode - if return_code == 0: - return return_code, '' - else: - stderr = str(stderr, encoding="utf-8") - return return_code, stderr + res = subprocess.Popen(invoke_command_tls.format(self.peer, orderer_url, orderer_tls_rootcert, + channel_name, cc_name, args), + shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + _, stderr = res.communicate() + return_code = res.returncode + if return_code == 0: + return return_code, '' + else: + stderr = str(stderr, encoding="utf-8") + return return_code, stderr except Exception as e: err_msg = "invoke failed for {}!".format(e) raise Exception(err_msg) diff --git a/src/api-engine/api/lib/peer/channel.py b/src/api-engine/api/lib/peer/channel.py index 8e9dea1de..640a453e5 100644 --- a/src/api-engine/api/lib/peer/channel.py +++ b/src/api-engine/api/lib/peer/channel.py @@ -4,11 +4,11 @@ import os import json import subprocess -from api.lib.peer.basicEnv import BasicEnv +from api.lib.peer.command import Command from api.config import FABRIC_TOOL -class Channel(BasicEnv): +class Channel(Command): """Call CMD to perform channel create, join and other related operations""" def __init__(self, version="2.2.0", peer=FABRIC_TOOL, **kwargs): @@ -85,7 +85,7 @@ def fetch(self, option, channel): "{}".format(option), "-c", channel - ]) + ]) except Exception as e: err_msg = "fetch a specified block failed {}!".format(e) raise Exception(err_msg) diff --git a/src/api-engine/api/lib/peer/basicEnv.py b/src/api-engine/api/lib/peer/command.py similarity index 89% rename from src/api-engine/api/lib/peer/basicEnv.py rename to src/api-engine/api/lib/peer/command.py index e58313c0f..769c7aead 100644 --- a/src/api-engine/api/lib/peer/basicEnv.py +++ b/src/api-engine/api/lib/peer/command.py @@ -2,7 +2,8 @@ from api.config import FABRIC_CFG -class BasicEnv: +# Command class reads local environment variables by given their names +class Command: def __init__(self, version, **kwargs): self.version = version # Setting environment variables according to user input. Recommended main settings: CORE_PEER_LOCALMSPID、