captive-cli 1.8.0 → 1.9.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e022def3e51501451ec37a1c8eb094447c85ca293b52cce2614a4172c7690215
4
- data.tar.gz: 47b7dd39c7312f18608ae35d046c49646b7846c58517e945129511199160d484
3
+ metadata.gz: f2945bf5e2de69e09faee353e8e2c27e3a62a1a9865746b71dd9f179405a0c1b
4
+ data.tar.gz: 70160d63a4ae4dfc3c401f121efb50c1a83e576bf327daa9088631ab51ea99e1
5
5
  SHA512:
6
- metadata.gz: 0cc2c4a6ca16167135d7464a27bb847e26cba1bdcc45c174703ac11159ee49ca06d47ebc418854a9d0281bb0fb074f3222b0590d5bf3fa4070a8b8678c9f0dfd
7
- data.tar.gz: bdcd2d55dec2beb74cbd0d4270f4fc007d9da8ec682fcfb17adf1558cb80c53d6bc111e579e5d7c258ffbfd7cf5d30d34f222baf55bc7c33f5e22df8cebfb799
6
+ metadata.gz: c7e83b774d39711cf093b71a4c0436070c010a45762643159ff622e005a0dc106b139088eb4c56a89661bde4297e84b8981282e763fbdd0ad49f9868f9c8176d
7
+ data.tar.gz: 27defed341c0decfcfdd47e63ac293f37af92f15daf45a627bc923202c5730436e2816b57c1b59dc32726cdc43cb462fadd85b6ffac2f0420ce682dc51190b36
@@ -20,6 +20,15 @@ module Captive
20
20
  def console_opened(app, session: nil) = signaler(app, "console", session: session)
21
21
  def db_dumped(app) = signaler(app, "db_dump")
22
22
  def db_restored(app) = signaler(app, "db_restore")
23
+ def db_psql_opened(app) = signaler(app, "db_psql")
24
+
25
+ def db_query_run(app, sql, read_only:)
26
+ signaler(app, "db_query", command: borne(sql), read_only: read_only)
27
+ end
28
+
29
+ def db_file_executed(app, file, content)
30
+ signaler(app, "db_exec", command: borne("-- #{file}\n#{content}"))
31
+ end
23
32
 
24
33
  def command_run(app, command, session: nil)
25
34
  signaler(app, "run", command: Array(command).join(" "), session: session)
@@ -27,10 +36,15 @@ module Captive
27
36
 
28
37
  private
29
38
 
39
+ # Un dump joué par erreur avec exec ne doit pas partir en entier dans une requête HTTP.
40
+ SQL_MAX = 65_536
41
+
42
+ def borne(sql) = sql.byteslice(0, SQL_MAX).scrub("")
43
+
30
44
  # Un dashboard indisponible ne doit jamais empêcher d'ouvrir une console : l'accès est ce
31
45
  # que l'utilisateur a demandé, le signaler ne l'est pas.
32
- def signaler(app, kind, command: nil, session: nil)
33
- @dashboard.record_activity(app, kind: kind, command: command, session: session)
46
+ def signaler(app, kind, command: nil, session: nil, **details)
47
+ @dashboard.record_activity(app, kind: kind, command: command, session: session, **details)
34
48
  rescue StandardError
35
49
  nil
36
50
  end
@@ -41,7 +41,7 @@ module Captive
41
41
  desc: "Joue le fichier dans une transaction unique " \
42
42
  "(--no-transaction pour CREATE INDEX CONCURRENTLY, pg_cron…)"
43
43
  def exec(app, file)
44
- result = DbExec.new(kubectl: kubectl).run(app, file, single_transaction: options[:transaction])
44
+ result = DbExec.new(kubectl: kubectl, trail: trail).run(app, file, single_transaction: options[:transaction])
45
45
  puts paint.green(" ✓ #{result} appliqué.")
46
46
  rescue Errno::ENOENT => e
47
47
  abort paint.red("✗ Fichier introuvable : #{e.message}")
@@ -49,16 +49,24 @@ module Captive
49
49
  abort paint.red("✗ #{e.message}")
50
50
  end
51
51
 
52
- desc "psql APP", "Ouvre une session psql interactive sur la base"
52
+ desc "psql APP", "Ouvre une session psql sur la base, ou joue une requête avec -c"
53
53
  long_desc <<~DESC
54
54
  Session psql interactive sur la base de l'app, sous un rôle admin
55
55
  non-superuser. L'équivalent du SQL Editor de Supabase, en ligne de commande.
56
56
 
57
+ Avec -c, joue la requête sans session interactive : utilisable par un agent.
58
+ --read-only refuse toute écriture (garde-fou, contournable par un SET).
59
+
57
60
  \x05Exemple :
58
61
  \x05 captive db psql my-app-staging
62
+ \x05 captive db psql my-app-staging -c "SELECT count(*) FROM users" --read-only
59
63
  DESC
64
+ method_option :command, aliases: "-c", type: :string, desc: "Requête SQL à jouer sans session interactive"
65
+ method_option :read_only, type: :boolean, default: false, desc: "Refuse toute écriture (avec -c)"
60
66
  def psql(app)
61
- DbPsql.new(kubectl: kubectl).run(app)
67
+ DbPsql.new(kubectl: kubectl, trail: trail).run(app, command: options[:command], read_only: options[:read_only] || false)
68
+ rescue Captive::ExecError => e
69
+ abort paint.red("✗ #{e.message}")
62
70
  end
63
71
 
64
72
  desc "restore APP DUMP_FILE", "Restaure la base depuis un dump local"
@@ -6,13 +6,15 @@ require_relative "db_target"
6
6
  module Captive
7
7
  module Commands
8
8
  class DbExec
9
- def initialize(kubectl:, target: DbTarget.new(kubectl: kubectl))
9
+ def initialize(kubectl:, trail:, target: DbTarget.new(kubectl: kubectl))
10
10
  @kubectl = kubectl
11
+ @trail = trail
11
12
  @target = target
12
13
  end
13
14
 
14
15
  def run(app, file, single_transaction: true)
15
16
  pod, user, password = @target.resolve(app)
17
+ @trail.db_file_executed(app, file, File.read(file))
16
18
  ok = @kubectl.psql_file(app, pod, file,
17
19
  user: user, password: password, single_transaction: single_transaction)
18
20
  raise ExecError, "L'exécution de #{file} a échoué" if ok == false
@@ -6,13 +6,28 @@ require_relative "db_target"
6
6
  module Captive
7
7
  module Commands
8
8
  class DbPsql
9
- def initialize(kubectl:, target: DbTarget.new(kubectl: kubectl))
9
+ def initialize(kubectl:, trail:, target: DbTarget.new(kubectl: kubectl))
10
10
  @kubectl = kubectl
11
+ @trail = trail
11
12
  @target = target
12
13
  end
13
14
 
14
- def run(app)
15
+ def run(app, command: nil, read_only: false)
16
+ return interactive(app, read_only) unless command
17
+
18
+ pod, user, password = @target.resolve(app)
19
+ @trail.db_query_run(app, command, read_only: read_only)
20
+ ok = @kubectl.psql_command(app, pod, command, user: user, password: password, read_only: read_only)
21
+ raise ExecError, "La requête a échoué" if ok == false
22
+ end
23
+
24
+ private
25
+
26
+ def interactive(app, read_only)
27
+ raise ExecError, "--read-only s'utilise avec -c" if read_only
28
+
15
29
  pod, user, password = @target.resolve(app)
30
+ @trail.db_psql_opened(app)
16
31
  @kubectl.psql_interactive(app, pod, user: user, password: password)
17
32
  end
18
33
  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
- puts paint.dim(" DNS à créer : captive domains list #{app}") unless options[:wait]
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
@@ -30,11 +30,12 @@ module Captive
30
30
 
31
31
  # Signale au dashboard un accès ouvert sur l'app. L'acteur n'est pas transmis : le
32
32
  # dashboard le dérive du token, ce qui le rend infalsifiable côté client.
33
- def record_activity(app, kind:, command: nil, session: nil)
33
+ def record_activity(app, kind:, command: nil, session: nil, read_only: nil)
34
34
  uri = URI.parse("#{DASHBOARD_URL}/api/apps/#{URI.encode_uri_component(app)}/activity")
35
35
  body = { kind: kind }
36
36
  body[:command] = command if command
37
37
  body[:session] = session if session
38
+ body[:read_only] = read_only unless read_only.nil?
38
39
 
39
40
  post(uri, body)
40
41
  end
@@ -168,6 +168,12 @@ module Captive
168
168
  run_interactive("exec", "-n", namespace, "-it", pod, "--", *psql_argv(user, password))
169
169
  end
170
170
 
171
+ # Sans -t : pas de TTY, pour qu'un agent puisse lancer la requête et lire le résultat.
172
+ def psql_command(namespace, pod, sql, user:, password:, read_only: false)
173
+ run_interactive("exec", "-n", namespace, pod, "--",
174
+ *psql_argv(user, password, [ "-v", "ON_ERROR_STOP=1", "-c", sql ], read_only: read_only))
175
+ end
176
+
171
177
  def exec_with_file(namespace, pod, file, *cmd)
172
178
  system("kubectl", *kubeconfig_args, "exec", "-n", namespace, "-i", pod, "--", *cmd, in: File.open(file))
173
179
  end
@@ -257,8 +263,11 @@ module Captive
257
263
 
258
264
  private
259
265
 
260
- def psql_argv(user, password, flags = [])
266
+ # shortcut: default_transaction_read_only se contourne par un SET, garde-fou contre
267
+ # l'erreur et non barrière ; un rôle Postgres lecture seule le jour où il en faut une.
268
+ def psql_argv(user, password, flags = [], read_only: false)
261
269
  [ "env", "PGPASSWORD=#{password}",
270
+ *("PGOPTIONS=-c default_transaction_read_only=on" if read_only),
262
271
  "psql", *flags, "-h", "localhost", "-U", user, "-d", "app", ]
263
272
  end
264
273
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Captive
4
- VERSION = "1.8.0"
4
+ VERSION = "1.9.1"
5
5
  end
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.8.0
4
+ version: 1.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Captive Studio