Class: DBus::Systemd::Manager

Inherits:
Object
  • Object
show all
Includes:
DBus::Systemd::Mixin::MethodMissing, DBus::Systemd::Mixin::Properties
Defined in:
lib/dbus/systemd/manager.rb

Constant Summary collapse

NODE =

systemd manager object dbus node path

'/org/freedesktop/systemd1'.freeze
INTERFACE =

systemd manager dbus interface

'org.freedesktop.systemd1.Manager'.freeze
UNIT_INDICES =

index map of unit array returned by ListUnits

{
  name: 0,
  description: 1,
  load_state: 2,
  active_state: 3,
  sub_state: 4,
  following: 5,
  object_path: 6,
  job_id: 7,
  job_type: 8,
  job_object_path: 9
}.freeze
JOB_INDICES =

index map of job array returned by ListJobs

{
  id: 0,
  unit: 1,
  type: 2,
  state: 3,
  object_path: 4,
  unit_object_path: 5
}.freeze

Instance Attribute Summary collapse

Attributes included from DBus::Systemd::Mixin::MethodMissing

#object

Instance Method Summary collapse

Methods included from DBus::Systemd::Mixin::Properties

#properties

Methods included from DBus::Systemd::Mixin::MethodMissing

#method_missing, #respond_to_missing?

Constructor Details

#initialize(bus = Systemd::Helpers.system_bus) ⇒ Manager

Create a systemd manager dbus proxy object

Parameters:

  • bus (DBus::SystemBus, DBus::SessionBus) (defaults to: Systemd::Helpers.system_bus)

    bus instance



77
78
79
80
81
82
# File 'lib/dbus/systemd/manager.rb', line 77

def initialize(bus = Systemd::Helpers.system_bus)
  @service = bus.service(Systemd::SERVICE)
  @object = @service.object(NODE)
  @object.default_iface = INTERFACE
  @object.introspect
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class DBus::Systemd::Mixin::MethodMissing

Instance Attribute Details

#serviceDBus::Service (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (DBus::Service)


71
72
73
# File 'lib/dbus/systemd/manager.rb', line 71

def service
  @service
end

Instance Method Details

#get_job_by_object_path(path) ⇒ DBus::Systemd::Job

get job by dbus node path

Parameters:

  • path (String)

    job dbus node path

Returns:



143
144
145
146
147
# File 'lib/dbus/systemd/manager.rb', line 143

def get_job_by_object_path(path)
  obj = @service.object(path)
                .tap(&:introspect)
  Job.new(obj.Get(Job::INTERFACE, 'Id').first, self)
end

#get_unit_by_object_path(path) ⇒ DBus::Systemd::Unit

get unit object by dbus node path

Parameters:

  • path (String)

    unit dbus node path

Returns:



106
107
108
109
110
# File 'lib/dbus/systemd/manager.rb', line 106

def get_unit_by_object_path(path)
  obj = @service.object(path)
                .tap(&:introspect)
  Unit.new(obj.Get(Unit::INTERFACE, 'Id').first, self)
end

#job(id) ⇒ DBus::Systemd::Job

get job by id

Parameters:

  • id (Integer)

    job id

Returns:



134
135
136
# File 'lib/dbus/systemd/manager.rb', line 134

def job(id)
  Job.new(id, self)
end

#jobsArray

array of jobs from ListJobs mapped to property hashes

Returns:

  • (Array)

    array of job property hashes



125
126
127
# File 'lib/dbus/systemd/manager.rb', line 125

def jobs
  self.ListJobs.first.map { |j| map_job(j) }
end

#map_job(job_array) ⇒ Hash

map job array from ListJobs to property hash

Parameters:

  • job_array (Array)

    job property array as returned by ListJobs

Returns:

  • (Hash)

    mapped job property hash



154
155
156
# File 'lib/dbus/systemd/manager.rb', line 154

def map_job(job_array)
  Helpers.map_array(job_array, JOB_INDICES)
end

#map_unit(unit_array) ⇒ Hash

map unit array from ListUnits to property hash

Parameters:

  • unit_array (Array)

    array as returned from ListUnits

Returns:

  • (Hash)

    unit property hash



117
118
119
# File 'lib/dbus/systemd/manager.rb', line 117

def map_unit(unit_array)
  Helpers.map_array(unit_array, UNIT_INDICES)
end

#unit(name) ⇒ DBus::Systemd::Unit

get a unit dbus proxy object by name

Parameters:

  • name (String)

    unit name (e.g. ‘sshd.service’)

Returns:



97
98
99
# File 'lib/dbus/systemd/manager.rb', line 97

def unit(name)
  Unit.new(name, self)
end

#unitsArray

get an array of mapped units/unit properties

Returns:

  • (Array)

    array of mapped unit property hashes



88
89
90
# File 'lib/dbus/systemd/manager.rb', line 88

def units
  self.ListUnits.first.map { |u| map_unit(u) }
end