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 @@ -13,6 +13,7 @@
require 'linux_admin/partition'
require 'linux_admin/distro'
require 'linux_admin/system'
require 'linux_admin/fstab'

class LinuxAdmin
extend Common
Expand Down
4 changes: 3 additions & 1 deletion lib/linux_admin/common.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'shellwords'

class LinuxAdmin
class CommandError < RuntimeError; end

module Common
def write(file, content)
raise ArgumentError, "file and content can not be empty" if file.blank? || content.blank?
Expand All @@ -24,7 +26,7 @@ def run(cmd, options = {})
elsif options[:return_exitstatus] || exitstatus == 0
exitstatus
else
raise "Error: Exit Code #{exitstatus}"
raise CommandError, "#{build_cmd(cmd, params)}: exit code: #{exitstatus}"
end
rescue
return nil if options[:return_exitstatus]
Expand Down
49 changes: 49 additions & 0 deletions lib/linux_admin/fstab.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# LinuxAdmin fstab Representation
#
# Copyright (C) 2013 Red Hat Inc.
# Licensed under the MIT License

require 'singleton'

class LinuxAdmin
class FSTabEntry < LinuxAdmin
attr_accessor :device
attr_accessor :mount_point
attr_accessor :fs_type
attr_accessor :mount_options
attr_accessor :dumpable
attr_accessor :fsck_order
end

class FSTab < LinuxAdmin
include Singleton

attr_accessor :entries

def initialize
refresh
end

private

def refresh
@entries = []
f = File.read('/etc/fstab')
f.each_line { |line|
first_char = line.strip[0]
if first_char != '#' && first_char !~ /\s/
columns = line.split
entry = FSTabEntry.new
entry.device = columns[0]
entry.mount_point = columns[1]
entry.fs_type = columns[2]
entry.mount_options = columns[3]
entry.dumpable = columns[4].to_i
entry.fsck_order = columns[5].to_i
@entries << entry
end
}
self
end
end
end
19 changes: 19 additions & 0 deletions lib/linux_admin/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,40 @@ def running?
def enable
run(cmd(:systemctl),
:params => { nil => ["enable", "#{id}.service"] })
self
end

def disable
run(cmd(:systemctl),
:params => { nil => ["disable", "#{id}.service"] })
self
end

def start
run(cmd(:service),
:params => { nil => [id, "start"] })
self
end

def stop
run(cmd(:service),
:params => { nil => [id, "stop"] })
self
end

def restart
status =
run(cmd(:service),
:params => { nil => [id, "restart"] },
:return_exitstatus => true)

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

self
end
end
end
27 changes: 27 additions & 0 deletions spec/fstab_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'spec_helper'

describe LinuxAdmin::FSTab do
it "creates FSTabEntry for each line in fstab" do
fstab = <<eos
/dev/sda1 / ext4 defaults 1 1
/dev/sda2 swap swap defaults 0 0
eos
File.should_receive(:read).with('/etc/fstab').and_return(fstab)
entries = LinuxAdmin::FSTab.instance.entries
entries.size.should == 2

entries[0].device.should == '/dev/sda1'
entries[0].mount_point.should == '/'
entries[0].fs_type.should == 'ext4'
entries[0].mount_options.should == 'defaults'
entries[0].dumpable.should == 1
entries[0].fsck_order.should == 1

entries[1].device.should == '/dev/sda2'
entries[1].mount_point.should == 'swap'
entries[1].fs_type.should == 'swap'
entries[1].mount_options.should == 'defaults'
entries[1].dumpable.should == 0
entries[1].fsck_order.should == 0
end
end
45 changes: 45 additions & 0 deletions spec/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
:params => { nil => [ 'enable', 'foo.service']})
@service.enable
end

it "returns self" do
@service.should_receive(:run) # stub out cmd invocation
@service.enable.should == @service
end
end

describe "#disable" do
Expand All @@ -51,6 +56,11 @@
:params => { nil => [ 'disable', 'foo.service']})
@service.disable
end

it "returns self" do
@service.should_receive(:run)
@service.disable.should == @service
end
end

describe "#start" do
Expand All @@ -60,6 +70,11 @@
:params => { nil => [ 'foo', 'start']})
@service.start
end

it "returns self" do
@service.should_receive(:run)
@service.start.should == @service
end
end

describe "#stop" do
Expand All @@ -69,5 +84,35 @@
:params => { nil => [ 'foo', 'stop']})
@service.stop
end

it "returns self" do
@service.should_receive(:run)
@service.stop.should == @service
end
end

describe "#restart" do
it "stops service" do
@service.should_receive(:run).
with(@service.cmd(:service),
:params => { nil => [ 'foo', 'restart']},
:return_exitstatus => true).and_return(0)
@service.restart
end

context "service restart fails" do
it "manually stops/starts service" do
@service.should_receive(:run).and_return(1)
@service.should_receive(:stop)
@service.should_receive(:start)
@service.restart
end
end

it "returns self" do
@service.should_receive(:run).and_return(0)
@service.restart.should == @service
end
end

end