Skip to content
Merged
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
1 change: 1 addition & 0 deletions lib/linux_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
56 changes: 56 additions & 0 deletions lib/linux_admin/systemctl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module LinuxAdmin
class Systemctl
include Common

attr_accessor :name

public

def initialize(name)
@name = name
end

def running?
run(cmd(:systemctl),
:params => {nil => ["status", name]}).exit_status == 0
end

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

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

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

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

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

# attempt to manually stop/start if restart fails
if status != 0
stop
start
end

self
end
end
end
101 changes: 101 additions & 0 deletions spec/systemctl_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
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 => %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
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 => %w(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 => %w(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 => %w(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 => %w(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 => %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
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