Class: Yast::SystemdServiceClass

Inherits:
Module
  • Object
show all
Includes:
Logger
Defined in:
library/systemd/src/modules/systemd_service.rb

Overview

Systemd.service unit control API

Examples:

How to use it in other yast libraries

require 'yast'
Yast.import 'SystemdService'

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

service = Yast::SystemdService.find('sshd') # service unit object

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

service = Yast::SystemdService.find('sshd.service')

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

service = Yast::SystemdService.find!('IcanHasMoar') # SystemdServiceNotFound: 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 = Yast::SystemdService.find('sshd', :type=>'Type')
service.properties.type  # 'simple'

Defined Under Namespace

Classes: Service

Constant Summary collapse

UNIT_SUFFIX =
".service".freeze

Instance Method Summary collapse

Instance Method Details

#all(propmap = {}) ⇒ Array<Service>

Parameters:

Returns:



130
131
132
133
134
# File 'library/systemd/src/modules/systemd_service.rb', line 130

def all(propmap = {})
  Systemctl.service_units.map do |service_unit|
    Service.new(service_unit, propmap)
  end
end

#find(service_name, propmap = {}) ⇒ Service?

Returns `nil` if not found

Parameters:

Returns:

  • (Service, nil)

    `nil` if not found



77
78
79
80
81
82
# File 'library/systemd/src/modules/systemd_service.rb', line 77

def find(service_name, propmap = {})
  service_name += UNIT_SUFFIX unless service_name.end_with?(UNIT_SUFFIX)
  service = Service.new(service_name, propmap)
  return nil if service.properties.not_found?
  service
end

#find!(service_name, propmap = {}) ⇒ Service

Parameters:

Returns:

Raises:



88
89
90
# File 'library/systemd/src/modules/systemd_service.rb', line 88

def find!(service_name, propmap = {})
  find(service_name, propmap) || raise(SystemdServiceNotFound, service_name)
end

#find_many(service_names, propmap = {}) ⇒ Array<Service,nil>

Returns `nil` if not found

Parameters:

Returns:

  • (Array<Service,nil>)

    `nil` if not found



120
121
122
123
124
125
126
# File 'library/systemd/src/modules/systemd_service.rb', line 120

def find_many(service_names, propmap = {})
  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