diff --git a/lib/linux_admin.rb b/lib/linux_admin.rb index 9daffc9..04a4dd3 100644 --- a/lib/linux_admin.rb +++ b/lib/linux_admin.rb @@ -13,6 +13,7 @@ require 'linux_admin/partition' require 'linux_admin/distro' require 'linux_admin/system' +require 'linux_admin/fstab' class LinuxAdmin extend Common diff --git a/lib/linux_admin/common.rb b/lib/linux_admin/common.rb index 17478b9..683b3cd 100644 --- a/lib/linux_admin/common.rb +++ b/lib/linux_admin/common.rb @@ -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? @@ -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] diff --git a/lib/linux_admin/fstab.rb b/lib/linux_admin/fstab.rb new file mode 100644 index 0000000..077cc2a --- /dev/null +++ b/lib/linux_admin/fstab.rb @@ -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 diff --git a/lib/linux_admin/service.rb b/lib/linux_admin/service.rb index ecc363e..c4d1eb9 100644 --- a/lib/linux_admin/service.rb +++ b/lib/linux_admin/service.rb @@ -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 diff --git a/spec/fstab_spec.rb b/spec/fstab_spec.rb new file mode 100644 index 0000000..5d08009 --- /dev/null +++ b/spec/fstab_spec.rb @@ -0,0 +1,27 @@ +require 'spec_helper' + +describe LinuxAdmin::FSTab do + it "creates FSTabEntry for each line in fstab" do + fstab = < { 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 @@ -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 @@ -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 @@ -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