Class: Hem::Lib::Vm::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/hem/lib/vm/command.rb

Constant Summary collapse

@@vm_inspector =
Inspector.new

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, opts = {}) ⇒ Command

Returns a new instance of Command.



12
13
14
15
16
17
18
19
20
# File 'lib/hem/lib/vm/command.rb', line 12

def initialize command, opts = {}
  @command = command
  @opts = {
      :auto_echo => false,
      :psuedo_tty => false,
      :pwd => opts[:pwd] || @@vm_inspector.project_mount_path,
      :append => ''
  }.merge(opts)
end

Class Attribute Details

.vm_inspectorObject

Returns the value of attribute vm_inspector.



6
7
8
# File 'lib/hem/lib/vm/command.rb', line 6

def vm_inspector
  @vm_inspector
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



10
11
12
# File 'lib/hem/lib/vm/command.rb', line 10

def command
  @command
end

#optsObject

Returns the value of attribute opts.



10
11
12
# File 'lib/hem/lib/vm/command.rb', line 10

def opts
  @opts
end

Instance Method Details

#<(cmd) ⇒ Object



42
43
44
# File 'lib/hem/lib/vm/command.rb', line 42

def < cmd
  pipe cmd, :on => :vm
end

#<<(cmd) ⇒ Object



38
39
40
# File 'lib/hem/lib/vm/command.rb', line 38

def << cmd
  pipe cmd, :on => :host
end

#pipe(cmd, pipe_opts = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hem/lib/vm/command.rb', line 22

def pipe cmd, pipe_opts = {}
  pipe_opts = pipe_opts.merge({ :on => :vm })
  cmd = "echo #{cmd.shellescape}" if @opts[:auto_echo]

  case pipe_opts[:on]
    when :vm
      @pipe_in_vm = cmd
    when :host
      @pipe = cmd
    else
      raise "Unknown pipe source: #{pipe_opts[:on]}"
  end
  @opts[:psuedo_tty] = false
  return self
end

#runObject



46
47
48
49
# File 'lib/hem/lib/vm/command.rb', line 46

def run
  return if @command.nil?
  Hem::Helper.shell to_s, @opts
end

#to_sObject

TODO Speed up Vagrant SSH connections May need to be disabled for windows (mm_send_fd: UsePrivilegeSeparation=yes not supported) gist.github.com/jedi4ever/5657094



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/hem/lib/vm/command.rb', line 55

def to_s
  require 'tempfile'
  config = ::Tempfile.new 'hem_ssh_config'
  config.write @@vm_inspector.ssh_config
  config.close

  psuedo_tty = opts[:psuedo_tty] ? '-t' : ''

  ssh_command = [
      'ssh',
      "-F #{config.path.shellescape}",
      psuedo_tty,
      'default'
  ].join(' ')

  pwd_set_command = " -- \"cd #{@opts[:pwd].shellescape}; exec /bin/bash"

  vm_command = [
      @pipe_in_vm.nil? ? nil : @pipe_in_vm.gsub(/(\\+)/, '\\\\\1'),
      @command
  ].compact.join(' | ')

  command = [
      ssh_command + pwd_set_command,
      vm_command.empty? ? nil : vm_command.shellescape
  ].compact.join(' -c ') + "#{@opts[:append].shellescape}\""

  [
      @pipe,
      command
  ].compact.join(' | ')
end

#to_strObject



88
89
90
# File 'lib/hem/lib/vm/command.rb', line 88

def to_str
  to_s
end