Class: Systemdy::Service
- Inherits:
-
Object
- Object
- Systemdy::Service
- Extended by:
- Forwardable
- Defined in:
- lib/systemdy/service.rb
Overview
Allows to control a life-cycle of a systemd’s service
Constant Summary collapse
- INFO_LOOKUP_COMMAND =
service look up information command
"getent services"- LIST_OF_ESSENTIAL_INFO_LOOKUP =
list of essential information on a provided service
%w( port protocol )
- LIST_OF_ACTIONS =
list of supported actions on a provided service
%w( start restart stop enable disable reload mask unmask )
- LIST_OF_STATUSES =
list of supported statuses on a provided service
%w( enabled active )
- LIST_OF_STATUS_PROPERTIES =
list of status properties on a provided service when status command is called
%w( Id Description ExecMainPID LoadState ActiveState FragmentPath ActiveEnterTimestamp InactiveEnterTimestamp ActiveExitTimestamp InactiveExitTimestamp )
Instance Attribute Summary collapse
-
#command ⇒ String
readonly
the default ‘systemctl’ command.
-
#name ⇒ String
readonly
the name of the service.
Instance Method Summary collapse
-
#disable ⇒ Object
execute action
disableon the service. -
#enable ⇒ Object
execute action
anableon the service. -
#exist? ⇒ Boolean
method for check if a created service
exist. -
#initialize(name) ⇒ Systemdy::Service
constructor
method for create a new Systemdy::Service object.
-
#is_active? ⇒ Boolean
check if the service is
active. -
#is_enabled? ⇒ Boolean
check if the service is
enabled. -
#mask ⇒ Object
execute action
maskon the service. -
#port ⇒ Object
return the available
portof the service. -
#properties ⇒ Hash
method for return a key/value pair of the service’s properties.
-
#protocol ⇒ Object
return the available
protocolof the service. -
#reload ⇒ Object
execute action
reloadon the service. -
#restart ⇒ Object
execute action
restarton the service. -
#start ⇒ Object
execute action
starton the service. -
#status ⇒ Hash
method for return the current status of the provided service.
-
#stop ⇒ Object
execute action
stopon the service. -
#unmask ⇒ Object
execute action
unmaskon the service.
Constructor Details
#initialize(name) ⇒ Systemdy::Service
method for create a new Systemdy::Service object
36 37 38 39 |
# File 'lib/systemdy/service.rb', line 36 def initialize(name) @command = SYSTEMCTL_COMMAND # constant contained in Systemdy.rb @name = name end |
Instance Attribute Details
#command ⇒ String (readonly)
the default ‘systemctl’ command
5 6 7 |
# File 'lib/systemdy/service.rb', line 5 def command @command end |
#name ⇒ String (readonly)
the name of the service
5 6 7 |
# File 'lib/systemdy/service.rb', line 5 def name @name end |
Instance Method Details
#disable ⇒ Object
This method is generated with use of metaprogramming techniques
execute action disable on the service
136 137 138 139 140 141 142 |
# File 'lib/systemdy/service.rb', line 136 LIST_OF_ACTIONS.each do |action| define_method action do return () unless exist? sudo = Etc.getpwuid(Process.uid).name != 'root' ? 'sudo' : '' `#{sudo} #{command} #{action} #{name}` end end |
#enable ⇒ Object
This method is generated with use of metaprogramming techniques
execute action anable on the service
136 137 138 139 140 141 142 |
# File 'lib/systemdy/service.rb', line 136 LIST_OF_ACTIONS.each do |action| define_method action do return () unless exist? sudo = Etc.getpwuid(Process.uid).name != 'root' ? 'sudo' : '' `#{sudo} #{command} #{action} #{name}` end end |
#exist? ⇒ Boolean
method for check if a created service exist
59 60 61 |
# File 'lib/systemdy/service.rb', line 59 def exist? check_if_a_service_exist(name) # class method contained in Systemdy/utility/validator.rb end |
#is_active? ⇒ Boolean
check if the service is active
202 203 204 205 206 207 |
# File 'lib/systemdy/service.rb', line 202 LIST_OF_STATUSES.each do |status| define_method "is_#{status}?" do return () unless exist? remove_newline_from_system_command(`#{command} is-#{status} #{name}`) == status end end |
#is_enabled? ⇒ Boolean
check if the service is enabled
202 203 204 205 206 207 |
# File 'lib/systemdy/service.rb', line 202 LIST_OF_STATUSES.each do |status| define_method "is_#{status}?" do return () unless exist? remove_newline_from_system_command(`#{command} is-#{status} #{name}`) == status end end |
#mask ⇒ Object
This method is generated with use of metaprogramming techniques
execute action mask on the service
136 137 138 139 140 141 142 |
# File 'lib/systemdy/service.rb', line 136 LIST_OF_ACTIONS.each do |action| define_method action do return () unless exist? sudo = Etc.getpwuid(Process.uid).name != 'root' ? 'sudo' : '' `#{sudo} #{command} #{action} #{name}` end end |
#port ⇒ Object
This method return an error message when there are no port available
This method is generated with use of metaprogramming techniques
return the available port of the service
78 79 80 81 82 83 84 85 |
# File 'lib/systemdy/service.rb', line 78 LIST_OF_ESSENTIAL_INFO_LOOKUP.each_with_index do |info, index| define_method info do return () unless exist? essential_info = return_an_array_from(`#{INFO_LOOKUP_COMMAND} #{name}`, argument_splitter: ' ') return (info) if essential_info.nil? || essential_info.empty? return_an_array_from(essential_info[1], argument_splitter: '/')[index] end end |
#properties ⇒ Hash
For the sake of brevity, all service-related properties are not shown in this example
method for return a key/value pair of the service’s properties
159 160 161 162 |
# File 'lib/systemdy/service.rb', line 159 def properties array_of_properties = return_an_array_from_system_command(`#{command} show #{name}`) array_of_properties.collect { |property| { property.split('=')[0] => property.split('=')[1] } }.reduce({}, :merge) end |
#protocol ⇒ Object
This method return an error message when there are no protocol available
This method is generated with use of metaprogramming techniques
return the available protocol of the service
78 79 80 81 82 83 84 85 |
# File 'lib/systemdy/service.rb', line 78 LIST_OF_ESSENTIAL_INFO_LOOKUP.each_with_index do |info, index| define_method info do return () unless exist? essential_info = return_an_array_from(`#{INFO_LOOKUP_COMMAND} #{name}`, argument_splitter: ' ') return (info) if essential_info.nil? || essential_info.empty? return_an_array_from(essential_info[1], argument_splitter: '/')[index] end end |
#reload ⇒ Object
This method is generated with use of metaprogramming techniques
execute action reload on the service
136 137 138 139 140 141 142 |
# File 'lib/systemdy/service.rb', line 136 LIST_OF_ACTIONS.each do |action| define_method action do return () unless exist? sudo = Etc.getpwuid(Process.uid).name != 'root' ? 'sudo' : '' `#{sudo} #{command} #{action} #{name}` end end |
#restart ⇒ Object
This method is generated with use of metaprogramming techniques
execute action restart on the service
136 137 138 139 140 141 142 |
# File 'lib/systemdy/service.rb', line 136 LIST_OF_ACTIONS.each do |action| define_method action do return () unless exist? sudo = Etc.getpwuid(Process.uid).name != 'root' ? 'sudo' : '' `#{sudo} #{command} #{action} #{name}` end end |
#start ⇒ Object
This method is generated with use of metaprogramming techniques
execute action start on the service
136 137 138 139 140 141 142 |
# File 'lib/systemdy/service.rb', line 136 LIST_OF_ACTIONS.each do |action| define_method action do return () unless exist? sudo = Etc.getpwuid(Process.uid).name != 'root' ? 'sudo' : '' `#{sudo} #{command} #{action} #{name}` end end |
#status ⇒ Hash
method for return the current status of the provided service
183 184 185 186 |
# File 'lib/systemdy/service.rb', line 183 def status return () unless exist? filter_by_keys(properties, LIST_OF_STATUS_PROPERTIES) end |
#stop ⇒ Object
This method is generated with use of metaprogramming techniques
execute action stop on the service
136 137 138 139 140 141 142 |
# File 'lib/systemdy/service.rb', line 136 LIST_OF_ACTIONS.each do |action| define_method action do return () unless exist? sudo = Etc.getpwuid(Process.uid).name != 'root' ? 'sudo' : '' `#{sudo} #{command} #{action} #{name}` end end |
#unmask ⇒ Object
This method is generated with use of metaprogramming techniques
execute action unmask on the service
136 137 138 139 140 141 142 |
# File 'lib/systemdy/service.rb', line 136 LIST_OF_ACTIONS.each do |action| define_method action do return () unless exist? sudo = Etc.getpwuid(Process.uid).name != 'root' ? 'sudo' : '' `#{sudo} #{command} #{action} #{name}` end end |