Class: Captive::Commands::DbRestore

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

Constant Summary collapse

TEMP_REMOTE =
"/controller/tmp/dump.tmp"
EXIT_FILE =
"/controller/tmp/restore.exit"
LOG_FILE =
"/controller/tmp/restore.log"
FLUX_KUSTOMIZATION =
"services"

Instance Method Summary collapse

Constructor Details

#initialize(kubectl:, trail:, poller: nil, flux: nil) ⇒ DbRestore

Returns a new instance of DbRestore.



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

def initialize(kubectl:, trail:, poller: nil, flux: nil)
  @kubectl = kubectl
  @trail   = trail
  @poller  = poller || Poller.new(interval: 10, timeout: 1800)
  @flux    = flux || Flux.new
end

Instance Method Details

#detect_format(filename) ⇒ Object



25
26
27
28
29
30
# File 'lib/captive/commands/db_restore.rb', line 25

def detect_format(filename)
  basename = File.basename(filename)
  return :custom if basename.match?(/\.(pgsql|dump|custom|tar\.gz|tgz)$/)

  :sql
end

#postgres_pod(app) ⇒ Object



32
33
34
# File 'lib/captive/commands/db_restore.rb', line 32

def postgres_pod(app)
  @kubectl.postgres_pod_name(app, app)
end

#run(app, dump_file) {|:scaling_down| ... } ⇒ Object

Yields:

  • (:scaling_down)

Raises:

  • (Errno::ENOENT)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/captive/commands/db_restore.rb', line 36

def run(app, dump_file)
  dump_file = resolve_path(dump_file)
  raise Errno::ENOENT, dump_file unless File.exist?(dump_file)

  # Après le contrôle du fichier : un dump introuvable n'a touché à rien, l'inscrire au
  # fil de l'app serait du bruit.
  @trail.db_restored(app)

  pg_pod    = postgres_pod(app)
  password  = @kubectl.postgres_secret_password(app, app)
  format    = detect_format(dump_file)
  stream    = build_stream_cmd(dump_file)

  yield :scaling_down if block_given?
  @flux.suspend_kustomization(FLUX_KUSTOMIZATION)
  @kubectl.scale(app, app, 0)

  begin
    yield :transferring if block_given?
    @kubectl.transfer_file_to_pod(app, pg_pod, stream, TEMP_REMOTE)
    yield :restoring if block_given?
    on_tick = lambda { |elapsed|
      activity = @kubectl.exec_on_pod_output(app, pg_pod, current_activity_cmd)
      yield(:waiting, elapsed, activity&.strip) if block_given?
    }
    restore_on_pod(app, pg_pod, password, format, &on_tick)
    cleanup_pod(app, pg_pod)
  ensure
    yield :scaling_up if block_given?
    @kubectl.scale(app, app, 1)
    @flux.resume_kustomization(FLUX_KUSTOMIZATION)
  end
end