-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupdate
executable file
·170 lines (140 loc) · 6.15 KB
/
update
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env ruby
require "erb"
require "fileutils"
require "open-uri"
FileUtils.rm_rf(Dir.glob("[0-9]*"))
github_postgis_image = "ghcr.io/gui/postgis"
github_pgrouting_image = "ghcr.io/gui/pgrouting"
docker_hub_postgis_image = "nickblah/postgis"
docker_hub_pgrouting_image = "nickblah/pgrouting"
all_variant_tags = []
versions = {}
def parse_apt_packages(output)
output
.split(/[\r\n]{2,}/)
.select { |lines| lines.include?("Package:") }
.map { |lines| Hash[lines.split(/[\r\n]/).map { |r| r.split(/:\s+/, 2) }] }
.sort_by { |package| package.fetch("Version") }
.reverse
end
library = URI.open("https://raw.githubusercontent.com/docker-library/official-images/master/library/postgres")
.read
.split(/[\r\n]{2,}/)
.select { |lines| lines.include?("Tags:") }
.map { |lines| Hash[lines.split(/[\r\n]/).map { |r| r.split(/:\s+/, 2) }] }
library.each do |postgres|
postgres_tags = postgres.fetch("Tags").split(/,\s*/)
postgres_version_tags = postgres_tags - ["latest"]
postgres_tag = postgres_version_tags.sort_by { |t| t.length }.last
postgres_version = postgres_version_tags.sort_by { |t| t.length }.first
if !postgres.fetch("Directory").include?("alpine")
system("docker", "pull", "postgres:#{postgres_tag}")
postgis_packages = parse_apt_packages(`docker run --rm postgres:#{postgres_tag} sh -c 'apt-get update && apt-cache search --full --names-only "^postgresql-$PG_MAJOR-postgis-[0-9\.]+$"'`)
pgrouting_packages = parse_apt_packages(`docker run --rm postgres:#{postgres_tag} sh -c 'apt-get update && apt-cache search --full --names-only "^postgresql-$PG_MAJOR-pgrouting(-[0-9\.]+)?$"'`)
variants = []
variant = {
:postgres_tag => postgres_tag,
:postgis_major => nil,
:postgis_version => nil,
:pgrouting_major => nil,
:pgrouting_version => nil,
}
postgis_packages.each do |postgis_package|
variant[:postgis_major] = postgis_package.fetch("Package").split("-postgis-", 2).last
variant[:postgis_version] = postgis_package.fetch("Version")
variant[:postgis_version_parts] = variant.fetch(:postgis_version).match(/^(((\d+)\.\d+)\.\d+)/)
variants << variant
pgrouting_packages.each do |pgrouting_package|
pgrouting_variant = variant.dup
pgrouting_variant[:pgrouting_version] = pgrouting_package.fetch("Version")
pgrouting_variant[:pgrouting_version_parts] = pgrouting_variant.fetch(:pgrouting_version).match(/^(((\d+)\.\d+)\.\d+)/)
pgrouting_variant[:pgrouting_major] = pgrouting_variant.fetch(:pgrouting_version).to_i.to_s
variants << pgrouting_variant
end
end
variants.each_with_index do |variant, variant_index|
template = ERB.new(File.read("Dockerfile.erb"), trim_mode: "-").result_with_hash(variant)
variant_tags = postgres_version_tags.map do |postgres_tag|
tags = [
"#{postgres_tag}-postgis-#{variant.fetch(:postgis_version_parts)[1]}",
"#{postgres_tag}-postgis-#{variant.fetch(:postgis_version_parts)[2]}",
"#{postgres_tag}-postgis-#{variant.fetch(:postgis_version_parts)[3]}",
]
if variant.fetch(:pgrouting_version)
tags += [
"#{postgres_tag}-postgis-#{variant.fetch(:postgis_version_parts)[1]}-pgrouting-#{variant.fetch(:pgrouting_version_parts)[1]}",
"#{postgres_tag}-postgis-#{variant.fetch(:postgis_version_parts)[2]}-pgrouting-#{variant.fetch(:pgrouting_version_parts)[1]}",
"#{postgres_tag}-postgis-#{variant.fetch(:postgis_version_parts)[3]}-pgrouting-#{variant.fetch(:pgrouting_version_parts)[1]}",
]
end
tags
end.flatten.uniq
if postgres_tags.include?("latest") && variant_index == 0
variant_tags << "latest"
end
variant_tags -= all_variant_tags
all_variant_tags += variant_tags
dir = "postgis-#{variant.fetch(:postgis_major)}"
if variant.fetch(:pgrouting_version)
dir += "-pgrouting-#{variant.fetch(:pgrouting_major)}"
end
filename = File.join(postgres.fetch("Directory"), dir, "Dockerfile")
FileUtils.mkdir_p(File.dirname(filename))
File.open(filename, "w") { |f| f.write(template) }
platforms = ["linux/amd64"]
# No ARM packages for postgis available for stretch.
unless postgres.fetch("Directory").include?("/stretch")
platforms << "linux/arm64"
end
docker_image_tags = variant_tags.map do |tag|
if variant.fetch(:pgrouting_major)
[
"#{github_pgrouting_image}:#{tag}",
"#{docker_hub_pgrouting_image}:#{tag}",
]
else
[
"#{github_postgis_image}:#{tag}",
"#{docker_hub_postgis_image}:#{tag}",
]
end
end.flatten
variant_tag = variant_tags.first
if variant.fetch(:pgrouting_major)
build_cache_tag = "#{github_pgrouting_image}:build-cache-#{variant_tag}"
else
build_cache_tag = "#{github_postgis_image}:build-cache-#{variant_tag}"
end
versions[postgres_version] ||= []
versions[postgres_version] << {
:postgis_major => variant.fetch(:postgis_major),
:pgrouting_major => variant.fetch(:pgrouting_major),
:variant_tag => variant_tag,
:variant_tags => variant_tags,
:build_cache_tag => build_cache_tag,
:docker_image_tags => docker_image_tags,
:filename => filename,
:platforms => platforms,
}
end
end
end
template = ERB.new(File.read("docker-build.erb"), trim_mode: "-").result_with_hash({
:versions => versions,
})
File.open("docker-build", "w") { |f| f.write(template) }
FileUtils.chmod(0755, "docker-build")
template = ERB.new(File.read("docker-push.erb"), trim_mode: "-").result_with_hash({
:versions => versions,
})
File.open("docker-push", "w") { |f| f.write(template) }
FileUtils.chmod(0755, "docker-push")
template = ERB.new(File.read("test.erb"), trim_mode: "-").result_with_hash({
:versions => versions,
})
File.open("test", "w") { |f| f.write(template) }
FileUtils.chmod(0755, "test")
template = ERB.new(File.read("README.md.erb"), trim_mode: "-").result_with_hash({
:versions => versions,
})
File.open("README.md", "w") { |f| f.write(template) }