Class: Yast2::Systemd::Service

Inherits:
Unit
  • Object
show all
Includes:
Yast::Logger
Defined in:
library/systemd/src/lib/yast2/systemd/service.rb

Overview

API to manage a systemd.service unit

Examples:

How to use it in other yast libraries

require 'yast'
require 'yast2/systemd/service'

## Get a service unit by its name
## If the service unit can't be found, you'll get nil

service = Yast2::Systemd::Service.find('sshd') # service unit object

# or using the full unit id 'sshd.service'

service = Yast2::Systemd::Service.find('sshd.service')

## If you can't handle any nil at the place of calling,
## use the finder with exclamation mark;
## Systemd::ServiceNotFound exception will be raised

service = Yast2::Systemd::Service.find!('IcanHasMoar') # Systemd::ServiceNotFound: Service unit 'IcanHasMoar' not found

## Get basic unit properties

service.unit_name   # 'sshd'
service.unit_type   # 'service'
service.id          # 'sshd.service'
service.description # 'OpenSSH Daemon'
service.path        # '/usr/lib/systemd/system/sshd.service'
service.loaded?     # true if it's loaded, false otherwise
service.running?    # true if it's active and running
service.enabled?    # true if enabled, false otherwise
service.disabled?   # true if disabled, false otherwise
service.status      # the same text output you get with `systemctl status sshd.service`
service.show        # equivalent of calling `systemctl show sshd.service`

## Service unit file commands

# Unit file commands do modifications on the service unit. Calling them triggers
# service properties reloading. If a command fails, the error message is available
# through the method #error as a string.

service.start       # true if unit has been activated successfully
service.stop        # true if unit has been deactivated successfully
service.enable      # true if unit has been enabled successfully
service.disable     # true if unit has been disabled successfully
service.error       # error string available if some of the actions above fails

## Extended service properties

# In case you need more details about the service unit than the default ones,
# you can extend the parameters for .find method. Those properties are
# then available on the service unit object under the #properties instance method.
# An extended property is always a string, you must convert it manually,
# no automatical casting is done by yast.
# To get an overview of available service properties, try e.g., `systemctl show sshd.service`

service = Yast2::Systemd::Service.find('sshd', :type=>'Type')
service.properties.type  # 'simple'

Constant Summary collapse

UNIT_SUFFIX =
".service".freeze
START_SERVICE_INSTSYS_COMMAND =

Available only on installation system

"/bin/service_start".freeze

Constants inherited from Unit

Unit::SUPPORTED_TYPES

Instance Attribute Summary

Attributes inherited from Unit

#error, #name, #properties, #propmap, #unit_name, #unit_type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Unit

#command, #disable, #enable, #initialize, #refresh!, #reload, #reload_or_restart, #reload_or_try_restart, #show, #status, #try_restart

Constructor Details

This class inherits a constructor from Yast2::Systemd::Unit

Class Method Details

.all(propmap = UnitPropMap.new) ⇒ Array<Service>

Parameters:

Returns:



137
138
139
# File 'library/systemd/src/lib/yast2/systemd/service.rb', line 137

def all(propmap = UnitPropMap.new)
  Systemctl.service_units.map { |s| new(s, propmap) }
end

.build(service_name, propmap = UnitPropMap.new) ⇒ Service

Instantiate a Systemd::Service object based on the given name

Use with caution as the service might exist or not. If you need to react when the service does not exist, use Systemd::Service.find.

Parameters:

Returns:

  • (Service)

    System service with the given name



149
150
151
152
# File 'library/systemd/src/lib/yast2/systemd/service.rb', line 149

def build(service_name, propmap = UnitPropMap.new)
  service_name += UNIT_SUFFIX unless service_name.end_with?(UNIT_SUFFIX)
  new(service_name, propmap)
end

.find(service_name, propmap = UnitPropMap.new) ⇒ Service?

Returns nil if not found.

Parameters:

Returns:

  • (Service, nil)

    nil if not found



85
86
87
88
89
90
# File 'library/systemd/src/lib/yast2/systemd/service.rb', line 85

def find(service_name, propmap = UnitPropMap.new)
  service = build(service_name, propmap)
  return nil if service.properties.not_found?

  service
end

.find!(service_name, propmap = UnitPropMap.new) ⇒ Service

Parameters:

Returns:

Raises:



96
97
98
# File 'library/systemd/src/lib/yast2/systemd/service.rb', line 96

def find!(service_name, propmap = UnitPropMap.new)
  find(service_name, propmap) || raise(Systemd::ServiceNotFound, service_name)
end

.find_many(service_names, propmap = UnitPropMap.new) ⇒ Array<Service,nil>

Returns nil if not found.

Parameters:

Returns:

  • (Array<Service,nil>)

    nil if not found



127
128
129
130
131
132
133
# File 'library/systemd/src/lib/yast2/systemd/service.rb', line 127

def find_many(service_names, propmap = UnitPropMap.new)
  services = find_many_at_once(service_names, propmap)
  return services unless services.empty?

  log.info "Retrying one by one"
  service_names.map { |n| find(n, propmap) }
end

Instance Method Details

#pidString

Returns:

  • (String)


161
162
163
# File 'library/systemd/src/lib/yast2/systemd/service.rb', line 161

def pid
  properties.pid
end

#restartObject



183
184
185
186
187
188
189
190
# File 'library/systemd/src/lib/yast2/systemd/service.rb', line 183

def restart
  # Delegate to Systemd::Unit#restart if not within installation
  return super unless installation_system?

  stop
  sleep(1)
  start
end

#running?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'library/systemd/src/lib/yast2/systemd/service.rb', line 165

def running?
  properties.running?
end

#socketYast2::Systemd::Socket?

Returns socket associated with service or nil if there is no such socket



196
197
198
# File 'library/systemd/src/lib/yast2/systemd/service.rb', line 196

def socket
  @socket ||= Socket.for_service(name)
end

#socket?Boolean

Determines whether the service has an associated socket

Returns:

  • (Boolean)

    true if an associated socket exists; false otherwise.



203
204
205
# File 'library/systemd/src/lib/yast2/systemd/service.rb', line 203

def socket?
  !socket.nil?
end

#startObject



173
174
175
176
# File 'library/systemd/src/lib/yast2/systemd/service.rb', line 173

def start
  command = "#{START_SERVICE_INSTSYS_COMMAND} #{unit_name.shellescape}"
  installation_system? ? run_instsys_command(command) : super
end

#static?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'library/systemd/src/lib/yast2/systemd/service.rb', line 169

def static?
  properties.static?
end

#stopObject



178
179
180
181
# File 'library/systemd/src/lib/yast2/systemd/service.rb', line 178

def stop
  command = "#{START_SERVICE_INSTSYS_COMMAND} --stop #{unit_name.shellescape}"
  installation_system? ? run_instsys_command(command) : super
end