Skip to content

Commit

Permalink
Add a get_gateway_info CLI command.
Browse files Browse the repository at this point in the history
Fiex ceph#332

Signed-off-by: Gil Bregman <[email protected]>
  • Loading branch information
gbregman committed Nov 26, 2023
1 parent 7280243 commit 5b5285c
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 3 deletions.
13 changes: 13 additions & 0 deletions control/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import json
import logging
import sys
import os

from functools import wraps
from google.protobuf import json_format
Expand Down Expand Up @@ -378,6 +379,18 @@ def set_spdk_nvmf_logs(self, args):
self.logger.info(
f"Set SPDK nvmf logs : {ret.status}")

@cli.cmd()
def get_gateway_info(self, args):
"""Get gateway's info"""
ver = os.getenv("NVMEOF_VERSION")
req = pb2.get_gateway_info_req(cli_version=ver)
gw_info = json_format.MessageToJson(
self.stub.get_gateway_info(req),
indent=4,
including_default_value_fields=True,
preserving_proto_field_name=True)
self.logger.info(f"Gateway info:\n{gw_info}")

def main(args=None):
client = GatewayClient()
parsed_args = client.cli.parser.parse_args(args)
Expand Down
46 changes: 46 additions & 0 deletions control/grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def __init__(self, config, gateway_state, omap_lock, spdk_rpc_client) -> None:
self.gateway_name = self.config.get("gateway", "name")
if not self.gateway_name:
self.gateway_name = socket.gethostname()
self.gateway_group = self.config.get("gateway", "group")
self._init_cluster_context()

def _init_cluster_context(self) -> None:
Expand Down Expand Up @@ -899,3 +900,48 @@ def disable_spdk_nvmf_logs_safe(self, request, context):
def disable_spdk_nvmf_logs(self, request, context):
with self.rpc_lock:
return self.disable_spdk_nvmf_logs_safe(request, context)

def parse_version(self, version):
if not version:
return None
try:
vlist = version.split(".")
if len(vlist) != 3:
raise Exception
v1 = int(vlist[0])
v2 = int(vlist[1])
v3 = int(vlist[2])
except Exception:
self.logger.error(f"Can't parse version \"{version}\"")
return None
return (v1, v2, v3)

def get_gateway_info(self, request, context):
"""Return gateway's info"""
self.logger.info(f"Received request to get gateway's info")
gw_version_string = os.getenv("NVMEOF_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,
gateway_version = gw_version_string,
gateway_name = self.gateway_name,
gateway_group = self.gateway_group,
gateway_addr = addr,
gateway_port = port,
status = True)
cli_ver = self.parse_version(cli_version_string)
gw_ver = self.parse_version(gw_version_string)
if cli_ver != None and gw_ver != None and cli_ver < gw_ver:
self.logger.error(f"CLI version {cli_version_string} is older than gateway's version {gw_version_string}")
ret.status = False
if not cli_version_string:
self.logger.error(f"No CLI version specified")
ret.status = False
if not gw_version_string:
self.logger.error(f"Gateway version not found")
ret.status = False
if not cli_ver or not gw_ver:
ret.status = False
self.logger.info(f"Gateway's info:\n{ret}")
return ret
17 changes: 17 additions & 0 deletions control/proto/gateway.proto
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ service Gateway {

// Set spdk nvmf logs
rpc set_spdk_nvmf_logs(set_spdk_nvmf_logs_req) returns(req_status) {}

// Set spdk nvmf logs
rpc get_gateway_info(get_gateway_info_req) returns(gateway_info) {}
}

// Request messages
Expand Down Expand Up @@ -144,6 +147,10 @@ message set_spdk_nvmf_logs_req {
optional string print_level = 3;
}

message get_gateway_info_req {
string cli_version = 1;
}

// Return messages

message bdev {
Expand Down Expand Up @@ -178,6 +185,16 @@ message subsystem {
repeated namespace namespaces = 11;
}

message gateway_info {
string cli_version = 1;
string gateway_version = 2;
string gateway_name = 3;
string gateway_group = 4;
string gateway_addr = 5;
string gateway_port = 6;
bool status = 7;
}

message listen_address {
string transport = 1;
string trtype = 2;
Expand Down
51 changes: 48 additions & 3 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import socket
from control.cli import main as cli
import spdk.rpc.bdev as rpc_bdev
import grpc
from control.proto import gateway_pb2 as pb2
from control.proto import gateway_pb2_grpc as pb2_grpc
import os

image = "mytestdevimage"
pool = "rbd"
Expand Down Expand Up @@ -30,11 +34,18 @@
def gateway(config):
"""Sets up and tears down Gateway"""

addr = config.get("gateway", "addr")
port = config.getint("gateway", "port")

with GatewayServer(config) as gateway:

# Start gateway
gateway.serve()
yield gateway.gateway_rpc

# Bind the client and Gateway
channel = grpc.insecure_channel(f"{addr}:{port}")
stub = pb2_grpc.GatewayStub(channel)
yield gateway.gateway_rpc, stub

# Stop gateway
gateway.server.stop(grace=1)
Expand All @@ -51,10 +62,44 @@ def test_get_subsystems_ipv6(self, caplog, gateway):
cli(["--server-address", server_addr_ipv6, "get_subsystems"])
assert "[]" in caplog.text

def test_get_gateway_info(self, caplog, gateway):
gw, stub = gateway
caplog.clear()
gw_info_req = pb2.get_gateway_info_req(cli_version="0.0.1")
ret = stub.get_gateway_info(gw_info_req)
assert not ret.status
assert "is older than gateway" in caplog.text
caplog.clear()
gw_info_req = pb2.get_gateway_info_req()
ret = stub.get_gateway_info(gw_info_req)
assert "No CLI version specified" in caplog.text
assert not ret.status
caplog.clear()
gw_info_req = pb2.get_gateway_info_req(cli_version="0.0.1.4")
ret = stub.get_gateway_info(gw_info_req)
assert "Can't parse version" in caplog.text
assert not ret.status
caplog.clear()
gw_info_req = pb2.get_gateway_info_req(cli_version="0.X.4")
ret = stub.get_gateway_info(gw_info_req)
assert "Can't parse version" in caplog.text
assert not ret.status
caplog.clear()
cli_ver = os.getenv("NVMEOF_VERSION")
gw.config.config["gateway"]["port"] = "6789"
gw.config.config["gateway"]["addr"] = "10.10.10.10"
gw_info_req = pb2.get_gateway_info_req(cli_version=cli_ver)
ret = stub.get_gateway_info(gw_info_req)
assert ret.status
assert f'cli_version: "{cli_ver}"' in caplog.text
assert f'gateway_version: "{cli_ver}"' in caplog.text
assert 'gateway_port: "6789"' in caplog.text
assert 'gateway_addr: "10.10.10.10"' in caplog.text
assert f'gateway_name: "{gw.gateway_name}"' in caplog.text

class TestCreate:
def test_create_bdev(self, caplog, gateway):
gw = gateway
gw, stub = gateway
bdev_found = False
caplog.clear()
cli(["create_bdev", "-i", image, "-p", pool, "-b", bdev])
Expand Down Expand Up @@ -88,7 +133,7 @@ def test_create_bdev_ipv6(self, caplog, gateway):
def test_resize_bdev(self, caplog, gateway):
caplog.clear()
bdev_found = False
gw = gateway
gw, stub = gateway
cli(["resize_bdev", "-b", bdev, "-s", "20"])
assert f"Resized bdev {bdev}: True" in caplog.text
bdev_list = rpc_bdev.bdev_get_bdevs(gw.spdk_rpc_client)
Expand Down

0 comments on commit 5b5285c

Please sign in to comment.