-
Notifications
You must be signed in to change notification settings - Fork 672
/
Rakefile
284 lines (231 loc) · 8.35 KB
/
Rakefile
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# frozen_string_literal: true
require "dotenv"
require "active_support"
MANIFESTS = {
container: ".Dockerfile",
build: ".Makefile",
}.freeze
Dotenv.load
class ::Hash
def recursive_merge(hash)
merge!(hash) { |_, old, new| old.instance_of?(Hash) ? old.recursive_merge(new) : new }
end
end
def architecture
if RUBY_PLATFORM.start_with?('aarch64')
'arm64'
else
'amd64'
end
end
def arch
if RUBY_PLATFORM.start_with?('aarch64')
'aarch64'
else
'x86_64'
end
end
def get_config_from(directory, engines_as_list: true)
main_config = YAML.safe_load(File.open(File.join(directory, "..", "..", "config.yaml")))
language_config = YAML.safe_load(File.open(File.join(directory, "..", "config.yaml")))
framework_config = YAML.safe_load(File.open(File.join(directory, "config.yaml")))
config = main_config.recursive_merge(language_config).recursive_merge(framework_config)
if config.dig("framework", "engines") && !engines_as_list
config["framework"]["engines"] = config.dig("framework", "engines").map do |row|
if row.is_a?(String) && config.dig("language", "engines", row)
{ row => config.dig("language", "engines", row) }
else
row
end
end
end
skippable_keys = framework_config["framework"].select { |_k, v| v.nil? }.keys
skippable_keys.each do |skippable_key|
config["framework"].except!(skippable_key)
config["language"].except!(skippable_key)
end
config
end
def custom_config(dict1, dict2, dict3)
keys = dict1.keys << dict2.keys << dict3.keys
data = {}
keys.flatten!.uniq.each do |key|
next if %w[version engines website github].include?(key)
data[key] = override_or_merge(dict3[key], dict2[key], dict1[key])
end
data
end
def override_or_merge(value3, value2, value1)
value = value3
if value
if value2
case value2
when Array
value.unshift(*value2)
when String
value = value2
end
end
else
value = value2
end
if value
if value1
case value1
when Array
value.unshift(*value1).uniq!
when String
value = value1
end
end
else
value = value1
end
value
end
def commands_for(language, framework, variant, provider = "docker")
config = YAML.safe_load(File.read("config.yaml"))
directory = Dir.pwd
main_config = YAML.safe_load(File.open(File.join(directory, "config.yaml")))
language_config = YAML.safe_load(File.open(File.join(directory, language, "config.yaml")))
framework_config = YAML.safe_load(File.open(File.join(directory, language, framework, "config.yaml")))
app_config = main_config.recursive_merge(language_config).recursive_merge(framework_config)
options = { language: language, framework: framework, variant: variant, manifest: "#{MANIFESTS[:container]}.#{variant}" }
commands = { build: [], collect: [], clean: [] }
# Compile first, only for non containers
if app_config.key?("binaries") && !(provider.start_with?("docker") || provider.start_with?("podman"))
commands << "docker build -f #{MANIFESTS[:container]}.#{variant} -t #{language}.#{framework} ."
commands << "docker run -td #{language}.#{framework} > cid.txt"
app_config["binaries"].each do |out|
if out.count(File::Separator).positive?
FileUtils.mkdir_p(File.join(directory, File.dirname(out)))
commands[:build] << "docker cp `cat cid-#{variants}.txt`:/opt/web/#{File.dirname(out)} ."
else
commands[:build] << "docker cp `cat cid-#{variants}.txt`:/opt/web/#{out} #{out}"
end
end
end
config["providers"][provider]["build"].each do |cmd|
commands[:build] << Mustache.render(cmd, options).to_s
end
config["providers"][provider]["metadata"].each do |cmd|
commands[:build] << Mustache.render(cmd, options).to_s
end
if app_config.key?("bootstrap") && config["providers"][provider].key?("exec")
remote_command = config["providers"][[provider]]["exec"]
app_config["bootstrap"].each do |cmd|
commands[:build] << Mustache.render(remote_command, options.merge!(command: cmd)).to_s
end
end
if config.dig("providers", provider).key?("reboot")
commands[:build] << config.dig("providers", provider, "reboot")
commands[:build] << "sleep 30"
end
commands[:collect] << "HOSTNAME=`cat #{language}/#{framework}/ip-#{variant}.txt` ENGINE=#{variant} LANGUAGE=#{language} FRAMEWORK=#{framework} DATABASE_URL=#{ENV.fetch(
"DATABASE_URL", nil
)} bundle exec rake collect"
config.dig("providers", provider, "clean").each do |cmd|
commands[:clean] << Mustache.render(cmd, options).to_s
end
commands
end
def create_dockerfile(directory, engine, config)
path = File.join(Dir.pwd, directory, "..", "#{engine}.Dockerfile")
path = File.readlink(path) if File.symlink?(path)
path = File.join(Dir.pwd, directory, "..", "Dockerfile") unless File.exist?(path)
# Path to remove stability suffix (stable, beta, alpha, or version) of php extensions
files = []
Dir.glob(config["files"]).each do |file|
variant_file = file.gsub(directory, File.join(directory, ".#{engine}"))
target = if file.include?(".#{engine}")
file.gsub(".#{engine}/", "").gsub("#{directory}/", "")
else
file.gsub("#{directory}/", "")
end
source = if File.exist?(variant_file)
variant_file
else
file
end
files << { source: source.gsub("#{directory}/", ""), target: target }
end
static_files = []
if config["static_files"]
Dir.glob(config["static_files"]).each do |static_file|
static_files << { source: static_file.gsub("#{directory}/", ""), target: static_file.gsub("#{directory}/", "") }
end
end
compiler = config.dig('language','compiler')
if compiler
config['language']['compiler'] = {compiler => true}
end
template = File.read(path)
config.merge!(template_variables).merge!({if: template_conditions}).merge!(files:, static_files:, environment: config["environment"]&.map do |k, v|
"#{k}=#{v}"
end)
File.write(File.join(directory, ".Dockerfile.#{engine}"), Mustache.render(template, config))
end
# This method returns a hash with variables usable in dockerfiles
def template_variables
{ arch:, architecture: }
end
def template_conditions
template_variables.flat_map{|k,v| {k.to_s => {v => true}}}.reduce(:merge)
end
desc "Create Dockerfiles"
task :config do
Dir.glob("*/*/config.yaml").each do |path|
directory = File.dirname(path)
config = get_config_from(directory, engines_as_list: false)
language_config = config["language"]
framework_config = config["framework"]
config.dig("framework", "engines")&.each do |engine|
engine.each do |name, data|
variables = custom_config(language_config, framework_config, data)
variables["files"].each { |f| f.prepend(directory, File::SEPARATOR) unless f.start_with?(directory) }.uniq!
variables["static_files"]&.each do |f|
f.prepend(directory, File::SEPARATOR) unless f.start_with?(directory)
end&.uniq!
create_dockerfile(directory, name, config.merge(variables))
end
end
language, framework = directory.split(File::SEPARATOR)
makefile = File.open(File.join(language, framework, MANIFESTS[:build]), "w")
engine = config.dig("framework", "engines").first.first.first
commands_for(language, framework, engine).each do |target, commands|
makefile.write("#{target}:\n")
commands.each do |command|
makefile.write("\t #{command}\n")
end
end
names = config.dig("framework", "engines")&.flat_map(&:keys)
command = names&.flat_map { |n| ["build.#{n}", "collect.#{n}", "clean.#{n}"] }&.join(" ")
makefile.write("run-all : #{command}\n")
makefile.close
end
end
desc "Clean unused file"
task :clean do
Dir.glob("d/serverino/.gitignore").each do |ignore_file|
directory = File.dirname(ignore_file)
File.foreach(ignore_file) do |line|
line.strip!
next if line.start_with?("!")
next if line.start_with?("#")
next if line.start_with?(".env")
next if line.empty?
Dir.glob(File.join(directory, line)).each do |path|
if File.exist?(path)
if File.file?(path)
warn "Delting file #{path}"
File.delete(path)
elsif File.directory?(path)
warn "Deleting directory #{path}"
FileUtils.rm_rf(path)
end
end
end
end
end
end
Dir.glob(".tasks/*.rake").each { |r| load r }