Class: Sensible::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/sensible/shell.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sensible) ⇒ Shell

Returns a new instance of Shell.



8
9
10
# File 'lib/sensible/shell.rb', line 8

def initialize(sensible)
  @sensible = sensible
end

Instance Attribute Details

#sensibleObject (readonly)

Returns the value of attribute sensible.



6
7
8
# File 'lib/sensible/shell.rb', line 6

def sensible
  @sensible
end

Instance Method Details

#exec_user_with_status(ssh, command, as_user: nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/sensible/shell.rb', line 74

def exec_user_with_status(ssh, command, as_user: nil)
  stdout    = ''
  stderr    = ''
  exit_code = nil

  ssh.open_channel do |ch|

    if as_user 
      ch.exec("su - #{as_user}") do |ch_exec, success|
        raise "could not exec sudo" unless success
        ch.send_data("#{command}\n")

        ch.on_data          { |_, data| stdout << data }
        ch.on_extended_data { |_, _, data| stderr << data }
        ch.on_request("exit-status") { |_, data| exit_code = data.read_long }

        # when done:
        ch.send_data("exit\n")
      end
    else
      ch.exec(command) do |ch_exec, success|
        raise "Could not exec #{cmd}" unless success

        ch.on_data          { |_, data| stdout << data }
        ch.on_extended_data { |_, _, data| stderr << data }
        ch.on_request("exit-status") { |_, data| exit_code = data.read_long }
      end
    end
  end

  ssh.loop
  [stdout, stderr, exit_code]
end

#exec_with_status(ssh, command) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sensible/shell.rb', line 57

def exec_with_status(ssh, command)
  stdout = stderr = ""
  exit_code = nil

  ssh.open_channel do |ch|
    ch.exec(command) do |ch, success|
      raise "could not exec #{command}" unless success

      ch.on_data          { |c, d| stdout << d }
      ch.on_extended_data { |c, t, d| stderr << d }
      ch.on_request("exit-status") { |c, data| exit_code = data.read_long }
    end
  end
  ssh.loop
  [stdout, stderr, exit_code]
end

#run_command(command, user = nil, show_output: false) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sensible/shell.rb', line 12

def run_command(command, user = nil, show_output: false)
  show_output = @sensible.opts.verbose

  if (@sensible.opts.host != nil)
    # Run command on remote machine
    Net::SSH.start(@sensible.opts.host, 'root') do |ssh| 
      out, err, code = exec_user_with_status(ssh, command, as_user: user)
      
      if @sensible.opts.verbose
        puts "STDOUT: #{out}"
        puts "STDERR: #{err}"
        puts "EXIT CODE: #{code}"
      end
      
      # Show a warning if the user does not exist!
      if err.include?("does not exist or the user entry does not contain all the required fields")
        Logger.error("User: '#{user}' does not exist!")
      end


      if code == 0
        return true
      else 
        return false
      end
    end
  else
    # Standard local shell command
    # If command contains new lines, use a temporary file
    if user != nil
      system("sudo runuser -l #{user} -c '#{command}'", out: (show_output ? $stdout : File::NULL), err: (show_output ? $stderr : File::NULL))
      return $?.success?
    end


    if command.include?("\n")
      system("bash", "-c", command, out: (show_output ? $stdout : File::NULL), err: (show_output ? $stderr : File::NULL)) 
      return $?.success?
    else
      system(command, out: (show_output ? $stdout : File::NULL), err: (show_output ? $stderr : File::NULL))
      return $?.success?    
    end
  end
end