native-packages 0.12.0 → 0.14.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/CONTRIBUTING.md +1 -1
- data/docs/_guides/apple-notarization.md +2 -0
- data/docs/_guides/distribution-recipes.md +2 -1
- data/docs/_guides/getting-started.md +4 -2
- data/docs/_guides/github-actions.md +4 -4
- data/docs/_guides/hosted-repositories.md +124 -5
- data/docs/_guides/native-recipes.md +3 -1
- data/docs/_guides/release-signing.md +3 -3
- data/docs/_guides/windows-signing.md +4 -2
- data/docs/_reference/commands.md +5 -3
- data/docs/_reference/configuration.md +4 -3
- data/docs/_reference/platforms.md +4 -4
- data/docs/_reference/troubleshooting.md +2 -2
- data/docs/index.md +3 -3
- data/examples/native-packages-all-formats.yaml +1 -1
- data/examples/native-packages-linux-distribution.yaml +1 -1
- data/examples/native-packages.yaml +1 -1
- data/lib/native_packages/apt_repository.rb +21 -28
- data/lib/native_packages/bucket.rb +265 -0
- data/lib/native_packages/build.rb +8 -4
- data/lib/native_packages/configuration.rb +5 -2
- data/lib/native_packages/dnf_repository.rb +22 -15
- data/lib/native_packages/flatpak_repository.rb +3 -1
- data/lib/native_packages/hosted_repository.rb +155 -7
- data/lib/native_packages/openpgp.rb +4 -2
- data/lib/native_packages/pacman_repository.rb +205 -0
- data/lib/native_packages/support.rb +1 -1
- metadata +3 -1
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
require "etc"
|
|
5
|
+
require "json"
|
|
6
|
+
require "net/http"
|
|
7
|
+
require "openssl"
|
|
8
|
+
require "securerandom"
|
|
9
|
+
require "socket"
|
|
10
|
+
require "time"
|
|
11
|
+
require "uri"
|
|
12
|
+
|
|
13
|
+
module NativePackages
|
|
14
|
+
# An S3-compatible bucket, such as Cloudflare R2 or Amazon S3, spoken to
|
|
15
|
+
# with plain HTTP requests signed with AWS Signature Version 4. Keys are
|
|
16
|
+
# addressed path-style (ENDPOINT/BUCKET/KEY), which R2, S3 and MinIO all
|
|
17
|
+
# accept.
|
|
18
|
+
class Bucket
|
|
19
|
+
KEYS = %w[endpoint name prefix region lock_timeout].freeze
|
|
20
|
+
ACCESS = "AWS_ACCESS_KEY_ID"
|
|
21
|
+
SECRET = "AWS_SECRET_ACCESS_KEY"
|
|
22
|
+
SESSION = "AWS_SESSION_TOKEN"
|
|
23
|
+
NAME = /\A[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]\z/
|
|
24
|
+
PREFIX = %r{\A[A-Za-z0-9._-]+(?:/[A-Za-z0-9._-]+)*\z}
|
|
25
|
+
LOCK_TIMEOUT = 15 * 60
|
|
26
|
+
# A lock older than this belongs to a publication that died.
|
|
27
|
+
STALE = 30 * 60
|
|
28
|
+
ATTEMPTS = 4
|
|
29
|
+
|
|
30
|
+
# The credentials from the environment, removed from it so that no later
|
|
31
|
+
# subprocess inherits them.
|
|
32
|
+
def self.environment = { access: ENV.delete(ACCESS), secret: ENV.delete(SECRET), session: ENV.delete(SESSION) }
|
|
33
|
+
|
|
34
|
+
def self.check(settings, label)
|
|
35
|
+
raise Error, "#{label}: expected a mapping" unless settings.is_a?(Hash)
|
|
36
|
+
unknown = settings.keys - KEYS
|
|
37
|
+
raise Error, "#{label}: unknown fields #{unknown.join(', ')}" unless unknown.empty?
|
|
38
|
+
endpoint = settings["endpoint"]
|
|
39
|
+
# Plain HTTP only reaches a server on this machine, such as a test one.
|
|
40
|
+
unless endpoint.is_a?(String) && (%r{\Ahttps://[^\s/]+/?\z}.match?(endpoint) || %r{\Ahttp://(?:localhost|127\.0\.0\.1|\[::1\])(?::\d+)?/?\z}.match?(endpoint))
|
|
41
|
+
raise Error, "#{label}.endpoint: expected the bucket service's https:// URL, without a path"
|
|
42
|
+
end
|
|
43
|
+
raise Error, "#{label}.name: expected the bucket's name" unless NAME.match?(settings["name"].to_s)
|
|
44
|
+
# Publishing deletes what the repository no longer holds under the
|
|
45
|
+
# prefix, so the repository never shares the bucket's root.
|
|
46
|
+
unless PREFIX.match?(settings["prefix"].to_s) && settings["prefix"].split("/").none? { |part| %w[. ..].include?(part) }
|
|
47
|
+
raise Error, "#{label}.prefix: name the folder in the bucket that holds this repository alone, such as apt"
|
|
48
|
+
end
|
|
49
|
+
raise Error, "#{label}.region: expected a string" if settings.key?("region") && !settings["region"].is_a?(String)
|
|
50
|
+
timeout = settings.fetch("lock_timeout", LOCK_TIMEOUT)
|
|
51
|
+
raise Error, "#{label}.lock_timeout: expected seconds to wait for another publication (0 or more)" unless timeout.is_a?(Integer) && !timeout.negative?
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Fails if two repositories write into the same bucket folder, or one into
|
|
55
|
+
# another's, since each deletes what it does not hold.
|
|
56
|
+
def self.check_overlaps(sections)
|
|
57
|
+
places = sections.filter_map do |label, settings|
|
|
58
|
+
bucket = settings.is_a?(Hash) && settings["bucket"]
|
|
59
|
+
[label, bucket["endpoint"].to_s.chomp("/"), bucket["name"], "#{bucket['prefix']}/"] if bucket.is_a?(Hash)
|
|
60
|
+
end
|
|
61
|
+
places.combination(2).each do |(first, *one), (second, *other)|
|
|
62
|
+
next unless one[0, 2] == other[0, 2] && (one[2].start_with?(other[2]) || other[2].start_with?(one[2]))
|
|
63
|
+
raise Error, "#{first}.bucket and #{second}.bucket: give each repository a prefix of its own"
|
|
64
|
+
end
|
|
65
|
+
nil
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
attr_reader :prefix, :lock_timeout
|
|
69
|
+
|
|
70
|
+
def initialize(settings, access: nil, secret: nil, session: nil)
|
|
71
|
+
missing = [[ACCESS, access], [SECRET, secret]].select { |_, value| value.to_s.empty? }.map(&:first)
|
|
72
|
+
raise Error, "set #{missing.join(' and ')} to publish to the bucket" unless missing.empty?
|
|
73
|
+
@endpoint = URI(settings.fetch("endpoint").chomp("/"))
|
|
74
|
+
@bucket = settings.fetch("name")
|
|
75
|
+
@prefix = settings.fetch("prefix")
|
|
76
|
+
@region = settings.fetch("region", "auto")
|
|
77
|
+
@lock_timeout = settings.fetch("lock_timeout", LOCK_TIMEOUT)
|
|
78
|
+
@access, @secret, @session = access, secret, session
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def key(relative) = "#{prefix}/#{relative}"
|
|
82
|
+
|
|
83
|
+
# Every object under the prefix: key => [size, ETag without quotes].
|
|
84
|
+
def list
|
|
85
|
+
objects = {}
|
|
86
|
+
token = nil
|
|
87
|
+
loop do
|
|
88
|
+
query = { "list-type" => "2", "prefix" => "#{prefix}/" }
|
|
89
|
+
query["continuation-token"] = token if token
|
|
90
|
+
body = request("GET", nil, query: query, expect: [200]).body
|
|
91
|
+
body.scan(%r{<Contents>(.*?)</Contents>}m).each do |(entry)|
|
|
92
|
+
objects[text(entry, "Key")] = [Integer(text(entry, "Size")), text(entry, "ETag").delete('"')]
|
|
93
|
+
end
|
|
94
|
+
break unless text(body, "IsTruncated") == "true"
|
|
95
|
+
token = text(body, "NextContinuationToken")
|
|
96
|
+
end
|
|
97
|
+
objects
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# The object's bytes, or nil if there is none.
|
|
101
|
+
def get(key)
|
|
102
|
+
response = request("GET", key, expect: [200, 404])
|
|
103
|
+
response.code == "404" ? nil : [response.body, response["etag"].to_s.delete('"')]
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# The object's Content-Type and Cache-Control, or nil if there is none.
|
|
107
|
+
def head(key)
|
|
108
|
+
response = request("HEAD", key, expect: [200, 404])
|
|
109
|
+
response.code == "404" ? nil : { "content-type" => response["content-type"], "cache-control" => response["cache-control"] }
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# Writes the object at `key` to `destination`, checking its MD5 against
|
|
113
|
+
# the ETag when that is one.
|
|
114
|
+
def download(key, destination, etag)
|
|
115
|
+
body, = get(key)
|
|
116
|
+
raise Error, "#{key} disappeared from the bucket while it was locked" unless body
|
|
117
|
+
raise Error, "#{key} arrived damaged" if etag.match?(/\A\h{32}\z/) && Digest::MD5.hexdigest(body) != etag
|
|
118
|
+
destination.dirname.mkpath
|
|
119
|
+
destination.binwrite(body)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# Stores `body` at `key`. With `create`, only if there is no such object
|
|
123
|
+
# yet: returns false if there was one.
|
|
124
|
+
def put(key, body, content_type:, cache_control:, create: false)
|
|
125
|
+
headers = { "content-type" => content_type, "cache-control" => cache_control }
|
|
126
|
+
headers["if-none-match"] = "*" if create
|
|
127
|
+
response = request("PUT", key, body: body, headers: headers, expect: create ? [200, 409, 412] : [200])
|
|
128
|
+
response.code == "200"
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# Deletes `key`; with `etag`, only that version of it.
|
|
132
|
+
def delete(key, etag: nil)
|
|
133
|
+
request("DELETE", key, headers: etag ? { "if-match" => %("#{etag}") } : {}, expect: [200, 204, 404, 412])
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# Runs the block holding the lock at PREFIX/.lock, which only one writer
|
|
137
|
+
# can create: others wait for it, up to `lock_timeout` seconds, and one
|
|
138
|
+
# older than STALE is broken, loudly.
|
|
139
|
+
def locked
|
|
140
|
+
token = SecureRandom.hex(16)
|
|
141
|
+
owner = ENV["GITHUB_REPOSITORY"] && ENV["GITHUB_RUN_ID"] ? "#{ENV['GITHUB_SERVER_URL'] || 'https://github.com'}/#{ENV['GITHUB_REPOSITORY']}/actions/runs/#{ENV['GITHUB_RUN_ID']}" : "#{Etc.getlogin || 'someone'}@#{Socket.gethostname} (pid #{Process.pid})"
|
|
142
|
+
body = JSON.generate("owner" => owner, "created" => Time.now.utc.iso8601, "token" => token)
|
|
143
|
+
deadline = monotonic + lock_timeout
|
|
144
|
+
delay = 1.0
|
|
145
|
+
until put(key(".lock"), body, content_type: "application/json", cache_control: "no-store", create: true)
|
|
146
|
+
held, etag = get(key(".lock"))
|
|
147
|
+
if held
|
|
148
|
+
details = begin
|
|
149
|
+
JSON.parse(held)
|
|
150
|
+
rescue JSON::ParserError
|
|
151
|
+
{}
|
|
152
|
+
end
|
|
153
|
+
created = Time.iso8601(details["created"].to_s) rescue Time.at(0)
|
|
154
|
+
if Time.now - created > STALE
|
|
155
|
+
warn "native-packages: WARNING: breaking the lock on #{@bucket}/#{prefix}, held by #{details['owner'] || 'unknown'} since #{details['created'] || 'an unknown time'}, more than #{STALE / 60} minutes ago"
|
|
156
|
+
delete(key(".lock"), etag: etag)
|
|
157
|
+
next
|
|
158
|
+
end
|
|
159
|
+
if monotonic + delay > deadline
|
|
160
|
+
raise Error, "#{@bucket}/#{prefix} is locked by #{details['owner'] || 'another publication'} since #{details['created']}; gave up after #{lock_timeout} seconds"
|
|
161
|
+
end
|
|
162
|
+
puts "Waiting for #{details['owner'] || 'another publication'} to finish publishing to #{@bucket}/#{prefix}"
|
|
163
|
+
sleep delay
|
|
164
|
+
delay = [delay * 2, 30.0].min
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
@token = token
|
|
168
|
+
begin
|
|
169
|
+
yield
|
|
170
|
+
ensure
|
|
171
|
+
@token = nil
|
|
172
|
+
held, etag = get(key(".lock"))
|
|
173
|
+
if held && (JSON.parse(held)["token"] rescue nil) == token
|
|
174
|
+
delete(key(".lock"), etag: etag)
|
|
175
|
+
else
|
|
176
|
+
warn "native-packages: WARNING: the lock on #{@bucket}/#{prefix} was broken by another publication while this one held it"
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
# Fails unless this publication still holds the lock.
|
|
182
|
+
def check_lock
|
|
183
|
+
held, = get(key(".lock"))
|
|
184
|
+
owned = held && (JSON.parse(held)["token"] rescue nil) == @token
|
|
185
|
+
raise Error, "lost the lock on #{@bucket}/#{prefix} to another publication; nothing was uploaded" unless owned
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
private
|
|
189
|
+
|
|
190
|
+
def monotonic = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
191
|
+
|
|
192
|
+
def text(xml, tag)
|
|
193
|
+
value = xml[%r{<#{tag}>(.*?)</#{tag}>}m, 1]
|
|
194
|
+
value&.gsub(/&(lt|gt|quot|apos|amp|#(\d+)|#x(\h+));/) do
|
|
195
|
+
{ "lt" => "<", "gt" => ">", "quot" => '"', "apos" => "'", "amp" => "&" }.fetch(Regexp.last_match(1)) do
|
|
196
|
+
(Regexp.last_match(2) ? Regexp.last_match(2).to_i : Regexp.last_match(3).to_i(16)).chr(Encoding::UTF_8)
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
# RFC 3986 encoding, as Signature Version 4 requires.
|
|
202
|
+
def escape(value, slash: true)
|
|
203
|
+
value.to_s.b.gsub(/[^A-Za-z0-9_.~\-#{slash ? '/' : ''}]/n) { |byte| format("%%%02X", byte.ord) }
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
def connection
|
|
207
|
+
@connection ||= Net::HTTP.new(@endpoint.host, @endpoint.port).tap do |http|
|
|
208
|
+
http.use_ssl = @endpoint.scheme == "https"
|
|
209
|
+
http.open_timeout = 30
|
|
210
|
+
http.read_timeout = 300
|
|
211
|
+
http.write_timeout = 300
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
# A signed request, retried on server and network errors. Fails on any
|
|
216
|
+
# status outside `expect`, naming the bucket's error code.
|
|
217
|
+
def request(method, key, query: {}, body: "", headers: {}, expect: [200])
|
|
218
|
+
path = "/#{escape(@bucket)}#{key ? "/#{escape(key)}" : ''}"
|
|
219
|
+
canonical_query = query.sort.map { |name, value| "#{escape(name, slash: false)}=#{escape(value, slash: false)}" }.join("&")
|
|
220
|
+
attempt = 0
|
|
221
|
+
begin
|
|
222
|
+
attempt += 1
|
|
223
|
+
signed = sign(method, path, canonical_query, body, headers)
|
|
224
|
+
request = Net::HTTPGenericRequest.new(method, !body.empty?, method != "HEAD", canonical_query.empty? ? path : "#{path}?#{canonical_query}")
|
|
225
|
+
signed.each { |name, value| request[name] = value }
|
|
226
|
+
# Objects such as Packages.gz arrive as stored, never decompressed.
|
|
227
|
+
request["accept-encoding"] = "identity"
|
|
228
|
+
request.body = body unless body.empty?
|
|
229
|
+
response = connection.request(request)
|
|
230
|
+
raise Retry, "#{method} #{key || @bucket}: HTTP #{response.code}" if response.code.start_with?("5") && attempt < ATTEMPTS
|
|
231
|
+
unless expect.include?(response.code.to_i)
|
|
232
|
+
code = response.body.to_s[%r{<Code>(.*?)</Code>}, 1]
|
|
233
|
+
raise Error, "bucket #{method} #{key || @bucket} failed: HTTP #{response.code}#{code ? " #{code}" : ''}"
|
|
234
|
+
end
|
|
235
|
+
response
|
|
236
|
+
rescue Retry, IOError, SystemCallError, Timeout::Error, OpenSSL::SSL::SSLError, Net::HTTPBadResponse => error
|
|
237
|
+
raise Error, "bucket #{method} #{key || @bucket} failed: #{error.message}" if attempt >= ATTEMPTS
|
|
238
|
+
@connection&.finish rescue nil
|
|
239
|
+
@connection = nil
|
|
240
|
+
sleep 2**attempt
|
|
241
|
+
retry
|
|
242
|
+
end
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
class Retry < StandardError; end
|
|
246
|
+
|
|
247
|
+
def sign(method, path, query, body, extra)
|
|
248
|
+
time = Time.now.utc
|
|
249
|
+
stamp = time.strftime("%Y%m%dT%H%M%SZ")
|
|
250
|
+
date = stamp[0, 8]
|
|
251
|
+
payload = Digest::SHA256.hexdigest(body)
|
|
252
|
+
default_port = @endpoint.port == (@endpoint.scheme == "https" ? 443 : 80)
|
|
253
|
+
headers = extra.merge("host" => default_port ? @endpoint.host : "#{@endpoint.host}:#{@endpoint.port}",
|
|
254
|
+
"x-amz-date" => stamp, "x-amz-content-sha256" => payload)
|
|
255
|
+
headers["x-amz-security-token"] = @session if @session && !@session.empty?
|
|
256
|
+
names = headers.keys.map(&:downcase).sort
|
|
257
|
+
canonical = [method, path, query, names.map { |name| "#{name}:#{headers.fetch(name).to_s.strip}\n" }.join, names.join(";"), payload].join("\n")
|
|
258
|
+
scope = "#{date}/#{@region}/s3/aws4_request"
|
|
259
|
+
digest = OpenSSL::Digest.new("SHA256")
|
|
260
|
+
signing_key = ["AWS4#{@secret}", date, @region, "s3", "aws4_request"].reduce { |key, part| OpenSSL::HMAC.digest(digest, key, part) }
|
|
261
|
+
signature = OpenSSL::HMAC.hexdigest(digest, signing_key, ["AWS4-HMAC-SHA256", stamp, scope, Digest::SHA256.hexdigest(canonical)].join("\n"))
|
|
262
|
+
headers.merge("authorization" => "AWS4-HMAC-SHA256 Credential=#{@access}/#{scope}, SignedHeaders=#{names.join(';')}, Signature=#{signature}")
|
|
263
|
+
end
|
|
264
|
+
end
|
|
265
|
+
end
|
|
@@ -13,7 +13,7 @@ module NativePackages
|
|
|
13
13
|
class Build
|
|
14
14
|
include Support
|
|
15
15
|
# Destinations signed with the OpenPGP key from the environment.
|
|
16
|
-
HOSTED = %w[apt dnf flatpak].freeze
|
|
16
|
+
HOSTED = %w[apt dnf pacman flatpak].freeze
|
|
17
17
|
attr_reader :configuration, :root, :project
|
|
18
18
|
|
|
19
19
|
def initialize(configuration)
|
|
@@ -426,6 +426,8 @@ module NativePackages
|
|
|
426
426
|
end
|
|
427
427
|
# Read once: the signing key leaves the environment when it is read.
|
|
428
428
|
signing = OpenPGP.environment unless (destinations & HOSTED).empty?
|
|
429
|
+
# Likewise the bucket's credentials, when a repository has a bucket.
|
|
430
|
+
credentials = Bucket.environment if (destinations & HOSTED).any? { |name| configuration.data[name].is_a?(Hash) && configuration.data[name]["bucket"] }
|
|
429
431
|
destinations.each do |destination|
|
|
430
432
|
if destination == "github"
|
|
431
433
|
paths = manifest.fetch("packages").flat_map { |entry| [entry.fetch("path"), entry["zsync"]].compact.map { |path| output / path } }
|
|
@@ -439,11 +441,13 @@ module NativePackages
|
|
|
439
441
|
run "gh", "release", "upload", "v#{manifest.fetch('version')}", *paths, checksums, "--repo", project.repository, "--clobber"
|
|
440
442
|
end
|
|
441
443
|
elsif destination == "apt"
|
|
442
|
-
AptRepository.
|
|
444
|
+
AptRepository.for(configuration).release(packages(manifest, output, "deb"), signing: signing, credentials: credentials || {})
|
|
443
445
|
elsif destination == "dnf"
|
|
444
|
-
DnfRepository.
|
|
446
|
+
DnfRepository.for(configuration).release(packages(manifest, output, "rpm"), signing: signing, credentials: credentials || {})
|
|
447
|
+
elsif destination == "pacman"
|
|
448
|
+
PacmanRepository.for(configuration).release(packages(manifest, output, "archlinux"), signing: signing, credentials: credentials || {})
|
|
445
449
|
elsif destination == "flatpak"
|
|
446
|
-
FlatpakRepository.
|
|
450
|
+
FlatpakRepository.for(configuration).publish(packages(manifest, output, "flatpak"), **signing)
|
|
447
451
|
elsif destination == "snapcraft"
|
|
448
452
|
Snap.new(configuration).publish(packages(manifest, output, "snap"))
|
|
449
453
|
else
|
|
@@ -7,6 +7,7 @@ require_relative "snap"
|
|
|
7
7
|
require_relative "apt_repository"
|
|
8
8
|
require_relative "dnf_repository"
|
|
9
9
|
require_relative "flatpak_repository"
|
|
10
|
+
require_relative "pacman_repository"
|
|
10
11
|
require_relative "windows_signing"
|
|
11
12
|
|
|
12
13
|
module NativePackages
|
|
@@ -17,11 +18,11 @@ module NativePackages
|
|
|
17
18
|
# without nFPM.
|
|
18
19
|
IMAGE_FORMATS = %w[appimage flatpak snap].freeze
|
|
19
20
|
# Publish destinations besides the configured downstream repositories.
|
|
20
|
-
DESTINATIONS = %w[github snapcraft apt dnf flatpak].freeze
|
|
21
|
+
DESTINATIONS = %w[github snapcraft apt dnf pacman flatpak].freeze
|
|
21
22
|
FORMATS = (%w[deb rpm archlinux apk ipk msix srpm appimage flatpak snap] + NATIVE_FORMATS).freeze
|
|
22
23
|
NFPM_VERSION = "2.47.0"
|
|
23
24
|
NAMES = %w[native-packages.yaml native-packages.yml].freeze
|
|
24
|
-
KEYS = %w[schema tool nfpm targets release assets templates repositories revisions libraries version_file version_section windows flatpak snap apt dnf].freeze
|
|
25
|
+
KEYS = %w[schema tool nfpm targets release assets templates repositories revisions libraries version_file version_section windows flatpak snap apt dnf pacman].freeze
|
|
25
26
|
attr_reader :root, :path, :data
|
|
26
27
|
|
|
27
28
|
def self.discover(root, explicit = nil)
|
|
@@ -191,6 +192,8 @@ module NativePackages
|
|
|
191
192
|
Snap.validate(data)
|
|
192
193
|
AptRepository.validate(data)
|
|
193
194
|
DnfRepository.validate(data)
|
|
195
|
+
PacmanRepository.validate(data)
|
|
196
|
+
Bucket.check_overlaps(data.slice("apt", "dnf", "pacman"))
|
|
194
197
|
FlatpakRepository.validate(data)
|
|
195
198
|
project.repositories
|
|
196
199
|
true
|
|
@@ -8,17 +8,14 @@ module NativePackages
|
|
|
8
8
|
# beside it as repomd.xml.asc. Beside them go the public key (NAME.asc) and
|
|
9
9
|
# a NAME.repo file for users to install.
|
|
10
10
|
class DnfRepository < HostedRepository
|
|
11
|
+
SECTION = "dnf"
|
|
11
12
|
TAGS = { 1000 => "name", 1001 => "version", 1002 => "release", 1003 => "epoch", 1022 => "arch", 1044 => "sourcerpm" }.freeze
|
|
12
13
|
# Signature header tags for DSA, RSA, PGP, GPG and OpenPGP signatures.
|
|
13
14
|
SIGNATURES = [267, 268, 1002, 1005, 278].freeze
|
|
14
15
|
|
|
15
16
|
def self.validate(data)
|
|
16
17
|
settings = data["dnf"]
|
|
17
|
-
check(settings, "dnf", KEYS) unless settings.nil?
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
def initialize(configuration)
|
|
21
|
-
super(configuration, configuration.data.fetch("dnf") { raise Error, "configure dnf to publish --to dnf" })
|
|
18
|
+
check(settings, "dnf", KEYS + %w[name bucket]) unless settings.nil?
|
|
22
19
|
end
|
|
23
20
|
|
|
24
21
|
# The name, version, release, epoch and architecture of the RPM at
|
|
@@ -96,9 +93,10 @@ module NativePackages
|
|
|
96
93
|
def publish(rpms, secret: nil, passphrase: nil)
|
|
97
94
|
raise Error, "the build contains no rpm packages to publish --to dnf" if rpms.empty?
|
|
98
95
|
raise Error, "install createrepo_c to publish --to dnf" unless available?("createrepo_c")
|
|
99
|
-
rpms.
|
|
100
|
-
self.class.header(path)
|
|
96
|
+
names = rpms.map do |path|
|
|
97
|
+
header = self.class.header(path)
|
|
101
98
|
place(path, directory / "packages" / path.basename)
|
|
99
|
+
header.fetch("name")
|
|
102
100
|
end
|
|
103
101
|
entries = Pathname.glob(directory / "packages/*.rpm").sort.to_h do |path|
|
|
104
102
|
header = self.class.header(path)
|
|
@@ -109,23 +107,32 @@ module NativePackages
|
|
|
109
107
|
# gzip, which every dnf and zypper reads; newer createrepo_c defaults to zstd.
|
|
110
108
|
run "createrepo_c", "--quiet", "--no-database", "--general-compress-type=gz", directory
|
|
111
109
|
signing.detach_sign(directory / "repodata/repomd.xml", directory / "repodata/repomd.xml.asc")
|
|
112
|
-
write(directory / "#{
|
|
110
|
+
write(directory / "#{repository}.asc", signing.public_key(armor: true))
|
|
113
111
|
end
|
|
114
112
|
signed = entries.values.all? { |_, header| header.fetch("signed") }
|
|
115
|
-
write(directory / "#{
|
|
116
|
-
[#{
|
|
117
|
-
name=#{
|
|
113
|
+
write(directory / "#{repository}.repo", <<~REPO)
|
|
114
|
+
[#{repository}]
|
|
115
|
+
name=#{repository}
|
|
118
116
|
baseurl=#{url}
|
|
119
117
|
enabled=1
|
|
120
118
|
repo_gpgcheck=1
|
|
121
119
|
gpgcheck=#{signed ? 1 : 0}
|
|
122
|
-
gpgkey=#{url}/#{
|
|
120
|
+
gpgkey=#{url}/#{repository}.asc
|
|
123
121
|
REPO
|
|
124
|
-
puts "Published #{entries.length} package#{"s" unless entries.length == 1} to #{directory}. Install with:", *instructions.map { |line| " #{line}" }
|
|
122
|
+
puts "Published #{entries.length} package#{"s" unless entries.length == 1} to #{directory}. Install with:", *instructions(names.uniq).map { |line| " #{line}" }
|
|
125
123
|
end
|
|
126
124
|
|
|
127
|
-
|
|
128
|
-
|
|
125
|
+
# The packages, and the metadata createrepo_c names after its checksum;
|
|
126
|
+
# repomd.xml, which points at the current metadata, changes.
|
|
127
|
+
def immutable
|
|
128
|
+
metadata = Pathname.glob(directory / "repodata/*").select { |path| path.file? && /\A\h{32,}-/.match?(path.basename.to_s) }
|
|
129
|
+
(Pathname.glob(directory / "packages/*.rpm") + metadata).sort
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def entry_points = [directory / "repodata/repomd.xml", directory / "repodata/repomd.xml.asc"]
|
|
133
|
+
|
|
134
|
+
def instructions(packages = [name])
|
|
135
|
+
["sudo curl -fsSLo /etc/yum.repos.d/#{repository}.repo #{url}/#{repository}.repo", "sudo dnf install #{packages.join(' ')}"]
|
|
129
136
|
end
|
|
130
137
|
end
|
|
131
138
|
end
|
|
@@ -27,10 +27,12 @@ module NativePackages
|
|
|
27
27
|
end
|
|
28
28
|
end
|
|
29
29
|
|
|
30
|
+
def self.for(configuration) = new(configuration)
|
|
31
|
+
|
|
30
32
|
def initialize(configuration)
|
|
31
33
|
settings = configuration.data.fetch("flatpak", {})["repository"]
|
|
32
34
|
raise Error, "configure flatpak.repository to publish --to flatpak" unless settings
|
|
33
|
-
super(configuration,
|
|
35
|
+
super(settings, root: configuration.root, package: configuration.name)
|
|
34
36
|
@flatpak = Flatpak.new(configuration)
|
|
35
37
|
end
|
|
36
38
|
|
|
@@ -1,15 +1,33 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "stringio"
|
|
4
|
+
require "zlib"
|
|
5
|
+
require "set"
|
|
6
|
+
require_relative "bucket"
|
|
3
7
|
require_relative "openpgp"
|
|
4
8
|
|
|
5
9
|
module NativePackages
|
|
6
10
|
# A signed package repository that native-packages writes into a directory,
|
|
7
|
-
# such as a GitHub Pages checkout, to be served as static files.
|
|
8
|
-
# all it does: the release process commits and pushes
|
|
11
|
+
# such as a GitHub Pages checkout, to be served as static files. Without a
|
|
12
|
+
# bucket, writing is all it does: the release process commits and pushes
|
|
13
|
+
# the directory. With one, publishing takes the bucket's lock, mirrors the
|
|
14
|
+
# repository into the directory, publishes there and uploads the changes.
|
|
9
15
|
class HostedRepository
|
|
10
16
|
include Support
|
|
11
17
|
KEYS = %w[directory url keep signing_key].freeze
|
|
12
18
|
KEEP = 3
|
|
19
|
+
# A repository name: a file name and, for DNF and pacman, a section name.
|
|
20
|
+
NAME = /\A[a-z0-9][a-z0-9_-]*\z/
|
|
21
|
+
# Files that never change once published, and everything else.
|
|
22
|
+
IMMUTABLE = "public, max-age=31536000, immutable"
|
|
23
|
+
MUTABLE = "public, max-age=60"
|
|
24
|
+
# Bucket objects that are not part of the repository's files.
|
|
25
|
+
LOCK = ".lock"
|
|
26
|
+
MANIFEST = ".published"
|
|
27
|
+
TYPES = { ".deb" => "application/vnd.debian.binary-package", ".rpm" => "application/x-rpm", ".zst" => "application/zstd",
|
|
28
|
+
".xz" => "application/x-xz", ".gz" => "application/gzip", ".xml" => "application/xml", ".sig" => "application/pgp-signature",
|
|
29
|
+
".asc" => "application/pgp-keys", ".gpg" => "application/pgp-keys", ".sources" => "text/plain; charset=utf-8",
|
|
30
|
+
".repo" => "text/plain; charset=utf-8", ".db" => "application/gzip", ".files" => "application/gzip" }.freeze
|
|
13
31
|
attr_reader :root, :name, :settings, :directory
|
|
14
32
|
|
|
15
33
|
# Checks the settings shared by every hosted repository, under `label`.
|
|
@@ -17,25 +35,68 @@ module NativePackages
|
|
|
17
35
|
raise Error, "#{label}: expected a mapping" unless settings.is_a?(Hash)
|
|
18
36
|
unknown = settings.keys - keys
|
|
19
37
|
raise Error, "#{label}: unknown fields #{unknown.join(', ')}" unless unknown.empty?
|
|
20
|
-
|
|
38
|
+
if settings.key?("bucket")
|
|
39
|
+
Bucket.check(settings["bucket"], "#{label}.bucket")
|
|
40
|
+
raise Error, "#{label}.directory: expected a directory to keep a copy of the bucket in" if settings.key?("directory") && !(settings["directory"].is_a?(String) && !settings["directory"].empty?)
|
|
41
|
+
else
|
|
42
|
+
raise Error, "#{label}.directory: name the directory to write the repository into" unless settings["directory"].is_a?(String) && !settings["directory"].empty?
|
|
43
|
+
end
|
|
21
44
|
unless settings["url"].is_a?(String) && %r{\Ahttps://[^\s/]+(?:/\S*)?\z}.match?(settings["url"])
|
|
22
45
|
raise Error, "#{label}.url: expected the https:// URL the directory is served at"
|
|
23
46
|
end
|
|
24
47
|
keep = settings.fetch("keep", KEEP)
|
|
25
48
|
raise Error, "#{label}.keep: expected how many versions of each package to keep (1 or more)" unless keep.is_a?(Integer) && keep.positive?
|
|
49
|
+
if settings.key?("name") && !NAME.match?(settings["name"].to_s)
|
|
50
|
+
raise Error, "#{label}.name: use lowercase letters, digits, underscores or hyphens"
|
|
51
|
+
end
|
|
26
52
|
OpenPGP.fingerprint(settings["signing_key"], "#{label}.signing_key")
|
|
27
53
|
end
|
|
28
54
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
55
|
+
# The repository a project's configuration sets up under SECTION.
|
|
56
|
+
def self.for(configuration)
|
|
57
|
+
settings = configuration.data.fetch(self::SECTION) { raise Error, "configure #{self::SECTION} to publish --to #{self::SECTION}" }
|
|
58
|
+
new(settings, root: configuration.root, package: configuration.name)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# `settings` is the repository's section, with its directory relative to
|
|
62
|
+
# `root`. `package`, the app's package name, is the default repository
|
|
63
|
+
# name; a repository shared by several apps names itself.
|
|
64
|
+
def initialize(settings, root:, package: nil)
|
|
65
|
+
@root = Pathname.new(root)
|
|
66
|
+
@name = package
|
|
32
67
|
@settings = settings
|
|
33
|
-
@directory = Pathname.new(settings.fetch("directory")).expand_path(root)
|
|
68
|
+
@directory = settings["directory"] && Pathname.new(settings.fetch("directory")).expand_path(root)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Publishes `packages` into the directory, or into the bucket when one
|
|
72
|
+
# is configured, with `signing` (OpenPGP.environment) and `credentials`
|
|
73
|
+
# (Bucket.environment).
|
|
74
|
+
def release(packages, signing: {}, credentials: {})
|
|
75
|
+
return publish(packages, **signing) unless settings["bucket"]
|
|
76
|
+
bucket = Bucket.new(settings.fetch("bucket"), **credentials)
|
|
77
|
+
return synchronize(bucket, packages, signing) if directory
|
|
78
|
+
Dir.mktmpdir("native-packages-repository-") do |copy|
|
|
79
|
+
@directory = Pathname.new(copy)
|
|
80
|
+
synchronize(bucket, packages, signing)
|
|
81
|
+
ensure
|
|
82
|
+
@directory = nil
|
|
83
|
+
end
|
|
34
84
|
end
|
|
35
85
|
|
|
86
|
+
# The files that never change once published, which caches may keep.
|
|
87
|
+
def immutable = []
|
|
88
|
+
|
|
89
|
+
# The files clients read first, which point at the rest: uploaded last.
|
|
90
|
+
def entry_points = []
|
|
91
|
+
|
|
36
92
|
def url = settings.fetch("url").chomp("/")
|
|
37
93
|
def keep = settings.fetch("keep", KEEP)
|
|
38
94
|
|
|
95
|
+
# The name of the files users install (key, source list) and of the
|
|
96
|
+
# repository itself, shared by every app that publishes into it. Defaults
|
|
97
|
+
# to the package name.
|
|
98
|
+
def repository = settings.fetch("name") { name or raise Error, "#{self.class::SECTION}.name: name the repository" }
|
|
99
|
+
|
|
39
100
|
def signer(secret: nil, passphrase: nil)
|
|
40
101
|
OpenPGP.new(OpenPGP.fingerprint(settings.fetch("signing_key"), "signing_key"), secret: secret, passphrase: passphrase)
|
|
41
102
|
end
|
|
@@ -69,5 +130,92 @@ module NativePackages
|
|
|
69
130
|
end
|
|
70
131
|
entries.except(*removed)
|
|
71
132
|
end
|
|
133
|
+
|
|
134
|
+
private
|
|
135
|
+
|
|
136
|
+
def synchronize(bucket, packages, signing)
|
|
137
|
+
bucket.locked do
|
|
138
|
+
remote = bucket.list.filter_map do |key, (size, etag)|
|
|
139
|
+
relative = key.delete_prefix("#{bucket.prefix}/")
|
|
140
|
+
[relative, [size, etag]] unless [LOCK, MANIFEST].include?(relative) || relative.end_with?("/")
|
|
141
|
+
end.to_h
|
|
142
|
+
mirror(bucket, remote)
|
|
143
|
+
history = bucket.get(bucket.key(MANIFEST))&.first.to_s.lines.to_h { |line| line.chomp.split(" ", 2).reverse }
|
|
144
|
+
publish(packages, **signing)
|
|
145
|
+
upload(bucket, remote, history)
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# Makes the directory hold exactly the bucket's files, downloading only
|
|
150
|
+
# those it lacks or holds in another version.
|
|
151
|
+
def mirror(bucket, remote)
|
|
152
|
+
directory.mkpath
|
|
153
|
+
files(directory).each { |path| path.delete unless remote.key?(path.relative_path_from(directory).to_s) }
|
|
154
|
+
count = 0
|
|
155
|
+
remote.each do |relative, (size, etag)|
|
|
156
|
+
path = directory / relative_path(relative)
|
|
157
|
+
next if path.file? && path.size == size && (!etag.match?(/\A\h{32}\z/) || Digest::MD5.file(path).hexdigest == etag)
|
|
158
|
+
bucket.download(bucket.key(relative), path, etag)
|
|
159
|
+
count += 1
|
|
160
|
+
end
|
|
161
|
+
puts "Downloaded #{count} of #{remote.length} files from the bucket"
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# Uploads what publishing added or changed, package files first and the
|
|
165
|
+
# files that point at them last, then deletes what it removed.
|
|
166
|
+
def upload(bucket, remote, history)
|
|
167
|
+
local = files(directory).to_h { |path| [path.relative_path_from(directory).to_s, path] }
|
|
168
|
+
fixed = immutable.to_set { |path| path.relative_path_from(directory).to_s }
|
|
169
|
+
changed = local.reject do |relative, path|
|
|
170
|
+
size, etag = remote[relative]
|
|
171
|
+
# Mirrored files are as the bucket holds them when publishing left them alone.
|
|
172
|
+
size == path.size && (etag.match?(/\A\h{32}\z/) ? Digest::MD5.file(path).hexdigest == etag : fixed.include?(relative))
|
|
173
|
+
end
|
|
174
|
+
packages = changed.keys.select { |relative| fixed.include?(relative) && !relative.end_with?(".sig") && !relative.include?("repodata/") }
|
|
175
|
+
changed.each_key do |relative|
|
|
176
|
+
next unless fixed.include?(relative)
|
|
177
|
+
raise Error, "#{relative} is already in the bucket with different contents" if remote.key?(relative)
|
|
178
|
+
digest = Digest::SHA256.file(local.fetch(relative)).hexdigest
|
|
179
|
+
if packages.include?(relative) && history.key?(relative) && history[relative] != digest
|
|
180
|
+
raise Error, "#{relative} was published before with different contents, and caches may still hold it; publish it with a new version or release number"
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
bucket.check_lock
|
|
184
|
+
first = entry_points.to_set { |path| path.relative_path_from(directory).to_s }
|
|
185
|
+
ordered = changed.keys.sort_by { |relative| [first.include?(relative) ? 1 : 0, relative] }
|
|
186
|
+
lasting, current = ordered.partition { |relative| fixed.include?(relative) }
|
|
187
|
+
put = ->(relative, cache) { bucket.put(bucket.key(relative), local.fetch(relative).binread, content_type: content_type(relative), cache_control: cache) }
|
|
188
|
+
lasting.each { |relative| put.call(relative, IMMUTABLE) }
|
|
189
|
+
unless packages.empty?
|
|
190
|
+
# Recorded for good, so a package pruned and published again with
|
|
191
|
+
# other contents is refused while caches may still hold the old one.
|
|
192
|
+
packages.each { |relative| history[relative] = Digest::SHA256.file(local.fetch(relative)).hexdigest }
|
|
193
|
+
bucket.put(bucket.key(MANIFEST), history.sort.map { |relative, digest| "#{digest} #{relative}\n" }.join,
|
|
194
|
+
content_type: "text/plain; charset=utf-8", cache_control: "no-store")
|
|
195
|
+
end
|
|
196
|
+
# Indexes after the packages they list, and the files that point at
|
|
197
|
+
# the indexes last.
|
|
198
|
+
current.each { |relative| put.call(relative, MUTABLE) }
|
|
199
|
+
removed = (remote.keys - local.keys).sort
|
|
200
|
+
removed.each { |relative| bucket.delete(bucket.key(relative)) }
|
|
201
|
+
puts "Uploaded #{changed.length} and deleted #{removed.length} files in the bucket under #{bucket.prefix}/"
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def content_type(relative)
|
|
205
|
+
name = File.basename(relative)
|
|
206
|
+
return "application/pgp-signature" if name.end_with?(".gpg", ".asc") && %w[Release.gpg repomd.xml.asc].include?(name)
|
|
207
|
+
return "text/plain; charset=utf-8" if %w[Release InRelease Packages].include?(name)
|
|
208
|
+
TYPES.fetch(File.extname(name), "application/octet-stream")
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
# The same bytes for the same index: no name or time in the header.
|
|
212
|
+
def gzip(text)
|
|
213
|
+
io = StringIO.new("".b)
|
|
214
|
+
writer = Zlib::GzipWriter.new(io, Zlib::BEST_COMPRESSION)
|
|
215
|
+
writer.mtime = 0
|
|
216
|
+
writer.write(text)
|
|
217
|
+
writer.finish
|
|
218
|
+
io.string
|
|
219
|
+
end
|
|
72
220
|
end
|
|
73
221
|
end
|
|
@@ -75,8 +75,10 @@ module NativePackages
|
|
|
75
75
|
verify(output)
|
|
76
76
|
end
|
|
77
77
|
|
|
78
|
-
|
|
79
|
-
|
|
78
|
+
# An ASCII-armored signature unless `armor` is false: pacman reads
|
|
79
|
+
# binary signatures only.
|
|
80
|
+
def detach_sign(input, output, armor: true)
|
|
81
|
+
sign("--detach-sign", input, output, *("--armor" if armor))
|
|
80
82
|
verify(output, input)
|
|
81
83
|
end
|
|
82
84
|
|