Skip to content

Commit

Permalink
variable renaming and comments
Browse files Browse the repository at this point in the history
Signed-off-by: Sandy Kaur <[email protected]>
  • Loading branch information
sskaur committed Aug 17, 2022
1 parent 543eeea commit e81871e
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 245 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ This daemon runs as root. It provides the ability to export existing RBD images
2. Modify the config file (default ceph-nvmeof.conf) to reflect the IP/ Port where the server can be reached:

gateway_addr = <IP address at which the client can reach the gateway>
gateway_port = <port at which the client can reach the gateway>
addr = <IP address at which the client can reach the gateway>
port = <port at which the client can reach the gateway>

3. To [enable mTLS](#mtls-configuration-for-testing-purposes) using self signed certificates, edit the config file to set:

Expand Down Expand Up @@ -123,16 +123,16 @@ Indicate the location of the keys and certificates in the config file:
2. Run the CLI (ensure a ceph pool 'rbd' with an rbdimage 'mytestdevimage' is created prior to this step):

$ python3 -m control.cli create_bdev -i mytestdevimage -p rbd -b Ceph0
INFO:root:Created bdev: Ceph0
INFO:root:Created bdev Ceph0: True
$ python3 -m control.cli create_subsystem -n nqn.2016-06.io.spdk:cnode1 -s SPDK00000000000001
INFO:root:Created subsystem: nqn.2016-06.io.spdk:cnode1
INFO:root:Created subsystem nqn.2016-06.io.spdk:cnode1: True
$ python3 -m control.cli add_namespace -n nqn.2016-06.io.spdk:cnode1 -b Ceph0
INFO:root:Added namespace 1 to nqn.2016-06.io.spdk:cnode1
INFO:root:Added namespace 1 to nqn.2016-06.io.spdk:cnode1: True
$ python3 -m control.cli add_host -n nqn.2016-06.io.spdk:cnode1 -t *
INFO:root:Allow open host access to nqn.2016-06.io.spdk:cnode1: True
INFO:root:Allowed open host access to nqn.2016-06.io.spdk:cnode1: True
$ python3 -m control.cli create_listener -n nqn.2016-06.io.spdk:cnode1 -s 5001
INFO:root:Created nqn.2016-06.io.spdk:cnode1 listener: True
Expand Down
10 changes: 5 additions & 5 deletions ceph-nvmeof.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
# Authors: [email protected], [email protected]
#

[config]
[gateway]

gateway_name =
gateway_group =
gateway_addr = 127.0.0.1
gateway_port = 5500
name =
group =
addr = 127.0.0.1
port = 5500
enable_auth = False

[ceph]
Expand Down
6 changes: 3 additions & 3 deletions control/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import logging
import argparse
from .server import GatewayServer
from .config import NVMeGWConfig
from .config import GatewayConfig

if __name__ == '__main__':
parser = argparse.ArgumentParser(prog="python3 -m control",
Expand All @@ -30,7 +30,7 @@
if not os.path.isfile(args.config):
logger.error(f"Config file {args.config} not found.")
raise FileNotFoundError
config = NVMeGWConfig(args.config)

config = GatewayConfig(args.config)
with GatewayServer(config) as gateway:
gateway.serve()
109 changes: 56 additions & 53 deletions control/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import logging
from .proto import gateway_pb2_grpc as pb2_grpc
from .proto import gateway_pb2 as pb2
from .config import NVMeGWConfig
from .config import GatewayConfig


def argument(*name_or_flags, **kwargs):
Expand Down Expand Up @@ -93,23 +93,23 @@ def stub(self):
raise AttributeError("stub is None. Set with connect method.")
return self._stub

def connect(self, nvme_config):
def connect(self, config):
"""Connects to server and sets stub."""

# Read in configuration parameters
host = nvme_config.get("config", "gateway_addr")
port = nvme_config.get("config", "gateway_port")
enable_auth = nvme_config.getboolean("config", "enable_auth")
host = config.get("gateway", "addr")
port = config.get("gateway", "port")
enable_auth = config.getboolean("gateway", "enable_auth")
server = "{}:{}".format(host, port)

if enable_auth:

# Create credentials for mutual TLS and a secure channel
with open(nvme_config.get("mtls", "client_cert"), "rb") as f:
with open(config.get("mtls", "client_cert"), "rb") as f:
client_cert = f.read()
with open(nvme_config.get("mtls", "client_key"), "rb") as f:
with open(config.get("mtls", "client_key"), "rb") as f:
client_key = f.read()
with open(nvme_config.get("mtls", "server_cert"), "rb") as f:
with open(config.get("mtls", "server_cert"), "rb") as f:
server_cert = f.read()

credentials = grpc.ssl_channel_credentials(
Expand All @@ -119,12 +119,11 @@ def connect(self, nvme_config):
)
channel = grpc.secure_channel(server, credentials)
else:

# Instantiate a channel without credentials
channel = grpc.insecure_channel(server)

# Bind the client and the server
self._stub = pb2_grpc.NVMEGatewayStub(channel)
self._stub = pb2_grpc.GatewayStub(channel)

@cli.cmd([
argument("-i", "--image", help="RBD image name", required=True),
Expand All @@ -140,14 +139,14 @@ def create_bdev(self, args):
"""Creates a bdev from an RBD image."""

try:
create_req = pb2.create_bdev_req(
req = pb2.create_bdev_req(
ceph_pool_name=args.pool,
rbd_name=args.image,
block_size=args.block_size,
bdev_name=args.bdev,
)
ret = self.stub.create_bdev(create_req)
self.logger.info(f"Created bdev: {ret.status}")
ret = self.stub.create_bdev(req)
self.logger.info(f"Created bdev {args.bdev}: {ret.status}")
except Exception as error:
self.logger.error(f"Failed to create bdev: \n {error}")

Expand All @@ -158,9 +157,9 @@ def delete_bdev(self, args):
"""Deletes a bdev."""

try:
delete_req = pb2.delete_bdev_req(bdev_name=args.bdev)
ret = self.stub.delete_bdev(delete_req)
self.logger.info(f"Deleted bdev: {delete_req.bdev_name}")
req = pb2.delete_bdev_req(bdev_name=args.bdev)
ret = self.stub.delete_bdev(req)
self.logger.info(f"Deleted bdev {args.bdev}: {ret.status}")
except Exception as error:
self.logger.error(f"Failed to delete bdev: \n {error}")

Expand All @@ -172,10 +171,10 @@ def create_subsystem(self, args):
"""Creates a subsystem."""

try:
create_req = pb2.create_subsystem_req(subsystem_nqn=args.subnqn,
serial_number=args.serial)
ret = self.stub.create_subsystem(create_req)
self.logger.info(f"Created subsystem: {ret.status}")
req = pb2.create_subsystem_req(subsystem_nqn=args.subnqn,
serial_number=args.serial)
ret = self.stub.create_subsystem(req)
self.logger.info(f"Created subsystem {args.subnqn}: {ret.status}")
except Exception as error:
self.logger.error(f"Failed to create subsystem: \n {error}")

Expand All @@ -186,9 +185,9 @@ def delete_subsystem(self, args):
"""Deletes a subsystem."""

try:
delete_req = pb2.delete_subsystem_req(subsystem_nqn=args.subnqn)
ret = self.stub.delete_subsystem(delete_req)
self.logger.info(f"Deleted subsystem: {delete_req.subsystem_nqn}")
req = pb2.delete_subsystem_req(subsystem_nqn=args.subnqn)
ret = self.stub.delete_subsystem(req)
self.logger.info(f"Deleted subsystem {args.subnqn}: {ret.status}")
except Exception as error:
self.logger.error(f"Failed to delete subsystem: \n {error}")

Expand All @@ -200,10 +199,11 @@ def add_namespace(self, args):
"""Adds a namespace to a subsystem."""

try:
create_req = pb2.add_namespace_req(subsystem_nqn=args.subnqn,
bdev_name=args.bdev)
ret = self.stub.add_namespace(create_req)
self.logger.info(f"Added namespace {ret.nsid} to {args.subnqn}")
req = pb2.add_namespace_req(subsystem_nqn=args.subnqn,
bdev_name=args.bdev)
ret = self.stub.add_namespace(req)
self.logger.info(
f"Added namespace {ret.nsid} to {args.subnqn}: {ret.status}")
except Exception as error:
self.logger.error(f"Failed to add namespace: \n {error}")

Expand All @@ -215,10 +215,12 @@ def remove_namespace(self, args):
"""Removes a namespace from a subsystem."""

try:
delete_req = pb2.remove_namespace_req(subsystem_nqn=args.subnqn,
nsid=args.nsid)
ret = self.stub.remove_namespace(delete_req)
self.logger.info(f"Deleted namespace {delete_req.nsid}: {ret}")
req = pb2.remove_namespace_req(subsystem_nqn=args.subnqn,
nsid=args.nsid)
ret = self.stub.remove_namespace(req)
self.logger.info(
f"Removed namespace {args.nsid} from {args.subnqn}:" +
f" {ret.status}")
except Exception as error:
self.logger.error(f"Failed to remove namespace: \n {error}")

Expand All @@ -230,16 +232,16 @@ def add_host(self, args):
"""Adds a host to a subsystem."""

try:
create_req = pb2.add_host_req(subsystem_nqn=args.subnqn,
host_nqn=args.host)
ret = self.stub.add_host(create_req)
req = pb2.add_host_req(subsystem_nqn=args.subnqn,
host_nqn=args.host)
ret = self.stub.add_host(req)
if args.host == "*":
self.logger.info(
f"Allowed open host access to {args.subnqn}: {ret.status}")
else:
self.logger.info(
f"Added host {args.host} access to {args.subnqn}: {ret.status}"
)
f"Added host {args.host} access to {args.subnqn}:" +
f" {ret.status}")
except Exception as error:
self.logger.error(f"Failed to add host: \n {error}")

Expand All @@ -251,16 +253,16 @@ def remove_host(self, args):
"""Removes a host from a subsystem."""

try:
delete_req = pb2.remove_host_req(subsystem_nqn=args.subnqn,
host_nqn=args.host)
ret = self.stub.remove_host(delete_req)
req = pb2.remove_host_req(subsystem_nqn=args.subnqn,
host_nqn=args.host)
ret = self.stub.remove_host(req)
if args.host == "*":
self.logger.info(
f"Disabled open host access to {args.subnqn}: {ret.status}")
else:
self.logger.info(
f"Removed host {args.host} access from {args.subnqn}: {ret.status}"
)
f"Removed host {args.host} access from {args.subnqn}:" +
f" {ret.status}")
except Exception as error:
self.logger.error(f"Failed to remove host: \n {error}")

Expand All @@ -273,18 +275,18 @@ def remove_host(self, args):
argument("-s", "--trsvcid", help="Port number", required=True),
])
def create_listener(self, args):
"""Creates a listener for a subsystem at a given TCP/IP address."""
"""Creates a listener for a subsystem at a given IP/Port."""

try:
create_req = pb2.create_listener_req(
req = pb2.create_listener_req(
nqn=args.subnqn,
gateway_name=args.gateway_name,
trtype=args.trtype,
adrfam=args.adrfam,
traddr=args.traddr,
trsvcid=args.trsvcid,
)
ret = self.stub.create_listener(create_req)
ret = self.stub.create_listener(req)
self.logger.info(f"Created {args.subnqn} listener: {ret.status}")
except Exception as error:
self.logger.error(f"Failed to create listener: \n {error}")
Expand All @@ -298,29 +300,30 @@ def create_listener(self, args):
argument("-s", "--trsvcid", help="Port number", required=True),
])
def delete_listener(self, args):
"""Deletes a listener from a subsystem at a given TCP/IP address."""
"""Deletes a listener from a subsystem at a given IP/Port."""

try:
delete_req = pb2.delete_listener_req(
req = pb2.delete_listener_req(
nqn=args.subnqn,
gateway_name=args.gateway_name,
trtype=args.trtype,
adrfam=args.adrfam,
traddr=args.traddr,
trsvcid=args.trsvcid,
)
ret = self.stub.delete_listener(delete_req)
self.logger.info(f"Deleted {args.traddr} from {args.subnqn}: {ret.status}")
ret = self.stub.delete_listener(req)
self.logger.info(
f"Deleted {args.traddr} from {args.subnqn}: {ret.status}")
except Exception as error:
self.logger.error(f"Failed to delete listener: \n {error}")

@cli.cmd()
def get_subsystems(self, args):
"""Get subsystems."""
"""Gets subsystems."""

try:
get_req = pb2.get_subsystems_req()
ret = self.stub.get_subsystems(get_req)
req = pb2.get_subsystems_req()
ret = self.stub.get_subsystems(req)
subsystems = json.loads(ret.subsystems)
formatted_subsystems = json.dumps(subsystems, indent=4)
self.logger.info(f"Get subsystems:\n{formatted_subsystems}")
Expand All @@ -331,8 +334,8 @@ def get_subsystems(self, args):
def main(args=None):
client = GatewayClient()
parsed_args = client.cli.parser.parse_args(args)
nvme_config = NVMeGWConfig(parsed_args.config)
client.connect(nvme_config)
config = GatewayConfig(parsed_args.config)
client.connect(config)
if parsed_args.subcommand is None:
client.cli.parser.print_help()
else:
Expand Down
29 changes: 17 additions & 12 deletions control/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,36 @@
import configparser


class NVMeGWConfig:
def __init__(self, gw_config_filename):
self.nvme_gw_config = configparser.ConfigParser()
self.nvme_gw_config.read(gw_config_filename)
class GatewayConfig:
"""Loads and returns config file settings.
Instance attributes:
config: Config parser object
"""
def __init__(self, conffile):
self.config = configparser.ConfigParser()
self.config.read(conffile)

def get(self, section, param):
return self.nvme_gw_config.get(section, param)
return self.config.get(section, param)

def getboolean(self, section, param):
return self.nvme_gw_config.getboolean(section, param)
return self.config.getboolean(section, param)

def getint(self, section, param):
return self.nvme_gw_config.getint(section, param)
return self.config.getint(section, param)

def getfloat(self, section, param):
return self.nvme_gw_config.getfloat(section, param)
return self.config.getfloat(section, param)

def get_with_default(self, section, param, value):
return self.nvme_gw_config.get(section, param, fallback=value)
return self.config.get(section, param, fallback=value)

def getboolean_with_default(self, section, param, value):
return self.nvme_gw_config.getboolean(section, param, fallback=value)
return self.config.getboolean(section, param, fallback=value)

def getint_with_default(self, section, param, value):
return self.nvme_gw_config.getint(section, param, fallback=value)
return self.config.getint(section, param, fallback=value)

def getfloat_with_default(self, section, param, value):
return self.nvme_gw_config.getfloat(section, param, fallback=value)
return self.config.getfloat(section, param, fallback=value)
Loading

0 comments on commit e81871e

Please sign in to comment.