Class: VagrantPlugins::RemoteShell

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

Instance Method Summary collapse

Constructor Details

#initialize(communication, machine) ⇒ RemoteShell

Returns a new instance of RemoteShell.



3
4
5
6
7
# File 'lib/niman/vagrant/shell.rb', line 3

def initialize(communication, machine)
  @channel = communication
  @machine = machine
  @platform = Niman::Platform.new(ruby_platform)
end

Instance Method Details

#create_file(path, content, use_sudo = false) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/niman/vagrant/shell.rb', line 48

def create_file(path, content, use_sudo=false)
  if content.include?("\n")
    cmd = "cat > #{path} << EOL\n#{content}\nEOL"
  else
    cmd  = "echo #{content} > #{path}"
  end
  self.exec(cmd, use_sudo)
end

#exec(command, use_sudo = false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/niman/vagrant/shell.rb', line 19

def exec(command, use_sudo=false)
  @machine.ui.info(command, {color: :green})
  opts = { error_check: false, elevated: use_sudo }
  handler = Proc.new do |type, data|
    if [:stderr, :stdout].include?(type)
      color = type == :stdout ? :green : :red
      data = data.chomp
      next if data.empty?
      options = {}
      options[:color] = :green
      @machine.ui.info(data.chomp, options)
    end
  end
  if use_sudo
    @channel.sudo(command, opts, &handler)
  else
    @channel.execute(command, opts, &handler)
  end
end

#osObject



9
10
11
12
13
14
15
16
17
# File 'lib/niman/vagrant/shell.rb', line 9

def os
  if @platform.linux?
    variant = @platform.linux_variant(-> (fn){ @machine.communicate.test("cat #{fn}")}, 
                                      -> (fn){ @channel.execute("cat #{fn}") { |type, data| data.chomp}})
    variant[:family]
  else
    raise Niman::UnsupportedOSError
  end
end


39
40
41
42
43
44
45
46
# File 'lib/niman/vagrant/shell.rb', line 39

def print(message, type)
  case type
  when :error
    @machine.ui.error(message, {:color => :red})
  else
    @machine.ui.info(message, {:color => :green})
  end
end