Skip to content

Commit

Permalink
Clean up Vagrant snapshots when VMs are destroyed
Browse files Browse the repository at this point in the history
Add a step to the VM destroy action that cleans up any Vagrant snapshots.
  • Loading branch information
Sharpie committed Aug 1, 2016
1 parent b80e43d commit 40bd74b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
2 changes: 2 additions & 0 deletions source/lib/vagrant-openstack-provider/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def self.action_destroy
b2.use Message, I18n.t('vagrant_openstack.not_created')
else
b2.use(ProvisionerCleanup, :before)
b2.use SnapshotCleanup if Gem::Version.new(Vagrant::VERSION) >= Gem::Version.new('1.8.0')
b2.use DeleteServer
b2.use DeleteStack
end
Expand Down Expand Up @@ -309,6 +310,7 @@ def self.action_snapshot_save
# TODO: Remove the if guard when Vagrant 1.8.0 is the minimum version.
# rubocop:disable IndentationWidth
if Gem::Version.new(Vagrant::VERSION) >= Gem::Version.new('1.8.0')
autoload :SnapshotCleanup, action_root.join('snapshot_cleanup')
autoload :SnapshotDelete, action_root.join('snapshot_delete')
autoload :SnapshotList, action_root.join('snapshot_list')
autoload :SnapshotRestore, action_root.join('snapshot_restore')
Expand Down
32 changes: 32 additions & 0 deletions source/lib/vagrant-openstack-provider/action/snapshot_cleanup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'vagrant-openstack-provider/action/abstract_action'

module VagrantPlugins
module Openstack
module Action
class SnapshotCleanup < AbstractAction
def initialize(app, _env)
@app = app
end

def call(env)
nova = env[:openstack_client].nova
machine_snapshots = nova.list_snapshots(env, env[:machine].id)

snapshots_to_clean = machine_snapshots.select do |s|
s.metadata.key?('vagrant_snapshot')
end

@app.call env

unless snapshots_to_clean.empty?
env[:ui].info("Deleting Vagrant snapshots: #{snapshots_to_clean.map(&:name)}")
end

snapshots_to_clean.each do |s|
nova.delete_snapshot(env, s.id)
end
end
end
end
end
end
6 changes: 4 additions & 2 deletions source/lib/vagrant-openstack-provider/client/domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,21 @@ class Image < Item
attr_accessor :size
attr_accessor :min_ram
attr_accessor :min_disk
attr_accessor :metadata

def initialize(id, name, visibility = nil, size = nil, min_ram = nil, min_disk = nil)
def initialize(id, name, visibility = nil, size = nil, min_ram = nil, min_disk = nil, metadata = {})
@visibility = visibility
@size = size
@min_ram = min_ram
@min_disk = min_disk
@metadata = metadata
super(id, name)
end

protected

def state
[@id, @name, @visibility, @size, @min_ram, @min_disk]
[@id, @name, @visibility, @size, @min_ram, @min_disk, @metadata]
end
end

Expand Down
19 changes: 16 additions & 3 deletions source/lib/vagrant-openstack-provider/client/nova.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,18 @@ def allocate_floating_ip(env, pool)
end

def get_all_images(env, headers = {})
images_json = get(env, "#{@session.endpoints[:compute]}/images", headers)
JSON.parse(images_json)['images'].map { |fl| Image.new(fl['id'], fl['name'], 'unknown') }
images_json = get(env, "#{@session.endpoints[:compute]}/images/detail", headers)
JSON.parse(images_json)['images'].map do |fl|
Image.new(
fl['id'],
fl['name'],
'unknown',
nil,
fl['minRam'],
fl['minDisk'],
fl['metadata']
)
end
end

# Get detailed information about an image
Expand Down Expand Up @@ -208,7 +218,10 @@ def create_snapshot(env, server_id, snapshot_name)
post(
env,
"#{@session.endpoints[:compute]}/servers/#{server_id}/action",
{ createImage: { name: snapshot_name } }.to_json)
{ createImage: {
name: snapshot_name,
metadata: { vagrant_snapshot: true }
} }.to_json)
end
end

Expand Down

0 comments on commit 40bd74b

Please sign in to comment.