Class: Captive::Commands::OneOffSession

Inherits:
Object
  • Object
show all
Defined in:
lib/captive/commands/one_off_session.rb

Overview

Exécute une commande dans un Job one-off qui reprend l'image et l'env de l'app, attaché devant un TTY (humain), en suivant ses logs sinon (agent, CI, pipe).

La session est tirée avant tout le reste et remise à l'appelant pour qu'il l'inscrive au fil, puis elle nomme le Job. Un one-off abandonné avant même sa création laisse donc une entrée qui pointe vers un log simplement vide, jamais vers la session d'un autre.

Instance Method Summary collapse

Constructor Details

#initialize(kubectl:, banner: UI::Banner.new, session_id: SessionId, tty: Terminal.method(:interactive?)) ⇒ OneOffSession

Returns a new instance of OneOffSession.



17
18
19
20
21
22
# File 'lib/captive/commands/one_off_session.rb', line 17

def initialize(kubectl:, banner: UI::Banner.new, session_id: SessionId, tty: Terminal.method(:interactive?))
  @kubectl    = kubectl
  @banner     = banner
  @session_id = session_id
  @tty        = tty
end

Instance Method Details

#attach(app, command) ⇒ Object



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

def attach(app, command)
  session = @session_id.generate.tap { |id| yield id }
  @banner.show(app)
  job = create_job(app, command, session, interactive: true)
  pod = @kubectl.wait_job_pod(app, job)
  begin
    @kubectl.wait_pod_ready(app, pod)
    @kubectl.attach(app, pod)
  ensure
    cleanup(app, job, pod)
  end
end

#interactive? ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/captive/commands/one_off_session.rb', line 24

def interactive?
  @tty.call
end

#run(app, command, &register) ⇒ Integer?

Returns exit code of the one-off command (nil when attached).

Returns:

  • (Integer, nil) —

    exit code of the one-off command (nil when attached)



29
30
31
# File 'lib/captive/commands/one_off_session.rb', line 29

def run(app, command, &register)
  interactive? ? attach(app, command, &register) : stream(app, command, &register)
end