Class: Chef::Provider::Service::Macosx
- Inherits:
-
Simple
- Object
- Chef::Provider
- Chef::Provider::Service
- Simple
- Chef::Provider::Service::Macosx
- Defined in:
- lib/chef/provider/service/macosx.rb
Constant Summary collapse
- PLIST_DIRS =
gather_plist_dirs
Instance Attribute Summary
Attributes inherited from Simple
Attributes inherited from Chef::Provider
#action, #current_resource, #logger, #new_resource, #recipe_name, #run_context
Class Method Summary collapse
Instance Method Summary collapse
- #define_resource_requirements ⇒ Object
- #disable_service ⇒ Object
-
#enable_service ⇒ Object
On OS/X, enabling a service has the side-effect of starting it, and disabling a service has the side-effect of stopping it.
- #load_current_resource ⇒ Object
- #load_service ⇒ Object
- #restart_service ⇒ Object
- #set_service_status ⇒ Object
- #shell_out_as_user(cmd) ⇒ Object
- #start_service ⇒ Object
- #stop_service ⇒ Object
- #this_version_or_newer?(this_version) ⇒ Boolean
- #unload_service ⇒ Object
Methods inherited from Simple
#reload_service, #shared_resource_requirements
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
Methods inherited from Chef::Provider
action, #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, #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 Mixin::LazyModuleInclude
#descendants, #include, #included
Methods included from Mixin::ShellOut
apply_default_env, maybe_add_timeout, #shell_out, #shell_out!
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 Mixin::PowershellExec
Methods included from DSL::Powershell
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::DataQuery
#data_bag, #data_bag_item, #search, #tagged?
Methods included from EncryptedDataBagItem::CheckEncrypted
Methods included from DSL::PlatformIntrospection
#older_than_win_2012_or_8?, #platform?, #platform_family?, #value_for_platform, #value_for_platform_family
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
Constructor Details
This class inherits a constructor from Chef::Provider::Service
Class Method Details
.gather_plist_dirs ⇒ Object
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_requirements ⇒ Object
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 107 |
# File 'lib/chef/provider/service/macosx.rb', line 76 def define_resource_requirements requirements.assert(:reload) do |a| a. Chef::Exceptions::UnsupportedAction, "#{self} does not support :reload" end requirements.assert(:all_actions) do |a| a.assertion { @plist_size < 2 } a. 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. 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. 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_service ⇒ Object
156 157 158 159 160 161 162 |
# File 'lib/chef/provider/service/macosx.rb', line 156 def disable_service unless @current_resource.enabled logger.trace("#{@new_resource} not enabled, not disabling") else unload_service end end |
#enable_service ⇒ Object
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.
148 149 150 151 152 153 154 |
# File 'lib/chef/provider/service/macosx.rb', line 148 def enable_service if @current_resource.enabled logger.trace("#{@new_resource} already enabled, not enabling") else load_service end end |
#load_current_resource ⇒ Object
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 74 |
# 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 # 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}'") cmd = "su " param = this_version_or_newer?("10.10") ? "" : "-l " param = "-l " if this_version_or_newer?("10.12") @base_user_cmd = cmd + param + "#{@console_user} -c" # 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_service ⇒ Object
164 165 166 167 168 |
# File 'lib/chef/provider/service/macosx.rb', line 164 def load_service session = @session_type ? "-S #{@session_type} " : "" cmd = "launchctl load -w " + session + @plist shell_out_as_user(cmd) end |
#restart_service ⇒ Object
133 134 135 136 137 138 139 140 141 |
# File 'lib/chef/provider/service/macosx.rb', line 133 def restart_service if @new_resource.restart_command super else unload_service sleep 1 load_service end end |
#set_service_status ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/chef/provider/service/macosx.rb', line 184 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 != 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
175 176 177 178 179 180 181 182 |
# File 'lib/chef/provider/service/macosx.rb', line 175 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_service ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/chef/provider/service/macosx.rb', line 109 def start_service if @current_resource.running logger.trace("#{@new_resource} already running, not starting") else if @new_resource.start_command super else load_service end end end |
#stop_service ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/chef/provider/service/macosx.rb', line 121 def stop_service unless @current_resource.running logger.trace("#{@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
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_service ⇒ Object
170 171 172 173 |
# File 'lib/chef/provider/service/macosx.rb', line 170 def unload_service cmd = "launchctl unload -w " + @plist shell_out_as_user(cmd) end |