3 RUNLEVEL_MULTI_TO_SINGLE = :'1'
5 RUNLEVEL_MULTI_ALL = [:'2',:'3',:'4',:'5']
8 RUNLEVEL_ALL = [:'0',:'1',:'2',:'3',:'4',:'5',:'6',:'S']
14 if name.nil? or name.empty? or not name.is_a?(String)
15 raise AIDS::Infection.new("Service name must be a non-empty string, got #{name.inspect}.")
17 name = $1 if name =~ %r{^/etc/init\.d/(.+)$}
18 if name =~ /[^[:alnum:]\-.]/
19 raise AIDS::Infection.new("Invalid init script name: #{name}.")
21 unless File.exist?("/etc/init.d/#{name}")
22 raise AIDS::Infection.new("Unknown service: #{name}.")
28 start_on_runlevels!(RUNLEVEL_MULTI_ALL)
32 stop_on_runlevels!(RUNLEVEL_MULTI_ALL)
36 started_on_runlevel?(RUNLEVEL_MULTI)
41 RUNLEVEL_ALL.each do |r|
42 if started_on_runlevel?(r)
44 elsif stopped_on_runlevel?(r)
55 def started_on_runlevel?(runlevel)
56 AIDS.validate_runlevel(runlevel)
57 not Dir.glob("/etc/rc#{runlevel}.d/S[0-9][0-9]#{@name}").empty?
60 def stopped_on_runlevel?(runlevel)
61 AIDS.validate_runlevel(runlevel)
62 not Dir.glob("/etc/rc#{runlevel}.d/K[0-9][0-9]#{@name}").empty?
65 def start_on_runlevels!(runlevels)
66 [runlevels].flatten.each do |r|
67 AIDS.validate_runlevel(r)
68 next if started_on_runlevel?(r)
73 def stop_on_runlevels!(runlevels)
74 [runlevels].flatten.each do |r|
75 AIDS.validate_runlevel(r)
76 next if stopped_on_runlevel?(r)
77 updatercd(:disable, r)
81 def set_default_runlevels!
86 def updatercd(action, runlevel=nil)
87 unless [:enable, :disable, :remove, :defaults].include?(action)
88 raise AIDS::Infection.new("Invalid action for updatercd: #{action}.")
90 AIDS.validate_runlevel(runlevel) if runlevel
91 # update-rc.d will baulk at being told to do anything with
93 if [:'0',:'1',:'6'].include?(runlevel)
94 raise AIDS::Infection.new("Unable to comply: update-rc.d is balls.")
100 '/usr/sbin/update-rc.d',
107 if (rc = $?.exitstatus) != 0
108 raise AIDS::Infection.new("update-rc.d returned #{rc}")
113 class Infection < Exception
116 def self.get_all_services
117 Dir.glob("/etc/init.d/*").map do |f|
118 next unless File.executable?(f)
125 def self.validate_runlevel(runlevel)
126 unless RUNLEVEL_ALL.include?(runlevel)
127 raise AIDS::Infection.new("Invalid runlevel: #{runlevel}.")