forked from puppetlabs/facter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
135 lines (117 loc) · 3.78 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
# Rakefile for facter
# We need access to the Puppet.version method
$LOAD_PATH.unshift(File.expand_path("lib"))
require 'facter/version'
require 'yaml'
$LOAD_PATH << File.join(File.dirname(__FILE__), 'tasks')
require 'rake'
begin
load File.join(File.dirname(__FILE__), 'ext', 'packaging', 'packaging.rake')
rescue LoadError
end
['rubygems',
'rspec',
'rspec/core/rake_task',
'rcov',].each do |lib|
begin
require lib
rescue LoadError
end
end
Dir['tasks/**/*.rake'].each { |t| load t }
build_defs_file = 'ext/build_defaults.yaml'
if File.exist?(build_defs_file)
begin
@build_defaults ||= YAML.load_file(build_defs_file)
rescue Exception => e
STDERR.puts "Unable to load yaml from #{build_defs_file}:"
STDERR.puts e
end
@packaging_url = @build_defaults['packaging_url']
@packaging_repo = @build_defaults['packaging_repo']
raise "Could not find packaging url in #{build_defs_file}" if @packaging_url.nil?
raise "Could not find packaging repo in #{build_defs_file}" if @packaging_repo.nil?
namespace :package do
desc "Bootstrap packaging automation, e.g. clone into packaging repo"
task :bootstrap do
if File.exist?("ext/#{@packaging_repo}")
puts "It looks like you already have ext/#{@packaging_repo}. If you don't like it, blow it away with package:implode."
else
cd 'ext' do
%x{git clone #{@packaging_url}}
end
end
end
desc "Remove all cloned packaging automation"
task :implode do
rm_rf "ext/#{@packaging_repo}"
end
end
end
task :default do
sh %{rake -T}
end
if defined?(RSpec::Core::RakeTask)
desc "Run all specs"
RSpec::Core::RakeTask.new do |t|
t.pattern ='spec/{unit,integration}/**/*_spec.rb'
t.fail_on_error = true
end
RSpec::Core::RakeTask.new('spec:rcov') do |t|
t.pattern ='spec/{unit,integration}/**/*_spec.rb'
t.fail_on_error = true
if defined?(Rcov)
t.rcov = true
t.rcov_opts = ['--exclude', 'spec/*,test/*,results/*,/usr/lib/*,/usr/local/lib/*,gems/*']
end
end
end
namespace :collect do
desc "Scrape EC2 Metadata into fixtures"
task :ec2_metadata do
collect_metadata
end
##
# collect_metadata walks the Amazon AWS EC2 Metadata API and records each
# request and response instance as a serialized YAML string. This method is
# intended to be used by Rake tasks Puppet users invoke to collect data for
# development and troubleshooting purposes.
def collect_metadata(key='/', date=Time.now.strftime("%F"), dir="spec/fixtures/unit/util/ec2")
require 'timeout'
require 'net/http'
require 'uri'
# Local variables
file_prefix = "ec2_meta_data#{key.gsub(/[^a-zA-Z0-9]+/, '_')}".gsub(/_+$/, '')
response = nil
Dir.chdir(dir) do
uri = URI("http://169.254.169.254/latest/meta-data#{key}")
Timeout::timeout(4) do
Net::HTTP.start(uri.host, uri.port) do |http|
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
write_fixture(request, "#{file_prefix}_request.yaml")
write_fixture(response, "#{file_prefix}_response.yaml")
end
end
end
##
# if the current key is a directory, decend into all of the files. If the
# current key is not, we've already written it out and we're done.
if key.end_with? "/"
response.read_body.lines.each do |line|
collect_metadata("#{key}#{line.chomp}", date, dir)
end
end
end
##
# write_fixture dumps an internal Ruby object to a file intended to be used
# as a fixture for spec testing.
#
# @return [String] Serialized string model representation of obj
def write_fixture(obj, filename, quiet=false)
File.open(filename, "w+") do |fd|
fd.write(YAML.dump(request))
end
puts "Wrote: #{dir}/#{request_file}" unless quiet
end
end