-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
294 lines (237 loc) · 6.77 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
285
286
287
288
289
290
291
292
293
294
# frozen_string_literal: true
require 'erb'
require 'yaml'
build_pkg_root = File.dirname(File.realpath(__FILE__))
docker_img_prefix = 'local/build-pkg'
require "#{build_pkg_root}/config"
# Openstruct/ERB wrapper
class Template
def initialize(hash)
hash.each do |key, value|
instance_variable_set('@' + key.to_s, value)
create_attr key
end
end
def create_method(name, &block)
self.class.send(:define_method, name, &block)
end
def create_attr(name)
create_method( name.to_sym ) do
instance_variable_get("@" + name.to_s)
end
end
def render(template)
ERB.new(template).result(binding)
end
end
def recipe_dir(root)
return if File.exist? 'recipe.rb'
if ENV['recipe'].nil?
warn 'ERR: unable to detect recipe and no "recipe" env var declared'
Kernel.exit 1
end
Dir.chdir "#{root}/recipes/#{ENV['recipe']}"
end
def check_publisher
if ENV['target'].nil?
warn 'ERR: unspecified target e.g deb or rpm'
Kernel.exit 1
end
return unless ENV['repo'].nil?
warn 'ERR: unspecified repo where to publish packages'
Kernel.exit 1
end
def clean_dummies
Dir['pkg/*dummy*'].each do |pkg|
rm_f pkg
end
end
desc 'Run rubocop'
task :cop do
sh 'rubocop'
end
# rubocop:disable Metrics/BlockLength
namespace :build do
desc 'Dump config as yaml into recipe dir'
task :config do
recipe_dir build_pkg_root
File.write 'config.yml', build_pkg_config.to_yaml
end
desc 'Invoke recipe build script'
task all: %w[build:config test:copy] do
recipe_dir build_pkg_root
sh './build'
end
desc 'Build package'
task :default do
recipe_dir build_pkg_root
target = ''
target = "--target #{ENV['target']}" unless ENV['target'].nil?
sh "fpm-cook package --no-deps #{target}"
end
desc 'Build Dockerfile from template'
task :template do
if ENV['image'].nil?
warn 'ERR: a Docker image must be specified'
Kernel.exit 1
end
dockerfile = "#{build_pkg_root}/docker/files/#{ENV['image']}"
template_file = "#{build_pkg_root}/docker/templates/#{ENV['image']}.erb"
if File.exist? dockerfile
next
end
template_contents = File.read(template_file)
dockerfile_contents = Template.new(build_pkg_config).render(template_contents)
File.write(dockerfile, dockerfile_contents)
end
desc 'Build the package in Docker'
task :docker do
recipe_dir build_pkg_root
Rake::Task['build:template'].invoke
dockerfile = "#{build_pkg_root}/docker/files/#{ENV['image']}"
sh 'bundle exec fpm-cook package --no-deps --docker '\
"--dockerfile #{dockerfile} "\
"--docker-image #{docker_img_prefix}/#{ENV['image']}"
end
desc 'Build the package in Vagrant'
task :vagrant do
recipe_dir build_pkg_root
sh 'vagrant up'
build = 'cd /recipe && /opt/cinc/embedded/bin/fpm-cook package --no-deps'
sh "vagrant ssh -c '#{build}'"
end
end
# rubocop:enable Metrics/BlockLength
task build: %w[build:default]
# rubocop:disable Metrics/BlockLength
namespace :clean do
desc 'Clean build files'
task :default do
recipe_dir build_pkg_root
sh 'sudo rm -rf tmp-build'
rm_rf 'tmp-dest'
rm_f 'build.yml'
end
desc 'Clean build artefacts'
task :pkg do
recipe_dir build_pkg_root
rm_rf 'pkg'
end
desc 'Clean Docker images'
task :docker do
cmd = "docker images -qf 'reference=#{docker_img_prefix}/*'"
`#{cmd}`.split("\n").each do |img|
sh "docker rmi #{img}"
end
end
desc 'Clean Vagrant box'
task :vagrant do
sh 'vagrant destroy -f'
rm_rf '.vagrant'
end
desc 'Clean Dockerfiles generated from templates'
task :dockerfiles do
rm_rf "#{build_pkg_root}/docker/files"
mkdir_p "#{build_pkg_root}/docker/files"
touch "#{build_pkg_root}/docker/files/.gitkeep"
end
desc 'Remove all build files'
task all: %w[clean:default clean:pkg clean:dockerfiles] do
recipe_dir build_pkg_root
rm_rf 'cache' unless File.exist? 'cache/dummy.tar.gz'
rm_rf 'go'
rm_rf 'git'
end
end
# rubocop:enable Metrics/BlockLength
task clean: %w[clean:default]
namespace :publish do
desc 'Invoke recipe publish script'
task :all do
recipe_dir build_pkg_root
sh './publish'
end
desc 'Setup publish targets'
task :setup do
recipe_dir build_pkg_root
check_publisher
clean_dummies
end
desc 'Add packages to local repository via repo-mgr'
task default: %w[publish:setup] do
Dir["pkg/*.#{ENV['target']}"].each do |pkg|
puts "===> Publish #{File.basename pkg}"
sh "repo-mgr add-pkg --repo #{ENV['repo']} --path #{pkg}"
end
end
desc 'Sync published packages using repo-mgr publisher'
task sync: %w[publish:setup publish:default] do
sh "repo-mgr sync --repo #{ENV['repo']}"
end
desc 'Remove published packages from local repository via repo-mgr'
task undo: %w[publish:setup] do
Dir["pkg/*.#{ENV['target']}"].each do |pkg|
puts "===> Unpublish #{File.basename pkg}"
sh "repo-mgr remove-pkg --repo #{ENV['repo']} --path #{pkg}"
end
end
end
# rubocop:disable Metrics/BlockLength
namespace :test do
desc 'Copy upstream test to recipe dir'
task :copy do
recipe_dir build_pkg_root
mkdir_p 'spec'
%w[spec_helper.rb size_spec.rb].each do |file|
cp "#{build_pkg_root}/spec/#{file}", "spec/#{file}"
end
end
desc 'Invoke test script from recipe dir'
task all: %w[test:copy] do
recipe_dir build_pkg_root
sh './test'
end
task :init do
if ENV['image'].nil?
warn 'ERR: a Docker image must be specified'
Kernel.exit 1
end
@container = "build-#{File.basename(Dir.pwd)}-#{ENV['image'].tr(':', '-')}"
@image = "#{docker_img_prefix}/#{ENV['image']}"
end
desc 'Setup test environment'
task setup: %w[test:init] do
recipe_dir build_pkg_root
system "docker rm -f #{@container}"
sh "docker run --volume #{Dir.pwd}:/build --name #{@container} "\
"--detach #{@image} tail -f /dev/null"
end
desc 'Install package in container'
task install: %w[test:init] do
recipe_dir build_pkg_root
sh "docker exec #{@container} /build/install"
end
desc 'Run test suite for recipe'
task run: %w[test:init] do
recipe_dir build_pkg_root
unless Dir.exist? 'spec'
warn 'The spec dir is missing - unable to run tests'
Kernel.exit 1
end
sh "docker exec --env IN_DOCKER=1 #{@container} /build/test"
end
desc 'Tear down test environment'
task clean: %w[test:init] do
recipe_dir build_pkg_root
sh "docker rm -f #{@container}"
end
desc 'Run recipe specific test suite'
task default: %w[test:setup test:install test:run test:clean]
end
# rubocop:enable Metrics/BlockLength
task test: %w[test:default]
task publish: %w[publish:default]
task lint: %i[cop]
task build: %w[build:default]
desc 'Default task - invoke build'
task default: %w[build:all]