Last active
February 8, 2018 14:28
-
-
Save muziyoshiz/f3013b2492d615254e6e to your computer and use it in GitHub Desktop.
Serverspec Rakefile for creating tasks from Ansible inventory file with server status
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rake' | |
require 'rspec/core/rake_task' | |
unless ENV['inventory'] | |
print "ERROR: Inventory file must be specified by 'inventory' environment variable\n" | |
print " e.g.) bundle exec rake inventory=./hosts spec:all\n" | |
exit | |
end | |
groups = {} | |
hosts = [] | |
current_group = nil | |
File::open(ENV['inventory']) do |f| | |
while line = f.gets | |
# Remove comment | |
md = line.match(/^([^;]+)/) | |
next unless md | |
line = md[0] | |
if line =~ /^\[([^\]]+)\]/ | |
current_group = $1 | |
elsif line =~ /^(\S+)/ | |
if current_group | |
groups[current_group] ||= [] | |
groups[current_group] << $1 | |
else | |
hosts << $1 | |
end | |
end | |
end | |
end | |
task :spec => 'spec:all' | |
task :default => :spec | |
namespace :spec do | |
task :all => groups.keys.map {|group| 'spec:' + group } | |
task :default => :all | |
# Tasks for groups | |
groups.keys.each do |group| | |
task group.to_sym => groups[group].map {|host| 'spec:' + group + ':' + host } | |
groups[group].each do |host| | |
desc "Run tests for group '#{group}'" | |
task_name = group + ':' + host | |
RSpec::Core::RakeTask.new(task_name) do |t| | |
ENV['TARGET_HOST'] = host | |
status = ENV['status'] ? ENV['status'] : 'running' | |
t.pattern = "spec/#{group}/#{status}/*_spec.rb" | |
end | |
end | |
end | |
# Tasks for hosts without groups | |
hosts += groups.values.flatten | |
hosts.uniq.each do |host| | |
desc "Run tests for host '#{host}'" | |
RSpec::Core::RakeTask.new(host) do |t| | |
ENV['TARGET_HOST'] = host | |
status = ENV['status'] ? ENV['status'] : 'running' | |
t.pattern = "spec/hosts/#{host}/#{status}/*_spec.rb" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment