Skip to content

Commit

Permalink
Use python constants for version values, not the environment
Browse files Browse the repository at this point in the history
Fixes #571

Signed-off-by: Gil Bregman <[email protected]>
  • Loading branch information
gbregman committed Apr 11, 2024
1 parent 1509b6a commit c0117e5
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 96 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ server.crt
server.key
client.crt
client.key
control/constants.py
13 changes: 1 addition & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,7 @@ ARG NVMEOF_NAME \
HUGEPAGES \
HUGEPAGES_DIR

ENV NVMEOF_VERSION="${NVMEOF_VERSION}" \
NVMEOF_GIT_REPO="${NVMEOF_GIT_REPO}" \
NVMEOF_GIT_BRANCH="${NVMEOF_GIT_BRANCH}" \
NVMEOF_GIT_COMMIT="${NVMEOF_GIT_COMMIT}" \
BUILD_DATE="${BUILD_DATE}" \
NVMEOF_SPDK_VERSION="${NVMEOF_SPDK_VERSION}" \
NVMEOF_CEPH_VERSION="${NVMEOF_CEPH_VERSION}" \
NVMEOF_GIT_MODIFIED_FILES="${NVMEOF_GIT_MODIFIED_FILES}" \
SPDK_GIT_REPO="${SPDK_GIT_REPO}" \
SPDK_GIT_BRANCH="${SPDK_GIT_BRANCH}" \
SPDK_GIT_COMMIT="${SPDK_GIT_COMMIT}" \
HUGEPAGES="${HUGEPAGES}" \
ENV HUGEPAGES="${HUGEPAGES}" \
HUGEPAGES_DIR="${HUGEPAGES_DIR}"

# Generic labels
Expand Down
46 changes: 37 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,43 @@ setup: ## Configure huge-pages (requires sudo/root password)

build pull logs: SVC ?= ceph spdk bdevperf nvmeof nvmeof-devel nvmeof-cli discovery

build: export NVMEOF_GIT_REPO != git remote get-url origin
build: export NVMEOF_GIT_BRANCH != git name-rev --name-only HEAD
build: export NVMEOF_GIT_COMMIT != git rev-parse HEAD
build: export SPDK_GIT_REPO != git -C spdk remote get-url origin
build: export SPDK_GIT_BRANCH != git -C spdk name-rev --name-only HEAD
build: export SPDK_GIT_COMMIT != git rev-parse HEAD:spdk
build: export BUILD_DATE != date -u +"%Y-%m-%d %H:%M:%S %Z"
build: export NVMEOF_GIT_MODIFIED_FILES != git status -s | grep -e "^ *M" | sed 's/^ *M //' | xargs
build: export CEPH_CLUSTER_CEPH_REPO_BASEURL != curl -s https://shaman.ceph.com/api/repos/ceph/$(CEPH_BRANCH)/$(CEPH_SHA)/centos/9/ | jq -r '.[] | select(.status == "ready" and .sha1 == "$(CEPH_SHA)" and .archs[] == "x86_64") | .url'
MK_NVMEOF_GIT_REPO != git remote get-url origin
MK_NVMEOF_GIT_BRANCH != git name-rev --name-only HEAD
MK_NVMEOF_GIT_COMMIT != git rev-parse HEAD
MK_SPDK_GIT_REPO != git -C spdk remote get-url origin
MK_SPDK_GIT_BRANCH != git -C spdk name-rev --name-only HEAD
MK_SPDK_GIT_COMMIT != git rev-parse HEAD:spdk
MK_BUILD_DATE != date -u +"%Y-%m-%d %H:%M:%S %Z"
MK_NVMEOF_GIT_MODIFIED_FILES != git status -s | grep -e "^ *M" | sed 's/^ *M //' | xargs
MK_CEPH_CLUSTER_CEPH_REPO_BASEURL != curl -s https://shaman.ceph.com/api/repos/ceph/$(CEPH_BRANCH)/$(CEPH_SHA)/centos/9/ | jq -r '.[] | select(.status == "ready" and .sha1 == "$(CEPH_SHA)" and .archs[] == "x86_64") | .url'

build: export NVMEOF_GIT_REPO = ${MK_NVMEOF_GIT_REPO}
build: export NVMEOF_GIT_BRANCH = ${MK_NVMEOF_GIT_BRANCH}
build: export NVMEOF_GIT_COMMIT = ${MK_NVMEOF_GIT_COMMIT}
build: export SPDK_GIT_REPO = ${MK_SPDK_GIT_REPO}
build: export SPDK_GIT_BRANCH = ${MK_SPDK_GIT_BRANCH}
build: export SPDK_GIT_COMMIT = ${MK_SPDK_GIT_COMMIT}
build: export BUILD_DATE = ${MK_BUILD_DATE}
build: export NVMEOF_GIT_MODIFIED_FILES = ${MK_NVMEOF_GIT_MODIFIED_FILES}
build: export CEPH_CLUSTER_CEPH_REPO_BASEURL = ${MK_CEPH_CLUSTER_CEPH_REPO_BASEURL}
build: control/constants.py

control/constants.py: .env Makefile
@rm -f control/constants.py
@echo "class GatewayConstants:" >> control/constants.py
@echo " NVMEOF_VERSION = \"${NVMEOF_VERSION}\"" >> control/constants.py
@echo " NVMEOF_CLI_VERSION = \"${NVMEOF_VERSION}\"" >> control/constants.py
@echo " NVMEOF_CEPH_VERSION = \"${CEPH_VERSION}\"" >> control/constants.py
@echo " NVMEOF_SPDK_VERSION = \"${SPDK_VERSION}\"" >> control/constants.py
@echo " NVMEOF_GIT_REPO = \"${MK_NVMEOF_GIT_REPO}\"" >> control/constants.py
@echo " NVMEOF_GIT_BRANCH = \"${MK_NVMEOF_GIT_BRANCH}\"" >> control/constants.py
@echo " NVMEOF_GIT_COMMIT = \"${MK_NVMEOF_GIT_COMMIT}\"" >> control/constants.py
@echo " SPDK_GIT_REPO = \"${MK_SPDK_GIT_REPO}\"" >> control/constants.py
@echo " SPDK_GIT_BRANCH = \"${MK_SPDK_GIT_BRANCH}\"" >> control/constants.py
@echo " SPDK_GIT_COMMIT = \"${MK_SPDK_GIT_COMMIT}\"" >> control/constants.py
@echo " BUILD_DATE = \"${MK_BUILD_DATE}\"" >> control/constants.py
@echo " NVMEOF_GIT_MODIFIED_FILES = \"${MK_NVMEOF_GIT_MODIFIED_FILES}\"" >> control/constants.py
@echo " CEPH_CLUSTER_CEPH_REPO_BASEURL = \"${MK_CEPH_CLUSTER_CEPH_REPO_BASEURL}\"" >> control/constants.py
up: ## Launch services
up: SCALE?= 1 ## Number of gateways
up:
Expand Down
23 changes: 7 additions & 16 deletions control/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from .proto import gateway_pb2 as pb2
from .utils import GatewayUtils
from .utils import GatewayEnumUtils
from .constants import GatewayConstants

BASE_GATEWAY_VERSION="1.1.0"

Expand Down Expand Up @@ -260,21 +261,12 @@ def version(self, args):
"""Get CLI version"""
rc = 0
out_func, err_func = self.get_output_functions(args)
errmsg = ""
ver = os.getenv("NVMEOF_VERSION")
if not ver:
rc = errno.ENOKEY
errmsg = "Can't get CLI version"
else:
rc = 0
errmsg = os.strerror(0)
rc = 0
errmsg = os.strerror(0)
if args.format == "text" or args.format == "plain":
if not ver:
err_func(errmsg)
else:
out_func(f"CLI version: {ver}")
out_func(f"CLI version: {GatewayConstants.NVMEOF_CLI_VERSION}")
elif args.format == "json" or args.format == "yaml":
cli_ver = pb2.cli_version(status=rc, error_message=errmsg, version=ver)
cli_ver = pb2.cli_version(status=rc, error_message=errmsg, version=GatewayConstants.NVMEOF_CLI_VERSION)
out_ver = json_format.MessageToJson(cli_ver,
indent=4,
including_default_value_fields=True,
Expand All @@ -285,7 +277,7 @@ def version(self, args):
obj = json.loads(out_ver)
out_func(yaml.dump(obj))
elif args.format == "python":
return pb2.cli_version(status=rc, error_message=errmsg, version=ver)
return pb2.cli_version(status=rc, error_message=errmsg, version=GatewayConstants.NVMEOF_CLI_VERSION)
else:
assert False

Expand All @@ -306,8 +298,7 @@ def parse_version_string(self, version):
return (v1, v2, v3)

def gw_get_info(self):
ver = os.getenv("NVMEOF_VERSION")
req = pb2.get_gateway_info_req(cli_version=ver)
req = pb2.get_gateway_info_req(cli_version=GatewayConstants.NVMEOF_CLI_VERSION)
gw_info = self.stub.get_gateway_info(req)
if gw_info.status == 0:
base_ver = self.parse_version_string(BASE_GATEWAY_VERSION)
Expand Down
85 changes: 33 additions & 52 deletions control/grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from .utils import GatewayLogger
from .state import GatewayState, GatewayStateHandler, OmapLock
from .cephutils import CephUtils
from .constants import GatewayConstants

# Assuming max of 32 gateways and protocol min 1 max 65519
CNTLID_RANGE_SIZE = 2040
Expand Down Expand Up @@ -73,41 +74,28 @@ def __init__(self, config: GatewayConfig, gateway_state: GatewayStateHandler, rp
self.gw_logger_object = GatewayLogger(config)
self.logger = self.gw_logger_object.logger
self.ceph_utils = ceph_utils
ver = os.getenv("NVMEOF_VERSION")
if ver:
self.logger.info(f"Using NVMeoF gateway version {ver}")
spdk_ver = os.getenv("NVMEOF_SPDK_VERSION")
if spdk_ver:
self.logger.info(f"Using SPDK version {spdk_ver}")
ceph_ver = os.getenv("NVMEOF_CEPH_VERSION")
if ceph_ver:
self.logger.info(f"Using vstart cluster version based on {ceph_ver}")
build_date = os.getenv("BUILD_DATE")
if build_date:
self.logger.info(f"NVMeoF gateway built on: {build_date}")
git_rep = os.getenv("NVMEOF_GIT_REPO")
if git_rep:
self.logger.info(f"NVMeoF gateway Git repository: {git_rep}")
git_branch = os.getenv("NVMEOF_GIT_BRANCH")
if git_branch:
self.logger.info(f"NVMeoF gateway Git branch: {git_branch}")
git_commit = os.getenv("NVMEOF_GIT_COMMIT")
if git_commit:
self.logger.info(f"NVMeoF gateway Git commit: {git_commit}")
git_modified = os.getenv("NVMEOF_GIT_MODIFIED_FILES")
if git_modified:
self.logger.info(f"NVMeoF gateway uncommitted modified files: {git_modified}")
git_spdk_rep = os.getenv("SPDK_GIT_REPO")
if git_spdk_rep:
self.logger.info(f"SPDK Git repository: {git_spdk_rep}")
git_spdk_branch = os.getenv("SPDK_GIT_BRANCH")
if git_spdk_branch:
self.logger.info(f"SPDK Git branch: {git_spdk_branch}")
git_spdk_commit = os.getenv("SPDK_GIT_COMMIT")
if git_spdk_commit:
self.logger.info(f"SPDK Git commit: {git_spdk_commit}")
self.logger.info(f"Using NVMeoF gateway version {GatewayConstants.NVMEOF_VERSION}")
self.logger.info(f"Using SPDK version {GatewayConstants.NVMEOF_SPDK_VERSION}")
self.logger.info(f"Using vstart cluster version based on {GatewayConstants.NVMEOF_CEPH_VERSION}")
self.logger.info(f"NVMeoF gateway built on: {GatewayConstants.BUILD_DATE}")
if GatewayConstants.NVMEOF_GIT_REPO:
self.logger.info(f"NVMeoF gateway Git repository: {GatewayConstants.NVMEOF_GIT_REPO}")
if GatewayConstants.NVMEOF_GIT_BRANCH:
self.logger.info(f"NVMeoF gateway Git branch: {GatewayConstants.NVMEOF_GIT_BRANCH}")
if GatewayConstants.NVMEOF_GIT_COMMIT:
self.logger.info(f"NVMeoF gateway Git commit: {GatewayConstants.NVMEOF_GIT_COMMIT}")
if GatewayConstants.NVMEOF_GIT_MODIFIED_FILES:
self.logger.info(f"NVMeoF gateway uncommitted modified files: {GatewayConstants.NVMEOF_GIT_MODIFIED_FILES}")
if GatewayConstants.SPDK_GIT_REPO:
self.logger.info(f"SPDK Git repository: {GatewayConstants.SPDK_GIT_REPO}")
if GatewayConstants.SPDK_GIT_BRANCH:
self.logger.info(f"SPDK Git branch: {GatewayConstants.SPDK_GIT_BRANCH}")
if GatewayConstants.SPDK_GIT_COMMIT:
self.logger.info(f"SPDK Git commit: {GatewayConstants.SPDK_GIT_COMMIT}")
if GatewayConstants.CEPH_CLUSTER_CEPH_REPO_BASEURL:
self.logger.debug(f"Ceph URL: {GatewayConstants.CEPH_CLUSTER_CEPH_REPO_BASEURL}")
self.ceph_utils.fetch_and_display_ceph_version()
requested_hugepages_val = os.getenv("HUGEPAGES", "")
requested_hugepages_val = os.getenv("HUGEPAGES", "2048")
if not requested_hugepages_val:
self.logger.warning("Can't get requested huge pages count")
else:
Expand All @@ -118,7 +106,7 @@ def __init__(self, config: GatewayConfig, gateway_state: GatewayStateHandler, rp
except ValueError:
self.logger.warning(f"Requested huge pages count value {requested_hugepages_val} is not numeric")
requested_hugepages_val = None
hugepages_file = os.getenv("HUGEPAGES_DIR", "")
hugepages_file = os.getenv("HUGEPAGES_DIR", "/sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages")
if not hugepages_file:
hugepages_file = "/sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages"
self.logger.warning("No huge pages file defined, will use /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages")
Expand Down Expand Up @@ -154,14 +142,14 @@ def __init__(self, config: GatewayConfig, gateway_state: GatewayStateHandler, rp
self.spdk_rpc_client = spdk_rpc_client
self.gateway_name = self.config.get("gateway", "name")
if not self.gateway_name:
self.gateway_name = socket.gethostname()
self.gateway_name = os.getenv("HOSTNAME", socket.gethostname())
self.gateway_group = self.config.get("gateway", "group")
override_hostname = self.config.get_with_default("gateway", "override_hostname", "")
if override_hostname:
self.host_name = override_hostname
self.logger.info(f"Gateway's host name was overridden to {override_hostname}")
else:
self.host_name = socket.gethostname()
self.host_name = os.getenv("HOSTNAME", socket.gethostname())
self.verify_nqns = self.config.getboolean_with_default("gateway", "verify_nqns", True)
self.ana_map = defaultdict(dict)
self.cluster_nonce = {}
Expand Down Expand Up @@ -2621,14 +2609,11 @@ def get_gateway_info_safe(self, request, context):

peer_msg = self.get_peer_message(context)
self.logger.info(f"Received request to get gateway's info{peer_msg}")
gw_version_string = os.getenv("NVMEOF_VERSION")
spdk_version_string = os.getenv("NVMEOF_SPDK_VERSION")
cli_version_string = request.cli_version
addr = self.config.get_with_default("gateway", "addr", "")
port = self.config.get_with_default("gateway", "port", "")
ret = pb2.gateway_info(cli_version = request.cli_version,
version = gw_version_string,
spdk_version = spdk_version_string,
version = GatewayConstants.NVMEOF_VERSION,
spdk_version = GatewayConstants.NVMEOF_SPDK_VERSION,
name = self.gateway_name,
group = self.gateway_group,
addr = addr,
Expand All @@ -2638,24 +2623,20 @@ def get_gateway_info_safe(self, request, context):
hostname = self.host_name,
status = 0,
error_message = os.strerror(0))
cli_ver = self.parse_version(cli_version_string)
gw_ver = self.parse_version(gw_version_string)
cli_ver = self.parse_version(request.cli_version)
gw_ver = self.parse_version(GatewayConstants.NVMEOF_VERSION)
if cli_ver != None and gw_ver != None and cli_ver < gw_ver:
ret.bool_status = False
ret.status = errno.EINVAL
ret.error_message = f"CLI version {cli_version_string} is older than gateway's version {gw_version_string}"
elif not gw_version_string:
ret.bool_status = False
ret.status = errno.ENOKEY
ret.error_message = "Gateway's version not found"
ret.error_message = f"CLI version {request.cli_version} is older than gateway's version {GatewayConstants.NVMEOF_VERSION}"
elif not gw_ver:
ret.bool_status = False
ret.status = errno.EINVAL
ret.error_message = f"Invalid gateway's version {gw_version_string}"
if not cli_version_string:
ret.error_message = f"Invalid gateway's version {GatewayConstants.NVMEOF_VERSION}"
if not request.cli_version:
self.logger.warning(f"No CLI version specified, can't check version compatibility")
elif not cli_ver:
self.logger.warning(f"Invalid CLI version {cli_version_string}, can't check version compatibility")
self.logger.warning(f"Invalid CLI version {request.cli_version}, can't check version compatibility")
if ret.status == 0:
log_func = self.logger.debug
else:
Expand Down
4 changes: 2 additions & 2 deletions control/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from typing import NamedTuple
from functools import wraps
from .utils import NICS
from .constants import GatewayConstants

COLLECTION_ELAPSED_WARNING = 0.8 # Percentage of the refresh interval before a warning message is issued
REGISTRY.unregister(GC_COLLECTOR) # Turn off garbage collector metrics
Expand Down Expand Up @@ -137,8 +138,7 @@ def __init__(self, spdk_rpc_client, config, gateway_rpc):

def _get_gw_metadata(self):
"""Fetch Gateway metadata"""
ver = os.getenv("NVMEOF_VERSION")
req = pb2.get_gateway_info_req(cli_version=ver)
req = pb2.get_gateway_info_req(cli_version=GatewayConstants.NVMEOF_CLI_VERSION)
metadata = self.gateway_rpc.get_gateway_info(req)

# Since empty values result in a missing label, when the group name is not
Expand Down
2 changes: 1 addition & 1 deletion control/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def __init__(self, config: GatewayConfig):

self.name = self.config.get("gateway", "name")
if not self.name:
self.name = socket.gethostname()
self.name = os.getenv("HOSTNAME", socket.gethostname())
self.logger.info(f"Starting gateway {self.name}")

self.allowed_consecutive_spdk_ping_failures = self.config.getint_with_default("gateway",
Expand Down
2 changes: 1 addition & 1 deletion control/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def __init__(self, config=None):
self.log_directory += "/"

if not gateway_name:
gateway_name = socket.gethostname()
gateway_name = os.getenv("HOSTNAME", socket.gethostname())
self.log_directory = self.log_directory + GatewayLogger.NVME_LOG_DIR_PREFIX + gateway_name

if GatewayLogger.logger:
Expand Down
5 changes: 5 additions & 0 deletions mk/containerized.mk
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,14 @@ image_name:
docker_compose_clean: down
$(DOCKER) system prune --all --force --volumes --filter label="io.ceph.nvmeof"

.PHONY:
const_clean:
rm -f control/constants.py

.PHONY:
clean_cache: ## Clean the Docker build cache
$(DOCKER) builder prune --force --all

CLEAN += docker_compose_clean
CLEAN += const_clean
ALL += pull up ps
7 changes: 4 additions & 3 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import grpc
from control.proto import gateway_pb2 as pb2
from control.proto import gateway_pb2_grpc as pb2_grpc
from control.constants import GatewayConstants
import os

image = "mytestdevimage"
Expand All @@ -23,7 +24,7 @@
nsid_ipv6 = "3"
anagrpid = "2"
anagrpid2 = "4"
host_name = socket.gethostname()
host_name = os.getenv("HOSTNAME", socket.gethostname())
addr = "127.0.0.1"
addr_ipv6 = "::1"
server_addr_ipv6 = "2001:db8::3"
Expand Down Expand Up @@ -97,7 +98,7 @@ def test_get_gateway_info(self, caplog, gateway):
assert "Invalid CLI version" in caplog.text
assert ret.status == 0
caplog.clear()
cli_ver = os.getenv("NVMEOF_VERSION")
cli_ver = GatewayConstants.NVMEOF_VERSION
save_port = gw.config.config["gateway"]["port"]
save_addr = gw.config.config["gateway"]["addr"]
gw.config.config["gateway"]["port"] = "6789"
Expand All @@ -116,7 +117,7 @@ def test_get_gateway_info(self, caplog, gateway):
cli(["version"])
assert f"CLI version: {cli_ver}" in caplog.text
caplog.clear()
spdk_ver = os.getenv("NVMEOF_SPDK_VERSION")
spdk_ver = GatewayConstants.NVMEOF_SPDK_VERSION
gw_info = cli_test(["gw", "info"])
assert gw_info != None
assert gw_info.cli_version == cli_ver
Expand Down

0 comments on commit c0117e5

Please sign in to comment.