Class: Yast2::Systemd::Target

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

Overview

API to manage a systemd.target unit

Examples:

How to find a custom systemd target


require 'yast'
require 'yast2/systemd/target'

# This will return either a target object or nil
target = Yast2::Systemd::Target.find('graphical')

# This returns target object or raises exception Systemd::TargetNotFound
target = Yast2::Systemd::Target.find!('whatever')

# This returns collection of all available targets
Yast2::Systemd::Target.all

How to find the current default target


target = Yast2::Systemd::Target.get_default
target.unit_name      # name of the default target
target.allow_isolate? # should return true

Set the default target


Yast2::Systemd::Target.set_default('multi-user') # returns true if success

# Or if we have already an target object, use this for default target
target = Yast2::Systemd::Target.find('graphical')
target.allow_isolate? # must be true to set default target
target.set_default # returns true if success

Constant Summary collapse

UNIT_SUFFIX =
".target".freeze
DEFAULT_TARGET =
"default.target".freeze
PROPMAP =
{ allow_isolate: "AllowIsolate" }.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, #restart, #show, #start, #status, #stop, #try_restart

Constructor Details

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

Class Method Details

.all(propmap = {}) ⇒ Object

Parameters:


74
75
76
77
78
79
# File 'library/systemd/src/lib/yast2/systemd/target.rb', line 74

def all(propmap = {})
  targets = Systemctl.target_units.map do |target_unit_name|
    find(target_unit_name, propmap)
  end
  targets.compact
end

.find(target_name, propmap = {}) ⇒ Object

Parameters:


56
57
58
59
60
61
62
63
64
65
66
# File 'library/systemd/src/lib/yast2/systemd/target.rb', line 56

def find(target_name, propmap = {})
  target_name += UNIT_SUFFIX unless target_name.end_with?(UNIT_SUFFIX)
  target = new(target_name, PROPMAP.merge(propmap))

  if target.properties.not_found?
    log.error "Target #{target_name} not found: #{target.properties.inspect}"
    return nil
  end

  target
end

.find!(target_name, propmap = {}) ⇒ Object

Parameters:


69
70
71
# File 'library/systemd/src/lib/yast2/systemd/target.rb', line 69

def find!(target_name, propmap = {})
  find(target_name, propmap) || raise(Systemd::TargetNotFound, target_name)
end

.get_defaultObject

rubocop:disable Naming/AccessorMethodName

Raises:

  • (SystemctlError)

81
82
83
84
85
86
# File 'library/systemd/src/lib/yast2/systemd/target.rb', line 81

def get_default # rubocop:disable Naming/AccessorMethodName
  result = Systemctl.execute("get-default")
  raise(SystemctlError, result) unless result.exit.zero?

  find(result.stdout.strip)
end

.set_default(target) ⇒ Object

rubocop:disable Naming/AccessorMethodName


88
89
90
91
92
93
94
95
96
97
# File 'library/systemd/src/lib/yast2/systemd/target.rb', line 88

def set_default(target) # rubocop:disable Naming/AccessorMethodName
  target_unit = target.is_a?(Systemd::Target) ? target : find(target)

  unless target_unit
    log.error "Cannot find target #{target.inspect}"
    return false
  end

  target_unit.set_default
end

Instance Method Details

#allow_isolate?Boolean

Returns:

  • (Boolean)

100
101
102
103
104
105
# File 'library/systemd/src/lib/yast2/systemd/target.rb', line 100

def allow_isolate?
  # We cannot find out a target properties from /mnt in inst-sys
  # systemctl doesn't return any properties in chroot
  # See bnc#889323
  ["yes", nil].include?(properties.allow_isolate)
end

#set_defaultObject


107
108
109
110
111
112
113
114
115
116
117
118
# File 'library/systemd/src/lib/yast2/systemd/target.rb', line 107

def set_default
  unless allow_isolate?
    log.error "Cannot set #{id.inspect} as default target: Cannot be isolated (#{properties.allow_isolate})"
    return false
  end

  # Constructing a fallback target ID if we can't get it from systemctl
  target_name = id || "#{name}.target"

  result = Systemctl.execute("set-default --force #{target_name}")
  result.exit.zero?
end