Skip to content

Commit

Permalink
Enabling multiple init schemes and platforms for service resource
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 15f213281c01db0ccd5b3c7fe0992d4eeb4ae4ba
Author: Tyler Flint <[email protected]>
Date:   Thu Jun 11 15:05:10 2015 -0600

    runit admin is sv not svc

commit 9b7c78c55ee872f47b60e0c627c71a2bf6ce0e74
Author: Tyler Flint <[email protected]>
Date:   Thu Jun 11 15:03:16 2015 -0600

    adding in control logic for runit init scheme

commit df41cad9cffbdd5ccf22ffb81fd88b16e67ae012
Author: Tyler Flint <[email protected]>
Date:   Thu Jun 11 14:49:26 2015 -0600

    attempt to provide a general pattern for handling dynamic init schemes across multiple platforms

Authored by Tyler Flint <[email protected]>
Reviewed by Greg Linton <[email protected]>
  • Loading branch information
tylerflint committed Jun 11, 2015
1 parent a6f5e05 commit e5afadb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
1 change: 1 addition & 0 deletions lib/hooky/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ module Error
class UnexpectedExit < StandardError; end
class UnknownAction < StandardError; end
class UnsupportedPlatform < StandardError; end
class UnsupportedOption < StandardError; end
end
end
10 changes: 10 additions & 0 deletions lib/hooky/resource/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ def only_if(&block)
end
end

protected

def run_command!(cmd, expect_code=0)
`#{cmd}`
code = $?.exitstatus
if code != expect_code
raise Hooky::Error::UnexpectedExit, "#{cmd} failed with exit code '#{code}'"
end
end

end
end
end
51 changes: 40 additions & 11 deletions lib/hooky/resource/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@ class Service < Base

field :recursive
field :service_name
field :init

actions :enable, :disable, :start, :stop, :restart, :reload
default_action :enable

def initialize(name)
service_name(name) unless service_name

# if init scheme is not provided, try to set reasonable defaults
if not init
case platform.name
when 'smartos'
init(:smf)
when 'ubuntu'
init(:upstart)
end
end
end

def run(action)
Expand All @@ -32,26 +43,44 @@ def run(action)
protected

def enable!
run_command! "svcadm enable -s #{"-r" if recursive} #{service_name}"
case init
when :smf
run_command! "svcadm enable -s #{"-r" if recursive} #{service_name}"
when :runit
run_command! "sv start #{service_name}"
else
Hooky::Error::UnsupportedOption, "Unsupported init schema '#{init}'"
end
end

def disable!
run_command! "svcadm disable -s #{service_name}"
case init
when :smf
run_command! "svcadm disable -s #{service_name}"
when :runit
run_command! "sv stop #{service_name}"
else
Hooky::Error::UnsupportedOption, "Unsupported init schema '#{init}'"
end
end

def restart!
run_command! "svcadm restart #{service_name}"
case init
when :smf
run_command! "svcadm restart #{service_name}"
when :runit
disable!; enable!
else
Hooky::Error::UnsupportedOption, "Unsupported init schema '#{init}'"
end
end

def reload!
run_command! "svcadm refresh #{service_name}"
end

def run_command!(cmd, expect_code=0)
`#{cmd}`
code = $?.exitstatus
if code != expect_code
raise Hooky::Error::UnexpectedExit, "#{cmd} failed with exit code '#{code}'"
case init
when :smf
run_command! "svcadm refresh #{service_name}"
else
Hooky::Error::UnsupportedOption, "Unsupported init schema '#{init}'"
end
end

Expand Down

0 comments on commit e5afadb

Please sign in to comment.