Skip to content

Instantly share code, notes, and snippets.

@1tylermitchell
Created December 8, 2015 18:19
Show Gist options
  • Save 1tylermitchell/6d33d78b34f6901245de to your computer and use it in GitHub Desktop.
Save 1tylermitchell/6d33d78b34f6901245de to your computer and use it in GitHub Desktop.
Partial serverspec Rake file that loops through all nodes in a HDFS cluster automatically
# Two approaches to getting the list of nodes automatically.
# Some stuff re: roles from serverspec-example repository
...
def hdfsnodes()
# Requires special access to HDFS reporting tool...
clusterreport,nodesreport = `sudo su hdfs -c 'hdfs dfsadmin -report'`.split("\n\n-------------------------------------------------\n")
nodesreplist=nodesreport.strip.split("\n")
nodenamelist = nodesreplist.grep(/Name/)
nodenamelist = nodenamelist.map! { |n| n.split(":")[0] == "Name" ? n.split(":")[1].strip : n }.sort
nodenamelist
end
def yarnnodes()
# All users should be able to run this
nodes = `yarn node -list`.split("Containers\n")[1].split("\n").map! { |n| n.split("\t")[2].split(":")[0] }
nodes
end
...
hosts = yarnnodes()
hosts.map! { |host|
host.strip!
{
:name => host,
:roles => roles(host),
:tags => tags(host)
}
}
...
task :all => hosts.map { |h| h[:name] }
hosts.each do |host|
desc "Run serverspec to host #{host[:name]}"
ServerspecTask.new(host[:name].to_sym) do |t|
dirs = host[:roles] + [ host[:name] ]
t.target = host[:name]
t.tags = host[:tags]
t.pattern = File.join('.', 'spec', '{' + dirs.join(",") + '}', '*_spec.rb')
end
end
end
...
# Per role tasks
namespace :role do
roles = hosts.map {|h| h[:roles]}
roles = roles.flatten.uniq
roles.each do |role|
desc "Run serverspec to role #{role}"
task "#{role}" => hosts.select { |h| h[:roles].include? role }.map {
|h| "check:server:" + h[:name]
}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment