-
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.
Merge pull request #216 from Tumblr/service-api-breakout
Support Systemd
- Loading branch information
Showing
6 changed files
with
143 additions
and
22 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
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,38 @@ | ||
require 'hostservice/upstart' | ||
require 'hostservice/systemd' | ||
|
||
module Jetpants | ||
module HostService | ||
def self.pick_by_preflight(host) | ||
# In /proc/1/stat we get to see the name of the init process (ie: pid1) so | ||
# we can best match it to our providers. | ||
# | ||
# We expect to see something like: | ||
# 1 (process_name) S 0 1 1 0 -1 4202752 2925 7358649115 10 1047 604 1079 [...snipped...] | ||
# | ||
# on upstart, we'd see: | ||
# 1 (init) S 0 1 1 0 -1 4202752 2925 7358649115 10 1047 604 1079 [...snipped...] | ||
# | ||
# on systemd we'd see: | ||
# 1 (systemd) S 0 1 1 0 -1 4219136 2502297 91460739 50 3010 3771 3113 [...snipped...] | ||
# | ||
# For more information, see proc(5) (`man proc`) and look for `/proc/[pid]/stat`. | ||
pid1_match = host.ssh_cmd('cat /proc/1/stat').match(/^1 \((.*?)\) /) | ||
throw "/proc/1/stat isn't matching, something is wrong!" if pid1_match.nil? | ||
pid1_name = pid1_match[1] | ||
|
||
provider = all_providers.find { |candidate| candidate.preflight(host, pid1_name) } | ||
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, | ||
Jetpants::HostService::Systemd, | ||
] | ||
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,41 @@ | ||
module Jetpants | ||
module HostService | ||
class Systemd | ||
def self.preflight(host, pid1_name) | ||
pid1_name == "systemd" | ||
end | ||
|
||
def initialize(host) | ||
@host = host | ||
end | ||
|
||
def start(name, options=[]) | ||
service(:start, name, options) | ||
end | ||
|
||
def restart(name, options=[]) | ||
service(:restart, name, options) | ||
end | ||
|
||
def stop(name) | ||
service(:stop, name) | ||
end | ||
|
||
def running?(name) | ||
status = service(:status, name).downcase | ||
|
||
return !status.include?("active: inactive") | ||
end | ||
|
||
def service_direct(operation, name, options='') | ||
service(operation, name, [options]) | ||
end | ||
|
||
def service(operation, name, options=[]) | ||
raise "Systemd doesn't support options! :( (Passed: #{options.join(' ')})" unless options.empty? | ||
|
||
@host.ssh_cmd "systemctl #{operation.to_s} #{name}".rstrip | ||
end | ||
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, pid1_name) | ||
pid1_name == "init" && 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 |