Last active
August 29, 2015 14:03
-
-
Save nicot/db18d612f491f65e72fc to your computer and use it in GitHub Desktop.
Destroy old VM's
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
#!/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