Skip to content

Commit

Permalink
fencing: Specify command to be logged by run_command
Browse files Browse the repository at this point in the history
Currently run_command special cases the logging of commands with ipmitool and removes everything
between the flat -P and -p. Instead of special casing commands with ipmitool allow the caller to
provide the command to log so that the command can do any necessary scrubbing of the message to be
logged.
  • Loading branch information
n3world committed Dec 1, 2016
1 parent e67fcd4 commit 053cbec
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 33 deletions.
8 changes: 2 additions & 6 deletions fence/agents/autodetect/fencing.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,17 +901,13 @@ def is_executable(path):
return True
return False

def run_command(options, command, timeout=None, env=None):
def run_command(options, command, timeout=None, env=None, log_command=None):
if timeout is None and "--power-timeout" in options:
timeout = options["--power-timeout"]
if timeout is not None:
timeout = float(timeout)

# For IPMI password occurs on command line, it should not be part of debug info
log_command = command
if "ipmitool" in log_command:
log_command = re.sub("-P (.+?) -p", "-P [set] -p", log_command)
logging.info("Executing: %s\n", log_command)
logging.info("Executing: %s\n", log_command or command)

try:
process = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
Expand Down
55 changes: 34 additions & 21 deletions fence/agents/ipmilan/fence_ipmilan.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,67 +14,80 @@
#END_VERSION_GENERATION

def get_power_status(_, options):
output = run_command(options, create_command(options, "status"))
output = _run_command(options, "status")
match = re.search('[Cc]hassis [Pp]ower is [\\s]*([a-zA-Z]{2,3})', str(output))
status = match.group(1) if match else None
return status

def set_power_status(_, options):
run_command(options, create_command(options, options["--action"]))
_run_command(options, options["--action"])
return

def reboot_cycle(_, options):
output = run_command(options, create_command(options, "cycle"))
output = _run_command(options, "cycle")
return bool(re.search('chassis power control: cycle', str(output).lower()))

def reboot_diag(_, options):
output = run_command(options, create_command(options, "diag"))
output = _run_command(options, "diag")
return bool(re.search('chassis power control: diag', str(output).lower()))

def _run_command(options, action):
cmd, log_cmd = create_command(options, action)
return run_command(options, cmd, log_command=log_cmd)

def create_command(options, action):
cmd = options["--ipmitool-path"]
class Cmd:
cmd = ""
log = ""

@classmethod
def append(cls, cmd, log=None):
cls.cmd += cmd
cls.log += (cmd if log is None else log)

# --use-sudo / -d
if "--use-sudo" in options:
Cmd.append(options["--sudo-path"] + " ")

Cmd.append(options["--ipmitool-path"])

# --lanplus / -L
if "--lanplus" in options and options["--lanplus"] in ["", "1"]:
cmd += " -I lanplus"
Cmd.append(" -I lanplus")
else:
cmd += " -I lan"
Cmd.append(" -I lan")
# --ip / -a
cmd += " -H " + options["--ip"]
Cmd.append(" -H " + options["--ip"])

# --username / -l
if "--username" in options and len(options["--username"]) != 0:
cmd += " -U " + quote(options["--username"])
Cmd.append(" -U " + quote(options["--username"]))

# --auth / -A
if "--auth" in options:
cmd += " -A " + options["--auth"]
Cmd.append(" -A " + options["--auth"])

# --password / -p
if "--password" in options:
cmd += " -P " + quote(options["--password"])
Cmd.append(" -P " + quote(options["--password"]), " -P [set]")
else:
cmd += " -P ''"
Cmd.append(" -P ''", " -P [set]")

# --cipher / -C
if "--cipher" in options:
cmd += " -C " + options["--cipher"]
Cmd.append(" -C " + options["--cipher"])

# --port / -n
if "--ipport" in options:
cmd += " -p " + options["--ipport"]
Cmd.append(" -p " + options["--ipport"])

if "--privlvl" in options:
cmd += " -L " + options["--privlvl"]
Cmd.append(" -L " + options["--privlvl"])

# --action / -o
cmd += " chassis power " + action

# --use-sudo / -d
if "--use-sudo" in options:
cmd = options["--sudo-path"] + " " + cmd
Cmd.append(" chassis power " + action)

return cmd
return (Cmd.cmd, Cmd.log)

def define_new_opts():
all_opt["lanplus"] = {
Expand Down
8 changes: 2 additions & 6 deletions fence/agents/lib/fencing.py.py
Original file line number Diff line number Diff line change
Expand Up @@ -911,17 +911,13 @@ def is_executable(path):
return True
return False

def run_command(options, command, timeout=None, env=None):
def run_command(options, command, timeout=None, env=None, log_command=None):
if timeout is None and "--power-timeout" in options:
timeout = options["--power-timeout"]
if timeout is not None:
timeout = float(timeout)

# For IPMI password occurs on command line, it should not be part of debug info
log_command = command
if "ipmitool" in log_command:
log_command = re.sub("-P (.+?) -p", "-P [set] -p", log_command)
logging.info("Executing: %s\n", log_command)
logging.info("Executing: %s\n", log_command or command)

try:
process = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
Expand Down

0 comments on commit 053cbec

Please sign in to comment.