From b4b0bf0b3f8eb7bbe988a1af010fd6f8c27d5d9e Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Mon, 18 Jan 2016 14:00:34 -0500 Subject: [PATCH] trim down service calls and add mac support --- lib/linux_admin/service.rb | 9 +- lib/linux_admin/service/brew_service.rb | 54 ++++++++++ lib/linux_admin/service/sys_v_init_service.rb | 34 +++--- lib/linux_admin/service/systemd_service.rb | 36 +++---- spec/service/brew_service_spec.rb | 100 ++++++++++++++++++ spec/service/sys_v_init_service_spec.rb | 32 +++--- spec/service/systemd_service_spec.rb | 33 +++--- spec/service_spec.rb | 12 +++ 8 files changed, 233 insertions(+), 77 deletions(-) create mode 100644 lib/linux_admin/service/brew_service.rb create mode 100644 spec/service/brew_service_spec.rb diff --git a/lib/linux_admin/service.rb b/lib/linux_admin/service.rb index 1df7ce6..e9c1f87 100644 --- a/lib/linux_admin/service.rb +++ b/lib/linux_admin/service.rb @@ -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 diff --git a/lib/linux_admin/service/brew_service.rb b/lib/linux_admin/service/brew_service.rb new file mode 100644 index 0000000..e9d6af4 --- /dev/null +++ b/lib/linux_admin/service/brew_service.rb @@ -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 diff --git a/lib/linux_admin/service/sys_v_init_service.rb b/lib/linux_admin/service/sys_v_init_service.rb index 9b08f72..91e0943 100644 --- a/lib/linux_admin/service/sys_v_init_service.rb +++ b/lib/linux_admin/service/sys_v_init_service.rb @@ -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 diff --git a/lib/linux_admin/service/systemd_service.rb b/lib/linux_admin/service/systemd_service.rb index cadde11..8c50439 100644 --- a/lib/linux_admin/service/systemd_service.rb +++ b/lib/linux_admin/service/systemd_service.rb @@ -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 diff --git a/spec/service/brew_service_spec.rb b/spec/service/brew_service_spec.rb new file mode 100644 index 0000000..d931891 --- /dev/null +++ b/spec/service/brew_service_spec.rb @@ -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 diff --git a/spec/service/sys_v_init_service_spec.rb b/spec/service/sys_v_init_service_spec.rb index 9fab27f..b0e5a15 100644 --- a/spec/service/sys_v_init_service_spec.rb +++ b/spec/service/sys_v_init_service_spec.rb @@ -1,20 +1,21 @@ describe LinuxAdmin::SysVInitService do before do @service = described_class.new 'foo' + allow(LinuxAdmin::Common).to receive(:cmd).with(:service).and_return("service") + allow(LinuxAdmin::Common).to receive(:cmd).with(:chkconfig).and_return("chkconfig") end describe "#running?" do it "checks service" do expect(LinuxAdmin::Common).to receive(:run) - .with(LinuxAdmin::Common.cmd(:service), - :params => {nil => %w(foo status)}).and_return(double(:exit_status => 0)) + .with("service", :params => %w(foo status)).and_return(double(:success? => true)) @service.running? end context "service is running" do it "returns true" do @service = described_class.new :id => :foo - expect(LinuxAdmin::Common).to receive(:run).and_return(double(:exit_status => 0)) + expect(LinuxAdmin::Common).to receive(:run).and_return(double(:success? => true)) expect(@service).to be_running end end @@ -22,7 +23,7 @@ context "service is not running" do it "returns false" do @service = described_class.new :id => :foo - expect(LinuxAdmin::Common).to receive(:run).and_return(double(:exit_status => 1)) + expect(LinuxAdmin::Common).to receive(:run).and_return(double(:success? => false)) expect(@service).not_to be_running end end @@ -30,9 +31,7 @@ describe "#enable" do it "enables service" do - expect(LinuxAdmin::Common).to receive(:run!) - .with(LinuxAdmin::Common.cmd(:chkconfig), - :params => {nil => %w(foo on)}) + expect(LinuxAdmin::Common).to receive(:run!).with("chkconfig", :params => %w(foo on)) @service.enable end @@ -44,9 +43,7 @@ describe "#disable" do it "disable service" do - expect(LinuxAdmin::Common).to receive(:run!) - .with(LinuxAdmin::Common.cmd(:chkconfig), - :params => {nil => %w(foo off)}) + expect(LinuxAdmin::Common).to receive(:run!).with("chkconfig", :params => %w(foo off)) @service.disable end @@ -58,9 +55,7 @@ describe "#start" do it "starts service" do - expect(LinuxAdmin::Common).to receive(:run!) - .with(LinuxAdmin::Common.cmd(:service), - :params => {nil => %w(foo start)}) + expect(LinuxAdmin::Common).to receive(:run!).with("service", :params => %w(foo start)) @service.start end @@ -72,9 +67,7 @@ describe "#stop" do it "stops service" do - expect(LinuxAdmin::Common).to receive(:run!) - .with(LinuxAdmin::Common.cmd(:service), - :params => {nil => %w(foo stop)}) + expect(LinuxAdmin::Common).to receive(:run!).with("service", :params => %w(foo stop)) @service.stop end @@ -87,14 +80,13 @@ describe "#restart" do it "stops service" do expect(LinuxAdmin::Common).to receive(:run) - .with(LinuxAdmin::Common.cmd(:service), - :params => {nil => %w(foo restart)}).and_return(double(:exit_status => 0)) + .with("service", :params => %w(foo restart)).and_return(double(:success? => true)) @service.restart end context "service restart fails" do it "manually stops/starts service" do - expect(LinuxAdmin::Common).to receive(:run).and_return(double(:exit_status => 1)) + expect(LinuxAdmin::Common).to receive(:run).and_return(double(:success? => false)) expect(@service).to receive(:stop) expect(@service).to receive(:start) @service.restart @@ -102,7 +94,7 @@ end it "returns self" do - expect(LinuxAdmin::Common).to receive(:run).and_return(double(:exit_status => 0)) + expect(LinuxAdmin::Common).to receive(:run).and_return(double(:success? => true)) expect(@service.restart).to eq(@service) end end diff --git a/spec/service/systemd_service_spec.rb b/spec/service/systemd_service_spec.rb index 23b352c..d50b7a2 100644 --- a/spec/service/systemd_service_spec.rb +++ b/spec/service/systemd_service_spec.rb @@ -1,32 +1,31 @@ describe LinuxAdmin::SystemdService do before do @service = described_class.new 'foo' + allow(LinuxAdmin::Common).to receive(:cmd).with(:systemctl).and_return("systemctl") end describe "#running?" do it "checks service" do expect(LinuxAdmin::Common).to receive(:run) - .with(LinuxAdmin::Common.cmd(:systemctl), - :params => {nil => %w(status foo)}).and_return(double(:exit_status => 0)) + .with("systemctl", + :params => %w(status foo)).and_return(double(:success? => true)) @service.running? end it "returns true when service is running" do - expect(LinuxAdmin::Common).to receive(:run).and_return(double(:exit_status => 0)) + expect(LinuxAdmin::Common).to receive(:run).and_return(double(:success? => true)) expect(@service).to be_running end it "returns false when service is not running" do - expect(LinuxAdmin::Common).to receive(:run).and_return(double(:exit_status => 1)) + expect(LinuxAdmin::Common).to receive(:run).and_return(double(:success? => false)) expect(@service).not_to be_running end end describe "#enable" do it "enables service" do - expect(LinuxAdmin::Common).to receive(:run!) - .with(LinuxAdmin::Common.cmd(:systemctl), - :params => {nil => %w(enable foo)}) + expect(LinuxAdmin::Common).to receive(:run!).with("systemctl", :params => %w(enable foo)) @service.enable end @@ -38,9 +37,7 @@ describe "#disable" do it "disables service" do - expect(LinuxAdmin::Common).to receive(:run!) - .with(LinuxAdmin::Common.cmd(:systemctl), - :params => {nil => %w(disable foo)}) + expect(LinuxAdmin::Common).to receive(:run!).with("systemctl", :params => %w(disable foo)) @service.disable end @@ -52,9 +49,7 @@ describe "#start" do it "starts service" do - expect(LinuxAdmin::Common).to receive(:run!) - .with(LinuxAdmin::Common.cmd(:systemctl), - :params => {nil => %w(start foo)}) + expect(LinuxAdmin::Common).to receive(:run!).with("systemctl", :params => %w(start foo)) @service.start end @@ -66,9 +61,7 @@ describe "#stop" do it "stops service" do - expect(LinuxAdmin::Common).to receive(:run!) - .with(LinuxAdmin::Common.cmd(:systemctl), - :params => {nil => %w(stop foo)}) + expect(LinuxAdmin::Common).to receive(:run!).with("systemctl", :params => %w(stop foo)) @service.stop end @@ -81,20 +74,20 @@ describe "#restart" do it "restarts service" do expect(LinuxAdmin::Common).to receive(:run) - .with(LinuxAdmin::Common.cmd(:systemctl), - :params => {nil => %w(restart foo)}).and_return(double(:exit_status => 0)) + .with("systemctl", + :params => %w(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(:exit_status => 1)) + 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(:exit_status => 0)) + expect(LinuxAdmin::Common).to receive(:run).and_return(double(:success? => true)) expect(@service.restart).to eq(@service) end end diff --git a/spec/service_spec.rb b/spec/service_spec.rb index bc80c46..620afac 100644 --- a/spec/service_spec.rb +++ b/spec/service_spec.rb @@ -10,6 +10,11 @@ expect(described_class.service_type).to eq(LinuxAdmin::SysVInitService) end + it "on sysv systems" do + stub_to_service_type(:brew_service) + expect(described_class.service_type).to eq(LinuxAdmin::BrewService) + end + it "should memoize results" do expect(described_class).to receive(:service_type_uncached).once.and_return("anything_non_nil") described_class.service_type @@ -33,6 +38,11 @@ stub_to_service_type(:sys_v_init_service) expect(described_class.new("xxx")).to be_kind_of(LinuxAdmin::SysVInitService) end + + it "on mac systems" do + stub_to_service_type(:brew_service) + expect(described_class.new("xxx")).to be_kind_of(LinuxAdmin::BrewService) + end end it "#id / #id=" do @@ -50,5 +60,7 @@ def stub_to_service_type(system) allow(LinuxAdmin::Common).to receive(:cmd?).with(:systemctl).and_return(system == :systemd_service) + allow(LinuxAdmin::Common).to receive(:cmd?).with(:service).and_return(system == :sys_v_init_service) + allow(LinuxAdmin::Common).to receive(:cmd?).with(:brew).and_return(system == :brew_service) end end