Skip to content

Instantly share code, notes, and snippets.

@sergiopena
Created March 20, 2013 07:15
Show Gist options
  • Save sergiopena/5202873 to your computer and use it in GitHub Desktop.
Save sergiopena/5202873 to your computer and use it in GitHub Desktop.
Script that given a pool of hypervisors (configured to support live migration), perform a random operations on the guests (power on, power off, pause, resume, undefine and live migration)
#!/usr/bin/env ruby
require 'rubygems'
require 'logger'
require 'libvirt'
require 'logger'
$log = Logger::new(STDOUT)
#$log.level = Logger::INFO
$log.level = Logger::DEBUG
hypervisors = %w( 192.168.2.52
192.168.2.53
192.168.2.54
)
threads = []
for hypervisor in hypervisors
threads << Thread.new(hypervisor) { |hyp|
p hyp
c = Libvirt::open("qemu+tcp://#{hyp.to_s}/system")
alive = c.list_domains
dead = c.list_defined_domains
alive.each { |domain|
d = c.lookup_domain_by_id(domain)
dice = rand(100)
case dice
when 0..20
$log.info "#{d.name} running with state #{d.state[0]} on #{hyp} dice: #{dice} was left as found"
when 21..60
$log.info "#{d.name} running with state #{d.state[0]} on #{hyp} dice: #{dice} was stopped"
d.destroy
when 61..84
if d.state[0] == 3
$log.info "#{d.name} running with state #{d.state[0]} on #{hyp} dice: #{dice} was resumed"
d.resume
else
$log.info "#{d.name} running with state #{d.state[0]} on #{hyp} dice: #{dice} was suspended"
d.suspend
end
when 85..94
candidates = hypervisors
candidates.delete(hyp)
dst_hyp = candidates[rand(candidates.length)]
$log.info "#{d.name} running with state #{d.state[0]} on #{hyp} migrated to #{dst_hyp}"
dst_conn = Libvirt::open("qemu+tcp://#{dst_hyp}/system")
d.migrate dst_conn,Libvirt::Domain::MIGRATE_UNDEFINE_SOURCE
dst_conn.close
when 95..99
$log.info "#{d.name} running with state #{d.state[0]} on #{hyp} dice: #{dice} was undefined"
d.undefine
end
}
dead.each { |domain|
d = c.lookup_domain_by_name(domain)
dice = rand(100)
case dice
when 0..20
$log.info "#{d.name} stopped with state #{d.state[0]} on #{hyp} dice: #{dice} was left as found"
when 21..94
$log.info "#{d.name} stopped with state #{d.state[0]} on #{hyp} dice: #{dice} was started"
d.create
when 95..99
$log.info "#{d.name} stopped with state #{d.state[0]} on #{hyp} dice: #{dice} was undefined"
d.undefine
end
}
c.close
}
end
threads.each { |aThread| aThread.join }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment