trellis-pg 0.2.1-aarch64-linux → 0.3.1-aarch64-linux
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/README.md +327 -25
- data/lib/trellis/3.3/trellis_ruby.so +0 -0
- data/lib/trellis/3.4/trellis_ruby.so +0 -0
- data/lib/trellis/4.0/trellis_ruby.so +0 -0
- data/lib/trellis/instance.rb +4 -0
- data/lib/trellis/log_bridge.rb +201 -0
- data/lib/trellis/metrics.rb +129 -11
- data/lib/trellis/migration.rb +34 -0
- data/lib/trellis/pg.rb +37 -0
- data/lib/trellis/railtie.rb +119 -3
- data/lib/trellis/release_check.rb +165 -0
- data/lib/trellis/schema_dump.rb +26 -2
- data/lib/trellis/target.rb +141 -0
- data/lib/trellis/test_support.rb +176 -2
- data/lib/trellis/version.rb +1 -1
- metadata +5 -2
data/lib/trellis/metrics.rb
CHANGED
|
@@ -13,17 +13,11 @@ module Trellis
|
|
|
13
13
|
# running handle and 0 after its last one shuts down; the other gauges of a
|
|
14
14
|
# stopped instance disappear after 60 seconds. See docs/observability.md.
|
|
15
15
|
#
|
|
16
|
-
# The binding opens no port.
|
|
17
|
-
#
|
|
18
|
-
#
|
|
19
|
-
#
|
|
20
|
-
#
|
|
21
|
-
# [200, { "content-type" => "text/plain; version=0.0.4" }, [Trellis::Metrics.render_prometheus]]
|
|
22
|
-
# }
|
|
23
|
-
#
|
|
24
|
-
# In a forking server each worker process has its own registry, holding
|
|
25
|
-
# only its own handles' series, so scrape every worker (or run the worker
|
|
26
|
-
# handle, staging: true, in a process of its own and scrape that).
|
|
16
|
+
# The binding opens no port. Trellis::Metrics::RackApp serves the text from
|
|
17
|
+
# a route of its own; Trellis::Metrics::Middleware appends it to the body of
|
|
18
|
+
# an exporter's route the app already has. Most series are set only in the
|
|
19
|
+
# process running the staging or drain work, so scrape that one (README,
|
|
20
|
+
# Metrics).
|
|
27
21
|
#
|
|
28
22
|
# A process forked while its parent had an engine running may have
|
|
29
23
|
# inherited the registry's lock held, so rendering there raises
|
|
@@ -34,5 +28,129 @@ module Trellis
|
|
|
34
28
|
def self.render_prometheus
|
|
35
29
|
Native.render_prometheus
|
|
36
30
|
end
|
|
31
|
+
|
|
32
|
+
# A Rack app answering GET and HEAD with render_prometheus, in the
|
|
33
|
+
# content type Prometheus expects, at whatever path it is mounted; every
|
|
34
|
+
# other method is a 405. It needs no Rack gem. Unauthenticated, as a
|
|
35
|
+
# Prometheus target is: guard it if the port is public.
|
|
36
|
+
#
|
|
37
|
+
# mount Trellis::Metrics::RackApp, at: "/metrics/trellis" # Rails
|
|
38
|
+
# map("/metrics/trellis") { run Trellis::Metrics::RackApp } # config.ru
|
|
39
|
+
class RackApp
|
|
40
|
+
CONTENT_TYPE = "text/plain; version=0.0.4; charset=utf-8"
|
|
41
|
+
|
|
42
|
+
def self.call(env)
|
|
43
|
+
new.call(env)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def call(env)
|
|
47
|
+
case env["REQUEST_METHOD"]
|
|
48
|
+
when "GET"
|
|
49
|
+
body = Metrics.render_prometheus
|
|
50
|
+
[200, headers(body), [body]]
|
|
51
|
+
when "HEAD"
|
|
52
|
+
[200, headers(Metrics.render_prometheus), []]
|
|
53
|
+
else
|
|
54
|
+
[405, { "allow" => "GET, HEAD", "content-length" => "0" }, []]
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
private
|
|
59
|
+
|
|
60
|
+
def headers(body)
|
|
61
|
+
{ "content-type" => CONTENT_TYPE, "content-length" => body.bytesize.to_s }
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Rack middleware that appends render_prometheus to the Prometheus text
|
|
66
|
+
# another exporter (prometheus-client's Prometheus::Middleware::Exporter,
|
|
67
|
+
# Yabeda's exporter) serves at `path`, so one scrape returns both. It
|
|
68
|
+
# needs no exporter gem and no Rack gem: it wraps any Rack app, and must
|
|
69
|
+
# sit outside the exporter it appends to.
|
|
70
|
+
#
|
|
71
|
+
# config.middleware.insert_before Prometheus::Middleware::Exporter,
|
|
72
|
+
# Trellis::Metrics::Middleware, path: "/metrics"
|
|
73
|
+
#
|
|
74
|
+
# A GET or HEAD of `path` (PATH_INFO, as the exporters match it) is
|
|
75
|
+
# answered by the wrapped app. Only a 200 with a text/plain body that is
|
|
76
|
+
# neither compressed nor chunk-framed (no Content-Encoding or
|
|
77
|
+
# Transfer-Encoding other than identity) is changed: it is buffered, a
|
|
78
|
+
# newline is added if it lacks one, the Trellis text follows, and
|
|
79
|
+
# Content-Length is the new size. Everything else passes through as
|
|
80
|
+
# the wrapped app returned it: other paths and methods, other statuses,
|
|
81
|
+
# compressed bodies (put Rack::Deflater outside this middleware), bodies
|
|
82
|
+
# with no each (Rack 3 streaming), an empty registry, and a process
|
|
83
|
+
# forked while its parent had an engine running (render_prometheus
|
|
84
|
+
# raises Trellis::ForkedHandleError there; the exporter's own series are
|
|
85
|
+
# still served). HEAD is answered as the GET would be, without the body.
|
|
86
|
+
#
|
|
87
|
+
# It does not dedupe, and the text format allows each metric name once,
|
|
88
|
+
# so don't name an app metric like one of the engine's trellis_* series.
|
|
89
|
+
class Middleware
|
|
90
|
+
def initialize(app, path: "/metrics")
|
|
91
|
+
@app = app
|
|
92
|
+
@path = path
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def call(env)
|
|
96
|
+
method = env["REQUEST_METHOD"]
|
|
97
|
+
return @app.call(env) unless (method == "GET" || method == "HEAD") && env["PATH_INFO"] == @path
|
|
98
|
+
|
|
99
|
+
# A HEAD is forwarded as the GET it stands for, so the wrapped app's
|
|
100
|
+
# Content-Length is the body's real size; the body is dropped below.
|
|
101
|
+
status, headers, body = @app.call(method == "HEAD" ? env.merge("REQUEST_METHOD" => "GET") : env)
|
|
102
|
+
status, headers, body = append(status, headers, body)
|
|
103
|
+
if method == "HEAD"
|
|
104
|
+
body.close if body.respond_to?(:close)
|
|
105
|
+
body = []
|
|
106
|
+
end
|
|
107
|
+
[status, headers, body]
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
private
|
|
111
|
+
|
|
112
|
+
def append(status, headers, body)
|
|
113
|
+
return [status, headers, body] unless appendable?(status, headers, body)
|
|
114
|
+
|
|
115
|
+
trellis = begin
|
|
116
|
+
Metrics.render_prometheus
|
|
117
|
+
rescue ForkedHandleError
|
|
118
|
+
return [status, headers, body]
|
|
119
|
+
rescue Exception
|
|
120
|
+
# The response is dropped, so close its body as the server would.
|
|
121
|
+
body.close if body.respond_to?(:close)
|
|
122
|
+
raise
|
|
123
|
+
end
|
|
124
|
+
return [status, headers, body] if trellis.empty?
|
|
125
|
+
|
|
126
|
+
text = read(body)
|
|
127
|
+
text << "\n" unless text.empty? || text.end_with?("\n")
|
|
128
|
+
text << trellis.b
|
|
129
|
+
headers = headers.reject { |name, _| name.casecmp?("content-length") }
|
|
130
|
+
headers["content-length"] = text.bytesize.to_s
|
|
131
|
+
[status, headers, [text]]
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def appendable?(status, headers, body)
|
|
135
|
+
status == 200 && body.respond_to?(:each) &&
|
|
136
|
+
header(headers, "content-type").to_s.start_with?("text/plain") &&
|
|
137
|
+
%w[content-encoding transfer-encoding].all? { |name| [nil, "identity"].include?(header(headers, name)) }
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def header(headers, name)
|
|
141
|
+
headers.each { |key, value| return value.to_s.downcase if key.casecmp?(name) }
|
|
142
|
+
nil
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# The whole body as bytes; the wrapped body is closed, as the server
|
|
146
|
+
# would have.
|
|
147
|
+
def read(body)
|
|
148
|
+
text = String.new(encoding: Encoding::BINARY)
|
|
149
|
+
body.each { |chunk| text << chunk.b }
|
|
150
|
+
text
|
|
151
|
+
ensure
|
|
152
|
+
body.close if body.respond_to?(:close)
|
|
153
|
+
end
|
|
154
|
+
end
|
|
37
155
|
end
|
|
38
156
|
end
|
data/lib/trellis/migration.rb
CHANGED
|
@@ -64,6 +64,16 @@ module Trellis
|
|
|
64
64
|
# ran before the failure in place, on both sides. Give a migration that
|
|
65
65
|
# defines transforms little else to do.
|
|
66
66
|
#
|
|
67
|
+
# == Environments Trellis is off in
|
|
68
|
+
#
|
|
69
|
+
# A database of an environment named in config.trellis.off_in (`%w[test]`)
|
|
70
|
+
# is built by loading the schema, which carries the targets as plain
|
|
71
|
+
# tables, not by a migration that defines one. Run in that environment, the
|
|
72
|
+
# three helpers raise ActiveRecord::MigrationError before running anything.
|
|
73
|
+
# Run from an environment Trellis is on in (`db:prepare` in development
|
|
74
|
+
# migrates the test database too), they do nothing and return nil: that
|
|
75
|
+
# database is reloaded from the new dump the next time it is prepared.
|
|
76
|
+
#
|
|
67
77
|
# == up and down, not change
|
|
68
78
|
#
|
|
69
79
|
# ActiveRecord can reverse `create_table` on its own, but not a Trellis
|
|
@@ -145,6 +155,8 @@ module Trellis
|
|
|
145
155
|
# does: Trellis.status. A read, so it runs whether or not a transaction
|
|
146
156
|
# is open.
|
|
147
157
|
def status(target_table)
|
|
158
|
+
return if off_database?("this status wasn't read: #{target_table}")
|
|
159
|
+
|
|
148
160
|
Migration.with_handle { Trellis.status(target_table) }
|
|
149
161
|
end
|
|
150
162
|
|
|
@@ -169,11 +181,33 @@ module Trellis
|
|
|
169
181
|
"cannot reverse a Trellis statement: #{statement}. Undo a define in down with " \
|
|
170
182
|
"PAUSE TRANSFORM and DROP TRANSFORM statements."
|
|
171
183
|
end
|
|
184
|
+
return if off_database?("this statement wasn't applied: #{statement}")
|
|
185
|
+
|
|
172
186
|
refuse_open_transaction!("this statement wasn't applied: #{statement}")
|
|
173
187
|
|
|
174
188
|
say_with_time("#{helper}(#{statement.inspect})") { Migration.with_handle { yield } }
|
|
175
189
|
end
|
|
176
190
|
|
|
191
|
+
# Whether the migrating database belongs to an environment Trellis is off
|
|
192
|
+
# in, so `what` is skipped: raises when that is also the environment
|
|
193
|
+
# running the migration.
|
|
194
|
+
def off_database?(what)
|
|
195
|
+
return false unless defined?(Trellis::Railtie)
|
|
196
|
+
|
|
197
|
+
environment = connection.pool.db_config.env_name
|
|
198
|
+
return false unless Trellis::Railtie.off_in?(environment)
|
|
199
|
+
|
|
200
|
+
if Trellis::Railtie.off_in?(ActiveRecord::Tasks::DatabaseTasks.env)
|
|
201
|
+
raise ActiveRecord::MigrationError,
|
|
202
|
+
Trellis::Railtie.off_message("#{what}. Build this database by loading the schema " \
|
|
203
|
+
"(`bin/rails db:schema:load`, or `db:test:prepare`) rather than " \
|
|
204
|
+
"migrating it", environment)
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
say Trellis::Railtie.off_message("#{what}; the next schema load of this database brings it in", environment)
|
|
208
|
+
true
|
|
209
|
+
end
|
|
210
|
+
|
|
177
211
|
def refuse_open_transaction!(what)
|
|
178
212
|
return unless connection.transaction_open?
|
|
179
213
|
|
data/lib/trellis/pg.rb
CHANGED
|
@@ -23,6 +23,7 @@ else
|
|
|
23
23
|
require_relative "trellis_ruby"
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
+
require_relative "log_bridge"
|
|
26
27
|
require_relative "instance"
|
|
27
28
|
require_relative "metrics"
|
|
28
29
|
|
|
@@ -51,6 +52,8 @@ require_relative "metrics"
|
|
|
51
52
|
# has_live_staging_worker?, watermark_token, await_converged, self_check,
|
|
52
53
|
# self_check_job.
|
|
53
54
|
# - Metrics: Trellis::Metrics.render_prometheus, which needs no handle.
|
|
55
|
+
# - Logs: Trellis.logger=, which sends the engine's log lines to a Logger
|
|
56
|
+
# (Trellis::LogBridge); needs no handle either.
|
|
54
57
|
#
|
|
55
58
|
# # A deploy's migration step: the defaults run nothing in the background.
|
|
56
59
|
# Trellis.connect(url: "host=localhost dbname=app")
|
|
@@ -97,6 +100,14 @@ module Trellis
|
|
|
97
100
|
nil
|
|
98
101
|
end
|
|
99
102
|
|
|
103
|
+
# Whether Trellis is on in this process's Rails environment: false when
|
|
104
|
+
# the app's config.trellis.off_in names it (`%w[test]`, so a test suite
|
|
105
|
+
# runs without Trellis). Always true without Rails. Code that builds on
|
|
106
|
+
# Trellis, such as a test helper that fills a target, branches on it.
|
|
107
|
+
def enabled?
|
|
108
|
+
!defined?(Trellis::Railtie) || Trellis::Railtie.enabled?
|
|
109
|
+
end
|
|
110
|
+
|
|
100
111
|
# Whether this process's default instance is connected. False before
|
|
101
112
|
# connect, after shutdown, and in a forked child that hasn't connected
|
|
102
113
|
# its own.
|
|
@@ -105,6 +116,32 @@ module Trellis
|
|
|
105
116
|
!default.nil? && default.connected?
|
|
106
117
|
end
|
|
107
118
|
|
|
119
|
+
# The Logger the engine's log lines go to, or nil (the default: they are
|
|
120
|
+
# dropped). See Trellis::LogBridge.
|
|
121
|
+
def logger
|
|
122
|
+
LogBridge.current&.logger
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# Sends the engine's log lines to `logger`, from a Ruby thread
|
|
126
|
+
# (Trellis::LogBridge), or to nowhere if it is nil. The most verbose
|
|
127
|
+
# level forwarded is the logger's own level now. Set it before connecting,
|
|
128
|
+
# to catch connect's own lines. The Rails integration sets it to
|
|
129
|
+
# Rails.logger. Raises ConflictError if another tracing subscriber was
|
|
130
|
+
# installed in this extension's native library first.
|
|
131
|
+
#
|
|
132
|
+
# Trellis.logger = Logger.new($stderr)
|
|
133
|
+
def logger=(logger)
|
|
134
|
+
LogBridge.install(logger)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Changes the most verbose level forwarded to Trellis.logger: a Logger level
|
|
138
|
+
# (:debug, :info, :warn, :error, or a Logger severity). Raises
|
|
139
|
+
# ValidationError if there is no logger.
|
|
140
|
+
def log_level=(level)
|
|
141
|
+
bridge = LogBridge.current or raise ValidationError, "Trellis.logger is not set: set it first"
|
|
142
|
+
bridge.level = level
|
|
143
|
+
end
|
|
144
|
+
|
|
108
145
|
# The default instance Trellis.connect opened, for code that takes an
|
|
109
146
|
# Instance. Raises ValidationError if there is none.
|
|
110
147
|
def default_instance
|
data/lib/trellis/railtie.rb
CHANGED
|
@@ -34,8 +34,19 @@ module Trellis
|
|
|
34
34
|
# the definitions in `structure.sql` (Trellis::SchemaDump). The database
|
|
35
35
|
# they dump into is config.trellis.dump_database, "primary" by default.
|
|
36
36
|
#
|
|
37
|
+
# `config.trellis.off_in` names the environments Trellis is off in. The
|
|
38
|
+
# default is `%w[test]`: a test suite's transactions never commit, so it
|
|
39
|
+
# can't see a target change. `off_in = []` turns Trellis on in tests. It
|
|
40
|
+
# is read from any environment, so a `db:prepare` or `db:test:prepare` run
|
|
41
|
+
# from development leaves Trellis out of the test database too. There,
|
|
42
|
+
# nothing connects at boot, Railtie.connect and `trellis:migrate` do
|
|
43
|
+
# nothing, the schema load and dump hooks leave the target tables as plain
|
|
44
|
+
# ones, and the migration helpers refuse (or skip, migrating that database
|
|
45
|
+
# from an environment Trellis is on in).
|
|
46
|
+
# Trellis.enabled? is the predicate; "Testing" in the README has the rest.
|
|
47
|
+
#
|
|
37
48
|
# When either is set, the app's handles connect in an after_initialize hook,
|
|
38
|
-
# except in these
|
|
49
|
+
# except in these cases:
|
|
39
50
|
#
|
|
40
51
|
# - An app booted by a rake task (`rails db:migrate`, `rails db:create`,
|
|
41
52
|
# ...): like Ecto, which migrates without starting the app, those get no
|
|
@@ -46,9 +57,16 @@ module Trellis
|
|
|
46
57
|
# `rails test` loads the tasks to run test:prepare, then boots the app
|
|
47
58
|
# outside any task, so it gets its handle (but `bin/rails db:test:prepare
|
|
48
59
|
# test` boots it inside the first task, so it doesn't).
|
|
60
|
+
# - Trellis is off in this environment (config.trellis.off_in).
|
|
49
61
|
# - config.trellis.connect_on_boot = false, for a server that forks with
|
|
50
62
|
# no hook to shut the handle down first (see below).
|
|
51
63
|
#
|
|
64
|
+
# The engine's log lines go to Rails.logger (Trellis::LogBridge), at
|
|
65
|
+
# Rails.logger's level or config.trellis.log_level (:debug, :info, :warn,
|
|
66
|
+
# :error), unless config.trellis.log_bridge = false or Trellis is off in
|
|
67
|
+
# this environment (nothing connects, so there are none). Trellis.logger =
|
|
68
|
+
# in a config/initializers file sends them elsewhere.
|
|
69
|
+
#
|
|
52
70
|
# A handle doesn't survive fork. A preforking server that
|
|
53
71
|
# loads the app before forking (Puma's preload_app!, on by default with
|
|
54
72
|
# more than one worker) must shut the boot handle down before it forks,
|
|
@@ -67,6 +85,9 @@ module Trellis
|
|
|
67
85
|
config.trellis.instances = {}
|
|
68
86
|
config.trellis.connect_on_boot = true
|
|
69
87
|
config.trellis.dump_database = "primary"
|
|
88
|
+
config.trellis.off_in = %w[test]
|
|
89
|
+
config.trellis.log_bridge = true
|
|
90
|
+
config.trellis.log_level = nil
|
|
70
91
|
|
|
71
92
|
rake_tasks do
|
|
72
93
|
namespace :trellis do
|
|
@@ -74,13 +95,28 @@ module Trellis
|
|
|
74
95
|
task migrate: :environment do
|
|
75
96
|
Trellis::Railtie.migrate_all
|
|
76
97
|
end
|
|
98
|
+
|
|
99
|
+
# No :environment: the check reads git and files, so it runs in a CI
|
|
100
|
+
# job with no database and none of the app's initializers.
|
|
101
|
+
desc "Fail if this release changes trellis-pg's version and adds a Trellis migration (BASE=<git ref>)"
|
|
102
|
+
task :release_check do
|
|
103
|
+
Trellis::Railtie.release_check(ENV.fetch("BASE", nil))
|
|
104
|
+
end
|
|
77
105
|
end
|
|
78
106
|
end
|
|
79
107
|
|
|
108
|
+
initializer "trellis.logger" do |app|
|
|
109
|
+
settings = app.config.trellis
|
|
110
|
+
next unless settings.log_bridge && ::Rails.logger && Trellis::Railtie.enabled?
|
|
111
|
+
|
|
112
|
+
Trellis::LogBridge.install(::Rails.logger, level: settings.log_level)
|
|
113
|
+
end
|
|
114
|
+
|
|
80
115
|
initializer "trellis.migration" do
|
|
81
116
|
ActiveSupport.on_load(:active_record) do
|
|
82
117
|
require "trellis/migration"
|
|
83
118
|
require "trellis/schema_dump"
|
|
119
|
+
require "trellis/target"
|
|
84
120
|
end
|
|
85
121
|
end
|
|
86
122
|
|
|
@@ -95,13 +131,16 @@ module Trellis
|
|
|
95
131
|
# instance from config.trellis.connect (when set), and each of
|
|
96
132
|
# config.trellis.instances. In a forked worker (Puma's
|
|
97
133
|
# before_worker_boot, Unicorn's after_fork, Passenger's
|
|
98
|
-
# starting_worker_process), or a rake task of your own.
|
|
134
|
+
# starting_worker_process), or a rake task of your own. Warns and
|
|
135
|
+
# connects nothing in an environment Trellis is off in. Raises
|
|
99
136
|
# ValidationError if neither is set, or if a named instance is already
|
|
100
137
|
# connected (as Trellis.connect does for the default one), and whatever
|
|
101
138
|
# Trellis.connect and Trellis::Instance.connect raise; the instances
|
|
102
139
|
# this call had connected are shut down first, so a failure leaves
|
|
103
140
|
# nothing half connected.
|
|
104
141
|
def connect
|
|
142
|
+
return skipped("Trellis::Railtie.connect connects nothing") unless enabled?
|
|
143
|
+
|
|
105
144
|
default = connect_options
|
|
106
145
|
named = instance_options
|
|
107
146
|
if default.nil? && named.empty?
|
|
@@ -187,6 +226,8 @@ module Trellis
|
|
|
187
226
|
# config.trellis.instances, each as with_handle does. Raises
|
|
188
227
|
# ValidationError if neither is set.
|
|
189
228
|
def migrate_all
|
|
229
|
+
return skipped("trellis:migrate migrates nothing") unless enabled?
|
|
230
|
+
|
|
190
231
|
default = connect_options
|
|
191
232
|
named = instance_options
|
|
192
233
|
if default.nil? && named.empty?
|
|
@@ -199,6 +240,40 @@ module Trellis
|
|
|
199
240
|
nil
|
|
200
241
|
end
|
|
201
242
|
|
|
243
|
+
# Runs Trellis::ReleaseCheck for this app against the git ref `base`,
|
|
244
|
+
# over every migrations path the app has (db/migrate, and each
|
|
245
|
+
# database's migrations_paths): prints its verdict, and exits 1 when
|
|
246
|
+
# the release both changes trellis-pg's version and adds a Trellis
|
|
247
|
+
# migration, or when the check can't run. What `rails
|
|
248
|
+
# trellis:release_check BASE=<ref>` runs.
|
|
249
|
+
def release_check(base)
|
|
250
|
+
require "trellis/release_check"
|
|
251
|
+
result = ReleaseCheck.call(root: ::Rails.root, base: base,
|
|
252
|
+
migrations_paths: migrations_paths, lockfile: lockfile)
|
|
253
|
+
abort result.message unless result.ok?
|
|
254
|
+
puts result.message
|
|
255
|
+
rescue ReleaseCheck::Error => e
|
|
256
|
+
abort "trellis:release_check: #{e.message}"
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
# Whether Trellis is off in `environment`, however it is spelled.
|
|
260
|
+
def off_in?(environment)
|
|
261
|
+
off_in.include?(environment.to_s)
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
# Whether Trellis is on in `environment` (this process's, by default):
|
|
265
|
+
# false when config.trellis.off_in names it. Trellis.enabled? is the
|
|
266
|
+
# shortcut for code that doesn't need the Railtie.
|
|
267
|
+
def enabled?(environment = ::Rails.env)
|
|
268
|
+
!off_in?(environment)
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
# A sentence saying Trellis is off in `environment`, and `what` that
|
|
272
|
+
# means was not done.
|
|
273
|
+
def off_message(what, environment = ::Rails.env)
|
|
274
|
+
"Trellis is off in the #{environment} environment (config.trellis.off_in): #{what}"
|
|
275
|
+
end
|
|
276
|
+
|
|
202
277
|
# config.trellis.connect, as Trellis.connect's keyword options, or nil
|
|
203
278
|
# when it isn't set (or there's no Rails app).
|
|
204
279
|
def connect_options
|
|
@@ -228,6 +303,42 @@ module Trellis
|
|
|
228
303
|
|
|
229
304
|
private
|
|
230
305
|
|
|
306
|
+
# The environments config.trellis.off_in names, as strings; empty when
|
|
307
|
+
# it isn't set (or there's no Rails app).
|
|
308
|
+
def off_in
|
|
309
|
+
app = ::Rails.respond_to?(:application) && ::Rails.application or return []
|
|
310
|
+
|
|
311
|
+
Array(app.config.trellis.off_in).map(&:to_s)
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
# Where the app keeps migrations: db/migrate, and the migrations_paths
|
|
315
|
+
# of every database in config/database.yml, read without connecting.
|
|
316
|
+
# An app whose database config can't be read here (no database.yml, a
|
|
317
|
+
# missing environment variable) is checked on db/migrate alone.
|
|
318
|
+
def migrations_paths
|
|
319
|
+
app = ::Rails.application
|
|
320
|
+
collect = lambda do |node|
|
|
321
|
+
next [] unless node.is_a?(Hash)
|
|
322
|
+
|
|
323
|
+
node.flat_map { |key, value| key.to_s == "migrations_paths" ? Array(value) : collect.call(value) }
|
|
324
|
+
end
|
|
325
|
+
configured = begin
|
|
326
|
+
collect.call(app.config.database_configuration)
|
|
327
|
+
rescue StandardError
|
|
328
|
+
[]
|
|
329
|
+
end
|
|
330
|
+
(app.paths["db/migrate"].to_a + configured).map(&:to_s).uniq
|
|
331
|
+
end
|
|
332
|
+
|
|
333
|
+
# The lockfile Bundler resolved the app with (it follows BUNDLE_GEMFILE,
|
|
334
|
+
# and a gems.rb has a gems.locked), or Gemfile.lock in an app that
|
|
335
|
+
# isn't running under Bundler.
|
|
336
|
+
def lockfile
|
|
337
|
+
defined?(::Bundler) ? ::Bundler.default_lockfile : "Gemfile.lock"
|
|
338
|
+
rescue StandardError
|
|
339
|
+
"Gemfile.lock"
|
|
340
|
+
end
|
|
341
|
+
|
|
231
342
|
def keyword_options(what, options)
|
|
232
343
|
unless options.respond_to?(:to_h)
|
|
233
344
|
raise ValidationError,
|
|
@@ -253,6 +364,11 @@ module Trellis
|
|
|
253
364
|
Trellis.default_instance
|
|
254
365
|
end
|
|
255
366
|
|
|
367
|
+
def skipped(what)
|
|
368
|
+
warn "trellis: #{off_message(what)}"
|
|
369
|
+
nil
|
|
370
|
+
end
|
|
371
|
+
|
|
256
372
|
def ignoring_errors
|
|
257
373
|
yield
|
|
258
374
|
rescue StandardError => e
|
|
@@ -274,7 +390,7 @@ module Trellis
|
|
|
274
390
|
private
|
|
275
391
|
|
|
276
392
|
def boot
|
|
277
|
-
return if rake_running?
|
|
393
|
+
return if rake_running? || !self.class.enabled?
|
|
278
394
|
|
|
279
395
|
settings = ::Rails.application.config.trellis
|
|
280
396
|
return unless settings.connect_on_boot && (settings.connect || !self.class.instance_options.empty?)
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "open3"
|
|
4
|
+
require "pathname"
|
|
5
|
+
|
|
6
|
+
module Trellis
|
|
7
|
+
# A static check for CI: does this release both change the trellis-pg
|
|
8
|
+
# gem's version and add a migration that uses Trellis?
|
|
9
|
+
#
|
|
10
|
+
# Such a release fails where migrations run before the old processes stop
|
|
11
|
+
# (a release phase, a pre-deploy job): while a worker of the old version is
|
|
12
|
+
# live, a define, ALTER, PAUSE, RESUME or DROP from the new version refuses
|
|
13
|
+
# with Trellis::VersionHandoverError (docs/embedding.md, "Rolling
|
|
14
|
+
# deploys"). The remedy is to ship the version change by itself and the
|
|
15
|
+
# migrations in the next release, and this check says so on the pull
|
|
16
|
+
# request instead of in the deploy.
|
|
17
|
+
#
|
|
18
|
+
# result = Trellis::ReleaseCheck.call(root: Rails.root, base: "origin/main")
|
|
19
|
+
# abort result.message unless result.ok?
|
|
20
|
+
#
|
|
21
|
+
# `rails trellis:release_check BASE=origin/main` runs it in a Rails app.
|
|
22
|
+
#
|
|
23
|
+
# It reads git and files only: no database, no Trellis handle, and none of
|
|
24
|
+
# the gem's extension. It compares the working tree under `root` with its
|
|
25
|
+
# merge base against `base`, so a branch that is behind `base` is judged by
|
|
26
|
+
# what the branch itself changes, and uncommitted work counts.
|
|
27
|
+
#
|
|
28
|
+
# It sees migrations only. A state-changing call from a deploy script or a
|
|
29
|
+
# console isn't in any file it reads; the engine's refusal covers those.
|
|
30
|
+
module ReleaseCheck
|
|
31
|
+
GEM_NAME = "trellis-pg"
|
|
32
|
+
|
|
33
|
+
# The check couldn't run: no base ref, not a git checkout, or a base
|
|
34
|
+
# this clone has no merge base with (a shallow clone, usually).
|
|
35
|
+
class Error < StandardError; end
|
|
36
|
+
|
|
37
|
+
# What the check found. `base` is the merge-base commit it compared with,
|
|
38
|
+
# the versions are trellis-pg's in the lockfile there and in the working
|
|
39
|
+
# tree (nil where the lockfile doesn't list the gem), and `migrations` are
|
|
40
|
+
# the migrations added since `base` that mention Trellis, relative to
|
|
41
|
+
# `root`.
|
|
42
|
+
Result = Data.define(:base, :old_version, :new_version, :migrations) do
|
|
43
|
+
# Any difference counts, a patch release and a downgrade too: Trellis
|
|
44
|
+
# versions match exactly, so every change of release is a handover. A
|
|
45
|
+
# release that adds the gem has no old worker to hand over from.
|
|
46
|
+
def version_changed?
|
|
47
|
+
!old_version.nil? && !new_version.nil? && old_version != new_version
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def ok?
|
|
51
|
+
!(version_changed? && migrations.any?)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def message
|
|
55
|
+
ok? ? ok_message : failure_message
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
private
|
|
59
|
+
|
|
60
|
+
def ok_message
|
|
61
|
+
version = if version_changed?
|
|
62
|
+
"#{GEM_NAME} changes from #{old_version} to #{new_version}"
|
|
63
|
+
else
|
|
64
|
+
"#{GEM_NAME} #{new_version || 'is not in the lockfile'}, unchanged"
|
|
65
|
+
end
|
|
66
|
+
"trellis:release_check: ok (#{version}; #{migrations.size} Trellis migration(s) added since #{base[0, 12]})"
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def failure_message
|
|
70
|
+
<<~MESSAGE.chomp
|
|
71
|
+
trellis:release_check: this release changes #{GEM_NAME} from #{old_version} to #{new_version} and adds Trellis migrations:
|
|
72
|
+
#{migrations.map { |path| " #{path}" }.join("\n")}
|
|
73
|
+
While a worker of #{old_version} is still running, #{new_version} may not define, alter, pause, resume
|
|
74
|
+
or drop a transform (Trellis::VersionHandoverError), so a deploy that migrates before the old
|
|
75
|
+
worker stops fails at these migrations. Ship the #{GEM_NAME} change by itself, then these
|
|
76
|
+
migrations in the next release.
|
|
77
|
+
MESSAGE
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
class << self
|
|
82
|
+
# Runs the check for the app at `root` against the git ref `base`: the
|
|
83
|
+
# branch a pull request targets, or the last deployed ref for a release
|
|
84
|
+
# of several merged pull requests. `migrations_paths` and `lockfile` are
|
|
85
|
+
# relative to `root` (absolute paths under it work too).
|
|
86
|
+
#
|
|
87
|
+
# Returns a Result; raises Error when the check can't run.
|
|
88
|
+
def call(root:, base:, migrations_paths: ["db/migrate"], lockfile: "Gemfile.lock")
|
|
89
|
+
root = Pathname(root).expand_path
|
|
90
|
+
base = base.to_s.strip
|
|
91
|
+
if base.empty?
|
|
92
|
+
raise Error, "set BASE to the git ref this release is compared with: the branch a pull " \
|
|
93
|
+
"request targets (BASE=origin/main), or the last deployed ref"
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Never an option to git, whatever the environment variable held.
|
|
97
|
+
raise Error, "#{base.inspect} is not a git ref" if base.start_with?("-")
|
|
98
|
+
|
|
99
|
+
merge_base = merge_base(root, base)
|
|
100
|
+
lockfile = relative(root, lockfile)
|
|
101
|
+
Result.new(
|
|
102
|
+
base: merge_base,
|
|
103
|
+
old_version: locked_version(git(root, "show", "#{merge_base}:./#{lockfile}", allow_failure: true)),
|
|
104
|
+
new_version: locked_version(read(root.join(lockfile))),
|
|
105
|
+
migrations: added_trellis_migrations(root, merge_base, migrations_paths.map { |path| relative(root, path) })
|
|
106
|
+
)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
private
|
|
110
|
+
|
|
111
|
+
def merge_base(root, base)
|
|
112
|
+
git(root, "rev-parse", "--is-inside-work-tree")
|
|
113
|
+
unless git(root, "rev-parse", "--verify", "--quiet", "#{base}^{commit}", allow_failure: true)
|
|
114
|
+
raise Error, "#{base} is not a commit this clone knows: fetch it first"
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
git(root, "merge-base", base, "HEAD", allow_failure: true)&.strip or
|
|
118
|
+
raise Error, "this checkout has no merge base with #{base}: fetch more history " \
|
|
119
|
+
"(a shallow clone has none; with actions/checkout, set fetch-depth: 0)"
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# trellis-pg's version in a lockfile's text: its `specs:` entry, without
|
|
123
|
+
# the platform a platform gem's entry carries (`0.2.0-x86_64-linux`).
|
|
124
|
+
# nil for no text or no entry.
|
|
125
|
+
def locked_version(text)
|
|
126
|
+
text&.[](/^ {4}#{Regexp.escape(GEM_NAME)} \(([^)-]+)/, 1)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# The migrations under `paths` that the working tree has and
|
|
130
|
+
# `merge_base` hasn't (committed, staged or untracked), and that
|
|
131
|
+
# mention Trellis: the Trellis::Migration helpers, or a direct
|
|
132
|
+
# Trellis.apply. An edited old migration doesn't run again, so only
|
|
133
|
+
# added (and renamed) files count.
|
|
134
|
+
def added_trellis_migrations(root, merge_base, paths)
|
|
135
|
+
added = git(root, "diff", "--name-only", "-z", "--diff-filter=AR", "--relative", merge_base, "--", *paths)
|
|
136
|
+
untracked = git(root, "ls-files", "-z", "--others", "--exclude-standard", "--", *paths)
|
|
137
|
+
(added.split("\0") + untracked.split("\0"))
|
|
138
|
+
.uniq
|
|
139
|
+
.select { |path| path.end_with?(".rb") && read(root.join(path))&.match?(/\bTrellis\b/) }
|
|
140
|
+
.sort
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def read(path)
|
|
144
|
+
path.file? ? path.read : nil
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def relative(root, path)
|
|
148
|
+
path = Pathname(path)
|
|
149
|
+
(path.absolute? ? path.relative_path_from(root) : path).to_s
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# git's stdout, run in `root`. A failing command raises Error, or
|
|
153
|
+
# returns nil with allow_failure.
|
|
154
|
+
def git(root, *args, allow_failure: false)
|
|
155
|
+
out, err, status = Open3.capture3("git", *args, chdir: root.to_s)
|
|
156
|
+
return out if status.success?
|
|
157
|
+
return nil if allow_failure
|
|
158
|
+
|
|
159
|
+
raise Error, "git #{args.first} failed in #{root}: #{err.strip}"
|
|
160
|
+
rescue Errno::ENOENT => e
|
|
161
|
+
raise Error, "can't run git in #{root}: #{e.message}"
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
end
|