-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
119 lines (102 loc) · 2.69 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
require 'rubygems'
require 'bundler/setup'
require 'docker'
require 'erb'
require 'fileutils'
require 'open3'
require 'rubocop/rake_task'
require 'yaml'
RuboCop::RakeTask.new
desc "Check erb syntax"
task :erb do
FileList['*.erb'].each do |template|
puts "---> syntax:#{template}"
Open3.popen3('ruby -c') do |stdin, stdout, stderr|
stdin.puts(ERB.new(File.read(template), nil, '-').src)
stdin.close
error = stderr.readline rescue false
if error
puts error
puts stderr.read
exit 1
end
stdout.close rescue false
stderr.close rescue false
end
end
end
desc "Run dockerfile_lint"
task :lint do
project_root = File.expand_path(File.dirname(__FILE__))
image = Docker::Image.create('fromImage' => 'projectatomic/dockerfile-lint')
FileList['**/Dockerfile'].exclude(/^vendor/).each do |dockerfile|
puts "---> lint:#{dockerfile}"
container = Docker::Container.create({
:Cmd => [ 'dockerfile_lint', '-r', '.dockerfile_lint.yml', '-f', dockerfile ],
:Image => image.id,
:HostConfig => {
:Privileged => true,
},
:Tty => true,
})
container.start({
:Binds => [ "#{project_root}:/root" ],
})
status_code = container.wait['StatusCode']
puts container.logs(stdout: true)
container.remove
if status_code.nonzero?
exit status_code
end
end
end
desc "Generate Dockerfiles"
task :update do
FileList['*.yml'].each do |dockerfile_yml|
dockerfile_erb = File.basename(dockerfile_yml,'.yml') + '.erb'
unless File.exist?(dockerfile_erb)
STDERR.puts "Template '#{dockerfile_erb}' not found. Skipping '#{dockerfile_yml}'."
next
end
puts "Processing #{dockerfile_yml}."
variant = File.basename(dockerfile_yml,'.yml')[11..-1]
config = YAML.load_file(dockerfile_yml)
config['parameters'].each do |parameter|
parameter.each do |k, v|
instance_variable_set "@#{k}", v
end
path = parameter[config['path_from']]
path = File.join(path, variant) if variant
puts "Generating #{path}/Dockerfile"
puts ""
puts " parameters:"
parameter.each do |k, v|
puts " #{k}: #{v}"
end
puts ""
template = File.read(dockerfile_erb)
dockerfile = ERB.new(template, nil, '-').result()
FileUtils.mkdir_p path
File.write("#{path}/Dockerfile", dockerfile)
end
end
end
desc "Check yaml syntax"
task :yaml do
FileList['*.yml'].each do |yaml|
puts "---> syntax:#{yaml}"
begin
YAML.load_file(yaml)
rescue => e
STDERR.puts e.message
exit 1
end
end
end
desc "Run erb and rubocop"
task :test => [
:erb,
:yaml,
:lint,
:rubocop,
]