bonnie-as-a-service/baas

121 lines
2.8 KiB
Ruby
Executable File

#!/usr/bin/env ruby
require 'rubygems'
require 'colorize'
require 'erb'
require 'hashie'
require 'net/ssh'
require 'net/scp'
require 'net/sftp'
require 'yaml'
started = Time.now.utc
module Baas
def self.line_clear!
puts
puts ('-' * 30).red
puts
end
def self.configuration
return @config if @config
@config = Hashie::Mash.new(YAML.load(File.open('targets.yml').read))
end
def self.rexec!(name, commands)
puts "rexec! for #{name} with #{commands.size} commands".green
config = configuration.hosts[name]
Net::SSH.start(config.hostname, config.username, :password => config.password) do |ssh|
commands.each do |command|
puts "executing on #{name}: `#{command}`".green
start = Time.now.utc
ssh.exec!(command) do |channel, type, data|
if type == :stdout
puts data
elsif type == :stderr
puts data.red
end
end
puts "`#{command}` took #{Time.now.utc - start} seconds"
end
end
end
def self.upload_scripts_to(name)
config = configuration.hosts[name]
dir = '~'
user = config.username
bonnie = ERB::new(File.open('templates/run-bonnie.sh.erb').read).result(binding)
Net::SCP.start(config.hostname, config.username, :password => config.password) do |scp|
scp.upload!(StringIO.new(bonnie), 'run-bonnie.sh')
end
end
def self.download_results_from(name)
config = configuration.hosts[name]
Net::SFTP.start(config.hostname, config.username, :password => config.password) do |sftp|
sftp.dir.foreach('.') do |file|
if file.name =~ /\.(csv|html)/
print "Downloading #{file.name}..."
sftp.download!(file.name, File.join('results', file.name))
puts "..done"
end
end
end
end
def self.run!
line_clear!
puts 'Using these defaults:'
configuration.defaults.each_pair do |name, value|
puts "\t#{name} = #{value}".yellow
end
puts "For these hosts:"
configuration.hosts.each_pair do |name, host|
puts "\t#{name} (a #{host.metadata.type} in #{host.metadata.cloud}/#{host.metadata.region})".yellow
end
line_clear!
threads = []
configuration.hosts.each_key do |name|
upload_scripts_to name
commands = [
"echo 'Connection created to #{name}'",
#"sh run-bonnie.sh \$HOME 2048 #{name}",
]
[2048, 4096, 8192].each do |size|
commands << "sh run-bonnie.sh /mnt/bonnie-test #{size} #{name}"
end
commands.freeze
threads << Thread.new do
rexec!(name, commands)
download_results_from name
rexec!(name, ['rm -f *.csv *.html'])
end
end
threads.each { |t| t.join }
line_clear!
end
end
Baas.run!
puts "Started at #{started}, finished at #{Time.now.utc}"