This repository has been archived by the owner on Dec 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathRakefile
74 lines (65 loc) · 1.96 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
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 = json['variables']['iso_name']
iso_urls = json['builders'].map do |builder|
builder['iso_urls'].at(1).sub('{{ user `mirror` }}', mirror).sub('{{ user `iso_name` }}', iso)
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
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