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 14, 2024
1 parent 9e9ce63 commit fd1af12
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 48 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Globals
# When updating versions change the values also in constants.py
VERSION="1.2.1"
CEPH_VERSION="18.2.2"
SPDK_VERSION="24.01"
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
16 changes: 16 additions & 0 deletions control/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#
# Copyright (c) 2024 International Business Machines
# All rights reserved.
#
# SPDX-License-Identifier: LGPL-3.0-or-later
#
# Author: [email protected]
#

#
# Make sure that these values are the same as i nthe .emv file
#
class GatewayConstants:
NVMEOF_VERSION="1.2.1"
NVMEOF_CLI_VERSION="1.2.1"
NVMEOF_SPDK_VERSION="24.01"
40 changes: 15 additions & 25 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,12 +74,8 @@ 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}")
self.logger.info(f"Using NVMeoF gateway version {GatewayConstants.NVMEOF_VERSION}")
self.logger.info(f"Using SPDK version {GatewayConstants.NVMEOF_SPDK_VERSION}")
ceph_ver = os.getenv("NVMEOF_CEPH_VERSION")
if ceph_ver:
self.logger.info(f"Using vstart cluster version based on {ceph_ver}")
Expand Down Expand Up @@ -107,7 +104,7 @@ def __init__(self, config: GatewayConfig, gateway_state: GatewayStateHandler, rp
if git_spdk_commit:
self.logger.info(f"SPDK Git commit: {git_spdk_commit}")
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 +115,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 +151,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.gateway_group = self.config.get_with_default("gateway", "group", "")
self.gateway_pool = self.config.get_with_default("ceph", "pool", "")
Expand Down Expand Up @@ -2621,14 +2618,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 +2632,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
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 fd1af12

Please sign in to comment.