Module: TasteTester::Windows

Defined in:
lib/taste_tester/windows.rb

Instance Method Summary collapse

Instance Method Details

#find_cz_pidsObject

‘START` will also create a parent process for `cmd.exe` when `ruby.exe` is created, so we also need to make sure that we find those and kill them after the ruby process is terminated. Otherwise many orphaned `cmd.exe` windows could be left open in the aftermath.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/taste_tester/windows.rb', line 55

def find_cz_pids
  require 'wmi-lite'
  wmi = WmiLite::Wmi.new
  cz_process_query = %{
    SELECT
      ProcessID,
      ParentProcessID
    FROM
      Win32_Process
    WHERE
      CommandLine LIKE "%chef-zero%"
      AND
      Name = "ruby.exe"
  }
  wmi.query(cz_process_query).map do |process|
    [process['processid'], process['parentprocessid']]
  end
end

#nuke_all_cz_pidsObject

It is possible to have multiple chef-zero processes running which may mess up taste-tester’s state. So… nuke ‘em all.



76
77
78
79
80
81
# File 'lib/taste_tester/windows.rb', line 76

def nuke_all_cz_pids
  find_cz_pids.each do |pid, parentpid|
    ::Mixlib::ShellOut.new("taskkill /F /PID #{pid}").run_command
    ::Mixlib::ShellOut.new("taskkill /F /PID #{parentpid}").run_command
  end
end

#start_win_chef_zero_serverObject

We use START to spawn a subprocess of chef-zero since if you were to do this directly within a terminal session it will collide with the running chef-zero process when the user tries to input commands leading to many keystrokes being lost :(



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
# File 'lib/taste_tester/windows.rb', line 23

def start_win_chef_zero_server
  # `START` needs quotes around one of the arguments to function correctly.
  # rubocop:disable Lint/PercentStringArray
  cmd = %W{
    START "taste-tester"
    /MIN
    #{TasteTester::Config.chef_zero_path}
    --port #{@state.port}
  }.join(' ')
  # rubocop:enable Lint/PercentStringArray

  if TasteTester::Config.my_hostname
    cmd << " --host #{TasteTester::Config.my_hostname}"
  else
    cmd << " --host #{@addr}"
  end
  if TasteTester::Config.chef_zero_logging
    cmd << " --log-file #{@log_file} --log-level debug"
  end
  cmd << ' --ssl' if TasteTester::Config.use_ssl
  cmd << " --file-store #{@fsroot}" if TasteTester::Config.bundle

  # Mixlib::Shellout will always wait for a process to finish before
  # returning, so we use `spawn` instead.
  spawn(cmd)
  sleep(2)
end