Skip to content

Instantly share code, notes, and snippets.

@nicot
Last active August 29, 2015 14:03
Show Gist options
  • Save nicot/db18d612f491f65e72fc to your computer and use it in GitHub Desktop.
Save nicot/db18d612f491f65e72fc to your computer and use it in GitHub Desktop.
Destroy old VM's
#!/usr/bin/env ruby
# destroy VM's that are older than 6 hours
def ex(cmd)
`#{cmd}`
end
output = ex('virsh list --all').split("\n").drop(2)
output.each do |line|
name = line.match(/(?:\d+|-)\s*(.+?)\s/)[1]
time = name.match(/.+?(\d{10})/)[1].to_i if name
if time && Time.at(time) < Time.now - 6 * 60 * 60
#puts "Destroying #{name}"
ex("virsh destroy #{name}") if line =~ /(running|paused)/
ex("virsh undefine #{name}")
end
end
output = ex('virsh vol-list default').split("\n")[2...-1]
output.each do |line|
name = line.match(/(\/.+?\.img)/)[1]
time = name.match(/.+?(\d{10})/) if name
if time && Time.at(time[1].to_i) < Time.now - 6 * 60 * 60
#puts "deleting #{name}"
ex("virsh vol-delete #{name}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment