Class: Firewalld::FWCmd

Inherits:
Object
  • Object
show all
Includes:
Yast::Logger
Defined in:
library/network/src/lib/network/firewalld.rb

Overview

Execute firewalld commands using firewall-cmd

Constant Summary collapse

BASH_SCR_PATH =
Yast::Path.new(".target.bash_output")
COMMAND =

Base firewall-cmd command

"LANG=C firewall-cmd".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(option_str) ⇒ FWCmd

Returns a new instance of FWCmd



47
48
49
# File 'library/network/src/lib/network/firewalld.rb', line 47

def initialize(option_str)
  @option_str = option_str
end

Instance Attribute Details

#option_strObject (readonly)

Returns the value of attribute option_str



45
46
47
# File 'library/network/src/lib/network/firewalld.rb', line 45

def option_str
  @option_str
end

Instance Method Details

#commandObject



51
52
53
# File 'library/network/src/lib/network/firewalld.rb', line 51

def command
  "#{COMMAND} #{option_str}".strip.squeeze(" ")
end

#fwd_output(output = true) ⇒ String, Boolean

Parameters:

  • output (Boolean) (defaults to: true)

    Whether command output is desirable or not

Returns:

  • (String)

    Firewalld command output if output = true

  • (Boolean)

    Firewalld command exit code if output = false



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'library/network/src/lib/network/firewalld.rb', line 58

def fwd_output(output = true)
  cmd_result = Yast::SCR.Execute(BASH_SCR_PATH, command)

  # See firewall-cmd manpage for exit codes. Not all of them justify an
  # exception. 0 and 1 can be used as true and false respectively.
  case cmd_result["exit"]
  when 0, 1
    if output
      cmd_result["stdout"]
    else
      log.debug "#{command} returned: #{cmd_result["stdout"]}"
      cmd_result["exit"].zero? ? true : false
    end
  else
    raise FirewallCMDError, "Calling firewall-cmd (cmd: #{command}) failed: #{cmd_result["stderr"]}"
  end
end