Class: RightScale::PowershellHost

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/windows/powershell_host.rb

Overview

This class is responsible for managing a Powershell process instance It allows running Powershell scripts in the associated instance and will log the script output.

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ PowershellHost

Start the Powershell process synchronously Set the instance variable :active to true once Powershell was successfully started

Parameters

options

Chef @node object

options

Associated Chef powershell provider name



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
# File 'lib/chef/windows/powershell_host.rb', line 36

def initialize(options = {})
  Log.debug(format_log_message("Initializing"))
  @node             = options[:node]
  @provider_name    = options[:provider_name]
  @pipe_name        = "#{@provider_name}_#{Time.now.strftime("%Y-%m-%d-%H%M%S")}"

  @response_mutex = Mutex.new
  @response_event = ConditionVariable.new

  Log.debug(format_log_message("Starting pipe server"))
  @pipe_server = RightScale::Windows::PowershellPipeServer.new(:pipe_name => @pipe_name) do |kind, payload|
    case kind
      when :is_ready then
        query
      when :respond then
        respond(payload)
    end
  end

  unless @pipe_server.start
    @pipe_server = nil
    return
  end

  Log.debug(format_log_message("Starting chef node server"))
  RightScale::Windows::ChefNodeServer.instance.start(:node => @node)

  Log.debug(format_log_message("Starting host"))
  start_powershell_process

  Log.debug(format_log_message("Initialized"))
end

Instance Method Details

#format_log_message(message) ⇒ Object



69
70
71
# File 'lib/chef/windows/powershell_host.rb', line 69

def format_log_message(message)
  "[PowershellHost #{@pipe_name}] - #{message}"
end

#run(script_path) ⇒ Object

Run Powershell script in associated Powershell process Log stdout and stderr to Chef logger

Argument

script_path(String)

Full path to Powershell script to be run

Return

res(Number)

The exit code of the script that was run. nil if no exit status was available.

Raise

RightScale::Exceptions:ApplicationError

If Powershell process is not running (i.e. :active is false)



84
85
86
87
88
89
# File 'lib/chef/windows/powershell_host.rb', line 84

def run(script_path)
  Log.debug(format_log_message("Running #{script_path}"))
  res = run_command("&\"#{script_path}\"")
  Log.debug(format_log_message("Finished #{script_path}"))
  res
end

#terminateObject

Terminate associated Powershell process :run cannot be called after :terminate This method is idempotent

Return

true

Always return true



97
98
99
100
101
102
103
# File 'lib/chef/windows/powershell_host.rb', line 97

def terminate
  Log.debug(format_log_message("Terminate requested"))
  res = run_command("break")
  Log.debug(format_log_message("Terminate signal sent"))

  true
end