Method: Lab::Drivers::WorkstationDriver#run_command

Defined in:
lib/lab/driver/workstation_driver.rb

#run_command(command) ⇒ Object



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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/lab/driver/workstation_driver.rb', line 55

def run_command(command)

  #
  # Generate a script name
  #
  script_rand_name = rand(1000000)

  #
  # Configure paths for each OS - We really can't filter command, so we're gonna 
  # stick it in a script
  #
  if @os == "windows"
    local_tempfile_path = "/tmp/lab_script_#{script_rand_name}.bat"
    remote_tempfile_path = "C:\\\\lab_script_#{script_rand_name}.bat"
    remote_output_file = "C:\\\\lab_command_output_#{script_rand_name}"
    remote_run_command = remote_tempfile_path
    File.open(local_tempfile_path, 'w') {|f| f.write(command) }
  else
    
    local_tempfile_path = "/tmp/lab_script_#{script_rand_name}.sh"
    remote_tempfile_path = "/tmp/lab_script_#{script_rand_name}.sh"
    remote_output_file = "/tmp/lab_command_output_#{script_rand_name}"
    local_output_file = "/tmp/lab_command_output_#{script_rand_name}"
    
    remote_run_command = remote_tempfile_path
    
    File.open(local_tempfile_path, 'w') {|f| f.write("#!/bin/sh\n#{command}\n")}
  end
  
  if @tools

    #puts "DEBUG: Running w/ tools"

    #
    # Copy our local tempfile to the guest
    #
    vmrunstr = "vmrun -T ws -gu #{@vm_user} -gp #{@vm_pass} " +
        "copyFileFromHostToGuest \'#{@location}\' \'#{local_tempfile_path}\'" +
        " \'#{remote_tempfile_path}\'"
    system_command(vmrunstr)

    if @os == "linux"
      #
      # Now run the command directly on the guest (linux - call w/ /bin/sh)
      #
      vmrunstr = "vmrun -T ws -gu #{@vm_user} -gp #{@vm_pass} " + 
          "runProgramInGuest \'#{@location}\' /bin/sh #{remote_tempfile_path} > #{remote_output_file}"
      system_command(vmrunstr)
    else
      #
      # Now run the command directly on the guest (windows)
      #
      vmrunstr = "vmrun -T ws -gu #{@vm_user} -gp #{@vm_pass} " + 
          "runProgramInGuest \'#{@location}\' #{remote_tempfile_path} > #{remote_output_file}" 
      system_command(vmrunstr)
    end
    
    #
    # Cleanup. Delete it on the guest
    #
    vmrunstr = "vmrun -T ws -gu #{@vm_user} -gp #{@vm_pass} " + 
        "deleteFileInGuest \'#{@location}\' \'#{remote_tempfile_path}\'"
    system_command(vmrunstr)

    #
    # Delete it locally
    #
    local_delete_command = "rm #{local_tempfile_path}"
    system_command(local_delete_command)
  else

    #
    # Use SCP / SSH
    #

    if @os == "linux"
      
      #
      # Copy it over
      #
      scp_to(local_tempfile_path, remote_tempfile_path)

      #
      # And ... execute it
      #
      ssh_exec("/bin/sh #{remote_tempfile_path} > #{remote_output_file}")

      #
      # Now copy the output back to us
      #
      scp_from(remote_output_file, local_output_file)

      # Now, let's look at the output of the command
      output_string = File.open(local_output_file,"r").read

      #
      # And clean up
      #
      ssh_exec("rm #{remote_output_file}")
      ssh_exec("rm #{remote_tempfile_path}")
      
      `rm #{local_output_file}`

    else
      raise "Hey, no tools, and windows? can't do nuttin for ya man."
    end
    
  end
output_string
end