-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Relocate the service implementation code in to a separate class
Allows for alternative service managers
- Loading branch information
Graham Christensen
committed
Jan 27, 2017
1 parent
05c7a89
commit 06e7e11
Showing
4 changed files
with
90 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'hostservice/upstart' | ||
|
||
module Jetpants | ||
module HostService | ||
def self.pick_by_preflight(host) | ||
# We want to pick the first provider which the machine supports. Ruby 1.9.2 has no | ||
# `first_where` method like | ||
# [1, 2, 3].first { |i| i > 1 } == 2 | ||
# but the next line fakes it, by deleting items until we find one which does match, then | ||
# taking the first item in the array. | ||
# This could be a `.map` or a `.select` but we really don't want to try any more than we | ||
# have to. | ||
provider = all_providers.drop_while { |candidate| ! candidate.preflight(host) }.first | ||
raise "Cannot detect a valid service provider for #{host}" if provider.nil? | ||
|
||
return provider.new(host) | ||
end | ||
|
||
def self.all_providers | ||
# Service managers that we can support, in order of most to least likely | ||
[ | ||
Jetpants::HostService::Upstart, | ||
] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
module Jetpants | ||
module HostService | ||
class Upstart | ||
def self.preflight(host) | ||
host.has_installed('service') | ||
end | ||
|
||
def initialize(host) | ||
@host = host | ||
end | ||
|
||
def start(name, options=[]) | ||
service(:start, name, options.join(' ')) | ||
end | ||
|
||
def restart(name, options=[]) | ||
service(:restart, name, options.join(' ')) | ||
end | ||
|
||
def stop(name) | ||
service(:stop, name) | ||
end | ||
|
||
def running?(name) | ||
status = service(:status, name).downcase | ||
# the service is running if the output of "service #{name} status" doesn't include any of | ||
# these strings | ||
not_running_strings = ['not running', 'stop/waiting'] | ||
|
||
not_running_strings.none? {|str| status.include? str} | ||
end | ||
|
||
def service_direct(operation, name, options='') | ||
service(operation, name, options) | ||
end | ||
|
||
# Performs the given operation (:start, :stop, :restart, :status) for the | ||
# specified service (ie "mysql"). Requires that the "service" bin is in | ||
# root's PATH. | ||
# Please be aware that the output format and exit codes for the service | ||
# binary vary between Linux distros! | ||
def service(operation, name, options='') | ||
@host.ssh_cmd "service #{name} #{operation.to_s} #{options}".rstrip | ||
end | ||
end | ||
end | ||
end |