Class: Homebrew::Services::FormulaWrapper Private

Inherits:
Object
  • Object
show all
Includes:
Utils::Output::Mixin
Defined in:
services/formula_wrapper.rb

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Defined Under Namespace

Classes: StatusOutputSuccessType

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Output::Mixin

#issue_reporting_message, #odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #opoo_once, #opoo_outside_github_actions, #opoo_without_github_actions_annotation, #pretty_cannot_install, #pretty_deprecated, #pretty_disabled, #pretty_duration, #pretty_incompatible, #pretty_install_status, #pretty_installed, #pretty_removed, #pretty_uninstalled, #pretty_unmarked, #pretty_upgradable, #pretty_warning

Constructor Details

#initialize(formula, service_name: nil) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Initialize a new Service instance with supplied formula.

Parameters:

  • formula (Formula)
  • service_name (String, nil) (defaults to: nil)

Raises:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'services/formula_wrapper.rb', line 62

def initialize(formula, service_name: nil)
  @service_name = T.let(nil, T.nilable(String))
  @service_names = T.let(nil, T.nilable(T::Array[String]))
  @service_files = T.let(nil, T.nilable(T::Array[Pathname]))
  @service_startup = T.let(nil, T.nilable(T::Boolean))

  @formula = formula
  @service_name_override = service_name
  @status_output_success_type = T.let(nil, T.nilable(StatusOutputSuccessType))
  @loaded_service_names = T.let(nil, T.nilable(T::Array[String]))

  return if System.launchctl? || System.systemctl?

  raise UsageError, System::MISSING_DAEMON_MANAGER_EXCEPTION_MESSAGE
end

Instance Attribute Details

#formula ⇒ Formula (readonly)

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Access the Formula instance.

Returns:



15
16
17
# File 'services/formula_wrapper.rb', line 15

def formula
  @formula
end

Class Method Details

.from(path_or_label) ⇒ FormulaWrapper?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Create a new Service instance from either a path or label.

Parameters:

Returns:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'services/formula_wrapper.rb', line 19

def self.from(path_or_label)
  label = path_or_label.to_s.sub(/\.(?:plist|service)\z/, "")
  match = label.match(path_or_label_regex)
  return unless match

  service_name = match[1]
  formula_name = match[2]
  return if service_name.nil? || formula_name.nil?

  begin
    new(Formulary.factory(formula_name), service_name:)
  rescue
    nil
  end
end

.service_file_label(file) ⇒ String?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'services/formula_wrapper.rb', line 36

def self.service_file_label(file)
  return if file.nil? || !File.file?(file)

  require "plist"
  plist = begin
    Plist.parse_xml(file, marshal: false)
  rescue
    nil
  end
  if plist.nil? && File.binread(file, 8) == "bplist00"
    require "system_command"
    result = SystemCommand.run(
      "/usr/bin/plutil",
      args:         ["-convert", "xml1", "-o", "-", file],
      print_stderr: false,
    )
    plist = result.plist if result.success?
  end
  label = plist["Label"] if plist
  label if label.is_a?(String) && label.present?
rescue
  nil
end

Instance Method Details

#active_service_name ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



259
260
261
# File 'services/formula_wrapper.rb', line 259

def active_service_name
  status_output_success_type.service_name
end

#dest ⇒ Pathname

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Path to destination service. If run as root, it's in boot_path, else user_path.

Returns:



203
204
205
# File 'services/formula_wrapper.rb', line 203

def dest
  destinations.fetch(0)
end

#dest_dir ⇒ Pathname

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Path to destination service directory. If run as root, it's boot_path, else user_path.

Returns:



197
198
199
# File 'services/formula_wrapper.rb', line 197

def dest_dir
  System.root? ? System.boot_path : System.user_path
end

#destinations ⇒ Array<Pathname>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



208
209
210
# File 'services/formula_wrapper.rb', line 208

def destinations
  service_names.map { |name| dest_dir/service_file_basename(name) }
end

#error? ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


305
306
307
308
309
# File 'services/formula_wrapper.rb', line 305

def error?
  return false if pid?

  (exit_code = self.exit_code).present? && !exit_code.zero?
end

#exit_code ⇒ Integer?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Get current exit code of daemon process from status output.

Returns:



324
325
326
# File 'services/formula_wrapper.rb', line 324

def exit_code
  Regexp.last_match(1).to_i if status_output =~ exit_code_regex(status_type)
end

#installed? ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns true if any version of the formula is installed.

Returns:

  • (Boolean)


222
223
224
# File 'services/formula_wrapper.rb', line 222

def installed?
  formula.any_version_installed?
end

#keep_alive? ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Delegate access to formula.service.keep_alive?.

Returns:

  • (Boolean)


102
103
104
105
106
107
108
# File 'services/formula_wrapper.rb', line 102

def keep_alive?
  return @keep_alive unless @keep_alive.nil?

  keep_alive = service? && load_service.keep_alive?
  @keep_alive = T.let(keep_alive, T.nilable(T::Boolean))
  @keep_alive ||= false
end

#loaded?(cached: false) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns true if the service is loaded, else false.

Parameters:

  • cached (Boolean) (defaults to: false)

Returns:

  • (Boolean)


234
235
236
237
238
239
240
241
242
# File 'services/formula_wrapper.rb', line 234

def loaded?(cached: false)
  reset_cache! unless cached

  if System.launchctl?
    status_output_success_type.success
  else # System.systemctl?
    loaded_service_names.present?
  end
end

#loaded_file ⇒ String?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



329
330
331
# File 'services/formula_wrapper.rb', line 329

def loaded_file
  Regexp.last_match(1) if status_output =~ loaded_file_regex(status_type)
end

#loaded_service_names ⇒ Array<String>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



245
246
247
248
249
250
251
252
253
254
255
256
# File 'services/formula_wrapper.rb', line 245

def loaded_service_names
  @loaded_service_names ||= if System.systemctl?
    service_names.select do |name|
      (timed? && System::Systemctl.quiet_run("status", "#{name}.timer")) ||
        System::Systemctl.quiet_run("status", "#{name}.service")
    end
  elsif System.launchctl?
    launchctl_service_names.select { |name| System.launchctl_service_running?(name) }
  else
    []
  end
end

#name ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Delegate access to formula.name.

Returns:



80
81
82
# File 'services/formula_wrapper.rb', line 80

def name
  @name ||= T.let(formula.name, T.nilable(String))
end

#owner ⇒ String?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'services/formula_wrapper.rb', line 278

def owner
  if System.launchctl? && registered_destination.exist?
    # read the username from the plist file
    require "plist"
    plist = begin
      Plist.parse_xml(registered_destination.read, marshal: false)
    rescue
      nil
    end
    plist_username = plist["UserName"] if plist

    return plist_username if plist_username.present?
    return "root" if registered_destination.dirname == System.boot_path
    return System.user if registered_destination.dirname == System.user_path
  end
  return "root" if boot_path_service_file_present?
  return System.user if user_path_service_file_present?

  nil
end

#path_dirs ⇒ Array<Pathname>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



111
112
113
114
115
# File 'services/formula_wrapper.rb', line 111

def path_dirs
  return [] unless service?

  load_service.path_dirs
end

#pid ⇒ Integer?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Get current PID of daemon process from status output.

Returns:



318
319
320
# File 'services/formula_wrapper.rb', line 318

def pid
  Regexp.last_match(1).to_i if status_output =~ pid_regex(status_type)
end

#pid? ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


300
301
302
# File 'services/formula_wrapper.rb', line 300

def pid?
  (pid = self.pid).present? && pid.positive?
end

#registered_destination ⇒ Pathname

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



213
214
215
216
217
218
# File 'services/formula_wrapper.rb', line 213

def registered_destination
  active_destination = dest_dir/service_file_basename(active_service_name)
  return active_destination if active_destination.exist?

  destinations.find(&:exist?) || dest
end

#reset_cache! ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



227
228
229
230
# File 'services/formula_wrapper.rb', line 227

def reset_cache!
  @status_output_success_type = nil
  @loaded_service_names = nil
end

#service? ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Delegate access to formula.service?.

Returns:

  • (Boolean)


86
87
88
# File 'services/formula_wrapper.rb', line 86

def service?
  @service ||= T.let(formula.service?, T.nilable(T::Boolean))
end

#service_contents ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Generate the service file content (plist or systemd unit), including any per-service user environment variable overrides, or read the package-provided service file if the formula's service block does not define a command.

Returns:



390
391
392
393
394
395
396
397
398
# File 'services/formula_wrapper.rb', line 390

def service_contents
  if !service_file_generated?
    source_service_file.read
  elsif System.launchctl?
    load_service.to_plist
  else
    load_service.to_systemd_unit
  end
end

#service_file ⇒ Pathname

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

service_file delegates with formula.launchd_service_path or formula.systemd_service_path for systemd.

Returns:



146
147
148
# File 'services/formula_wrapper.rb', line 146

def service_file
  service_files.fetch(0)
end

#service_file_generated? ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


118
119
120
# File 'services/formula_wrapper.rb', line 118

def service_file_generated?
  service? && load_service.command?
end

#service_file_present?(type: nil) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns true if service is present (e.g. .plist is present in boot or user service path), else false Accepts type with values :root for boot path or :user for user path.

Parameters:

  • type (Symbol, nil) (defaults to: nil)

Returns:

  • (Boolean)


266
267
268
269
270
271
272
273
274
275
# File 'services/formula_wrapper.rb', line 266

def service_file_present?(type: nil)
  case type
  when :root
    boot_path_service_file_present?
  when :user
    user_path_service_file_present?
  else
    boot_path_service_file_present? || user_path_service_file_present?
  end
end

#service_files ⇒ Array<Pathname>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



151
152
153
154
155
156
157
# File 'services/formula_wrapper.rb', line 151

def service_files
  @service_files ||= if System.launchctl?
    formula.launchd_service_paths
  else # System.systemctl?
    formula.systemd_service_paths
  end
end

#service_name ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

service_name delegates with formula.plist_name or formula.service_name for systemd (e.g., sh.brew.<formula>).

Returns:



125
126
127
128
129
130
131
# File 'services/formula_wrapper.rb', line 125

def service_name
  @service_name ||= if System.launchctl?
    @service_name_override || formula.plist_name
  else # System.systemctl?
    @service_name_override || formula.service_name
  end
end

#service_names ⇒ Array<String>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



134
135
136
137
138
139
140
141
142
# File 'services/formula_wrapper.rb', line 134

def service_names
  @service_names ||= if @service_name_override
    [@service_name_override]
  elsif System.launchctl?
    formula.plist_names
  else # System.systemctl?
    formula.service_names
  end
end

#service_startup? ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Whether the service should be launched at startup

Returns:

  • (Boolean)


187
188
189
190
191
192
193
# File 'services/formula_wrapper.rb', line 187

def service_startup?
  @service_startup ||= if service?
    load_service.requires_root?
  else
    false
  end
end

#source_service_file ⇒ Pathname

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



160
161
162
# File 'services/formula_wrapper.rb', line 160

def source_service_file
  service_files.find(&:exist?) || service_file
end

#timed? ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Delegate access to formula.service.timed?.

Returns:

  • (Boolean)


92
93
94
95
96
97
98
# File 'services/formula_wrapper.rb', line 92

def timed?
  return @timed unless @timed.nil?

  timed = service? && load_service.timed?
  @timed = T.let(timed, T.nilable(T::Boolean))
  @timed ||= false
end

#timer_contents ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



401
402
403
404
405
406
407
408
409
410
411
412
# File 'services/formula_wrapper.rb', line 401

def timer_contents
  if service_file_generated?
    load_service.to_systemd_timer
  else
    service_pattern = Regexp.union(service_names.map { |name| "#{name}.service" })
    timer_file.read.sub(
      /^([ \t]*Unit[ \t]*=[ \t]*)#{service_pattern}([ \t]*)$/,
    ) do
      "#{Regexp.last_match(1)}#{service_name}.service#{Regexp.last_match(2)}"
    end
  end
end

#timer_dest ⇒ Pathname

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



176
177
178
# File 'services/formula_wrapper.rb', line 176

def timer_dest
  timer_destinations.fetch(0)
end

#timer_destinations ⇒ Array<Pathname>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



181
182
183
# File 'services/formula_wrapper.rb', line 181

def timer_destinations
  service_names.map { |name| dest_dir/"#{name}.timer" }
end

#timer_file ⇒ Pathname

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



165
166
167
168
# File 'services/formula_wrapper.rb', line 165

def timer_file
  files = formula.systemd_timer_paths
  files.find(&:exist?) || files.fetch(0)
end

#timer_name ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



171
172
173
# File 'services/formula_wrapper.rb', line 171

def timer_name
  "#{service_name}.timer"
end

#to_hash ⇒ Hash{Symbol => T.anything}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'services/formula_wrapper.rb', line 334

def to_hash
  status = if pid?
    :started
  elsif !loaded?(cached: true)
    :none
  elsif (last_exit_code = exit_code).present? && last_exit_code.zero?
    if timed?
      :scheduled
    else
      :stopped
    end
  elsif error?
    :error
  elsif unknown_status?
    :unknown
  else
    :other
  end

  hash = {
    name:,
    service_name: active_service_name,
    running:      pid?,
    loaded:       loaded?(cached: true),
    schedulable:  timed?,
    pid:,
    exit_code:,
    user:         owner,
    status:,
    file:         service_file_present? ? registered_destination : source_service_file,
    registered:   service_file_present?,
    loaded_file:,
  }

  return hash unless service?

  service = load_service

  return hash if service.command.blank?

  hash[:command] = service.manual_command
  hash[:working_dir] = service.working_dir
  hash[:root_dir] = service.root_dir
  hash[:log_path] = service.log_path
  hash[:error_log_path] = service.error_log_path
  hash[:interval] = service.interval
  hash[:cron] = service.cron.presence

  hash
end

#unknown_status? ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


312
313
314
# File 'services/formula_wrapper.rb', line 312

def unknown_status?
  status_output.blank? && !pid?
end