captive-cli 1.9.0 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/captive/activity_trail.rb +0 -1
- data/lib/captive/commands/addons_commands.rb +17 -0
- data/lib/captive/commands/db_commands.rb +7 -32
- data/lib/captive/commands/domains_commands.rb +7 -1
- data/lib/captive/commands/supabase_realtime_change.rb +32 -0
- data/lib/captive/commands/user_login.rb +8 -3
- data/lib/captive/dashboard/addons_resource.rb +7 -0
- data/lib/captive/errors.rb +0 -1
- data/lib/captive/kubectl.rb +0 -24
- data/lib/captive/loader.rb +1 -1
- data/lib/captive/presenter/edge_function.rb +2 -1
- data/lib/captive/presenter/supabase_realtime.rb +38 -0
- data/lib/captive/version.rb +1 -1
- metadata +3 -3
- data/lib/captive/commands/db_restore.rb +0 -145
- data/lib/captive/flux.rb +0 -24
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fbb9f68824463afe138a9f65081be8fe6b582b6ac25c6ec8235e7b64c45832cc
|
|
4
|
+
data.tar.gz: 0f112e5a5350bb911c77eed140d6162c5c99a78be0c065f4ac326a3c6a80ecf7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5f9ea0724abef388ba43af81aa988820cd51e047b55bfa1b81919f7b6c0e1bc06fd98af49be1c29596d917715f0a8b5943bb277f91bbe0ce0d00c7125fc0b92d
|
|
7
|
+
data.tar.gz: f4f7c16ef49fe3bb08bcef569da7cd5e5197eb50d68ac59b5012a3d33e84a79657aafb8de19ceea682ec06f4ea5bf35599df87ae44dfae889d901b14daa0d75e
|
|
@@ -19,7 +19,6 @@ module Captive
|
|
|
19
19
|
# passe par kubectl exec et n'est capturé nulle part, donc n'en porte pas.
|
|
20
20
|
def console_opened(app, session: nil) = signaler(app, "console", session: session)
|
|
21
21
|
def db_dumped(app) = signaler(app, "db_dump")
|
|
22
|
-
def db_restored(app) = signaler(app, "db_restore")
|
|
23
22
|
def db_psql_opened(app) = signaler(app, "db_psql")
|
|
24
23
|
|
|
25
24
|
def db_query_run(app, sql, read_only:)
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative "supabase_auth_change"
|
|
4
|
+
require_relative "supabase_realtime_change"
|
|
4
5
|
|
|
5
6
|
module Captive
|
|
6
7
|
module Commands
|
|
@@ -66,6 +67,22 @@ module Captive
|
|
|
66
67
|
puts SupabaseAuthChange.new(addons: addons_api, paint: paint).run(app, options)
|
|
67
68
|
end
|
|
68
69
|
|
|
70
|
+
desc "realtime APP", "Affiche ou modifie l'option Temps réel de l'add-on Supabase"
|
|
71
|
+
long_desc <<~DESC
|
|
72
|
+
Sans option : affiche l'option Temps réel (diffusion, présence) et Postgres Changes.
|
|
73
|
+
Avec --enabled (et --postgres-changes, désactivé s'il est omis) : les modifie.
|
|
74
|
+
Postgres Changes exige PostgreSQL 18.6. Réservé aux admins de l'Organization.
|
|
75
|
+
|
|
76
|
+
\x05Exemples :
|
|
77
|
+
\x05 captive addons realtime my-app-staging
|
|
78
|
+
\x05 captive addons realtime my-app-staging --enabled=true --postgres-changes=true
|
|
79
|
+
DESC
|
|
80
|
+
method_option :enabled, enum: %w[true false], desc: "Option Temps réel"
|
|
81
|
+
method_option :postgres_changes, enum: %w[true false], desc: "Écoute des changements de la base"
|
|
82
|
+
def realtime(app)
|
|
83
|
+
puts SupabaseRealtimeChange.new(addons: addons_api, paint: paint).run(app, options)
|
|
84
|
+
end
|
|
85
|
+
|
|
69
86
|
private
|
|
70
87
|
|
|
71
88
|
def presenter = Presenter::Addon.new(paint: paint)
|
|
@@ -69,44 +69,19 @@ module Captive
|
|
|
69
69
|
abort paint.red("✗ #{e.message}")
|
|
70
70
|
end
|
|
71
71
|
|
|
72
|
-
desc "restore APP DUMP_FILE", "
|
|
72
|
+
desc "restore APP DUMP_FILE", "Restauration de base : passe par l'équipe plateforme"
|
|
73
73
|
long_desc <<~DESC
|
|
74
|
-
|
|
75
|
-
|
|
74
|
+
La restauration arrête l'app et suspend Flux : elle est réservée à l'équipe
|
|
75
|
+
plateforme. Transmets-lui le dump et la commande à lancer.
|
|
76
76
|
|
|
77
77
|
\x05Exemple :
|
|
78
78
|
\x05 captive db restore my-app dump.pgsql
|
|
79
|
-
\x05 captive db restore my-app 20260602_backup.tar.gz
|
|
80
79
|
DESC
|
|
81
80
|
def restore(app, dump_file)
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
scaling_up: "Relance de l'app...",
|
|
87
|
-
}
|
|
88
|
-
spinner = restore_spinner
|
|
89
|
-
DbRestore.new(kubectl: kubectl, trail: trail).run(app, dump_file) do |step, elapsed = nil, info = nil|
|
|
90
|
-
if step == :waiting
|
|
91
|
-
spinner.auto_spin unless spinner.spinning?
|
|
92
|
-
spinner.update(info: (info && !info.empty?) ? info : "en cours... #{elapsed.to_i}s")
|
|
93
|
-
else
|
|
94
|
-
spinner.success if spinner.spinning?
|
|
95
|
-
puts paint.dim(" → #{labels[step]}")
|
|
96
|
-
end
|
|
97
|
-
end
|
|
98
|
-
puts paint.green(" ✓ Restauration terminée.")
|
|
99
|
-
rescue Errno::ENOENT => e
|
|
100
|
-
abort paint.red("✗ Fichier introuvable : #{e.message}")
|
|
101
|
-
rescue Captive::RestoreError => e
|
|
102
|
-
spinner.error if spinner.spinning?
|
|
103
|
-
abort paint.red("✗ #{e.message}")
|
|
104
|
-
end
|
|
105
|
-
|
|
106
|
-
private
|
|
107
|
-
|
|
108
|
-
def restore_spinner
|
|
109
|
-
TTY::Spinner.new(" [:spinner] :info", format: :dots)
|
|
81
|
+
abort paint.yellow(<<~MSG)
|
|
82
|
+
La restauration de base passe par l'équipe plateforme. Transmets-lui le dump et cette commande :
|
|
83
|
+
captive-admin db restore #{app} #{dump_file}
|
|
84
|
+
MSG
|
|
110
85
|
end
|
|
111
86
|
end
|
|
112
87
|
end
|
|
@@ -43,7 +43,7 @@ module Captive
|
|
|
43
43
|
def add(app, hostname)
|
|
44
44
|
domains_api.add(app, hostname)
|
|
45
45
|
announcer.call("#{hostname} ajouté") { |wait| wait.until_applied(app, hostname) }
|
|
46
|
-
|
|
46
|
+
dns_notice(app) unless options[:wait]
|
|
47
47
|
end
|
|
48
48
|
|
|
49
49
|
desc "rm APP DOMAINE", "Supprime un domaine de l'app"
|
|
@@ -90,6 +90,12 @@ module Captive
|
|
|
90
90
|
private
|
|
91
91
|
|
|
92
92
|
def presenter = Presenter::Domain.new(paint: paint)
|
|
93
|
+
# Le nom exact (sous-domaine ou « @ ») vient de l'API une fois l'ajout appliqué ; la cible est connue dès maintenant.
|
|
94
|
+
def dns_notice(app)
|
|
95
|
+
puts " Chez votre fournisseur DNS, créez un CNAME vers #{app}.app.captive.dev."
|
|
96
|
+
puts paint.dim(" (domaine racine : un enregistrement A, voir captive domains list #{app})")
|
|
97
|
+
end
|
|
98
|
+
|
|
93
99
|
def domain_wait = DomainWait.new(domains: domains_api, timeout: options[:timeout])
|
|
94
100
|
|
|
95
101
|
def announcer
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../errors"
|
|
4
|
+
|
|
5
|
+
module Captive
|
|
6
|
+
module Commands
|
|
7
|
+
# `addons realtime` : sans réglage, les lignes qui décrivent l'option ; avec --enabled, l'état
|
|
8
|
+
# voulu complet envoyé au dashboard, que captive-platform applique plus tard (ADR 0039, 0043).
|
|
9
|
+
# Postgres Changes absent vaut false, comme côté dashboard.
|
|
10
|
+
class SupabaseRealtimeChange
|
|
11
|
+
def initialize(addons:, paint:)
|
|
12
|
+
@addons = addons
|
|
13
|
+
@paint = paint
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# `options` : les options Thor de la commande, `enabled` et `postgres_changes` ("true"/"false").
|
|
17
|
+
def run(app, options)
|
|
18
|
+
enabled, postgres_changes = options.values_at("enabled", "postgres_changes")
|
|
19
|
+
return etat(app) unless enabled || postgres_changes
|
|
20
|
+
raise ValidationError, "Préciser --enabled avec --postgres-changes." unless enabled
|
|
21
|
+
|
|
22
|
+
@addons.set_supabase_realtime(app, enabled: enabled == "true", postgres_changes: postgres_changes == "true")
|
|
23
|
+
[ @paint.green(" ✓ Option Temps réel en cours de modification : #{CLIDependencies::ASYNC_NOTICE}"),
|
|
24
|
+
@paint.dim(" Suivre : captive addons realtime #{app}"), ]
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
def etat(app) = Presenter::SupabaseRealtime.new(paint: @paint).lines(@addons.supabase_realtime(app), app: app)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -5,12 +5,17 @@ require "fileutils"
|
|
|
5
5
|
module Captive
|
|
6
6
|
module Commands
|
|
7
7
|
class UserLogin
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
# Sous WSL, wslview ouvre le navigateur Windows ; sans aucune commande, l'URL affichée suffit.
|
|
9
|
+
Browser = Struct.new(:open_cmds) do
|
|
10
|
+
def self.for(platform = RUBY_PLATFORM)
|
|
11
|
+
new(platform.include?("darwin") ? %w[open] : %w[wslview xdg-open])
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def open(url) = open_cmds.any? { |cmd| system(cmd, url) }
|
|
10
15
|
end
|
|
11
16
|
|
|
12
17
|
def initialize(github:, cluster:, store:, kubeconfig_path:,
|
|
13
|
-
ui: method(:puts), browser: Browser.
|
|
18
|
+
ui: method(:puts), browser: Browser.for)
|
|
14
19
|
@github = github
|
|
15
20
|
@cluster = cluster
|
|
16
21
|
@store = store
|
|
@@ -7,6 +7,7 @@ module Captive
|
|
|
7
7
|
class AddonsResource
|
|
8
8
|
PATH = AppPath.new("addons")
|
|
9
9
|
SUPABASE_AUTH_PATH = AppPath.new("addons/supabase/auth")
|
|
10
|
+
SUPABASE_REALTIME_PATH = AppPath.new("addons/supabase/realtime")
|
|
10
11
|
|
|
11
12
|
def initialize(api:)
|
|
12
13
|
@api = api
|
|
@@ -21,6 +22,12 @@ module Captive
|
|
|
21
22
|
def set_supabase_auth(app, autoconfirm:, signup_open:)
|
|
22
23
|
@api.put(SUPABASE_AUTH_PATH.call(app), { autoconfirm: autoconfirm, signup_open: signup_open })
|
|
23
24
|
end
|
|
25
|
+
|
|
26
|
+
def supabase_realtime(app) = @api.get(SUPABASE_REALTIME_PATH.call(app))
|
|
27
|
+
|
|
28
|
+
def set_supabase_realtime(app, enabled:, postgres_changes:)
|
|
29
|
+
@api.put(SUPABASE_REALTIME_PATH.call(app), { enabled: enabled, postgres_changes: postgres_changes })
|
|
30
|
+
end
|
|
24
31
|
end
|
|
25
32
|
end
|
|
26
33
|
end
|
data/lib/captive/errors.rb
CHANGED
|
@@ -4,7 +4,6 @@ module Captive
|
|
|
4
4
|
class NoPodFound < StandardError; end
|
|
5
5
|
class UnknownAppType < StandardError; end
|
|
6
6
|
class PollTimeout < StandardError; end
|
|
7
|
-
class RestoreError < StandardError; end
|
|
8
7
|
class DumpError < StandardError; end
|
|
9
8
|
class ExecError < StandardError; end
|
|
10
9
|
class AuthError < StandardError; end
|
data/lib/captive/kubectl.rb
CHANGED
|
@@ -139,17 +139,6 @@ module Captive
|
|
|
139
139
|
run("create", "secret", "generic", name, "-n", namespace, *literals, "--dry-run=client", "-o", "yaml")
|
|
140
140
|
end
|
|
141
141
|
|
|
142
|
-
def scale(namespace, deployment, replicas)
|
|
143
|
-
run("scale", "deployment", deployment, "-n", namespace, "--replicas=#{replicas}")
|
|
144
|
-
end
|
|
145
|
-
|
|
146
|
-
def restore(namespace, pod, dump_file, format)
|
|
147
|
-
cmd = format == :custom ?
|
|
148
|
-
[ "pg_restore", "--clean", "--if-exists", "--no-owner", "--no-acl", "-d", "app" ] :
|
|
149
|
-
[ "psql", "app" ]
|
|
150
|
-
exec_with_file(namespace, pod, dump_file, *cmd)
|
|
151
|
-
end
|
|
152
|
-
|
|
153
142
|
def admin_credentials(namespace, app)
|
|
154
143
|
[ ADMIN_ROLE, get_secret_password(namespace, ADMIN_SECRET) ]
|
|
155
144
|
rescue RuntimeError => e
|
|
@@ -224,10 +213,6 @@ module Captive
|
|
|
224
213
|
end
|
|
225
214
|
end
|
|
226
215
|
|
|
227
|
-
def transfer_file_to_pod(namespace, pod, stream_cmd, remote_path)
|
|
228
|
-
system_pipe("#{stream_cmd} | #{kubectl_cmd} exec -i #{pod} -n #{namespace} -- bash -c 'cat > #{remote_path}'")
|
|
229
|
-
end
|
|
230
|
-
|
|
231
216
|
def dump_from_pod(namespace, pod, local_path, cmd)
|
|
232
217
|
system_pipe("#{kubectl_cmd} exec #{pod} -n #{namespace} -- bash -c '#{cmd}' > #{Shellwords.escape(local_path)}")
|
|
233
218
|
end
|
|
@@ -244,15 +229,6 @@ module Captive
|
|
|
244
229
|
system("bash", "-c", cmd)
|
|
245
230
|
end
|
|
246
231
|
|
|
247
|
-
def exec_on_pod(namespace, pod, cmd)
|
|
248
|
-
run_interactive("exec", pod, "-n", namespace, "--", "bash", "-c", cmd)
|
|
249
|
-
end
|
|
250
|
-
|
|
251
|
-
def exec_on_pod_output(namespace, pod, cmd)
|
|
252
|
-
out, _err, status = Open3.capture3("kubectl", *kubeconfig_args, "exec", pod, "-n", namespace, "--", "bash", "-c", cmd)
|
|
253
|
-
status.success? ? out : nil
|
|
254
|
-
end
|
|
255
|
-
|
|
256
232
|
def run(*args)
|
|
257
233
|
out, err, status = Open3.capture3("kubectl", *kubeconfig_args, *args)
|
|
258
234
|
raise AuthError, "Votre session a expiré. Reconnectez-vous avec : captive login" if err.include?("Unauthorized")
|
data/lib/captive/loader.rb
CHANGED
|
@@ -25,6 +25,7 @@ require_relative "presenter/scheduled_task"
|
|
|
25
25
|
require_relative "presenter/domain"
|
|
26
26
|
require_relative "presenter/addon"
|
|
27
27
|
require_relative "presenter/supabase_auth"
|
|
28
|
+
require_relative "presenter/supabase_realtime"
|
|
28
29
|
require_relative "presenter/edge_function"
|
|
29
30
|
require_relative "activity_trail"
|
|
30
31
|
require_relative "errors"
|
|
@@ -39,4 +40,3 @@ require_relative "commands/apps_restart"
|
|
|
39
40
|
require_relative "commands/domain_wait"
|
|
40
41
|
require_relative "commands/domain_announcer"
|
|
41
42
|
require_relative "commands/db_dump"
|
|
42
|
-
require_relative "commands/db_restore"
|
|
@@ -14,7 +14,8 @@ module Captive
|
|
|
14
14
|
def function(fonction)
|
|
15
15
|
erreurs = fonction["errors_24h"]
|
|
16
16
|
erreurs = erreurs.positive? ? @paint.red("#{erreurs} erreurs") : "0 erreur"
|
|
17
|
-
"#{@paint.bold(fonction["name"])} #{fonction["invocations_24h"]} appels #{erreurs} (24 h)"
|
|
17
|
+
ligne = "#{@paint.bold(fonction["name"])} #{fonction["invocations_24h"]} appels #{erreurs} (24 h)"
|
|
18
|
+
fonction["public"] ? "#{ligne} #{@paint.yellow("public")}" : ligne
|
|
18
19
|
end
|
|
19
20
|
|
|
20
21
|
def invocation(appel)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Captive
|
|
4
|
+
module Presenter
|
|
5
|
+
# L'option Temps réel de l'Add-on Supabase, comme sur la page Realtime du dashboard.
|
|
6
|
+
# Postgres Changes n'est disponible qu'à partir de PostgreSQL 18.6 (ADR 0043).
|
|
7
|
+
class SupabaseRealtime
|
|
8
|
+
VALEURS = { true => "oui", false => "non" }.freeze
|
|
9
|
+
|
|
10
|
+
def initialize(paint:)
|
|
11
|
+
@paint = paint
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def lines(settings, app:)
|
|
15
|
+
[
|
|
16
|
+
"Temps réel #{valeur(settings["enabled"])}",
|
|
17
|
+
[ "Postgres Changes #{valeur(settings["postgres_changes"] || false)}", indisponible(settings) ].compact.join(" "),
|
|
18
|
+
(@paint.yellow("Changement en cours…") if settings["pending"]),
|
|
19
|
+
@paint.dim(modification(settings, app)),
|
|
20
|
+
].compact
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def modification(settings, app)
|
|
26
|
+
return "Modifiable par un admin de l'Organization" unless settings["can_edit"]
|
|
27
|
+
|
|
28
|
+
"Modifier : captive addons realtime #{app} --enabled=true|false [--postgres-changes=true|false]"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def valeur(value) = VALEURS.fetch(value, "inconnu")
|
|
32
|
+
|
|
33
|
+
def indisponible(settings)
|
|
34
|
+
@paint.dim("(PostgreSQL 18.6 requis)") if settings["postgres_changes_available"] == false
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
data/lib/captive/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: captive-cli
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.10.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Captive Studio
|
|
@@ -119,7 +119,6 @@ files:
|
|
|
119
119
|
- lib/captive/commands/db_dump.rb
|
|
120
120
|
- lib/captive/commands/db_exec.rb
|
|
121
121
|
- lib/captive/commands/db_psql.rb
|
|
122
|
-
- lib/captive/commands/db_restore.rb
|
|
123
122
|
- lib/captive/commands/db_target.rb
|
|
124
123
|
- lib/captive/commands/domain_announcer.rb
|
|
125
124
|
- lib/captive/commands/domain_wait.rb
|
|
@@ -132,6 +131,7 @@ files:
|
|
|
132
131
|
- lib/captive/commands/run.rb
|
|
133
132
|
- lib/captive/commands/supabase_auth_change.rb
|
|
134
133
|
- lib/captive/commands/supabase_commands.rb
|
|
134
|
+
- lib/captive/commands/supabase_realtime_change.rb
|
|
135
135
|
- lib/captive/commands/user_login.rb
|
|
136
136
|
- lib/captive/credentials.rb
|
|
137
137
|
- lib/captive/credentials_store.rb
|
|
@@ -146,7 +146,6 @@ files:
|
|
|
146
146
|
- lib/captive/dashboard_client.rb
|
|
147
147
|
- lib/captive/dashboard_response.rb
|
|
148
148
|
- lib/captive/errors.rb
|
|
149
|
-
- lib/captive/flux.rb
|
|
150
149
|
- lib/captive/github_device_flow.rb
|
|
151
150
|
- lib/captive/http_client.rb
|
|
152
151
|
- lib/captive/kubectl.rb
|
|
@@ -164,6 +163,7 @@ files:
|
|
|
164
163
|
- lib/captive/presenter/log_colorizer.rb
|
|
165
164
|
- lib/captive/presenter/scheduled_task.rb
|
|
166
165
|
- lib/captive/presenter/supabase_auth.rb
|
|
166
|
+
- lib/captive/presenter/supabase_realtime.rb
|
|
167
167
|
- lib/captive/session_id.rb
|
|
168
168
|
- lib/captive/session_token.rb
|
|
169
169
|
- lib/captive/shell_completions.rb
|
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "pathname"
|
|
4
|
-
require "open3"
|
|
5
|
-
require "shellwords"
|
|
6
|
-
require_relative "../errors"
|
|
7
|
-
require_relative "../poller"
|
|
8
|
-
require_relative "../flux"
|
|
9
|
-
|
|
10
|
-
module Captive
|
|
11
|
-
module Commands
|
|
12
|
-
class DbRestore
|
|
13
|
-
TEMP_REMOTE = "/controller/tmp/dump.tmp"
|
|
14
|
-
EXIT_FILE = "/controller/tmp/restore.exit"
|
|
15
|
-
LOG_FILE = "/controller/tmp/restore.log"
|
|
16
|
-
FLUX_KUSTOMIZATION = "services"
|
|
17
|
-
|
|
18
|
-
def initialize(kubectl:, trail:, poller: nil, flux: nil)
|
|
19
|
-
@kubectl = kubectl
|
|
20
|
-
@trail = trail
|
|
21
|
-
@poller = poller || Poller.new(interval: 10, timeout: 1800)
|
|
22
|
-
@flux = flux || Flux.new
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
def detect_format(filename)
|
|
26
|
-
basename = File.basename(filename)
|
|
27
|
-
return :custom if basename.match?(/\.(pgsql|dump|custom|tar\.gz|tgz)$/)
|
|
28
|
-
|
|
29
|
-
:sql
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
def postgres_pod(app)
|
|
33
|
-
@kubectl.postgres_pod_name(app, app)
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
def run(app, dump_file)
|
|
37
|
-
dump_file = resolve_path(dump_file)
|
|
38
|
-
raise Errno::ENOENT, dump_file unless File.exist?(dump_file)
|
|
39
|
-
|
|
40
|
-
# Après le contrôle du fichier : un dump introuvable n'a touché à rien, l'inscrire au
|
|
41
|
-
# fil de l'app serait du bruit.
|
|
42
|
-
@trail.db_restored(app)
|
|
43
|
-
|
|
44
|
-
pg_pod = postgres_pod(app)
|
|
45
|
-
password = @kubectl.postgres_secret_password(app, app)
|
|
46
|
-
format = detect_format(dump_file)
|
|
47
|
-
stream = build_stream_cmd(dump_file)
|
|
48
|
-
|
|
49
|
-
yield :scaling_down if block_given?
|
|
50
|
-
@flux.suspend_kustomization(FLUX_KUSTOMIZATION)
|
|
51
|
-
@kubectl.scale(app, app, 0)
|
|
52
|
-
|
|
53
|
-
begin
|
|
54
|
-
yield :transferring if block_given?
|
|
55
|
-
@kubectl.transfer_file_to_pod(app, pg_pod, stream, TEMP_REMOTE)
|
|
56
|
-
yield :restoring if block_given?
|
|
57
|
-
on_tick = lambda { |elapsed|
|
|
58
|
-
activity = @kubectl.exec_on_pod_output(app, pg_pod, current_activity_cmd)
|
|
59
|
-
yield(:waiting, elapsed, activity&.strip) if block_given?
|
|
60
|
-
}
|
|
61
|
-
restore_on_pod(app, pg_pod, password, format, &on_tick)
|
|
62
|
-
cleanup_pod(app, pg_pod)
|
|
63
|
-
ensure
|
|
64
|
-
yield :scaling_up if block_given?
|
|
65
|
-
@kubectl.scale(app, app, 1)
|
|
66
|
-
@flux.resume_kustomization(FLUX_KUSTOMIZATION)
|
|
67
|
-
end
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
private
|
|
71
|
-
|
|
72
|
-
def inner_is_custom?(file)
|
|
73
|
-
inner = `tar -tzf #{Shellwords.escape(file)} 2>/dev/null | grep -Ev '/$' | head -1`.strip
|
|
74
|
-
inner.match?(/\.(pgsql|dump|custom)$/)
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
def build_stream_cmd(file)
|
|
78
|
-
basename = File.basename(file)
|
|
79
|
-
escaped = Shellwords.escape(file)
|
|
80
|
-
|
|
81
|
-
if basename.match?(/\.tar\.gz$|\.tgz$/)
|
|
82
|
-
inner = `tar -tzf #{escaped} 2>/dev/null | grep -Ev '/$' | head -1`.strip
|
|
83
|
-
"tar -xOf #{escaped} #{Shellwords.escape(inner)}"
|
|
84
|
-
elsif basename.match?(/\.(gz)$/)
|
|
85
|
-
"gunzip -c #{escaped}"
|
|
86
|
-
else
|
|
87
|
-
"cat #{escaped}"
|
|
88
|
-
end
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
def restore_on_pod(app, pg_pod, password, format, &on_tick)
|
|
92
|
-
pgpass = Shellwords.escape(password)
|
|
93
|
-
@kubectl.exec_on_pod(app, pg_pod,
|
|
94
|
-
"PGPASSWORD=#{pgpass} psql -h localhost -U app -d app " \
|
|
95
|
-
"-c 'DROP SCHEMA public CASCADE; CREATE SCHEMA public; GRANT ALL ON SCHEMA public TO app;' 2>&1")
|
|
96
|
-
|
|
97
|
-
cmd = restore_cmd(password, format)
|
|
98
|
-
inner_script = "#{cmd}; echo $? > #{EXIT_FILE}"
|
|
99
|
-
@kubectl.exec_on_pod(app, pg_pod,
|
|
100
|
-
"rm -f #{EXIT_FILE}; nohup bash -c #{Shellwords.escape(inner_script)} > #{LOG_FILE} 2>&1 < /dev/null &")
|
|
101
|
-
|
|
102
|
-
exit_code = wait_for_exit_code(app, pg_pod, &on_tick)
|
|
103
|
-
raise RestoreError, "La restauration a échoué (code #{exit_code})" unless exit_code == "0"
|
|
104
|
-
end
|
|
105
|
-
|
|
106
|
-
def wait_for_exit_code(app, pg_pod, &on_tick)
|
|
107
|
-
exit_code = nil
|
|
108
|
-
@poller.poll_until(context: "fin du restore (#{EXIT_FILE})", on_tick: on_tick) do
|
|
109
|
-
exit_code = @kubectl.exec_on_pod_output(app, pg_pod, "cat #{EXIT_FILE} 2>/dev/null")
|
|
110
|
-
exit_code && !exit_code.strip.empty?
|
|
111
|
-
end
|
|
112
|
-
exit_code.strip
|
|
113
|
-
end
|
|
114
|
-
|
|
115
|
-
def restore_cmd(password, format)
|
|
116
|
-
pgpass = Shellwords.escape(password)
|
|
117
|
-
if format == :custom
|
|
118
|
-
"(PGPASSWORD=#{pgpass} pg_restore --no-owner --no-acl --verbose " \
|
|
119
|
-
"-h localhost -U app -d app #{TEMP_REMOTE} 2>&1 | grep -v 'no privileges'; exit ${PIPESTATUS[0]})"
|
|
120
|
-
else
|
|
121
|
-
"PGPASSWORD=#{pgpass} psql -h localhost -U app -d app < #{TEMP_REMOTE} 2>&1"
|
|
122
|
-
end
|
|
123
|
-
end
|
|
124
|
-
|
|
125
|
-
def current_activity_cmd
|
|
126
|
-
"psql -U postgres -d app -tAc " \
|
|
127
|
-
"\"SELECT query FROM pg_stat_activity WHERE datname='app' AND pid != pg_backend_pid() " \
|
|
128
|
-
"AND state='active' AND query !~ 'autovacuum' ORDER BY query_start DESC LIMIT 1;\" 2>/dev/null"
|
|
129
|
-
end
|
|
130
|
-
|
|
131
|
-
def cleanup_pod(app, pg_pod)
|
|
132
|
-
@kubectl.run("exec", pg_pod, "-n", app, "--", "rm", "-f", TEMP_REMOTE)
|
|
133
|
-
rescue StandardError
|
|
134
|
-
nil # ignore cleanup errors
|
|
135
|
-
end
|
|
136
|
-
|
|
137
|
-
def resolve_path(path)
|
|
138
|
-
return path if Pathname.new(path).absolute?
|
|
139
|
-
|
|
140
|
-
base = ENV.fetch("CAPTIVE_ORIG_DIR", Dir.pwd)
|
|
141
|
-
File.join(base, path)
|
|
142
|
-
end
|
|
143
|
-
end
|
|
144
|
-
end
|
|
145
|
-
end
|
data/lib/captive/flux.rb
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "open3"
|
|
4
|
-
require_relative "errors"
|
|
5
|
-
|
|
6
|
-
module Captive
|
|
7
|
-
class Flux
|
|
8
|
-
def suspend_kustomization(name)
|
|
9
|
-
capture("suspend", "kustomization", name)
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
def resume_kustomization(name)
|
|
13
|
-
capture("resume", "kustomization", name)
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
private
|
|
17
|
-
|
|
18
|
-
def capture(*args)
|
|
19
|
-
Open3.capture3("flux", *args)
|
|
20
|
-
rescue Errno::ENOENT
|
|
21
|
-
raise MissingDependencyError, "flux introuvable. Installe-le : brew install flux"
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
end
|