Class: Hobo::Lib::Vm::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/hobo/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/hobo/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/hobo/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/hobo/lib/vm/command.rb', line 10

def command
  @command
end

#optsObject

Returns the value of attribute opts.



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

def opts
  @opts
end

Instance Method Details

#<(pipe) ⇒ Object



29
30
31
32
33
34
# File 'lib/hobo/lib/vm/command.rb', line 29

def < pipe
  pipe = "echo '#{pipe.shellescape}'" if opts[:auto_echo]
  @pipe_in_vm = pipe
  @opts[:psuedo_tty] = false
  return self
end

#<<(pipe) ⇒ Object



22
23
24
25
26
27
# File 'lib/hobo/lib/vm/command.rb', line 22

def << pipe
  pipe = "echo #{pipe.shellescape}" if opts[:auto_echo]
  @pipe = pipe
  @opts[:psuedo_tty] = false
  return self
end

#runObject

TODO Refactor in to ssh helper with similar opts to shell helper TODO Migrate all vm_shell functionality this direction



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hobo/lib/vm/command.rb', line 38

def run
  return if @command.nil?
  require 'net/ssh/simple'
  opts = @@vm_inspector.ssh_config.merge(@opts)

  Net::SSH::Simple.sync do
    ssh_opts = {
        :user => opts[:ssh_user],
        :port => opts[:ssh_port],
        :forward_agent => true,
        :global_known_hosts_file => "/dev/null",
        :paranoid => false,
        :user_known_hosts_file => "/dev/null"
    }

    ssh_opts[:keys] = [opts[:ssh_identity]] if opts[:ssh_identity]

    tmp = Tempfile.new "vm_command_exec"

    begin
      filename = File.basename(tmp.path)
      remote_file = "/tmp/#{filename}"
      tmp.write "#{@command}#{opts[:append]}"
      tmp.close

      scp_put opts[:ssh_host], tmp.path, remote_file, ssh_opts
      result = ssh opts[:ssh_host], "cd #{opts[:pwd]}; exec /bin/bash #{remote_file}", ssh_opts
      ssh opts[:ssh_host], "rm #{remote_file}", ssh_opts

      # Throw exception if exit code not 0

      return opts[:capture] ? result.stdout : result.success
    ensure
      tmp.unlink
    end
  end
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



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
107
108
109
110
111
112
113
# File 'lib/hobo/lib/vm/command.rb', line 80

def to_s
  opts = @@vm_inspector.ssh_config.merge(@opts)

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

  ssh_command = [
      "ssh",
      "-o 'UserKnownHostsFile /dev/null'",
      "-o 'StrictHostKeyChecking no'",
      "-o 'ForwardAgent yes'",
      "-o 'LogLevel FATAL'",
      "-p #{opts[:ssh_port]}",
      "-i #{opts[:ssh_identity].shellescape}",
      psuedo_tty,
      "#{opts[:ssh_user].shellescape}@#{opts[:ssh_host].shellescape}"
  ].join(" ")

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

  vm_command = [
      @pipe_in_vm,
      @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



115
116
117
# File 'lib/hobo/lib/vm/command.rb', line 115

def to_str
  to_s
end