lib/linux_admin/service.rb in linux_admin-0.10.1 vs lib/linux_admin/service.rb in linux_admin-0.11.0
- old
+ new
@@ -1,62 +1,43 @@
-# LinuxAdmin Service Representation
-#
-# Copyright (C) 2013 Red Hat Inc.
-# Licensed under the MIT License
-
module LinuxAdmin
class Service
+ extend Common
include Common
+ include Logging
- attr_accessor :id
- private
-
- public
-
- def initialize(id)
- @id = id
+ def self.service_type(reload = false)
+ return @service_type if @service_type && !reload
+ @service_type = service_type_uncached
end
- def running?
- run(cmd(:service),
- :params => { nil => [id, "status"] }).exit_status == 0
+ class << self
+ private
+ alias_method :orig_new, :new
end
- def enable
- run!(cmd(:chkconfig),
- :params => { nil => [id, "on"] })
- self
+ def self.new(*args)
+ if self == LinuxAdmin::Service
+ service_type.new(*args)
+ else
+ orig_new(*args)
+ end
end
- def disable
- run!(cmd(:chkconfig),
- :params => { nil => [id, "off"] })
- self
- end
+ attr_accessor :name
- def start
- run!(cmd(:service),
- :params => { nil => [id, "start"] })
- self
+ def initialize(name)
+ @name = name
end
- def stop
- run!(cmd(:service),
- :params => { nil => [id, "stop"] })
- self
- end
+ alias_method :id, :name
+ alias_method :id=, :name=
- def restart
- status =
- run(cmd(:service),
- :params => { nil => [id, "restart"] }).exit_status
+ private
- # attempt to manually stop/start if restart fails
- if status != 0
- self.stop
- self.start
- end
-
- self
+ def self.service_type_uncached
+ cmd?(:systemctl) ? SystemdService : SysVInitService
end
+ private_class_method :service_type_uncached
end
end
+
+Dir.glob(File.join(File.dirname(__FILE__), "service", "*.rb")).each { |f| require f }