forked from tmclaugh/suitcase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
356 lines (284 loc) · 11.2 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# Rakefile around our Packer build
load "./site.rb"
MASTER_DIR = 'images/master'
AWS_HVM_DIR = 'images/aws-hvm'
AWS_PV_DIR = 'images/aws-paravirt'
VG_DIR = 'images/vagrant'
AWS_HVM_TMPL = 'image-templates/aws-hvm.json'
AWS_PV_TMPL = 'image-templates/aws-paravirt.json'
MASTER_TMPL = 'image-templates/virtualbox.json'
VAGRANT_TMPL = 'image-templates/vagrant.json'
S3_MASTER = "#{S3_BUCKET}/master"
S3_VAGRANT = "#{S3_BUCKET}/vagrant"
S3_AWS_HVM = "#{S3_BUCKET}/aws-hvm"
S3_AWS_PV = "#{S3_BUCKET}/aws-pv"
if RUBY_PLATFORM =~ /.*darwin.*/
packer_cmd = '/usr/local/bin/packer'
else
packer_cmd = '~/bin/packer'
end
$command = { 'packer' => packer_cmd }
namespace :packer do
if ENV['os']
os=ENV['os']
os_name=os.split('.')[0..-2].join('.')
end
if ENV['timestamp']
timestamp = ENV['timestamp']
else
timestamp = Time.new.strftime("%Y%m%d%H%M%S")
end
master_image = "#{MASTER_DIR}/#{os_name}-#{timestamp}/#{os_name}-#{timestamp}-disk1.vmdk"
vagrant_image = "#{VG_DIR}/#{os_name}-#{timestamp}.box"
aws_hvm_image = "#{AWS_HVM_DIR}/#{os_name}-#{timestamp}/#{os_name}-#{timestamp}-aws-hvm-disk1.vmdk"
aws_pv_image = "#{AWS_PV_DIR}/#{os_name}-#{timestamp}/#{os_name}-#{timestamp}-aws-paravirt-disk1.vmdk"
master_upload_done = "#{master_image}.upload_done"
vagrant_upload_done = "#{vagrant_image}.upload_done"
aws_hvm_upload_done = "#{aws_hvm_image}.upload_done"
aws_pv_upload_done = "#{aws_pv_image}.upload_done"
def build_image(os, timestamp, template)
if os == nil
abort("ERROR: no os defined!")
end
sh %{ #{$command['packer']} build -var-file=#{os} -var timestamp=#{timestamp} #{template} }
end
# Basic tasks
desc "Build images"
task :build, [:image] do |t, args|
args.with_defaults(:image => :all)
image = args[:image]
Rake::Task["packer:build_#{image}"].invoke
end
desc "Build all images"
task :build_all => [:build_vagrant, :build_awshvm, :build_awspv]
desc "Clean artifacts"
task :clean, [:image] do |t, args|
if os == nil
abort("ERROR: no os defined!")
end
args.with_defaults(:image => :all)
image = args[:image]
Rake::Task["packer:clean_#{image}"].invoke
end
task :clean_all => ['clean_vagrant', 'clean_awshvm', 'clean_awspv', 'clean_master']
desc "Upload artifacts"
task :upload, [:image] do |t, args|
args.with_defaults(:image => :all)
image = args[:image]
Rake::Task["packer:upload_#{image}"].invoke
end
task :upload_all => ['upload_master', 'upload_vagrant', 'upload_awshvm', 'upload_awspv']
desc "Fake upload artifacts"
task :fake_upload, [:image] do |t, args|
args.with_defaults(:image => :all)
image = args[:image]
Rake::Task["packer:fake_upload_#{image}"].invoke
end
task :fake_upload_all => ['fake_upload_master', 'fake_upload_vagrant', 'fake_upload_awshvm', 'fake_upload_awspv']
desc "Fetch images"
task :fetch, [:image] do |t, args|
args.with_defaults(:image => :all)
image = args[:image]
Rake::Task["packer:fetch_#{image}"].invoke
end
task :fetch_all => ['fetch_master', 'fetch_vagrant', 'fetch_awshvm', 'fetch_awspv']
desc "Register images"
task :register, [:image] do |t, args|
args.with_defaults(:image => :all)
image = args[:image]
Rake::Task["packer:register_#{image}"].invoke
end
task :register_all => ['register_master', 'register_vagrant', 'register_awshvm', 'register_awspv']
desc "Deregister images"
task :deregister, [:image] do |t, args|
args.with_defaults(:image => :all)
image = args[:image]
Rake::Task["packer:deregister_#{image}"].invoke
end
task :deregister_all => ['deregister_master', 'deregister_vagrant', 'deregister_awshvm', 'deregister_awspv']
desc "Delete images from S3"
task :delete, [:image] do |t, args|
args.with_defaults(:image => :all)
image = args[:image]
Rake::Task["packer:delete_#{image}"].invoke
end
task :delete_all => ['delete_awshvm', 'delete_awspv', 'delete_master', 'delete_vagrant']
# Master image related tasks.
desc "Build master image"
task :build_master => "#{master_image}"
file "#{master_image}" do
build_image(os, timestamp, MASTER_TMPL)
end
desc "Upload master image"
task :upload_master => :build_master do
unless FileUtils.uptodate?("#{master_upload_done}", ["#{master_image}"])
sh %{aws s3 cp --recursive #{File.dirname("#{master_image}")} #{S3_MASTER}/#{File.basename(File.dirname("#{master_image}"))}}
FileUtils.touch("#{master_upload_done}")
end
end
desc "Pretend we uploaded a master image"
task :fake_upload_master => :build_master do
FileUtils.touch("#{master_upload_done}")
end
desc "Register master image (NOOP procedure)"
task :register_master => :upload_master
desc "Deregister master image (NOOP procedure)"
task :deregister_master
desc "Fetch master image"
task :fetch_master do
unless File.exist?("#{master_image}")
sh %{aws s3 cp --recursive #{S3_MASTER}/#{File.basename(File.dirname("#{master_image}"))} #{File.dirname("#{master_image}")}}
end
end
desc "Cleanup master image"
task :clean_master do
if File.exist?("#{master_image}")
File.delete("#{master_image}")
FileUtils.rm_rf(File.dirname("#{master_image}"))
end
end
desc "Delete master image from S3"
task :delete_master do
sh %{aws s3 rm --recursive #{S3_MASTER}/#{File.basename(File.dirname("#{master_image}"))}}
end
# Vagrant related tasks
desc "Build Vagrant"
task :build_vagrant => "#{vagrant_image}"
file "#{vagrant_image}" => :build_master do
build_image(os, timestamp, VAGRANT_TMPL)
end
desc "Upload Vagrant image"
task :upload_vagrant => :build_vagrant do
unless FileUtils.uptodate?("#{vagrant_upload_done}", ["#{vagrant_image}"])
sh %{aws s3 cp #{vagrant_image} #{S3_VAGRANT}/}
FileUtils.touch("#{vagrant_upload_done}")
end
end
desc "Pretend we uploaded a Vagrant image"
task :fake_upload_vagrant => :build_vagrant do
FileUtils.touch("#{vagrant_upload_done}")
end
desc "Register Vagrant image (NOOP procedure)"
task :register_vagrant => :upload_vagrant
desc "Deregister vagrant image (NOOP procedure)"
task :deregister_vagrant
desc "Fetch vagrant image"
task :fetch_vagrant do
unless File.exist?("#{vagrant_image}")
sh %{aws s3 cp #{S3_VAGRANT}/#{File.basename("#{vagrant_image}")} #{File.dirname("#{vagrant_image}")}}
end
end
desc "Cleanup Vagrant"
task :clean_vagrant do
if File.exists?("#{vagrant_image}")
File.delete("#{vagrant_image}")
File.delete("#{vagrant_upload_done}")
end
end
desc "Delete Vagrant image from S3"
task :delete_vagrant do
sh %{aws s3 rm #{S3_VAGRANT}/#{File.basename("#{vagrant_image}")}}
end
# AWS HVM related tasks
desc "Build AWS HVM"
task :build_awshvm => "#{aws_hvm_image}"
file "#{aws_hvm_image}" => :build_master do
build_image(os, timestamp, AWS_HVM_TMPL)
end
desc "Upload AWS HVM image"
task :upload_awshvm => :build_awshvm do
unless FileUtils.uptodate?("#{aws_hvm_upload_done}", ["#{aws_hvm_image}"])
sh %{aws s3 cp --recursive #{File.dirname("#{aws_hvm_image}")} #{S3_AWS_HVM}/#{File.basename(File.dirname("#{aws_hvm_image}"))}}
FileUtils.touch("#{aws_hvm_upload_done}")
end
end
desc "Pretend we uploaded an AWS HVM image"
task :fake_upload_awshvm => :build_awshvm do
FileUtils.touch("#{aws_hvm_upload_done}")
end
desc "Register AWS HVM with SR-IOV support"
task :register_awshvm => :upload_awshvm do
sh %{sh files/registerami.sh -i #{aws_hvm_image} -t hvm -a #{PROD_ACCOUNTNUM} -b #{S3_AWS_HVM.gsub('s3://', '')}/#{File.basename(File.dirname("#{aws_hvm_image}"))}/ami}
sh %{aws ec2 describe-images --filter Name=name,Values=#{os_name}-#{timestamp}-aws-hvm --output table --query 'Images[*][ImageId, Name]' }
end
desc "Deregister AWS HVM image"
task :deregister_awshvm do
ami_id = %x{aws ec2 describe-images --filter Name=name,Values=#{os_name}-#{timestamp}-aws-hvm --output text --query 'Images[*][ImageId]'}
if ami_id != ''
sh %{aws ec2 deregister-image --image-id #{ami_id}}
end
end
desc "Fetch AWS HVM image"
task :fetch_awshvm do
unless File.exist?("#{aws_hvm_image}")
sh %{aws s3 cp --recursive --exclude ami/* #{S3_AWS_HVM}/#{File.basename(File.dirname("#{aws_hvm_image}"))} #{File.dirname("#{aws_hvm_image}")}}
end
end
desc "Cleanup AWS HVM image"
task :clean_awshvm do
if File.exists?("#{aws_hvm_image}")
sh %{VBoxManage closemedium disk #{aws_hvm_image} --delete}
FileUtils.rm_rf(File.dirname("#{aws_hvm_image}"))
end
end
desc "Delete AWS HVM image from S3"
task :delete_awshvm do
ami_id = %x{aws ec2 describe-images --filter Name=name,Values=#{os_name}-#{timestamp}-aws-hvm --output text --query 'Images[*][ImageId]' }
if ami_id == ''
sh %{aws s3 rm --recursive #{S3_AWS_HVM}/#{File.basename(File.dirname("#{aws_hvm_image}"))}}
else
abort "ERROR: Must degregister associated AMI first. #{ami_id}"
end
end
# AWS PV related tasks
desc "Build AWS paravirt"
task :build_awspv => "#{aws_pv_image}"
file "#{aws_pv_image}" => :build_master do
build_image(os, timestamp, AWS_PV_TMPL)
end
desc "Upload AWS paravirt image"
task :upload_awspv => :build_awspv do
unless FileUtils.uptodate?("#{aws_pv_upload_done}", ["#{aws_pv_image}"])
sh %{aws s3 cp --recursive #{File.dirname("#{aws_pv_image}")} #{S3_AWS_PV}/#{File.basename(File.dirname("#{aws_pv_image}"))}}
FileUtils.touch("#{aws_pv_upload_done}")
end
end
desc "Pretend we uploaded an AWS paravirt image"
task :fake_upload_awspv => :build_awspv do
FileUtils.touch("#{aws_pv_upload_done}")
end
desc "Register AWS paravirt image"
task :register_awspv => :upload_awspv do
sh %{sh files/registerami.sh -i #{aws_pv_image} -t paravirt -a #{PROD_ACCOUNTNUM} -b #{S3_AWS_PV.gsub('s3://', '')}/#{File.basename(File.dirname("#{aws_pv_image}"))}/ami}
sh %{aws ec2 describe-images --filter Name=name,Values=#{os_name}-#{timestamp}-aws-paravirt --output table --query 'Images[*][ImageId, Name]' }
end
desc "Deregister AWS paravirt image"
task :deregister_awspv do
ami_id = %x{aws ec2 describe-images --filter Name=name,Values=#{os_name}-#{timestamp}-aws-paravirt --output text --query 'Images[*][ImageId]' }
if ami_id != ''
sh %{aws ec2 deregister-image --image-id #{ami_id}}
end
end
desc "Fetch AWS paravirt image"
task :fetch_awspv do
unless File.exist?("#{aws_pv_image}")
sh %{aws s3 cp --recursive --exclude ami/* #{S3_AWS_PV}/#{File.basename(File.dirname("#{aws_pv_image}"))} #{File.dirname("#{aws_pv_image}")}}
end
end
desc "Cleanup AWS paravirt image"
task :clean_awspv do
if File.exists?("#{aws_pv_image}")
sh %{VBoxManage closemedium disk #{aws_pv_image} --delete}
FileUtils.rm_rf(File.dirname("#{aws_pv_image}"))
end
end
desc "Delete AWS paravirt image from S3"
task :delete_awspv do
ami_id = %x{aws ec2 describe-images --filter Name=name,Values=#{os_name}-#{timestamp}-aws-paravirt --output text --query 'Images[*][ImageId]' }
if ami_id == ''
sh %{aws s3 rm --recursive #{S3_AWS_PV}/#{File.basename(File.dirname("#{aws_pv_image}"))}}
else
abort "ERROR: Must degregister associated AMI first. #{ami_id}"
end
end
end