Class: Systemdy::Service

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Systemdy::Service

method for create a new Systemdy::Service object

Examples:

Create an object

my_postgresql_service = Systemdy::Service.new('postgresql')

Parameters:

  • name (String)

    the name of the systemd service to control



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

#commandString (readonly)

the default ‘systemctl’ command

Returns:

  • (String)

    the current value of command



5
6
7
# File 'lib/systemdy/service.rb', line 5

def command
  @command
end

#nameString (readonly)

the name of the service

Returns:

  • (String)

    the current value of name



5
6
7
# File 'lib/systemdy/service.rb', line 5

def name
  @name
end

Instance Method Details

#disableObject

Note:

This method is generated with use of metaprogramming techniques

execute action disable on the service

Examples:

disable a service

my_postgresql_service.disable


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 default_error_message() unless exist?
        sudo = Etc.getpwuid(Process.uid).name != 'root' ? 'sudo' : ''
        `#{sudo} #{command} #{action} #{name}` 
    end
end

#enableObject

Note:

This method is generated with use of metaprogramming techniques

execute action anable on the service

Examples:

enable a service

my_postgresql_service.enable


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 default_error_message() 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

Examples:

Create an object

my_postgresql_service.exist? #=> true

Returns:

  • (Boolean)

    the presence of the service on the system



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

Examples:

check if a service is active

my_postgresql_service.is_active? #=> true

Returns:

  • (Boolean)

    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 default_error_message() unless exist?
        remove_newline_from_system_command(`#{command} is-#{status} #{name}`) == status
    end
end

#is_enabled?Boolean

check if the service is enabled

Examples:

check if a service is enabled

my_postgresql_service.is_enabled? #=> true

Returns:

  • (Boolean)

    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 default_error_message() unless exist?
        remove_newline_from_system_command(`#{command} is-#{status} #{name}`) == status
    end
end

#maskObject

Note:

This method is generated with use of metaprogramming techniques

execute action mask on the service

Examples:

mask a service

my_postgresql_service.mask


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 default_error_message() unless exist?
        sudo = Etc.getpwuid(Process.uid).name != 'root' ? 'sudo' : ''
        `#{sudo} #{command} #{action} #{name}` 
    end
end

#portObject

TODO:

This method return an error message when there are no port available

Note:

This method is generated with use of metaprogramming techniques

return the available port of the service

Examples:

get the service port

my_postgresql_service.port #=> "5432"


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 default_error_message() unless exist?
        essential_info = return_an_array_from(`#{INFO_LOOKUP_COMMAND} #{name}`, argument_splitter: ' ')
        return info_lookup_error_message(info) if essential_info.nil? || essential_info.empty?
        return_an_array_from(essential_info[1], argument_splitter: '/')[index]
    end
end

#propertiesHash

Note:

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

Examples:

display all the properties related to a service


my_postgresql_service.properties 

  {
    "Type"=>"oneshot",              
    "Restart"=>"no",          
    "ExecMainPID"=>"48615",
    "NotifyAccess"=>"none"
  }

Returns:

  • (Hash)

    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

#protocolObject

TODO:

This method return an error message when there are no protocol available

Note:

This method is generated with use of metaprogramming techniques

return the available protocol of the service

Examples:

restart a service

my_postgresql_service.protocol #=> "tcp"


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 default_error_message() unless exist?
        essential_info = return_an_array_from(`#{INFO_LOOKUP_COMMAND} #{name}`, argument_splitter: ' ')
        return info_lookup_error_message(info) if essential_info.nil? || essential_info.empty?
        return_an_array_from(essential_info[1], argument_splitter: '/')[index]
    end
end

#reloadObject

Note:

This method is generated with use of metaprogramming techniques

execute action reload on the service

Examples:

reload a service

my_postgresql_service.reload


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 default_error_message() unless exist?
        sudo = Etc.getpwuid(Process.uid).name != 'root' ? 'sudo' : ''
        `#{sudo} #{command} #{action} #{name}` 
    end
end

#restartObject

Note:

This method is generated with use of metaprogramming techniques

execute action restart on the service

Examples:

restart a service

my_postgresql_service.restart


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 default_error_message() unless exist?
        sudo = Etc.getpwuid(Process.uid).name != 'root' ? 'sudo' : ''
        `#{sudo} #{command} #{action} #{name}` 
    end
end

#startObject

Note:

This method is generated with use of metaprogramming techniques

execute action start on the service

Examples:

start a service

my_postgresql_service.start


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 default_error_message() unless exist?
        sudo = Etc.getpwuid(Process.uid).name != 'root' ? 'sudo' : ''
        `#{sudo} #{command} #{action} #{name}` 
    end
end

#statusHash

method for return the current status of the provided service

Examples:

display the current status of the service

my_postgresql_service.status

  {
    "Id"=>"postgresql.service",              
    "Description"=>"PostgreSQL RDBMS",          
    "ExecMainPID"=>"48615",
    "LoadState"=>"loaded",
    "ActiveState"=>"active",
    "FragmentPath"=>"/lib/Systemdy/system/postgresql.service",
    "ActiveEnterTimestamp"=>"Thu 2022-09-29 17:13:07 CEST",
    "InactiveEnterTimestamp"=>"Thu 2022-09-29 17:12:44 CEST",
    "ActiveExitTimestamp"=>"Thu 2022-09-29 17:12:44 CEST",
    "InactiveExitTimestamp"=>"Thu 2022-09-29 17:13:07 CEST"
  }

Returns:

  • (Hash)

    the service’s current status



183
184
185
186
# File 'lib/systemdy/service.rb', line 183

def status 
    return default_error_message() unless exist?
    filter_by_keys(properties, LIST_OF_STATUS_PROPERTIES) 
end

#stopObject

Note:

This method is generated with use of metaprogramming techniques

execute action stop on the service

Examples:

stop a service

my_postgresql_service.stop


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 default_error_message() unless exist?
        sudo = Etc.getpwuid(Process.uid).name != 'root' ? 'sudo' : ''
        `#{sudo} #{command} #{action} #{name}` 
    end
end

#unmaskObject

Note:

This method is generated with use of metaprogramming techniques

execute action unmask on the service

Examples:

unmask a service

my_postgresql_service.unmask


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 default_error_message() unless exist?
        sudo = Etc.getpwuid(Process.uid).name != 'root' ? 'sudo' : ''
        `#{sudo} #{command} #{action} #{name}` 
    end
end