Class: Chef::Provider::Powershell

Inherits:
Execute
  • Object
show all
Defined in:
lib/chef/providers/windows/powershell_provider.rb

Overview

Powershell chef provider.

Constant Summary collapse

SCRIPT_TEMP_DIR_PATH =

use a unique dir name instead of cluttering temp directory with leftover scripts like the original script provider.

::File.join(::Dir.tmpdir, "chef-powershell-06D9AC00-8D64-4213-A46A-611FBAFB4426")

Instance Method Summary collapse

Instance Method Details

#action_runObject

runs the PowerShell script in a child process.

Return

true

Always return true

Raise

RightScale::Exceptions::Exec

Invalid process exit status



53
54
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
# File 'lib/chef/providers/windows/powershell_provider.rb', line 53

def action_run
  nickname         = @new_resource.name
  source           = @new_resource.source
  script_file_path = @new_resource.source_path
  environment      = @new_resource.environment
  current_state    = all_state

  # 1. Write script source into file, if necessary.
  if script_file_path
    (raise RightScale::Exceptions::Exec, "Missing script file \"#{script_file_path}\"") unless ::File.file?(script_file_path)
  else
    FileUtils.mkdir_p(SCRIPT_TEMP_DIR_PATH)
    script_file_path = ::File.join(SCRIPT_TEMP_DIR_PATH, "powershell_provider_source.ps1")
    ::File.open(script_file_path, "w") { |f| f.write(source) }
  end

  begin
    # 2. Setup environment.
    environment = {} if environment.nil?
    environment['RS_ALREADY_RUN'] = current_state[:chef_state].past_scripts.include?(nickname) ? '1' : nil
    environment['RS_REBOOT'] = current_state[:cook_state].reboot?
    @new_resource.environment(environment)

    # 3. execute and wait
    RightScale::Windows::ChefNodeServer.instance.start(:node => node)

    # new_resource points to the powershell script resource in this limited
    # context. there is no current resource in script execution context.
    RightScale::Windows::ChefNodeServer.instance.new_resource = @new_resource

    command = format_command(script_file_path)
    @new_resource.command(command)
    ::Chef::Log.info("Running \"#{nickname}\"")
    super

    # super provider raises an exception on failure, so record success at
    # this point.
    current_state[:chef_state].record_script_execution(nickname)
    @new_resource.updated_by_last_action(true)

    # a script may have requested reboot via rs_shutdown command line
    # tool. if the script requested immediate shutdown then we must call
    # exit here to interrupt the Chef converge (assuming any subsequent
    # boot recipes are pending). otherwise, defer shutdown until scripts/
    # recipes finish or another script escalates to an immediate shutdown.
    exit 0 if RightScale::ShutdownRequestProxy.instance.immediately?
  ensure
    (FileUtils.rm_rf(SCRIPT_TEMP_DIR_PATH) rescue nil) if ::File.directory?(SCRIPT_TEMP_DIR_PATH)
  end

  true
end

#load_current_resourceObject

No concept of a ‘current’ resource for Powershell execution, this is a no-op

Return

true

Always return true



42
43
44
# File 'lib/chef/providers/windows/powershell_provider.rb', line 42

def load_current_resource
  true
end