Skip to content

Instantly share code, notes, and snippets.

@bjoernalbers
Created September 30, 2016 21:11
Show Gist options
  • Save bjoernalbers/f40b3fc17067959b9e4663be5021c1be to your computer and use it in GitHub Desktop.
Save bjoernalbers/f40b3fc17067959b9e4663be5021c1be to your computer and use it in GitHub Desktop.
osx_service - Ansible module to manage launchd services on Mac OS X
#!/usr/bin/ruby
# WANT_JSON
#
# osx_service - Ansible module to manage launchd services on Mac OS X
#
#
# The MIT License (MIT)
#
# Copyright (c) 2016 Björn Albers <bjoernalbers@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
require 'rubygems'
require 'json'
require 'open3'
class AnsibleModule
attr_reader :options, :changed
def initialize(options)
@options = options
@changed = false
check_options!
end
def run
apply_service_state
output
exit
rescue => e
output(msg: e.message, failed: true)
exit(1)
end
def output(opts = {})
print JSON.dump(output_hash.merge(opts))
end
def output_hash
options.merge(changed: changed)
end
def service
@service ||= Service.new(options[:name])
end
private
def check_options!
unless missing_options.empty?
raise ArgumentError, "Missing attributes: #{missing_options.join(', ')}"
end
end
def missing_options
[:name, :state].select { |key| options[key].nil? || options[key].empty? }
end
def apply_service_state
@changed =
case options.fetch(:state).to_sym
when :started then service.start
when :stopped then service.stop
when :restarted then service.restart
else raise ArgumentError, 'invalid state'
end
end
end
class Service
attr_reader :label
def initialize(label)
@label = label
end
def start
stopped? ? launchctl('load', '-w', plist).run! : false
end
def stop
started? ? launchctl('unload', '-w', plist).run! : false
end
def restart
stop
start
end
def started?
launchctl('list', label).run
end
def stopped?
!started?
end
private
def launchctl(*args)
Launchctl.new(*args)
end
def plist
@plist ||= File.join('/Library/LaunchDaemons', "#{label}.plist")
end
end
class Launchctl
class LaunchctlError < StandardError; end
COMMAND = '/bin/launchctl'
def initialize(*args)
raise LaunchctlError, 'Please run as root!' unless root?
@args = args
end
def run
run!
rescue LaunchctlError
false
end
def run!
@stdout, @stderr, @status = Open3.capture3(COMMAND, *@args)
raise LaunchctlError, error if failed?
true
end
def failed?
# NOTE: `launchctl` might exit successfully even when an error occurred.
# That's why we have to check STDERR too.
!@stderr.empty? || !@status.success?
end
def error
@stderr
end
private
def root?
Process.euid.zero?
end
end
begin
options = JSON.parse(File.read(ARGV.first), symbolize_names: true)
AnsibleModule.new(options).run
rescue => e
print JSON.dump(failed: true, msg: e.message)
exit(1)
end
@bjoernalbers
Copy link
Author

TODO: Convert to Python and ask to officially include it in ansible-modules-extras!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment