Module: LinuxAdmin::Common

Included in:
LinuxAdmin, LinuxAdmin
Defined in:
lib/linux_admin/common.rb

Instance Method Summary collapse

Instance Method Details

#cmd(cmd) ⇒ Object



5
6
7
# File 'lib/linux_admin/common.rb', line 5

def cmd(cmd)
  Distro.local.class::COMMANDS[cmd]
end

#run(cmd, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/linux_admin/common.rb', line 9

def run(cmd, options = {})
  params = options[:params] || options[:parameters]

  launch_params = {}
  launch_params[:chdir] = options[:chdir] if options[:chdir]

  output = ""
  error  = ""
  status = nil

  begin
    output, error = launch(build_cmd(cmd, params), launch_params)
    status = exitstatus
  ensure
    output ||= ""
    error  ||= ""
    self.exitstatus = nil
  end
rescue Errno::ENOENT => err
  raise NoSuchFileError.new(err.message) if NoSuchFileError.detected?(err.message)
  raise
else
  CommandResult.new(output, error, status)
end

#run!(cmd, options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/linux_admin/common.rb', line 34

def run!(cmd, options = {})
  params = options[:params] || options[:parameters]
  command_result = run(cmd, options)

  if command_result.exit_status != 0
    message = "#{cmd} exit code: #{command_result.exit_status}"
    raise CommandResultError.new(message, command_result)
  end

  command_result
end