Debian Codegraph @

packages/packages-groonga-org-package-task.rb

raw symbols SCIP
1 # Copyright(C) 2014-2021 Sutou Kouhei <[email protected]>
2 # Copyright(C) 2020-2023 Horimoto Yasuhiro <[email protected]>
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
18 apache_arrow_repository = ENV["APACHE_ARROW_REPOSITORY"]
19 if apache_arrow_repository.nil?
20 raise "Specify APACHE_ARROW_REPOSITORY environment variable"
21 end
22 require "#{apache_arrow_repository}/dev/tasks/linux-packages/package-task"
23
24 require_relative "launchpad-helper"
25 require_relative "repository-helper"
26
27 class PackagesGroongaOrgPackageTask < PackageTask
28 include LaunchpadHelper
29 include RepositoryHelper
30
31 def define
32 super
33 define_source_task
34 define_windows_task
35 define_release_tasks
36 define_ubuntu_tasks
37 end
38
39 private
40 def detect_release_time
41 release_time_env = ENV["RELEASE_TIME"]
42 if release_time_env
43 Time.parse(release_time_env).utc
44 else
45 Time.now.utc
46 end
47 end
48
49 def define_apt_task
50 namespace :apt do
51 desc "Clean APT repositories"
52 task :clean do
53 rm_rf("#{apt_dir}/repositories")
54 end
55 end
56 task apt: ["apt:clean"]
57 super
58 end
59
60 def define_yum_task
61 namespace :yum do
62 desc "Clean Yum repositories"
63 task :clean do
64 rm_rf("#{yum_dir}/repositories")
65 end
66 end
67 task yum: ["yum:clean"]
68 super
69 end
70
71 def yum_build(console: false)
72 super
73 yum_keep_srpm_paths_for_compatibility
74 yum_remove_srpm_from_non_primary_architectures
75 end
76
77 def yum_keep_srpm_paths_for_compatibility
78 repositories_path = "#{yum_dir}/repositories"
79
80 yum_targets.each do |target|
81 distribution, version, architecture = split_target(target)
82
83 # Rename SRPM directory only for the following distributions
84 # to keep backward compatibility:
85 #
86 # * AlmaLinux 8
87 # * AlmaLinux 9
88 # * AlmaLinux 10
89 # * Amazon Linux 2023
90 #
91 # They keep using source/SRPMS/*.src.rpm for SRPM path.
92 case "#{distribution}-#{version}"
93 when "almalinux-8",
94 "almalinux-9",
95 "almalinux-10",
96 "amazon-linux-2023"
97 next unless yum_primary_architecture?(architecture)
98 distribution_path = "#{repositories_path}/#{distribution}/#{version}"
99 backward_compatible_path = "#{distribution_path}/source/SRPMS"
100 new_path = "#{distribution_path}/Source/Packages"
101 mkdir_p(File.dirname(backward_compatible_path))
102 mv(new_path, backward_compatible_path)
103 rm_rf(File.dirname(new_path))
104 else
105 # Use SRPM path as-is for other distributions: Source/Packages/*.src.rpm
106 end
107 end
108 end
109
110 def yum_remove_srpm_from_non_primary_architectures
111 repositories_path = "#{yum_dir}/repositories"
112 yum_targets.each do |target|
113 distribution, version, architecture = split_target(target)
114 next if yum_primary_architecture?(architecture)
115 srpm_path =
116 "#{repositories_path}/#{distribution}/#{version}/Source/Packages"
117 rm_rf(File.dirname(srpm_path))
118 end
119 end
120
121 def yum_primary_architecture?(architecture)
122 architecture.nil?
123 end
124
125 def enable_source?
126 true
127 end
128
129 def source_dir
130 "source"
131 end
132
133 def source_targets
134 return [] unless enable_source?
135
136 targets = (ENV["SOURCE_TARGETS"] || "").split(",")
137 return targets unless targets.empty?
138
139 source_targets_default
140 end
141
142 def define_source_task
143 namespace :source do
144 desc "Clean files for source"
145 task :clean do
146 rm_rf("#{source_dir}/repositories")
147 end
148 end
149 desc "Release files for source"
150 task source: ["source:clean"]
151 end
152
153 def enable_windows?
154 true
155 end
156
157 def windows_dir
158 "windows"
159 end
160
161 def windows_targets
162 return [] unless enable_windows?
163
164 targets = (ENV["WINDOWS_TARGETS"] || "").split(",")
165 return targets unless targets.empty?
166
167 windows_targets_default
168 end
169
170 def define_windows_task
171 namespace :windows do
172 desc "Clean files for Windows"
173 task :clean do
174 rm_rf("#{windows_dir}/repositories")
175 end
176 end
177 desc "Release files for Windows"
178 task windows: ["windows:clean"]
179 end
180
181 def docker_image(os, architecture)
182 "ghcr.io/#{github_repository}-package:#{super}"
183 end
184
185 def use_built_package?
186 false
187 end
188
189 def github_repository
190 raise NotImplementedError
191 end
192
193 def github_actions_workflow_file_name(target_namespace, target)
194 raise NotImplementedError
195 end
196
197 def github_actions_artifact_name(target_namespace, target)
198 raise NotImplementedError
199 end
200
201 def github_download_url(base_name)
202 "https://github.com/#{github_repository}/releases/download/#{tag_name}/#{base_name}"
203 end
204
205 def github_access_token
206 ENV["GITHUB_ACCESS_TOKEN"]
207 end
208
209 def detect_built_package_url_github_actions(target_namespace,
210 target,
211 branch,
212 artifact_name)
213 require "octokit"
214 client = Octokit::Client.new
215 client.access_token = github_access_token
216 workflow_file_name = github_actions_workflow_file_name(target_namespace,
217 target)
218 workflow_runs_response = client.workflow_runs(github_repository,
219 workflow_file_name,
220 branch: branch)
221 workflow_runs_response.workflow_runs.each do |workflow_run|
222 artifacts_response = client.get(workflow_run.artifacts_url, per_page: 50)
223 next if artifacts_response.total_count.zero?
224
225 artifacts_response.artifacts.each do |artifact|
226 if artifact.name == artifact_name
227 return artifact.archive_download_url
228 end
229 end
230 end
231 message = "can't detect built package URL:"
232 message << " target_namespace=<#{target_namespace.inspect}>"
233 message << " target=<#{target.inspect}>"
234 message << " branch=<#{branch.inspect}>"
235 message << " workflow_file_name=<#{workflow_file_name.inspect}>"
236 message << " artifact_name=<#{artifact_name.inspect}>"
237 raise message
238 end
239
240 def built_package_url(target_namespace, target)
241 raise NotImplementedError
242 end
243
244 def built_package_n_split_components
245 0
246 end
247
248 def open_url(url, &block)
249 if url.start_with?("https://api.github.com/")
250 access_token = github_access_token
251 URI(url).open("Authorization" => "token #{access_token}",
252 &block)
253 else
254 super
255 end
256 end
257
258 def use_packages_groonga_org?(target_namespace)
259 true
260 end
261
262 def download_repositories_dir(target_namespace)
263 base_dir = __send__("#{target_namespace}_dir")
264 case target_namespace
265 when :source
266 "#{base_dir}/repositories/source/#{@package}"
267 when :windows
268 "#{base_dir}/repositories/windows/#{@package}"
269 else
270 "#{base_dir}/repositories"
271 end
272 end
273
274 def download_packages(target_namespace)
275 base_dir = __send__("#{target_namespace}_dir")
276 repositories_dir = download_repositories_dir(target_namespace)
277 mkdir_p(repositories_dir)
278 download_dir = "#{base_dir}/tmp/downloads/#{@version}"
279 mkdir_p(download_dir)
280
281 __send__("#{target_namespace}_targets").each do |target|
282 branch = ENV["BRANCH"]
283 if branch
284 artifact_name = github_actions_artifact_name(target_namespace, target)
285 url = detect_built_package_url_github_actions(target_namespace,
286 target,
287 branch,
288 artifact_name)
289 download_path = "#{download_dir}/#{artifact_name}.zip"
290 archive = download(url, download_path)
291 else
292 url = built_package_url(target_namespace, target)
293 archive = download(url, download_dir)
294 end
295 case target_namespace
296 when :apt, :yum
297 cd(repositories_dir) do
298 case File.extname(archive)
299 when ".zip"
300 sh("unzip", "-o", archive)
301 else
302 sh("tar",
303 "xf", archive,
304 "--strip-components=#{built_package_n_split_components}")
305 end
306 end
307 when :source
308 cd(repositories_dir) do
309 cp(archive, ".")
310 archive_base_name = File.basename(archive)
311 sh("gpg",
312 "--local-user", repository_gpg_key_ids[0],
313 "--armor",
314 "--detach-sign",
315 archive_base_name)
316 latest_link_base_name = archive_base_name.gsub(@version, "latest")
317 ln_sf(archive_base_name, latest_link_base_name)
318 end
319 when :windows
320 cd(repositories_dir) do
321 cp(archive, ".")
322 archive_base_name = File.basename(archive)
323 latest_link_base_name = archive_base_name.gsub(@version, "latest")
324 ln_sf(archive_base_name, latest_link_base_name)
325 end
326 end
327 end
328
329 case target_namespace
330 when :source, :windows
331 return unless use_packages_groonga_org?(target_namespace)
332 rsync_dir =
333 "#{repository_rsync_base_path}/#{target_namespace}/#{@package}/"
334 sh("rsync",
335 "-av",
336 "--include=.htaccess",
337 "--exclude=*",
338 rsync_dir,
339 "#{repositories_dir}/")
340 end
341 end
342
343 def prepare(target_namespace)
344 case target_namespace
345 when :windows, :source
346 return unless use_packages_groonga_org?(target_namespace)
347
348 repositories_dir = download_repositories_dir(target_namespace)
349 mkdir_p(repositories_dir)
350
351 htaccess_path = "#{repositories_dir}/.htaccess"
352 if File.exist?(htaccess_path)
353 htaccess_content = File.read(htaccess_path)
354 else
355 htaccess_content = ""
356 end
357 File.open(htaccess_path, "w") do |htaccess|
358 htaccess_content.each_line do |line|
359 htaccess.puts(line) unless line.include?("-latest")
360 end
361 __send__("#{target_namespace}_targets").each do |target|
362 redirect_url = built_package_url(target_namespace, target)
363 [
364 target,
365 target.gsub(@version, "latest")
366 ].each do |redirect_target_file|
367 redirect_target =
368 "/#{target_namespace}/#{@package}/#{redirect_target_file}"
369 htaccess.puts("Redirect #{redirect_target} #{redirect_url}")
370 if target_namespace == :source
371 htaccess.puts("Redirect #{redirect_target}.asc #{redirect_url}.asc")
372 end
373 end
374 end
375 end
376 end
377 end
378
379 def tag_name
380 @version
381 end
382
383 def release(target_namespace)
384 base_dir = __send__("#{target_namespace}_dir")
385 repositories_dir = "#{base_dir}/repositories"
386 destination = "#{repository_rsync_base_path}/"
387 rsync_options = ["-av"]
388 case target_namespace
389 when :source
390 sh({"GH_TOKEN" => github_access_token},
391 "gh",
392 "release",
393 "upload",
394 tag_name,
395 "--clobber",
396 "--repo", github_repository,
397 *Dir.glob("#{repositories_dir}/#{target_namespace}/#{@package}/*.asc"))
398 rsync_options << "--exclude=*.asc"
399 rsync_options << "--exclude=*.tar.gz"
400 rsync_options << "--exclude=*.zip"
401 when :windows
402 else
403 rsync_options << "--exclude=*.buildinfo"
404 rsync_options << "--exclude=*.changes"
405 destination << "incoming/"
406 end
407
408 return unless use_packages_groonga_org?(target_namespace)
409 sh("rsync",
410 *rsync_options,
411 "#{repositories_dir}/",
412 destination)
413 end
414
415 def define_release_tasks
416 [:apt, :yum, :source, :windows].each do |target_namespace|
417 tasks = []
418 namespace target_namespace do
419 enabled = __send__("enable_#{target_namespace}?")
420 if use_built_package?
421 target_task = Rake.application[target_namespace]
422 target_task.prerequisites.delete("#{target_namespace}:build")
423 desc "Download #{target_namespace} packages"
424 task :download do
425 download_packages(target_namespace) if enabled
426 end
427 tasks << "#{target_namespace}:download"
428 end
429
430 desc "Prepare #{target_namespace} packages"
431 task :prepare do
432 prepare(target_namespace) if enabled
433 end
434 tasks << "#{target_namespace}:prepare"
435
436 desc "Release #{target_namespace} packages"
437 task :release do
438 release(target_namespace) if enabled
439 end
440 tasks << "#{target_namespace}:release"
441 end
442 task target_namespace => tasks
443 end
444 end
445
446 def dput_configuration_name
447 ENV["DPUT_CONFIGURATION_NAME"] || "groonga-ppa"
448 end
449
450 def dput_incoming
451 ENV["DPUT_INCOMING"] || "~groonga/ppa/ubuntu/"
452 end
453 end