Class: Chef::Provider::Service::Macosx

Inherits:
Simple show all
Defined in:
lib/chef/provider/service/macosx.rb

Constant Summary collapse

PLIST_DIRS =
gather_plist_dirs

Constants included from Mixin::ShellOut

Mixin::ShellOut::DEPRECATED_OPTIONS

Instance Attribute Summary

Attributes inherited from Simple

#status_load_success

Attributes inherited from Chef::Provider

#action, #cookbook_name, #current_resource, #new_resource, #recipe_name, #run_context

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Simple

#reload_service, #shared_resource_requirements, #whyrun_supported?

Methods inherited from Chef::Provider::Service

#action_disable, #action_enable, #action_mask, #action_reload, #action_restart, #action_start, #action_stop, #action_unmask, #initialize, #load_new_resource_state, #mask_service, #reload_service, #shared_resource_requirements, #supports, #unmask_service, #user_services_requirements, #whyrun_supported?

Methods included from Mixin::Command

#chdir_or_tmpdir, #handle_command_failures, #output_of_command, #run_command, #run_command_and_return_stdout_stderr, #run_command_with_systems_locale

Methods included from Mixin::Command::Windows

#popen4

Methods included from Mixin::Command::Unix

#popen4

Methods inherited from Chef::Provider

#action_nothing, #check_resource_semantics!, #cleanup_after_converge, #converge_by, #converge_if_changed, #events, include_resource_dsl, include_resource_dsl_module, #initialize, #node, #process_resource_requirements, provides, provides?, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, supports?, use_inline_resources, #whyrun_mode?, #whyrun_supported?

Methods included from Mixin::Provides

#provided_as, #provides, #provides?

Methods included from Mixin::DescendantsTracker

#descendants, descendants, direct_descendants, #direct_descendants, find_descendants_by_name, #find_descendants_by_name, #inherited, store_inherited

Methods included from DeprecatedLWRPClass

#const_missing, #deprecated_constants, #register_deprecated_lwrp_class

Methods included from Mixin::LazyModuleInclude

#descendants, #include, #included

Methods included from Mixin::NotifyingBlock

#notifying_block, #subcontext_block

Methods included from DSL::DeclareResource

#build_resource, #declare_resource, #delete_resource, #delete_resource!, #edit_resource, #edit_resource!, #find_resource, #find_resource!, #with_run_context

Methods included from Mixin::ShellOut

#run_command_compatible_options, #shell_out, #shell_out!, #shell_out_with_systems_locale, #shell_out_with_systems_locale!

Methods included from Mixin::PowershellOut

#powershell_out, #powershell_out!

Methods included from Mixin::WindowsArchitectureHelper

#assert_valid_windows_architecture!, #disable_wow64_file_redirection, #forced_32bit_override_required?, #is_i386_process_on_x86_64_windows?, #node_supports_windows_architecture?, #node_windows_architecture, #restore_wow64_file_redirection, #valid_windows_architecture?, #with_os_architecture, #wow64_architecture_override_required?, #wow64_directory

Methods included from DSL::PlatformIntrospection

#docker?, #platform?, #platform_family?, #value_for_platform, #value_for_platform_family

Constructor Details

This class inherits a constructor from Chef::Provider::Service

Class Method Details

.gather_plist_dirsObject



34
35
36
37
38
39
40
41
# File 'lib/chef/provider/service/macosx.rb', line 34

def self.gather_plist_dirs
  locations = %w{/Library/LaunchAgents
                 /Library/LaunchDaemons
                 /System/Library/LaunchAgents
                 /System/Library/LaunchDaemons }
  Chef::Util::PathHelper.home("Library", "LaunchAgents") { |p| locations << p }
  locations
end

Instance Method Details

#define_resource_requirementsObject



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
105
106
# File 'lib/chef/provider/service/macosx.rb', line 75

def define_resource_requirements
  requirements.assert(:reload) do |a|
    a.failure_message Chef::Exceptions::UnsupportedAction, "#{self} does not support :reload"
  end

  requirements.assert(:all_actions) do |a|
    a.assertion { @plist_size < 2 }
    a.failure_message Chef::Exceptions::Service, "Several plist files match service name. Please use full service name."
  end

  requirements.assert(:all_actions) do |a|
    a.assertion { ::File.exists?(@plist.to_s) }
    a.failure_message Chef::Exceptions::Service,
      "Could not find plist for #{@new_resource}"
  end

  requirements.assert(:enable, :disable) do |a|
    a.assertion { !@service_label.to_s.empty? }
    a.failure_message Chef::Exceptions::Service,
      "Could not find service's label in plist file '#{@plist}'!"
  end

  requirements.assert(:all_actions) do |a|
    a.assertion { @plist_size > 0 }
    # No failure here in original code - so we also will not
    # fail. Instead warn that the service is potentially missing
    a.whyrun "Assuming that the service would have been previously installed and is currently disabled." do
      @current_resource.enabled(false)
      @current_resource.running(false)
    end
  end
end

#disable_serviceObject



155
156
157
158
159
160
161
# File 'lib/chef/provider/service/macosx.rb', line 155

def disable_service
  unless @current_resource.enabled
    Chef::Log.debug("#{@new_resource} not enabled, not disabling")
  else
    unload_service
  end
end

#enable_serviceObject

On OS/X, enabling a service has the side-effect of starting it, and disabling a service has the side-effect of stopping it.

This makes some sense on OS/X since launchctl is an “init”-style supervisor that will restart daemons that are crashing, etc.



147
148
149
150
151
152
153
# File 'lib/chef/provider/service/macosx.rb', line 147

def enable_service
  if @current_resource.enabled
    Chef::Log.debug("#{@new_resource} already enabled, not enabling")
  else
    load_service
  end
end

#load_current_resourceObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/chef/provider/service/macosx.rb', line 49

def load_current_resource
  @current_resource = Chef::Resource::MacosxService.new(@new_resource.name)
  @current_resource.service_name(@new_resource.service_name)
  @plist_size = 0
  @plist = @new_resource.plist ? @new_resource.plist : find_service_plist
  @service_label = find_service_label
  # LauchAgents should be loaded as the console user.
  @console_user = @plist ? @plist.include?("LaunchAgents") : false
  @session_type = @new_resource.session_type

  if @console_user
    @console_user = Etc.getlogin
    Chef::Log.debug("#{new_resource} console_user: '#{@console_user}'")
    cmd = "su "
    param = this_version_or_newer?("10.10") ? "" : "-l "
    @base_user_cmd = cmd + param + "#{@console_user} -c"
    # Default LauchAgent session should be Aqua
    @session_type = "Aqua" if @session_type.nil?
  end

  Chef::Log.debug("#{new_resource} Plist: '#{@plist}' service_label: '#{@service_label}'")
  set_service_status

  @current_resource
end

#load_serviceObject



163
164
165
166
167
# File 'lib/chef/provider/service/macosx.rb', line 163

def load_service
  session = @session_type ? "-S #{@session_type} " : ""
  cmd = "launchctl load -w " + session + @plist
  shell_out_as_user(cmd)
end

#restart_serviceObject



132
133
134
135
136
137
138
139
140
# File 'lib/chef/provider/service/macosx.rb', line 132

def restart_service
  if @new_resource.restart_command
    super
  else
    unload_service
    sleep 1
    load_service
  end
end

#set_service_statusObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/chef/provider/service/macosx.rb', line 183

def set_service_status
  return if @plist == nil || @service_label.to_s.empty?

  cmd = "launchctl list #{@service_label}"
  res = shell_out_as_user(cmd)

  if res.exitstatus == 0
    @current_resource.enabled(true)
  else
    @current_resource.enabled(false)
  end

  if @current_resource.enabled
    res.stdout.each_line do |line|
      case line.downcase
      when /\s+\"pid\"\s+=\s+(\d+).*/
        pid = $1
        @current_resource.running(!pid.to_i.zero?)
        Chef::Log.debug("Current PID for #{@service_label} is #{pid}")
      end
    end
  else
    @current_resource.running(false)
  end
end

#shell_out_as_user(cmd) ⇒ Object



174
175
176
177
178
179
180
181
# File 'lib/chef/provider/service/macosx.rb', line 174

def shell_out_as_user(cmd)
  if @console_user
    shell_out_with_systems_locale("#{@base_user_cmd} '#{cmd}'")
  else
    shell_out_with_systems_locale(cmd)

  end
end

#start_serviceObject



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/chef/provider/service/macosx.rb', line 108

def start_service
  if @current_resource.running
    Chef::Log.debug("#{@new_resource} already running, not starting")
  else
    if @new_resource.start_command
      super
    else
      load_service
    end
  end
end

#stop_serviceObject



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/chef/provider/service/macosx.rb', line 120

def stop_service
  unless @current_resource.running
    Chef::Log.debug("#{@new_resource} not running, not stopping")
  else
    if @new_resource.stop_command
      super
    else
      unload_service
    end
  end
end

#this_version_or_newer?(this_version) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/chef/provider/service/macosx.rb', line 45

def this_version_or_newer?(this_version)
  Gem::Version.new(node["platform_version"]) >= Gem::Version.new(this_version)
end

#unload_serviceObject



169
170
171
172
# File 'lib/chef/provider/service/macosx.rb', line 169

def unload_service
  cmd = "launchctl unload -w " + @plist
  shell_out_as_user(cmd)
end