Module: OsCtld::ContainerControl::Utils::Runscript::Frontend

Included in:
Commands::Exec::Frontend, Commands::Runscript::Frontend
Defined in:
lib/osctld/container_control/utils/runscript.rb

Constant Summary collapse

TRANSIENT_CLEANUP_TIMEOUT =

Bound exit cleanup separately from startup and the user's payload.

30

Instance Method Summary collapse

Instance Method Details

#add_network_opts(opts) ⇒ Object



38
39
40
41
42
# File 'lib/osctld/container_control/utils/runscript.rb', line 38

def add_network_opts(opts)
  opts.update(
    init_script: File.join('/', File.basename(init_script.path))
  )
end

#cleanup_init_script ⇒ Object



57
58
59
# File 'lib/osctld/container_control/utils/runscript.rb', line 57

def cleanup_init_script
  @init_script && unlink_file(@init_script.path)
end

#init_script ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/osctld/container_control/utils/runscript.rb', line 44

def init_script
  return @init_script if @init_script

  f = Tempfile.create(['.runscript', '.sh'], ct.get_run_conf.rootfs)
  f.chmod(0o500)
  f.puts('#!/bin/sh')
  f.puts('echo ready')
  f.puts('read _')
  f.close

  @init_script = f
end

#runscript_mode(run:, network:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/osctld/container_control/utils/runscript.rb', line 11

def runscript_mode(run:, network:)
  running = run ? ct.current_state == :running : ct.running?

  if running
    :running
  elsif run && network
    :run_network
  elsif run
    :run
  else
    raise ContainerControl::Error, 'container not running'
  end
end

#sync_state_after_transient_run(mode) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/osctld/container_control/utils/runscript.rb', line 25

def sync_state_after_transient_run(mode)
  return unless %i[run run_network].include?(mode)

  ct.current_state
  # STOPPED can precede the exit worker's rootfs remount. Use the
  # current or retired run's latched completion, not a fresh run config.
  promise = ct.get_exit_promise
  return unless promise
  return if promise.wait(timeout: TRANSIENT_CLEANUP_TIMEOUT)

  raise ContainerControl::Error, 'Transient container cleanup has not finished'
end


61
62
63
64
65
# File 'lib/osctld/container_control/utils/runscript.rb', line 61

def unlink_file(path)
  File.unlink(path)
rescue SystemCallError
  # pass
end