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

Instance Attribute Summary

Attributes inherited from Simple

#status_load_success

Attributes inherited from Chef::Provider

#action, #after_resource, #current_resource, #logger, #new_resource, #run_context

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Simple

#reload_service, #shared_resource_requirements

Methods inherited from Chef::Provider::Service

#initialize, #load_new_resource_state, #mask_service, #reload_service, #shared_resource_requirements, #supports, #unmask_service, #user_services_requirements

Methods included from Chef::Platform::ServiceHelpers

#config_for_service, #service_resource_providers

Methods inherited from Chef::Provider

action, action_description, action_descriptions, #action_nothing, #check_resource_semantics!, #cleanup_after_converge, #compile_and_converge_action, #converge_by, #converge_if_changed, #cookbook_name, #description, #events, include_resource_dsl?, include_resource_dsl_module, #initialize, #introduced, #load_after_resource, #node, #process_resource_requirements, provides, provides?, #recipe_name, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, supports?, use, use_inline_resources, #validate_required_properties!, #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 Mixin::LazyModuleInclude

#descendants, #include, #included

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::Secret

#default_secret_config, #default_secret_service, #secret, #with_secret_config, #with_secret_service

Methods included from DSL::RenderHelpers

#render_json, #render_toml, #render_yaml

Methods included from DSL::ReaderHelpers

#parse_file, #parse_json, #parse_toml, #parse_yaml

Methods included from DSL::Powershell

#ps_credential

Methods included from DSL::RegistryHelper

#registry_data_exists?, #registry_get_subkeys, #registry_get_values, #registry_has_subkeys?, #registry_key_exists?, #registry_value_exists?

Methods included from DSL::ChefVault

#chef_vault, #chef_vault_item, #chef_vault_item_for_environment

Methods included from DSL::DataQuery

#data_bag, #data_bag_item, #search, #tagged?

Methods included from EncryptedDataBagItem::CheckEncrypted

#encrypted?

Methods included from DSL::PlatformIntrospection

#older_than_win_2012_or_8?, #platform?, #platform_family?, #value_for_platform, #value_for_platform_family

Methods included from DSL::Recipe

#exec, #have_resource_class_for?, #resource_class_for

Methods included from DSL::Definitions

add_definition, #evaluate_resource_definition, #has_resource_definition?

Methods included from DSL::Resources

add_resource_dsl, remove_resource_dsl

Methods included from DSL::Cheffish

load_cheffish

Methods included from DSL::RebootPending

#reboot_pending?

Methods included from DSL::IncludeRecipe

#include_recipe, #load_recipe

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!, #resources, #with_run_context

Methods included from DSL::Compliance

#include_input, #include_profile, #include_waiver

Constructor Details

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

Class Method Details

.gather_plist_dirsObject



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

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



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/provider/service/macosx.rb', line 73

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.exist?(@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



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

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

#enable_serviceObject

On macOS, 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 macOS since launchctl is an "init"-style supervisor that will restart daemons that are crashing, etc.

FIXME: Does this make any sense at all? The difference between enabled and running as state would seem to only be useful for completely broken services (enabled, not restarting, but not running => totally broken?).

It seems like otherwise :enable is equivalent to :start, and :disable is equivalent to :stop? But just with strangely different behavior in the face of a broken service?



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

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

#load_current_resourceObject



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

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 || find_service_plist
  @service_label = find_service_label
  # LaunchAgents 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.getpwuid(::File.stat("/dev/console").uid).name
    logger.trace("#{new_resource} console_user: '#{@console_user}'")

    @base_user_cmd = "su -l #{@console_user} -c"
    logger.trace("#{new_resource} base_user_cmd: '#{@base_user_cmd}'")

    # Default LaunchAgent session should be Aqua
    @session_type = "Aqua" if @session_type.nil?
  end

  logger.trace("#{new_resource} Plist: '#{@plist}' service_label: '#{@service_label}'")
  set_service_status

  @current_resource
end

#load_serviceObject



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

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

#restart_serviceObject



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

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

#set_service_statusObject



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/chef/provider/service/macosx.rb', line 190

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

  cmd = "/bin/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 != 0)
        logger.trace("Current PID for #{@service_label} is #{pid}")
      end
    end
  else
    @current_resource.running(false)
  end
end

#shell_out_as_user(cmd) ⇒ Object



181
182
183
184
185
186
187
188
# File 'lib/chef/provider/service/macosx.rb', line 181

def shell_out_as_user(cmd)
  if @console_user
    shell_out("#{@base_user_cmd} '#{cmd}'", default_env: false)
  else
    shell_out(cmd, default_env: false)

  end
end

#start_serviceObject



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

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

#stop_serviceObject



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

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

#unload_serviceObject



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

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