Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/linux_admin/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ def initialize(name)
private

def self.service_type_uncached
Common.cmd?(:systemctl) ? SystemdService : SysVInitService
{
:systemctl => SystemdService,
:service => SysVInitService,
:brew => BrewService,
}.each do |key, service|
return service if Common.cmd?(key)
end
nil
end
private_class_method :service_type_uncached
end
Expand Down
54 changes: 54 additions & 0 deletions lib/linux_admin/service/brew_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# s = LinuxAdmin::Service.new("postgresql")
begin
require 'bundler'
rescue LoaderException
end

module LinuxAdmin
class BrewService < Service
def running?
# brew services list shows all the services running / installed
::Bundler.with_clean_env {
Common.run(Common.cmd(:brew), :params => %w(services list))
.output.split("\n").any? { |x| x.starts_with?("#{name} ") }
}
end

def enable
run_cmd!("start", name)
end

def disable
run_cmd!("stop", name)
end

def start
run_cmd!("start", name)
end

def stop
run_cmd!("stop", name)
end

def restart
# attempt to manually stop/start if restart fails
unless run_cmd("restart", name)
stop
start
end

self
end

private

def run_cmd(*actions)
::Bundler.with_clean_env { Common.run(Common.cmd(:brew), :params => %w(services) + actions).success? }
end

def run_cmd!(*actions)
::Bundler.with_clean_env { Common.run!(Common.cmd(:brew), :params => %w(services) + actions) }
self
end
end
end
34 changes: 17 additions & 17 deletions lib/linux_admin/service/sys_v_init_service.rb
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
module LinuxAdmin
class SysVInitService < Service
def running?
Common.run(Common.cmd(:service),
:params => {nil => [name, "status"]}).exit_status == 0
run_cmd(name, "status")
end

def enable
Common.run!(Common.cmd(:chkconfig),
:params => {nil => [name, "on"]})
Common.run!(Common.cmd(:chkconfig), :params => [name, "on"])
self
end

def disable
Common.run!(Common.cmd(:chkconfig),
:params => {nil => [name, "off"]})
Common.run!(Common.cmd(:chkconfig), :params => [name, "off"])
self
end

def start
Common.run!(Common.cmd(:service),
:params => {nil => [name, "start"]})
self
run_cmd!(name, "start")
end

def stop
Common.run!(Common.cmd(:service),
:params => {nil => [name, "stop"]})
self
run_cmd!(name, "stop")
end

def restart
status =
Common.run(Common.cmd(:service),
:params => {nil => [name, "restart"]}).exit_status

# attempt to manually stop/start if restart fails
if status != 0
unless run_cmd(name, "restart")
self.stop
self.start
end

self
end

private

def run_cmd(*actions)
Common.run(Common.cmd(:service), :params => actions).success?
end

def run_cmd!(*actions)
Common.run!(Common.cmd(:service), :params => actions)
self
end
end
end
36 changes: 17 additions & 19 deletions lib/linux_admin/service/systemd_service.rb
Original file line number Diff line number Diff line change
@@ -1,46 +1,44 @@
module LinuxAdmin
class SystemdService < Service
def running?
Common.run(Common.cmd(:systemctl),
:params => {nil => ["status", name]}).exit_status == 0
run_cmd("status", name)
end

def enable
Common.run!(Common.cmd(:systemctl),
:params => {nil => ["enable", name]})
self
run_cmd!("enable", name)
end

def disable
Common.run!(Common.cmd(:systemctl),
:params => {nil => ["disable", name]})
self
run_cmd!("disable", name)
end

def start
Common.run!(Common.cmd(:systemctl),
:params => {nil => ["start", name]})
self
run_cmd!("start", name)
end

def stop
Common.run!(Common.cmd(:systemctl),
:params => {nil => ["stop", name]})
self
run_cmd!("stop", name)
end

def restart
status =
Common.run(Common.cmd(:systemctl),
:params => {nil => ["restart", name]}).exit_status

# attempt to manually stop/start if restart fails
if status != 0
unless run_cmd("restart", name)
stop
start
end

self
end

private

def run_cmd(*actions)
Common.run(Common.cmd(:systemctl), :params => actions).success?
end

def run_cmd!(*actions)
Common.run!(Common.cmd(:systemctl), :params => actions)
self
end
end
end
100 changes: 100 additions & 0 deletions spec/service/brew_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
describe LinuxAdmin::BrewService do
before do
@service = described_class.new 'foo'
allow(LinuxAdmin::Common).to receive(:cmd).with(:brew).and_return("brew")
end

describe "#running?" do
it "checks service" do
expect(LinuxAdmin::Common).to receive(:run)
.with("brew",
:params => %w(services list)).and_return(double(:output => "foo params running\nother param running\n"))
@service.running?
end

it "returns true when service is running" do
expect(LinuxAdmin::Common).to receive(:run)
.and_return(double(:output => "foo param file\nother params running\n"))
expect(@service).to be_running
end

it "returns false when service is not running" do
expect(LinuxAdmin::Common).to receive(:run).and_return(double(:output => "other\n"))
expect(@service).not_to be_running
end

it "returns false when service is not running (even though name is close" do
expect(LinuxAdmin::Common).to receive(:run)
.and_return(double(:output => "foo2 param running\nother param running\n"))
expect(@service).not_to be_running
end
end

describe "#enable" do
it "enables service" do
expect(LinuxAdmin::Common).to receive(:run!).with("brew", :params => %w(services start foo))
@service.enable
end

it "returns self" do
expect(LinuxAdmin::Common).to receive(:run!) # stub out cmd invocation
expect(@service.enable).to eq(@service)
end
end

describe "#disable" do
it "stops the service" do
expect(LinuxAdmin::Common).to receive(:run!).with("brew", :params => %w(services stop foo))
@service.disable
end

it "returns self" do
expect(LinuxAdmin::Common).to receive(:run!)
expect(@service.disable).to eq(@service)
end
end

describe "#start" do
it "starts service" do
expect(LinuxAdmin::Common).to receive(:run!).with("brew", :params => %w(services start foo))
@service.start
end

it "returns self" do
expect(LinuxAdmin::Common).to receive(:run!)
expect(@service.start).to eq(@service)
end
end

describe "#stop" do
it "stops service" do
expect(LinuxAdmin::Common).to receive(:run!).with("brew", :params => %w(services stop foo))
@service.stop
end

it "returns self" do
expect(LinuxAdmin::Common).to receive(:run!)
expect(@service.stop).to eq(@service)
end
end

describe "#restart" do
it "restarts service" do
expect(LinuxAdmin::Common).to receive(:run)
.with("brew", :params => %w(services restart foo)).and_return(double(:success? => true))
@service.restart
end

it "manually stops then starts service when restart fails" do
expect(LinuxAdmin::Common).to receive(:run).and_return(double(:success? => false))
expect(@service).to receive(:stop)
expect(@service).to receive(:start)
@service.restart
end

it "returns self" do
expect(LinuxAdmin::Common).to receive(:run).and_return(double(:success? => true))
expect(@service.restart).to eq(@service)
end
end
end
Loading