Created — forked from raggi/launchd.rb

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

A first stab at a launchd provider for chef

View launchd.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
class Chef::Provider::Service::Launchd < Chef::Provider::Service::Simple
 
LAUNCHCTL_FORMAT = /([\d-]+)\s+([\d-]+)\s+([[:print:]]+)/
 
def initialize(new_resource, run_context)
raise ArgumentError, "run_context cannot be nil" unless run_context
super
 
run_context.node
end
 
def load_current_resource
@current_resource = Chef::Resource::Service.new(@new_resource.name)
@current_resource.service_name(@new_resource.service_name)
 
if @new_resource.status_command
Chef::Log.debug("#{@new_resource} you have specified a status command, running..")
 
begin
if run_command_with_systems_locale(:command => @new_resource.status_command) == 0
@current_resource.running true
end
rescue Chef::Exceptions::Exec
@current_resource.running false
nil
end
else
pid = launchctl_pid
 
if pid
@current_resource.enabled true
if pid > 0
@current_resource.running true
else
@current_resource.running false
end
else
@current_resource.enabled false
end
end
 
@current_resource
end
 
def start_service
if @current_resource.running
Chef::Log.debug("#{@new_resource}: Already running, not starting")
else
if @new_resource.start_command
super
else
file = launchctl_file
run_command_with_systems_locale(:command => "launchctl load #{file}")
run_command_with_systems_locale(:command => "launchctl start #{launchctl_name}")
end
end
end
 
def stop_service
unless @current_resource.running
Chef::Log.debug("#{@new_resource}: Not running, not stopping")
else
if @new_resource.stop_command
super
else
file = launchctl_file
run_command_with_systems_locale(:command => "launchctl unload #{file}")
end
end
end
 
def restart_service
if @new_resource.restart_command
super
else
run_command_with_systems_locale(:command => "launchctl stop #{launchctl_name}")
end
end
 
def reload_service
if @new_resource.reload_command
super
else
pid = launchctl_pid
run_command_with_systems_locale(:command => "kill -HUP #{pid}") if pid
end
end
 
def enable_service
# Macports integration:
run_command_with_systems_locale(:command => "port load #{@new_resource.service_name}") rescue nil
file = launchctl_file
puts "==========- #{@new_resource.service_name} -================="
puts "=========== #{file} ======================"
run_command_with_systems_locale(:command => "launchctl load #{file}")
end
 
def disable_service
file = launchctl_file
run_command_with_systems_locale(:command => "launchctl unload #{file}")
# Macports integration:
run_command_with_systems_locale(:command => "port unload #{@new_resource.service_name}") rescue nil
end
 
# A pid or 0 if not running
def launchctl_pid
command = "launchctl list | grep #{@new_resource.service_name}"
status = popen4(command) do |pid, stdin, stdout, stderr|
stdout.each_line do |line|
line =~ LAUNCHCTL_FORMAT
pid, status, name = Regexp.last_match.captures
return pid.to_i if name =~ /#{Regexp.escape @new_resource.service_name}$/
end
end
nil
end
def launchctl_name
command = "launchctl list | grep #{@new_resource.service_name}"
status = popen4(command) do |pid, stdin, stdout, stderr|
stdout.each_line do |line|
line =~ LAUNCHCTL_FORMAT
pid, status, name = Regexp.last_match.captures
return name if name =~ /#{Regexp.escape @new_resource.service_name}$/
end
end
end
 
def launchctl_file
Dir["{#{::File.expand_path('~')}/,/}Library/{LaunchDaemons,LaunchAgents}/*#{@new_resource.service_name}.plist"].first
end
 
end
 
Chef::Platform.set :platform => :mac_os_x, :resource => :service, :provider => Chef::Provider::Service::Launchd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.