-
Notifications
You must be signed in to change notification settings - Fork 243
/
Rakefile
113 lines (99 loc) · 3.02 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
require 'json'
require 'net/http'
require 'pathname'
require 'rainbow'
require 'rspec/core/rake_task'
require 'uri'
VAGRANT_PROVIDERS = {
virtualbox: {
builder_type: 'virtualbox-iso'
},
vmware_desktop: {
builder_type: 'vmware-iso'
}
}.freeze
task default: ['packer:validate', 'packer:check_iso_url']
namespace :packer do
desc 'Validate all the packer templates'
task :validate do
Pathname.glob('*.json').sort.each do |template|
puts Rainbow("Validating #{template}...").green
unless system "packer validate #{template}"
puts Rainbow("#{template} is not a valid packer template").red
raise "#{template} is not a valid packer template"
end
end
end
desc 'Check if all the ISO URLs are available'
task :check_iso_url do
Pathname.glob('*.json').sort.each do |template|
json = JSON.parse(template.read)
mirror = json['variables']['mirror']
iso_urls = json['builders'].map do |builder|
builder['iso_url'].sub('{{user `mirror`}}', mirror)
end
iso_urls.uniq.each do |iso_url|
puts Rainbow("Checking if #{iso_url} is available...").green
request_head(iso_url) do |response|
unless available?(response)
puts Rainbow("#{iso_url} is not available: uri=#{response.uri}, message=#{response.message}").red
raise "#{iso_url} is not available"
end
end
end
end
end
desc 'Build and upload the vagrant box to Atlas'
task :release, [:template, :slug, :version, :provider] do |_t, args|
template = Pathname.new(args[:template])
slug = args[:slug]
version = args[:version]
provider = args[:provider]
json = JSON.parse(template.read)
builders = json['builders']
builders.select! do |builder|
builder['type'] == VAGRANT_PROVIDERS[provider.to_sym][:builder_type]
end
post_processors = json['post-processors']
post_processors << atlas_post_processor_config(slug, version, provider)
json['post-processors'] = [post_processors]
file = Tempfile.open('packer-templates') do |f|
f.tap do |f|
JSON.dump(json, f)
end
end
unless system("packer build -var-file=vars/release.json '#{file.path}'")
puts Rainbow("Failed to release #{slug} to Atlas").red
raise "Failed to release #{slug} to Atlas"
end
end
end
desc 'Run serverspec tests'
RSpec::Core::RakeTask.new(:spec, :host) do |_t, args|
ENV['HOST'] = args[:host]
end
def request_head(uri, &block)
uri = URI(uri)
response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
http.request_head(uri)
end
if response.is_a?(Net::HTTPRedirection)
request_head(response['Location'], &block)
else
yield response
end
end
def available?(response)
response.is_a?(Net::HTTPSuccess)
end
def atlas_post_processor_config(slug, version, provider)
{
'type' => 'atlas',
'artifact' => slug,
'artifact_type' => 'vagrant.box',
'metadata' => {
'version' => version,
'provider' => provider
}
}
end