forked from kaorimatz/packer-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
106 lines (94 loc) · 2.81 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
require 'json'
require 'net/http'
require 'pathname'
require 'rainbow'
require 'rspec/core/rake_task'
require 'uri'
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
fail "#{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']
json['builders'].each do |builder|
iso_url = builder['iso_url'].sub('{{user `mirror`}}', mirror)
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: #{response.message}").red
fail "#{iso_url} is not available"
end
end
end
end
end
desc 'Push the packer template to Atlas'
task :push, [:template, :slug, :version] do |t, args|
template = Pathname.new(args[:template])
slug = args[:slug]
version = args[:version]
json = JSON.parse(template.read)
post_processors = json['post-processors']
post_processors << atlas_post_processor_config(slug, version)
json['post-processors'] = [post_processors]
json['push'] = push_config(slug)
file = Tempfile.open('packer-templates') do |f|
f.tap do |f|
JSON.dump(json, f)
end
end
unless system("packer push -var-file=vars/release.json '#{file.path}'")
puts Rainbow("Failed to push #{template} to Atlas").red
fail "Failed to push #{template} to Atlas"
end
end
end
namespace :spec do
Pathname.glob('*.json').sort.each do |template|
name = template.basename('.json').to_s
host = name.gsub(/[.]/, '_')
desc "Run serverspec to #{host}"
RSpec::Core::RakeTask.new(host) do |task|
ENV['HOST'] = host
task.pattern = 'spec/*_spec.rb'
end
end
end
def request_head(uri, &block)
uri = URI(uri)
Net::HTTP.start(uri.host, uri.port) do |http|
http.request_head(uri, &block)
end
end
def available?(response)
response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPRedirection)
end
def atlas_post_processor_config(slug, version)
{
'type' => 'atlas',
'artifact' => slug,
'artifact_type' => 'vagrant.box',
'metadata' => {
'version' => version,
'provider' => 'virtualbox',
},
}
end
def push_config(slug)
{
'name' => slug,
'base_dir' => File.dirname(__FILE__),
'vcs' => true,
}
end