-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rakefile
196 lines (178 loc) · 6.16 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
# encoding: utf-8
require 'fileutils'
require 'erb'
require 'ostruct'
require 'aws-sdk'
def rake_tasks
documentation = ''
s = `rake -T`.split("\n")
s.each do |l|
documentation << " #{l}\n" if l =~ /^rake/
end
documentation
end
def gets3
%w{ AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_REGION }.each do |key|
fail "Set an environment variable for #{key}" if ENV[key].nil?
end
AWS::S3.new
end
def check_build_vars
%w{ NMDPACKER_OS NMDPACKER_VER NMDPACKER_BITS }.each do |key|
fail "Set an environment variable for #{key} run bundle exec rake -D for
extended detail" if ENV[key].nil?
end
end
desc 'Validate all the packer templates in servers directory.'
task :validate do
templates = Dir.glob('servers/*.json')
templates.each do |template|
if !system "packer validate #{template}"
puts "#{template} is invalid."
else
puts "#{template} is valid."
end
end
end
desc '"clean[iso|box|all]" - downloaded iso files, built virtual boxes, all.'
task :clean, :action do |t, args|
action = args[:action]
puts action ? "Starting action #{t} #{action}" : 'No action specified.
Try "rake -D" to see a list of available actions. '
case action
when 'iso'
FileUtils.rm_rf(Dir.glob('./packer_cache/*'))
when 'box'
puts 'Deleting boxes'
FileUtils.rm_rf(Dir.glob('./builds/virtualbox/*.box'))
FileUtils.rm_rf(Dir.glob('./builds/vmware/*.box'))
when 'all'
FileUtils.rm_rf(Dir.glob('./packer_cache/*'))
FileUtils.rm_rf(Dir.glob('./builds/virtualbox/*.box'))
end
end
desc '"upload[vmware]" Upload boxes to the designated s3 bucket. Defaults to
virtualbox if vmware is not specified. Requires AWS_SECRET_ACCESS_KEY,
AWS_ACCESS_KEY_ID, AWS_REGION, & AWS_BUCKET_NAME. environment variables be set.'
task :upload, :vmware do |t, args|
s3 = gets3
if ENV['AWS_BUCKET_NAME']
bucket_name = ENV['AWS_BUCKET_NAME']
else
bucket_name = 'nmd-virtualbox'
bucket_name = 'nmd-vmware' if args[:vmware]
end
begin
bucket = s3.buckets.create(bucket_name)
rescue Exception => e
bucket = s3.buckets[bucket_name]
end
bucket.acl = :public_read
Dir.chdir '.' do
boxes = Dir.glob('./builds/virtualbox/*.box')
boxes = Dir.glob('./builds/vmware/*.box') if args[:vmware]
puts 'Nothing to upload.' if boxes.empty?
boxes.each do |box|
# @TODO: What if there are no tags
tag = `git describe --abbrev=0 --tags`.gsub("\n","")
tag_hash = `git show-ref --tags -d | grep #{tag}\^\{\} | awk '{print $1}'`.gsub("\n","")
latest_hash = `git rev-parse HEAD`.gsub("\n","")
# If the latest hash doesn't match the tag hash then call it latest.
tag = 'latest' if latest_hash != tag_hash
# Add the tag to the target name.
target_name = box.split('/').last
target_name = target_name.gsub(/(.*).box/, "\\1-#{tag}.box")
puts "Setting tag to #{tag}."
puts "Action #{t}: Uploading #{box} to the #{bucket_name} #{target_name} (this could take some time) ..."
object = bucket.objects.create(target_name, Pathname.new(box))
object.acl = :public_read
puts "Uploaded to #{object.public_url}"
end
end
end
desc '"delete[BUCKET_NAME, OBJECT_NAME]" s3: Delete an object or a bucket (and
its contents). Requires AWS_SECRET_ACCESS_KEY,AWS_ACCESS_KEY_ID, &
AWS_REGION environment variables be set.'
task :delete, :bucket_name, :object_name do |t, args|
s3 = gets3
bucket_name = args[:bucket_name]
object_name = args[:object_name]
bucket = s3.buckets[bucket_name]
if bucket.exists?
if object_name.nil?
bucket.delete!
puts "Removed the #{bucket_name} bucket and it's contents."
else
obj = bucket.objects[object_name]
if obj.exists?
obj.delete
puts "Removed #{object_name} from #{bucket_name}."
else
puts "#{object_name} does not exist in #{bucket_name}."
end
end
else
puts "Could not find a bucket named #{bucket_name}."
end
end
desc 'Build a base vagrant box from chef cookbooks - Requires environment variables be set -
Settings are read from the following shell environment variables.
All required variables can be set to * to build all defined servers.
"NMDPACKER_OS: ex: OS=centos" - Required
"NMDPACKER_VER: VER=5.10" - Required
"NMDPACKER_BITS: ex: BITS=64" - Required
"NMDPACKER_VAR: default: base ex: base,lamp, etc" - optional
"NMDPACKER_ONLY: Typically virtualbox-iso or vmware-iso" - optional
"NMDPACKER_BOX: Adds the new box to your local vagrant" - optional
"NMDPACKER_UPLOAD: Uploads the box to s3." - optional'
task :build do
check_build_vars
nmdpacker_os = ENV['NMDPACKER_OS']
nmdpacker_ver = ENV['NMDPACKER_VER']
nmdpacker_bits = ENV['NMDPACKER_BITS']
nmdpacker_var = ENV['NMDPACKER_VAR']
nmdpacker_only = ENV['NMDPACKER_ONLY']
nmdpacker_box = ENV['NMDPACKER_BOX']
nmdpacker_upload = ENV['NMDPACKER_UPLOAD']
if ENV['NMDPACKER_VAR']
nmdpacker_var = ENV['NMDPACKER_VAR']
else
nmdpacker_var = 'base'
end
Dir.chdir '.' do
FileUtils.rm './Berkshelf.lock', force: true
`bundle exec berks install --path vendor/cookbooks `
FileUtils.rm_rf(Dir.glob('./packer-*'))
if nmdpacker_bits
processor = nmdpacker_bits == '64' ? "{amd64,x86_64}" : "i386"
else
processor = '*'
end
templates = Dir.glob("./servers/#{nmdpacker_os}-#{nmdpacker_ver}-#{processor}-#{nmdpacker_var}.json")
if nmdpacker_only
templates.each do |template|
exec "packer build -only=#{nmdpacker_only} #{template}"
end
else
templates.each do |template|
puts "#{templates}"
exec "packer build #{template}"
end
end
if nmdpacker_box
Dir.glob('./builds/virtualbox/nmd*').each do |template|
box_name = template.match(/(nmd.*).box/).captures[0]
exec "vagrant box remove #{box_name}"
exec "vagrant box add #{box_name} #{template}"
end
Dir.glob('./builds/vmware/nmd*').each do |template|
box_name = template.match(/(nmd.*).box/).captures[0]
exec "vagrant box remove #{box_name}"
exec "vagrant box add #{box_name} #{template}"
end
end
if nmdpacker_upload.nil? || Rake::Task['upload'].invoke
end
end
end
task :default, [:action] => :clean