projects
/
aids.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Flesh out the status handling a bit more, actually check all multi-user runlevels
[aids.git]
/
lib
/
aids.rb
diff --git
a/lib/aids.rb
b/lib/aids.rb
index c2a7e66c020df30a629c59db8c9609c22fcbe1bd..388c2395ee3c54b07275ef4412e56deae7a1f436 100644
(file)
--- a/
lib/aids.rb
+++ b/
lib/aids.rb
@@
-1,33
+1,35
@@
module AIDS
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)
class Service
attr_reader :name
def initialize(name)
if name.nil? or name.empty? or not name.is_a?(String)
- raise
Excep
tion.new("Service name must be a non-empty string, got #{name.inspect}.")
+ raise
AIDS::Infec
tion.new("Service name must be a non-empty string, got #{name.inspect}.")
end
name = $1 if name =~ %r{^/etc/init\.d/(.+)$}
if name =~ /[^[:alnum:]\-.]/
end
name = $1 if name =~ %r{^/etc/init\.d/(.+)$}
if name =~ /[^[:alnum:]\-.]/
- raise
Excep
tion.new("Invalid init script name: #{name}.")
+ raise
AIDS::Infec
tion.new("Invalid init script name: #{name}.")
end
unless File.exist?("/etc/init.d/#{name}")
end
unless File.exist?("/etc/init.d/#{name}")
- raise
Excep
tion.new("Unknown service: #{name}.")
+ raise
AIDS::Infec
tion.new("Unknown service: #{name}.")
end
@name = name
end
def enable!
end
@name = name
end
def enable!
- start_on_runlevel
!(RUNLEVEL_MULTI
)
+ start_on_runlevel
s!(RUNLEVEL_MULTI_ALL
)
end
def disable!
end
def disable!
- stop_on_runlevel
!(RUNLEVEL_MULTI
)
+ stop_on_runlevel
s!(RUNLEVEL_MULTI_ALL
)
end
def enabled?
end
def enabled?
@@
-35,13
+37,17
@@
module AIDS
end
def status
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
end
+ status
end
private
end
private
@@
-56,16
+62,20
@@
module AIDS
not Dir.glob("/etc/rc#{runlevel}.d/K[0-9][0-9]#{@name}").empty?
end
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
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!
end
def set_default_runlevels!
@@
-75,13
+85,13
@@
module AIDS
def updatercd(action, runlevel=nil)
unless [:enable, :disable, :remove, :defaults].include?(action)
def updatercd(action, runlevel=nil)
unless [:enable, :disable, :remove, :defaults].include?(action)
- raise
Excep
tion.new("Invalid action for updatercd: #{action}.")
+ raise
AIDS::Infec
tion.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.
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
Excep
tion.new("Unable to comply: update-rc.d is balls.")
+ if [
:'0',:'1',:'6'].include?(runlevel
)
+ raise
AIDS::Infec
tion.new("Unable to comply: update-rc.d is balls.")
end
pid = Process.fork do
$stdout.close
end
pid = Process.fork do
$stdout.close
@@
-95,11
+105,14
@@
module AIDS
end
Process.wait(pid)
if (rc = $?.exitstatus) != 0
end
Process.wait(pid)
if (rc = $?.exitstatus) != 0
- raise
Excep
tion.new("update-rc.d returned #{rc}")
+ raise
AIDS::Infec
tion.new("update-rc.d returned #{rc}")
end
end
end
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)
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
end.compact
end
+ private
+
def self.validate_runlevel(runlevel)
def self.validate_runlevel(runlevel)
- unless
['0', '1', '2', '3', '4', '5', '6', 'S'].include?(runlevel.to_s
)
- raise
Excep
tion.new("Invalid runlevel: #{runlevel}.")
+ unless
RUNLEVEL_ALL.include?(runlevel
)
+ raise
AIDS::Infec
tion.new("Invalid runlevel: #{runlevel}.")
end
end
end
end
end
end