Module: Kitchen::Localhost::ShellOut

Includes:
ShellOut
Included in:
Driver::Localhost, Platform::Localhost, Transport::Localhost::Connection
Defined in:
lib/kitchen/localhost/shell_out.rb

Overview

Take Kitchen's ShellOut module and add support for Windows Powershell.

Author:

Instance Method Summary collapse

Instance Method Details

#cp_r(source, dest) ⇒ Object

Do a recursive copy of one path to another, while running the paths through PowerShell on Windows platforms to translate any PSH variables.

Parameters:

  • source (String)

    a source path

  • dest (String)

    a destination path


73
74
75
76
77
78
79
# File 'lib/kitchen/localhost/shell_out.rb', line 73

def cp_r(source, dest)
  if windows_os?
    source = run_command_psh("\"#{source}\"").strip
    dest = run_command_psh("\"#{dest}\"").strip
  end
  FileUtils.cp_r(source, dest)
end

#mkdir_p(path) ⇒ Object

Do a recursive mkdir of a given path, while running the path through PowerShell on Windows platforms to translate any PSH variables.

Parameters:

  • path (String)

    a directory to create


98
99
100
101
# File 'lib/kitchen/localhost/shell_out.rb', line 98

def mkdir_p(path)
  path = run_command_psh("\"#{path}\"").strip if windows_os?
  FileUtils.mkdir_p(path)
end

#rm_rf(path) ⇒ Object

Do a recursive delete of a given path, while running the path through PowerShell on Windows platforms to translate any PSH variables.

Parameters:

  • path (String)

    a directory to create


87
88
89
90
# File 'lib/kitchen/localhost/shell_out.rb', line 87

def rm_rf(path)
  path = run_command_psh("\"#{path}\"").strip if windows_os?
  FileUtils.rm_rf(path)
end

#run_command(cmd) ⇒ Object

Run a given command through the regular shell on any *nix systems or PowerShell on Windows.

Parameters:

  • cmd (String)

    a command to run


41
42
43
# File 'lib/kitchen/localhost/shell_out.rb', line 41

def run_command(cmd)
  windows_os? ? run_command_psh(cmd) : run_command_sh(cmd)
end

#run_command_psh(cmd) ⇒ Object

Run a given command through PowerShell, accounting for quotes and other difficult-to-account for characters by writing the command out to a temp file, executing it, and cleaning up after.

Parameters:

  • cmd (String)

    a PowerShell command or script to run

Raises:

  • (ShellCommandFailed)

    if the command exits non-zero


54
55
56
57
58
59
60
61
62
63
64
# File 'lib/kitchen/localhost/shell_out.rb', line 54

def run_command_psh(cmd)
  script = Tempfile.new(%w(kitchen-localhost .ps1))
  script.write(cmd)
  script.close
  begin
    res = run_command_sh("powershell #{script.path}")
  ensure
    script.unlink
  end
  res
end

#run_command_shObject


33
# File 'lib/kitchen/localhost/shell_out.rb', line 33

alias_method :run_command_sh, :run_command

#windows_os?TrueClass, FalseClass

Check whether the localhost instance is Windows

Returns:

  • (TrueClass, FalseClass)

    whether localhost is Windows


108
109
110
# File 'lib/kitchen/localhost/shell_out.rb', line 108

def windows_os?
  !RUBY_PLATFORM.match(/mswin|mingw32|windows/).nil?
end