Module: Runit

Defined in:
lib/runit.rb,
lib/runit/config.rb

Defined Under Namespace

Classes: Config

Constant Summary collapse

SERVICE_SHORTCUTS =
{
  'rails' => 'rails-*',
  'tunnel' => 'tunnel_*',
  'praefect' => 'praefect*',
  'gitaly' => '{gitaly,praefect*}',
  'db' => '{redis,redis-cluster,postgresql,postgresql-geo,clickhouse}',
  'rails-migration-dependencies' => '{redis,redis-cluster,postgresql,postgresql-geo,gitaly,praefect*,minio}',
  'workhorse' => 'gitlab-workhorse'
}.freeze
SERVICES_DIR =
Pathname.new(__dir__).join('../services').expand_path
LOG_DIR =
Pathname.new(__dir__).join('../log').expand_path
ALL_DATA_ORIENTED_SERVICE_NAMES =
%w[minio openldap gitaly praefect redis redis-cluster postgresql-geo postgresql].freeze
STOP_RETRY_COUNT =
3

Class Method Summary collapse

Class Method Details

.all_service_namesObject



187
188
189
190
191
192
193
194
# File 'lib/runit.rb', line 187

def self.all_service_names
  return [] unless SERVICES_DIR.exist?

  # praefect-gitaly-* services are stopped/started automatically.
  Pathname.new(SERVICES_DIR).children.filter_map do |path|
    path.basename.to_s if path.directory? && !path.basename.to_s.start_with?('praefect-gitaly-')
  end.sort
end

.cleaned_path_envObject

Runit does not handle ENOTDIR from execve well, so let’s try to prevent that. gitlab.com/gitlab-org/gitlab-development-kit/issues/666#note_241939982



64
65
66
67
68
69
70
# File 'lib/runit.rb', line 64

def self.cleaned_path_env
  valid_path_entries = ENV['PATH'].split(File::PATH_SEPARATOR).select do |dir|
    File.directory?(dir)
  end

  { 'PATH' => valid_path_entries.join(File::PATH_SEPARATOR) }
end

.data_oriented_service_namesObject



177
178
179
180
181
# File 'lib/runit.rb', line 177

def self.data_oriented_service_names
  ALL_DATA_ORIENTED_SERVICE_NAMES.select do |service_name|
    SERVICES_DIR.join(service_name).exist?
  end
end

.ensure_services_are_supervised(services) ⇒ Object



173
174
175
# File 'lib/runit.rb', line 173

def self.ensure_services_are_supervised(services)
  services.each { |svc| wait_runsv_supervise_ok!(svc) }
end

.expand_services(services) ⇒ Object



196
197
198
199
200
201
202
# File 'lib/runit.rb', line 196

def self.expand_services(services)
  return SERVICES_DIR.glob('*').sort if services.empty?

  services.flat_map do |svc|
    service_shortcut(svc) || SERVICES_DIR.join(svc)
  end.uniq.sort
end

.kill_processes(pids) ⇒ Object



280
281
282
283
284
285
# File 'lib/runit.rb', line 280

def self.kill_processes(pids)
  pids.each do |pid|
    Process.kill('TERM', pid)
  rescue SystemCallError
  end
end

.log_files(services) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
# File 'lib/runit.rb', line 254

def self.log_files(services)
  return LOG_DIR.glob(File.join('*', 'current')) if services.empty?

  services.flat_map do |svc|
    shortcut = log_shortcut(svc)
    next shortcut if shortcut

    current_log = LOG_DIR.join(svc, 'current')
    current_log if current_log.exist?
  end.compact.uniq
end

.log_shortcut(svc) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/runit.rb', line 266

def self.log_shortcut(svc)
  glob = SERVICE_SHORTCUTS[svc]
  return unless glob

  if glob.include?('/')
    GDK::Output.error "invalid service shortcut: #{svc} -> #{glob}"

    abort
  end

  shortcut_logs = LOG_DIR.glob(File.join(glob, 'current'))
  shortcut_logs unless shortcut_logs.empty?
end

.non_data_oriented_service_namesObject



183
184
185
# File 'lib/runit.rb', line 183

def self.non_data_oriented_service_names
  all_service_names - data_oriented_service_names
end

.runit_installed!Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/runit.rb', line 88

def self.runit_installed!
  return if Utils.executable_exist?('runsvdir')

  abort <<~MESSAGE

    ERROR: gitlab-development-kit requires Runit to be installed.
    You can install Runit with:

      #{runit_instructions}

  MESSAGE
end

.runit_instructionsObject



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/runit.rb', line 101

def self.runit_instructions
  if GDK::Dependencies.homebrew_available?
    'brew install runit'
  elsif GDK::Dependencies.macports_available?
    'sudo port install runit'
  elsif GDK::Dependencies.linux_apt_available?
    'sudo apt install runit'
  else
    '(no copy-paste Runit installation snippet available for your OS)'
  end
end

.runsvdir_base_argsObject



72
73
74
# File 'lib/runit.rb', line 72

def self.runsvdir_base_args
  ['runsvdir', '-P', GDK.root.join('services').to_s]
end

.runsvdir_pid(args) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/runit.rb', line 76

def self.runsvdir_pid(args)
  pgrep = Shellout.new(%w[pgrep runsvdir]).run
  return if pgrep.empty?

  pids = pgrep.split("\n").map { |str| Integer(str) }
  runsvdir_ps = "#{args.join(' ')} "

  pids.find do |pid|
    Shellout.new(%W[ps -o args= -p #{pid}]).run.start_with?(runsvdir_ps)
  end
end

.service_shortcut(svc) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/runit.rb', line 204

def self.service_shortcut(svc)
  glob = SERVICE_SHORTCUTS[svc]
  return unless glob

  if glob.include?('/')
    GDK::Output.error "invalid service shortcut: #{svc} -> #{glob}"

    abort
  end

  shortcut_services = SERVICES_DIR.glob(glob)
  shortcut_services.empty? ? nil : shortcut_services
end

.start(services, quiet: false) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/runit.rb', line 113

def self.start(services, quiet: false)
  services = Array(services)

  if services.empty?
    # Redis, PostgresSQL, etc should be started first.
    data_oriented_service_names.reverse_each.all? { |service_name| sv('start', [service_name], quiet: quiet) }
    services = non_data_oriented_service_names
  end

  sv('start', services, quiet: quiet)
end

.start_runsvdirObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/runit.rb', line 24

def self.start_runsvdir
  runit_installed!

  runit_config = Runit::Config.new(GDK.root)

  if GDK.config.gdk.experimental.ruby_services?
    # To make transition easier, we merge legacy services that haven't been migrated yet
    # so that using experimental ruby services will always working even when partially migrated
    services = GDK::Services.enabled
    new_services = services.map(&:name)
    legacy_services = runit_config.services_from_procfile.reject { |legacy| new_services.include?(legacy.name) }

    runit_config.render(services: services + legacy_services)
  else
    runit_config.render
  end

  # It is important that we use an absolute path with `runsvdir`: this
  # allows us to distinguish processes belonging to different GDK
  # installations on the same machine.
  args = runsvdir_base_args
  return if runsvdir_pid(args)

  dots = '.' * 395

  Process.fork do
    Dir.chdir('/')
    Process.setsid

    # Cargo-culting the use of 395 periods from omnibus-gitlab.
    # https://gitlab.com/gitlab-org/omnibus-gitlab/blob/5dfdcafa30ad6e203a04a917f180b630d5121cf6/config/templates/runit/runsvdir-start.erb#L42
    args << "log: #{dots}"

    spawn(cleaned_path_env, *args, in: '/dev/null', out: '/dev/null', err: '/dev/null')
  end
end

.stop(quiet: false) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/runit.rb', line 125

def self.stop(quiet: false)
  # Redis, PostgresSQL, etc should be stopped last.
  stop_services(non_data_oriented_service_names, quiet: quiet)
  data_oriented_service_names.all? { |service_name| stop_services([service_name], quiet: quiet) }

  unload_runsvdir!
end

.stop_services(services, quiet: false) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/runit.rb', line 133

def self.stop_services(services, quiet: false)
  # The first stop attempt may fail; ignore its return value.
  stopped = false

  STOP_RETRY_COUNT.times do |i|
    # From http://smarden.org/runit/sv.8.html:
    #
    # down: If the service is running, send it the TERM signal, and the CONT signal. If ./run exits, start ./finish if it exists. After it stops, do not restart service.
    # force-stop: Same as down, but wait up to (default) 7 seconds for the service to become down. Then report the status, and on timeout send the service the kill command.
    #
    stopped = sv('force-stop', services, quiet: quiet)
    break if stopped

    GDK::Output.notice("Retrying stop (#{i + 1}/#{STOP_RETRY_COUNT})")
  end

  true
end

.sv(cmd, services, quiet: false) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/runit.rb', line 158

def self.sv(cmd, services, quiet: false)
  start_runsvdir
  expanded_services = expand_services(services)
  ensure_services_are_supervised(expanded_services)

  expanded_services = expanded_services.filter { |es| !es.to_s.include?('redis-cluster') } unless GDK.config.redis_cluster.enabled?
  return true if expanded_services.empty? # silent skip assuming successful

  command = ['sv', '-w', GDK.config.gdk.runit_wait_secs.to_s, cmd, *expanded_services.map(&:to_s)]

  sh = Shellout.new(command, chdir: GDK.root)
  quiet ? sh.run : sh.stream
  sh.success?
end

.tail(services) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/runit.rb', line 240

def self.tail(services)
  log_files_for_services = log_files(services)
  if log_files_for_services.empty?
    GDK::Output.warn(<<~MSG)
      No matching services to tail.

      To view a list of services and shortcuts, run `gdk tail --help`.
    MSG
    return true
  end

  exec('tail', '-qF', *log_files_for_services.map(&:to_s))
end

.unload_runsvdir!Object



152
153
154
155
156
# File 'lib/runit.rb', line 152

def self.unload_runsvdir!
  # Unload runsvdir: this is safe because we have just stopped all services.
  pid = runsvdir_pid(runsvdir_base_args)
  !Process.kill('HUP', pid).nil?
end

.wait_runsv_supervise_ok!(service_dir) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/runit.rb', line 218

def self.wait_runsv_supervise_ok!(service_dir)
  unless service_dir.directory?
    GDK::Output.error "unknown runit service: #{service_dir}"

    abort
  end

  50.times do
    begin
      service_dir.join('supervise', 'ok').open(File::WRONLY | File::NONBLOCK).close
    rescue StandardError
      sleep 0.1
      next
    end
    return
  end

  GDK::Output.error "timeout waiting for runsv in #{service_dir}"

  abort
end