Class: Wocker::Cli::ExecCommand

Inherits:
Clamp::Command
  • Object
show all
Defined in:
lib/wocker/cli/exec_command.rb

Instance Method Summary collapse

Instance Method Details

#executeObject



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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/wocker/cli/exec_command.rb', line 17

def execute
  best_guess_workdir = workdir || Wockerfile.workdir || "/vagrant"
  best_guess_workdir_cmdfied = best_guess_workdir.gsub("/", "\\")
  best_workdir_cmd = "C:\\#{best_guess_workdir_cmdfied}"

  cmd = command_list.join " "
  if detach?
    if workdir
      STDERR.puts "workdir not supported with detach"
      exit 1
    end

    interactive_flags = "-i 1" if interactive?

    psexec_wrapper_cmd = "psexec -accepteula -nobanner -d -s #{interactive_flags} #{cmd} >nul 2>&1"
    Wocker::Vagrant.run "winrm", "-s", "cmd", "-c", psexec_wrapper_cmd
  elsif interactive?
    File.unlink "wocker-exec-out" if File.exist? "wocker-exec-out"

    psexec_wrapper_cmd = "psexec -accepteula -nobanner -s -i 1 cmd.exe /C \"cd #{best_workdir_cmd} & #{cmd} >C:\\vagrant\\wocker-exec-out 2>&1\" >nul 2>&1"

    psexec_thr = Thread.new do
      Wocker::Vagrant.run "winrm", "-s", "cmd", "-c", psexec_wrapper_cmd
    end
    FileUtils.touch "wocker-exec-out"
    log_tail_k = Kommando.run_async "tail -f wocker-exec-out", output: true

    require 'io/console'
    loop do
      c = STDIN.getch
      case c
      when "\u0003"
        `wocker keyboard key control c`
        # workarounds Terminate Batch Job Y/N
        sleep 0.1
        `wocker keyboard key control c`
        break
      when "\r"
        `wocker keyboard key enter`
      else
        `wocker keyboard write #{c}`
      end

      sleep 0.001
    end

    #psexec_thr.join
    psexec_thr.kill
    log_tail_k.kill

    File.unlink "wocker-exec-out" if File.exist? "wocker-exec-out"

  else
    Wocker::Vagrant.run "winrm", "-c", "cd #{best_guess_workdir}; #{cmd}"
  end
end