Skip to content

Commit

Permalink
utils: make the run_shell_command a common helper
Browse files Browse the repository at this point in the history
Signed-off-by: Xiubo Li <[email protected]>
  • Loading branch information
lxbsz committed Jul 23, 2021
1 parent b1a608a commit 723bb91
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
20 changes: 5 additions & 15 deletions ceph_iscsi_config/gateway.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import subprocess
import netifaces

from rtslib_fb.utils import RTSLibError
Expand All @@ -11,7 +10,7 @@
from ceph_iscsi_config.lun import LUN
from ceph_iscsi_config.client import GWClient
from ceph_iscsi_config.lio import LIO
from ceph_iscsi_config.utils import this_host, CephiSCSIError
from ceph_iscsi_config.utils import this_host, CephiSCSIError, run_shell_cmd

__author__ = '[email protected]'

Expand All @@ -26,15 +25,6 @@ def __init__(self, logger, config, name=None):
else:
self.hostname = this_host()

def _run_ceph_cmd(self, cmd, stderr=None, shell=True):
if not stderr:
stderr = subprocess.STDOUT
try:
result = subprocess.check_output(cmd, stderr=stderr, shell=shell)
except subprocess.CalledProcessError as err:
return None, err
return result, None

def ceph_rm_blocklist(self, blocklisted_ip):
"""
Issue a ceph osd blocklist rm command for a given IP on this host
Expand All @@ -46,13 +36,13 @@ def ceph_rm_blocklist(self, blocklisted_ip):
"{}".format(blocklisted_ip))

conf = settings.config
result, err = self._run_ceph_cmd(
result, err = run_shell_cmd(
"ceph -n {client_name} --conf {cephconf} osd blocklist rm "
"{blocklisted_ip}".format(blocklisted_ip=blocklisted_ip,
client_name=conf.cluster_client_name,
cephconf=conf.cephconf))
if err:
result, err = self._run_ceph_cmd(
result, err = run_shell_cmd(
"ceph -n {client_name} --conf {cephconf} osd blacklist rm "
"{blocklisted_ip}".format(blocklisted_ip=blocklisted_ip,
client_name=conf.cluster_client_name,
Expand Down Expand Up @@ -86,13 +76,13 @@ def osd_blocklist_cleanup(self):

# NB. Need to use the stderr override to catch the output from
# the command
blocklist, err = self._run_ceph_cmd(
blocklist, err = run_shell_cmd(
"ceph -n {client_name} --conf {cephconf} osd blocklist ls".
format(client_name=conf.cluster_client_name,
cephconf=conf.cephconf))

if err:
blocklist, err = self._run_ceph_cmd(
blocklist, err = run_shell_cmd(
"ceph -n {client_name} --conf {cephconf} osd blacklist ls".
format(client_name=conf.cluster_client_name,
cephconf=conf.cephconf))
Expand Down
14 changes: 7 additions & 7 deletions ceph_iscsi_config/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ class CephiSCSIInval(CephiSCSIError):
pass


def shellcommand(command_string):

def run_shell_cmd(cmd, stderr=None, shell=True):
if not stderr:
stderr = subprocess.STDOUT
try:
response = subprocess.check_output(command_string, shell=True)
except subprocess.CalledProcessError:
return None
else:
return response
result = subprocess.check_output(cmd, stderr=stderr, shell=shell)
except subprocess.CalledProcessError as err:
return None, err
return result, None


def normalize_ip_address(ip_address):
Expand Down

0 comments on commit 723bb91

Please sign in to comment.