3 RUNLEVEL_MULTI_TO_SINGLE = '1'
12 if name.nil? or name.empty? or not name.is_a?(String)
13 raise Exception.new("Service name must be a non-empty string, got #{name.inspect}.")
15 name = $1 if name =~ %r{^/etc/init\.d/(.+)$}
16 if name =~ /[^[:alnum:]\-.]/
17 raise Exception.new("Invalid init script name: #{name}.")
19 unless File.exist?("/etc/init.d/#{name}")
20 raise Exception.new("Unknown service: #{name}.")
26 start_on_runlevel!(RUNLEVEL_MULTI)
30 stop_on_runlevel!(RUNLEVEL_MULTI)
34 started_on_runlevel?(RUNLEVEL_MULTI)
38 if started_on_runlevel?(RUNLEVEL_MULTI)
40 elsif stopped_on_runlevel?(RUNLEVEL_MULTI)
49 def started_on_runlevel?(runlevel)
50 AIDS.validate_runlevel(runlevel)
51 not Dir.glob("/etc/rc#{runlevel}.d/S[0-9][0-9]#{@name}").empty?
54 def stopped_on_runlevel?(runlevel)
55 AIDS.validate_runlevel(runlevel)
56 not Dir.glob("/etc/rc#{runlevel}.d/K[0-9][0-9]#{@name}").empty?
59 def start_on_runlevel!(runlevel)
60 AIDS.validate_runlevel(runlevel)
61 return true if started_on_runlevel?(runlevel)
62 updatercd(:enable, runlevel)
65 def stop_on_runlevel!(runlevel)
66 AIDS.validate_runlevel(runlevel)
67 return true if stopped_on_runlevel?(runlevel)
68 updatercd(:disable, runlevel)
71 def set_default_runlevels!
76 def updatercd(action, runlevel=nil)
77 unless [:enable, :disable, :remove, :defaults].include?(action)
78 raise Exception.new("Invalid action for updatercd: #{action}.")
80 AIDS.validate_runlevel(runlevel) if runlevel
81 # update-rc.d will baulk at being told to do anything with
83 if ['0', '1', '6'].include?(runlevel.to_s)
84 raise Exception.new("Unable to comply: update-rc.d is balls.")
90 '/usr/sbin/update-rc.d',
97 if (rc = $?.exitstatus) != 0
98 raise Exception.new("update-rc.d returned #{rc}")
103 def self.get_all_services
104 Dir.glob("/etc/init.d/*").map do |f|
105 next unless File.executable?(f)
110 def self.validate_runlevel(runlevel)
111 unless ['0', '1', '2', '3', '4', '5', '6', 'S'].include?(runlevel.to_s)
112 raise Exception.new("Invalid runlevel: #{runlevel}.")