Flesh out the status handling a bit more, actually check all multi-user runlevels
authorSteven McDonald <steven@steven-mcdonald.id.au>
Sat, 13 Oct 2012 03:58:55 +0000 (14:58 +1100)
committerSteven McDonald <steven@steven-mcdonald.id.au>
Sat, 13 Oct 2012 03:58:55 +0000 (14:58 +1100)
bin/aids
lib/aids.rb

index 7b3fa5022aff8741be234aa8824f8c0881121479..5388ea942bba7414a2768b0930a6de3a332ad31c 100755 (executable)
--- a/bin/aids
+++ b/bin/aids
@@ -13,17 +13,34 @@ def main(argv)
                        svc.disable!
                when 'list'
                        if svc
-                               puts svc.status
+                               display_status(svc)
                        else
-                               AIDS.get_all_services.sort_by{|s|s.name}.each do |s|
-                                       puts "#{s.name}: #{s.status}"
-                               end
+                               AIDS.get_all_services.sort_by{|s|s.name}.each{|s|display_status(s)}
                        end
                else
                        usage
        end
 end
 
+def display_status(svc)
+       print "#{svc.name[0..14]}:#{' ' * (16 - svc.name[0..14].length)}"
+       status = svc.status
+       AIDS::RUNLEVEL_ALL.each do |r|
+               case status[r]
+                       when :start
+                               prettystatus = 'on'
+                       when :stop
+                               prettystatus = 'off'
+                       when :none
+                               prettystatus = 'none'
+                       else
+                               next
+               end
+                       print "#{r}: #{prettystatus + ' ' * (8 - prettystatus.length)}"
+               end
+       puts
+end
+
 def usage
        $stderr.puts <<-EOF
 AIDS - Assistant for Initialisation of Debian Services
index c2a7e66c020df30a629c59db8c9609c22fcbe1bd..388c2395ee3c54b07275ef4412e56deae7a1f436 100644 (file)
@@ -1,33 +1,35 @@
 module AIDS
-       RUNLEVEL_SINGLE          = 'S'
-       RUNLEVEL_MULTI_TO_SINGLE = '1'
-       RUNLEVEL_MULTI           = '2'
-       RUNLEVEL_HALT            = '0'
-       RUNLEVEL_REBOOT          = '6'
+       RUNLEVEL_SINGLE          = :'S'
+       RUNLEVEL_MULTI_TO_SINGLE = :'1'
+       RUNLEVEL_MULTI           = :'2'
+       RUNLEVEL_MULTI_ALL       = [:'2',:'3',:'4',:'5']
+       RUNLEVEL_HALT            = :'0'
+       RUNLEVEL_REBOOT          = :'6'
+       RUNLEVEL_ALL             = [:'0',:'1',:'2',:'3',:'4',:'5',:'6',:'S']
 
        class Service
                attr_reader :name
 
                def initialize(name)
                        if name.nil? or name.empty? or not name.is_a?(String)
-                               raise Exception.new("Service name must be a non-empty string, got #{name.inspect}.")
+                               raise AIDS::Infection.new("Service name must be a non-empty string, got #{name.inspect}.")
                        end
                        name = $1 if name =~ %r{^/etc/init\.d/(.+)$}
                        if name =~ /[^[:alnum:]\-.]/
-                               raise Exception.new("Invalid init script name: #{name}.")
+                               raise AIDS::Infection.new("Invalid init script name: #{name}.")
                        end
                        unless File.exist?("/etc/init.d/#{name}")
-                               raise Exception.new("Unknown service: #{name}.")
+                               raise AIDS::Infection.new("Unknown service: #{name}.")
                        end
                        @name = name
                end
 
                def enable!
-                       start_on_runlevel!(RUNLEVEL_MULTI)
+                       start_on_runlevels!(RUNLEVEL_MULTI_ALL)
                end
 
                def disable!
-                       stop_on_runlevel!(RUNLEVEL_MULTI)
+                       stop_on_runlevels!(RUNLEVEL_MULTI_ALL)
                end
 
                def enabled?
@@ -35,13 +37,17 @@ module AIDS
                end
 
                def status
-                       if started_on_runlevel?(RUNLEVEL_MULTI)
-                               :enabled
-                       elsif stopped_on_runlevel?(RUNLEVEL_MULTI)
-                               :disabled
-                       else
-                               :unknown
+                       status = {}
+                       RUNLEVEL_ALL.each do |r|
+                               if started_on_runlevel?(r)
+                                       status[r] = :start
+                               elsif stopped_on_runlevel?(r)
+                                       status[r] = :stop
+                               else
+                                       status[r] = :none
+                               end
                        end
+                       status
                end
 
                private
@@ -56,16 +62,20 @@ module AIDS
                        not Dir.glob("/etc/rc#{runlevel}.d/K[0-9][0-9]#{@name}").empty?
                end
 
-               def start_on_runlevel!(runlevel)
-                       AIDS.validate_runlevel(runlevel)
-                       return true if started_on_runlevel?(runlevel)
-                       updatercd(:enable, runlevel)
+               def start_on_runlevels!(runlevels)
+                       [runlevels].flatten.each do |r|
+                               AIDS.validate_runlevel(r)
+                               next if started_on_runlevel?(r)
+                               updatercd(:enable, r)
+                       end
                end
 
-               def stop_on_runlevel!(runlevel)
-                       AIDS.validate_runlevel(runlevel)
-                       return true if stopped_on_runlevel?(runlevel)
-                       updatercd(:disable, runlevel)
+               def stop_on_runlevels!(runlevels)
+                       [runlevels].flatten.each do |r|
+                               AIDS.validate_runlevel(r)
+                               next if stopped_on_runlevel?(r)
+                               updatercd(:disable, r)
+                       end
                end
 
                def set_default_runlevels!
@@ -75,13 +85,13 @@ module AIDS
 
                def updatercd(action, runlevel=nil)
                        unless [:enable, :disable, :remove, :defaults].include?(action)
-                               raise Exception.new("Invalid action for updatercd: #{action}.")
+                               raise AIDS::Infection.new("Invalid action for updatercd: #{action}.")
                        end
                        AIDS.validate_runlevel(runlevel) if runlevel
                        # update-rc.d will baulk at being told to do anything with
                        # these runlevels.
-                       if ['0', '1', '6'].include?(runlevel.to_s)
-                               raise Exception.new("Unable to comply: update-rc.d is balls.")
+                       if [:'0',:'1',:'6'].include?(runlevel)
+                               raise AIDS::Infection.new("Unable to comply: update-rc.d is balls.")
                        end
                        pid = Process.fork do
                                $stdout.close
@@ -95,11 +105,14 @@ module AIDS
                        end
                        Process.wait(pid)
                        if (rc = $?.exitstatus) != 0
-                               raise Exception.new("update-rc.d returned #{rc}")
+                               raise AIDS::Infection.new("update-rc.d returned #{rc}")
                        end
                end
        end
 
+       class Infection < Exception
+       end
+
        def self.get_all_services
                Dir.glob("/etc/init.d/*").map do |f|
                        next unless File.executable?(f)
@@ -107,9 +120,11 @@ module AIDS
                end.compact
        end
 
+       private
+
        def self.validate_runlevel(runlevel)
-               unless ['0', '1', '2', '3', '4', '5', '6', 'S'].include?(runlevel.to_s)
-                       raise Exception.new("Invalid runlevel: #{runlevel}.")
+               unless RUNLEVEL_ALL.include?(runlevel)
+                       raise AIDS::Infection.new("Invalid runlevel: #{runlevel}.")
                end
        end
 end