From 88a2a3c1d98e880bf19a91f29069675c7207e743 Mon Sep 17 00:00:00 2001 From: Nick Carboni Date: Thu, 23 Jul 2015 16:00:35 -0400 Subject: [PATCH 1/3] Added a systemctl class to the linux admin gem As init has been depricated it is quickly becoming necessary to use systemctl in its place. --- lib/linux_admin.rb | 1 + lib/linux_admin/systemctl.rb | 57 ++++++++++++++++++++ spec/systemctl_spec.rb | 102 +++++++++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 lib/linux_admin/systemctl.rb create mode 100644 spec/systemctl_spec.rb diff --git a/lib/linux_admin.rb b/lib/linux_admin.rb index 3f1a5ac..d3f2203 100644 --- a/lib/linux_admin.rb +++ b/lib/linux_admin.rb @@ -15,6 +15,7 @@ require 'linux_admin/yum' require 'linux_admin/service' +require 'linux_admin/systemctl' require 'linux_admin/mountable' require 'linux_admin/disk' require 'linux_admin/hosts' diff --git a/lib/linux_admin/systemctl.rb b/lib/linux_admin/systemctl.rb new file mode 100644 index 0000000..4de789e --- /dev/null +++ b/lib/linux_admin/systemctl.rb @@ -0,0 +1,57 @@ +module LinuxAdmin + class Systemctl + include Common + + attr_accessor :id + private + + public + + def initialize(id) + @id = id + end + + def running? + run(cmd(:systemctl), + :params => { nil => ["status", id] }).exit_status == 0 + end + + def enable + run!(cmd(:systemctl), + :params => { nil => ["enable", id] }) + self + end + + def disable + run!(cmd(:systemctl), + :params => { nil => ["disable", id] }) + self + end + + def start + run!(cmd(:systemctl), + :params => { nil => ["start", id] }) + self + end + + def stop + run!(cmd(:systemctl), + :params => { nil => ["stop", id] }) + self + end + + def restart + status = + run(cmd(:systemctl), + :params => { nil => ["restart", id] }).exit_status + + # attempt to manually stop/start if restart fails + if status != 0 + self.stop + self.start + end + + self + end + end +end diff --git a/spec/systemctl_spec.rb b/spec/systemctl_spec.rb new file mode 100644 index 0000000..3cf9416 --- /dev/null +++ b/spec/systemctl_spec.rb @@ -0,0 +1,102 @@ +describe LinuxAdmin::Systemctl do + before(:each) do + @service = LinuxAdmin::Systemctl.new 'foo' + end + + describe "#running?" do + it "checks service" do + expect(@service).to receive(:run). + with(@service.cmd(:systemctl), + :params => { nil => ['status', 'foo']}).and_return(double(:exit_status => 0)) + @service.running? + end + + it "returns true when service is running" do + expect(@service).to receive(:run).and_return(double(:exit_status => 0)) + expect(@service).to be_running + end + + it "returns false when service is not running" do + expect(@service).to receive(:run).and_return(double(:exit_status => 1)) + expect(@service).not_to be_running + end + end + + describe "#enable" do + it "enables service" do + expect(@service).to receive(:run!). + with(@service.cmd(:systemctl), + :params => { nil => ['enable', 'foo']}) + @service.enable + end + + it "returns self" do + expect(@service).to receive(:run!) # stub out cmd invocation + expect(@service.enable).to eq(@service) + end + end + + describe "#disable" do + it "disables service" do + expect(@service).to receive(:run!). + with(@service.cmd(:systemctl), + :params => { nil => ['disable', 'foo']}) + @service.disable + end + + it "returns self" do + expect(@service).to receive(:run!) + expect(@service.disable).to eq(@service) + end + end + + describe "#start" do + it "starts service" do + expect(@service).to receive(:run!). + with(@service.cmd(:systemctl), + :params => { nil => ['start', 'foo']}) + @service.start + end + + it "returns self" do + expect(@service).to receive(:run!) + expect(@service.start).to eq(@service) + end + end + + describe "#stop" do + it "stops service" do + expect(@service).to receive(:run!). + with(@service.cmd(:systemctl), + :params => { nil => ['stop', 'foo']}) + @service.stop + end + + it "returns self" do + expect(@service).to receive(:run!) + expect(@service.stop).to eq(@service) + end + end + + describe "#restart" do + it "restarts service" do + expect(@service).to receive(:run). + with(@service.cmd(:systemctl), + :params => { nil => ['restart', 'foo']}).and_return(double(:exit_status => 0)) + @service.restart + end + + it "manually stops then starts service when restart fails" do + expect(@service).to receive(:run).and_return(double(:exit_status => 1)) + expect(@service).to receive(:stop) + expect(@service).to receive(:start) + @service.restart + end + + it "returns self" do + expect(@service).to receive(:run).and_return(double(:exit_status => 0)) + expect(@service.restart).to eq(@service) + end + end + +end From 0c46d30836226efe186e10f8ea37c2d89b6b9bd9 Mon Sep 17 00:00:00 2001 From: Nick Carboni Date: Thu, 23 Jul 2015 16:12:24 -0400 Subject: [PATCH 2/3] Fixed rubocop issues --- lib/linux_admin/systemctl.rb | 17 ++++++------ spec/systemctl_spec.rb | 53 ++++++++++++++++++------------------ 2 files changed, 34 insertions(+), 36 deletions(-) diff --git a/lib/linux_admin/systemctl.rb b/lib/linux_admin/systemctl.rb index 4de789e..8a4341c 100644 --- a/lib/linux_admin/systemctl.rb +++ b/lib/linux_admin/systemctl.rb @@ -3,7 +3,6 @@ class Systemctl include Common attr_accessor :id - private public @@ -13,42 +12,42 @@ def initialize(id) def running? run(cmd(:systemctl), - :params => { nil => ["status", id] }).exit_status == 0 + :params => {nil => ["status", id]}).exit_status == 0 end def enable run!(cmd(:systemctl), - :params => { nil => ["enable", id] }) + :params => {nil => ["enable", id]}) self end def disable run!(cmd(:systemctl), - :params => { nil => ["disable", id] }) + :params => {nil => ["disable", id]}) self end def start run!(cmd(:systemctl), - :params => { nil => ["start", id] }) + :params => {nil => ["start", id]}) self end def stop run!(cmd(:systemctl), - :params => { nil => ["stop", id] }) + :params => {nil => ["stop", id]}) self end def restart status = run(cmd(:systemctl), - :params => { nil => ["restart", id] }).exit_status + :params => {nil => ["restart", id]}).exit_status # attempt to manually stop/start if restart fails if status != 0 - self.stop - self.start + stop + start end self diff --git a/spec/systemctl_spec.rb b/spec/systemctl_spec.rb index 3cf9416..17ae42e 100644 --- a/spec/systemctl_spec.rb +++ b/spec/systemctl_spec.rb @@ -5,28 +5,28 @@ describe "#running?" do it "checks service" do - expect(@service).to receive(:run). - with(@service.cmd(:systemctl), - :params => { nil => ['status', 'foo']}).and_return(double(:exit_status => 0)) + expect(@service).to receive(:run) + .with(@service.cmd(:systemctl), + :params => {nil => %w(status foo)}).and_return(double(:exit_status => 0)) @service.running? end it "returns true when service is running" do - expect(@service).to receive(:run).and_return(double(:exit_status => 0)) - expect(@service).to be_running + expect(@service).to receive(:run).and_return(double(:exit_status => 0)) + expect(@service).to be_running end it "returns false when service is not running" do - expect(@service).to receive(:run).and_return(double(:exit_status => 1)) - expect(@service).not_to be_running + expect(@service).to receive(:run).and_return(double(:exit_status => 1)) + expect(@service).not_to be_running end end describe "#enable" do it "enables service" do - expect(@service).to receive(:run!). - with(@service.cmd(:systemctl), - :params => { nil => ['enable', 'foo']}) + expect(@service).to receive(:run!) + .with(@service.cmd(:systemctl), + :params => {nil => %w(enable foo)}) @service.enable end @@ -38,9 +38,9 @@ describe "#disable" do it "disables service" do - expect(@service).to receive(:run!). - with(@service.cmd(:systemctl), - :params => { nil => ['disable', 'foo']}) + expect(@service).to receive(:run!) + .with(@service.cmd(:systemctl), + :params => {nil => %w(disable foo)}) @service.disable end @@ -52,9 +52,9 @@ describe "#start" do it "starts service" do - expect(@service).to receive(:run!). - with(@service.cmd(:systemctl), - :params => { nil => ['start', 'foo']}) + expect(@service).to receive(:run!) + .with(@service.cmd(:systemctl), + :params => {nil => %w(start foo)}) @service.start end @@ -66,9 +66,9 @@ describe "#stop" do it "stops service" do - expect(@service).to receive(:run!). - with(@service.cmd(:systemctl), - :params => { nil => ['stop', 'foo']}) + expect(@service).to receive(:run!) + .with(@service.cmd(:systemctl), + :params => {nil => %w(stop foo)}) @service.stop end @@ -80,17 +80,17 @@ describe "#restart" do it "restarts service" do - expect(@service).to receive(:run). - with(@service.cmd(:systemctl), - :params => { nil => ['restart', 'foo']}).and_return(double(:exit_status => 0)) + expect(@service).to receive(:run) + .with(@service.cmd(:systemctl), + :params => {nil => %w(restart foo)}).and_return(double(:exit_status => 0)) @service.restart end it "manually stops then starts service when restart fails" do - expect(@service).to receive(:run).and_return(double(:exit_status => 1)) - expect(@service).to receive(:stop) - expect(@service).to receive(:start) - @service.restart + expect(@service).to receive(:run).and_return(double(:exit_status => 1)) + expect(@service).to receive(:stop) + expect(@service).to receive(:start) + @service.restart end it "returns self" do @@ -98,5 +98,4 @@ expect(@service.restart).to eq(@service) end end - end From 6894857d80f2df7c434d8fbf59c502dc493f36c0 Mon Sep 17 00:00:00 2001 From: Nick Carboni Date: Fri, 24 Jul 2015 14:31:07 -0400 Subject: [PATCH 3/3] Changed service name from id to name in systemctl class --- lib/linux_admin/systemctl.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/linux_admin/systemctl.rb b/lib/linux_admin/systemctl.rb index 8a4341c..ea7e733 100644 --- a/lib/linux_admin/systemctl.rb +++ b/lib/linux_admin/systemctl.rb @@ -2,47 +2,47 @@ module LinuxAdmin class Systemctl include Common - attr_accessor :id + attr_accessor :name public - def initialize(id) - @id = id + def initialize(name) + @name = name end def running? run(cmd(:systemctl), - :params => {nil => ["status", id]}).exit_status == 0 + :params => {nil => ["status", name]}).exit_status == 0 end def enable run!(cmd(:systemctl), - :params => {nil => ["enable", id]}) + :params => {nil => ["enable", name]}) self end def disable run!(cmd(:systemctl), - :params => {nil => ["disable", id]}) + :params => {nil => ["disable", name]}) self end def start run!(cmd(:systemctl), - :params => {nil => ["start", id]}) + :params => {nil => ["start", name]}) self end def stop run!(cmd(:systemctl), - :params => {nil => ["stop", id]}) + :params => {nil => ["stop", name]}) self end def restart status = run(cmd(:systemctl), - :params => {nil => ["restart", id]}).exit_status + :params => {nil => ["restart", name]}).exit_status # attempt to manually stop/start if restart fails if status != 0