forked from the-benchmarker/web-frameworks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
198 lines (165 loc) · 6.28 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
# frozen_string_literal: true
require 'dotenv'
require 'active_support'
Dir.glob('.tasks/*.rake').each { |r| load r }
MANIFESTS = {
container: '.Dockerfile',
build: '.Makefile'
}.freeze
Dotenv.load
class ::Hash
def recursive_merge(h)
merge!(h) { |_key, _old, _new| _old.instance_of?(Hash) ? _old.recursive_merge(_new) : _new }
end
end
def default_provider
if RbConfig::CONFIG['host_os'] =~ /linux/
'docker'
else
'docker-machine'
end
end
def commands_for(language, framework, provider)
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 }
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]} -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.txt`:/opt/web/#{File.dirname(out)} ."
else
commands[:build] << "docker cp `cat cid.txt`:/opt/web/#{out} #{out}"
end
end
end
config['providers'][provider]['build'].each do |cmd|
commands[:build] << Mustache.render(cmd, options.merge!(manifest: MANIFESTS[:container])).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[:build] << 'curl --retry 5 --retry-delay 5 --retry-max-time 180 --retry-connrefused http://`cat ip.txt`:3000 -v'
commands[:collect] << "LANGUAGE=#{language} FRAMEWORK=#{framework} DATABASE_URL=#{ENV['DATABASE_URL']} 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(language, framework, **options)
directory = File.join(Dir.pwd, language, framework)
main_config = YAML.safe_load(File.open(File.join(Dir.pwd, 'config.yaml')))
language_config = YAML.safe_load(File.open(File.join(Dir.pwd, language, '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.key?('sources')
files = []
config['sources'].each do |path|
Dir.glob(File.join(directory, path)).each do |f|
if f =~ /^*\.\./
filename = f.gsub(directory, '').gsub!(%r{/\.\./\.}, '')
File.open(File.join(directory, filename), 'w') { |stream| stream.write(File.read(f)) }
files << filename
else
files << f.gsub!(directory, '').gsub!(%r{^/}, '')
end
end
end
config['sources'] = files
end
if config.key?('files')
files = []
config['files'].each do |path|
Dir.glob(File.join(directory, path)).each do |f|
if f =~ /^*\.\./
filename = f.gsub(directory, '').gsub!(%r{/\.\./\.}, '')
File.open(File.join(directory, filename), 'w') { |stream| stream.write(File.read(f)) }
files << filename
else
files << f.gsub!(directory, '').gsub!(%r{^/}, '')
end
end
end
config['files'] = files
end
template = nil
if options[:provider].start_with?('docker') || options[:provider].start_with?('podman')
template = File.join(directory, '..', 'Dockerfile')
elsif config.key?('binaries')
template = File.join(directory, '..', '.build', options[:provider], 'Dockerfile')
end
if config.key?('environment')
environment = []
config.fetch('environment').each do |key, value|
environment << "#{key} #{value}"
end
config['environment'] = environment
end
if template
File.open(File.join(directory, MANIFESTS[:container]), 'w') do |f|
f.write(Mustache.render(File.read(template), config))
end
end
end
task :config do
provider = ENV.fetch('PROVIDER') { default_provider }
collect = ENV.fetch('COLLECT', 'on')
sieger_options = ENV.fetch('SIEGER_OPTIONS', '-r GET:/ -c 10')
clean = ENV.fetch('CLEAN', 'on')
Dir.glob('*/*/config.yaml').each do |path|
directory = File.dirname(path)
language, framework = directory.split(File::Separator)
create_dockerfile(language, framework, provider: provider)
makefile = File.open(File.join(language, framework, MANIFESTS[:build]), 'w')
commands_for(language, framework, provider).each do |target, commands|
makefile.write("#{target}:\n")
commands.each do |command|
makefile.write("\t #{command}\n")
end
end
makefile.close
end
end
task :clean do
Dir.glob('**/.gitignore').each do |ignore_file|
directory = File.dirname(ignore_file)
next if directory.start_with?('lib')
next if directory.start_with?('bin')
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 |file|
if File.exist?(file)
if File.file?(file)
warn "Delting file #{file}"
File.delete(file)
elsif File.directory?(file)
warn "Deleting directory #{file}"
FileUtils.rm_rf(file)
end
end
end
end
end
end