Skip to content

Commit

Permalink
Merge pull request #113 from p0deje/cleanup_disk_on_failure
Browse files Browse the repository at this point in the history
Cleanup created disk on failure
  • Loading branch information
erjohnso committed Oct 23, 2015
2 parents a0848b0 + 4e4022c commit 2137b19
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions lib/vagrant-google/action/run_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ module Action
class RunInstance
include Vagrant::Util::Retryable

FOG_ERRORS = [
Fog::Compute::Google::NotFound,
Fog::Compute::Google::Error,
Fog::Errors::Error
]

def initialize(app, env)
@app = app
@logger = Log4r::Logger.new("vagrant_google::action::run_instance")
Expand Down Expand Up @@ -80,6 +86,7 @@ def call(env)
# Check if disk type is available in the zone and set the proper resource link
disk_type = get_disk_type(env, disk_type, zone)

disk_created_by_vagrant = false
if disk_name.nil?
# no disk_name... disk_name defaults to instance name
disk = env[:google_compute].disks.create(
Expand All @@ -89,6 +96,7 @@ def call(env)
zone_name: zone,
source_image: image
)
disk_created_by_vagrant = true
disk.wait_for { disk.ready? }
else
disk = env[:google_compute].disks.get(disk_name, zone)
Expand All @@ -101,6 +109,7 @@ def call(env)
zone_name: zone,
source_image: image
)
disk_created_by_vagrant = true
disk.wait_for { disk.ready? }
end
end
Expand All @@ -125,9 +134,13 @@ def call(env)
}
server = env[:google_compute].servers.create(defaults)
@logger.info("Machine '#{zone}:#{name}' created.")
rescue Fog::Compute::Google::NotFound => e
raise Errors::FogError, :message => e.message
rescue Fog::Compute::Google::Error => e
rescue *FOG_ERRORS => e
# there is a chance Google responded with error but actually created
# instance, so we need to remove it
cleanup_instance(env)
# there is a chance Google has failed to create instance, so we need
# to remove created disk
cleanup_disk(disk.name, env) if disk && disk_created_by_vagrant
raise Errors::FogError, :message => e.message
end

Expand Down Expand Up @@ -207,6 +220,22 @@ def get_external_ip(env, external_ip)
# Resolve the name to IP address
address.address
end

def cleanup_instance(env)
zone = env[:machine].provider_config.zone
zone_config = env[:machine].provider_config.get_zone_config(zone)
server = env[:google_compute].servers.get(zone_config.name, zone)
server.destroy(false) if server
end

def cleanup_disk(disk_name, env)
zone = env[:machine].provider_config.zone
autodelete_disk = env[:machine].provider_config.get_zone_config(zone).autodelete_disk
if autodelete_disk
disk = env[:google_compute].disks.get(disk_name, zone)
disk.destroy(false) if disk
end
end
end
end
end
Expand Down

0 comments on commit 2137b19

Please sign in to comment.