Module: SvCtl

Defined in:
lib/svctl.rb,
lib/svctl/service.rb,
lib/svctl/version.rb,
lib/svctl/item_file.rb,
lib/svctl/cli/command.rb

Defined Under Namespace

Modules: Cli Classes: ItemFile, Service

Constant Summary collapse

RUNSVDIR =

Directory with runlevels

'/etc/runit/runsvdir'.freeze
SERVICE_DIR =

Directory with available services

'/etc/runit/services'.freeze
PROTECTED_SERVICES_FILE =

A list of protected services

'/run/runit/protected-services.txt'.freeze
VERSION =
'24.05.0'.freeze

Class Method Summary collapse

Class Method Details

.all_servicesArray<Service>

List all available services

Returns:



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/svctl.rb', line 17

def self.all_services
  svdir = File.join(SERVICE_DIR)
  ret = []

  Dir.entries(svdir).each do |v|
    next if %w[. ..].include?(v)

    ret << Service.new(v, nil)
  end

  ret
end

.disable(service, runlevel = 'current') ⇒ Object

Disable service from selected runlevel

Parameters:

  • service (String)
  • runlevel (String) (defaults to: 'current')


59
60
61
62
63
64
# File 'lib/svctl.rb', line 59

def self.disable(service, runlevel = 'current')
  sv = Service.new(service, runlevel)
  raise 'service not found' unless sv.exist?

  sv.disable if sv.enabled?
end

.enable(service, runlevel = 'current') ⇒ Object

Enable service in selected runlevel

Parameters:

  • service (String)
  • runlevel (String) (defaults to: 'current')


49
50
51
52
53
54
# File 'lib/svctl.rb', line 49

def self.enable(service, runlevel = 'current')
  sv = Service.new(service, runlevel)
  raise 'service not found' unless sv.exist?

  sv.enable
end

.protect(service_pattern) ⇒ Object

Ignore service changes on system configuration switch

Parameters:

  • service_pattern (String)


68
69
70
71
72
# File 'lib/svctl.rb', line 68

def self.protect(service_pattern)
  ItemFile.new(PROTECTED_SERVICES_FILE) do |list|
    list << service_pattern
  end
end

.protected_servicesArray<String>

List protected services

Returns:

  • (Array<String>)


84
85
86
87
88
89
90
91
92
# File 'lib/svctl.rb', line 84

def self.protected_services
  ret = nil

  ItemFile.new(PROTECTED_SERVICES_FILE) do |list|
    ret = list.get
  end

  ret
end

.runlevelString

Returns current runlevel.

Returns:

  • (String)

    current runlevel



110
111
112
# File 'lib/svctl.rb', line 110

def self.runlevel
  File.basename(File.readlink(File.join(RUNSVDIR, 'current')))
end

.runlevel_services(runlevel = 'current') ⇒ Array<Service>

List seervices from selected runlevel

Parameters:

  • runlevel (String) (defaults to: 'current')

Returns:



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/svctl.rb', line 33

def self.runlevel_services(runlevel = 'current')
  svdir = File.join(RUNSVDIR, runlevel)
  ret = []

  Dir.entries(svdir).each do |v|
    next if %w[. ..].include?(v)

    ret << Service.new(v, runlevel)
  end

  ret
end

.runlevelsArray<String>

List all runlevels

Returns:

  • (Array<String>)


96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/svctl.rb', line 96

def self.runlevels
  ret = []

  Dir.entries(RUNSVDIR).each do |v|
    next if %w[. .. current previous].include?(v)
    next unless Dir.exist?(File.join(RUNSVDIR, v))

    ret << v
  end

  ret
end

.switch(runlevel) ⇒ Object

Switch to new runlevel

Parameters:

  • runlevel (String)


116
117
118
# File 'lib/svctl.rb', line 116

def self.switch(runlevel)
  system('runsvchdir', runlevel)
end

.unprotect(service_pattern) ⇒ Object

Do not ignore service changes on system configuration switch

Parameters:

  • service_pattern (String)


76
77
78
79
80
# File 'lib/svctl.rb', line 76

def self.unprotect(service_pattern)
  ItemFile.new(PROTECTED_SERVICES_FILE) do |list|
    list.delete(service_pattern)
  end
end