Skip to content

Commit

Permalink
Merge pull request #151 from Temikus/cleanup_fix
Browse files Browse the repository at this point in the history
Cleanup fix
  • Loading branch information
erjohnso authored Oct 7, 2016
2 parents 46cce29 + 30e677c commit 4b245e3
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 46 deletions.
3 changes: 2 additions & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
2.0.0-p598
2.2.3
# Tracking Vagrant 1.8.5
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ group :development do
# We depend on Vagrant for development, but we don't add it as a
# gem dependency because we expect to be installed within the
# Vagrant environment itself using `vagrant plugin`.
gem 'vagrant', git: "git://github.com/mitchellh/vagrant.git"
gem 'vagrant', git: "https://github.com/mitchellh/vagrant.git"
gem 'vagrant-spec', git: "https://github.com/mitchellh/vagrant-spec.git"
end

Expand Down
20 changes: 7 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,15 @@ Service Account for API Access.
## Quick Start

After installing the plugin (instructions above), the quickest way to get
started is to actually use a dummy Google box and specify all the details
manually within a `config.vm.provider` block. So first, add the Google box
using any name you want:
started is to actually use a dummy Google box from Atlas and specify all the
details manually within a `config.vm.provider` block.

```sh
$ vagrant box add gce https://github.com/mitchellh/vagrant-google/raw/master/google.box
...
```

And then make a Vagrantfile that looks like the following, filling in
your information where necessary.
So first, make a Vagrantfile that looks like the following, filling in
your information where necessary:

```ruby
Vagrant.configure("2") do |config|
config.vm.box = "gce"
config.vm.box = "google/gce"

config.vm.provider :google do |google, override|
google.google_project_id = "YOUR_GOOGLE_CLOUD_PROJECT_ID"
Expand Down Expand Up @@ -229,7 +223,7 @@ zone you want to actually use, however. This looks like this:
```ruby
Vagrant.configure("2") do |config|

config.vm.box = "gce"
config.vm.box = "google/gce"

config.vm.provider :google do |google|
google.google_project_id = "YOUR_GOOGLE_CLOUD_PROJECT_ID"
Expand All @@ -241,7 +235,7 @@ Vagrant.configure("2") do |config|

google.zone_config "us-central1-f" do |zone1f|
zone1f.name = "testing-vagrant"
zone1f.image = "debian-8-jessie-v20160511"
zone1f.image = "debian-8-jessie-v20160923"
zone1f.machine_type = "n1-standard-4"
zone1f.zone = "us-central1-f"
zone1f.metadata = {'custom' => 'metadata', 'testing' => 'foobarbaz'}
Expand Down
6 changes: 4 additions & 2 deletions lib/vagrant-google/action/assign_instance_groups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class AssignInstanceGroups
def initialize(app, env)
@app = app
@logger = Log4r::Logger.new(
"vagrant_google::action::assign_instance_groups")
"vagrant_google::action::assign_instance_groups"
)
end

def call(env)
Expand Down Expand Up @@ -58,7 +59,8 @@ def call(env)
)
unless response.body["status"] == "DONE"
operation = env[:google_compute].operations.get(
operation.body["name"], zone)
operation.body["name"], zone
)
env[:ui].info(I18n.t("vagrant_google.waiting_for_operation",
name: operation.body["name"]))
operation.wait_for { ready? }
Expand Down
17 changes: 4 additions & 13 deletions lib/vagrant-google/action/run_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ def call(env) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
zone_name: zone,
source_image: image
)
disk_created_by_vagrant = true
disk.wait_for { disk.ready? }
disk_created_by_vagrant = true
end
end

Expand All @@ -137,11 +137,9 @@ def call(env) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
server = env[:google_compute].servers.create(defaults)
@logger.info("Machine '#{zone}:#{name}' created.")
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
# TODO: Cleanup the Fog catch-all once Fog implements better exceptions
# There is a chance Google has failed to create an instance, so we need
# to clean up the created disk.
cleanup_disk(disk.name, env) if disk && disk_created_by_vagrant
raise Errors::FogError, :message => e.message
end
Expand Down Expand Up @@ -223,13 +221,6 @@ def get_external_ip(env, external_ip)
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
Expand Down
8 changes: 5 additions & 3 deletions lib/vagrant-google/action/sync_folders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ def call(env) # rubocop:disable Metrics/MethodLength
# Create the guest path
env[:machine].communicate.sudo("mkdir -p '#{guestpath}'")
env[:machine].communicate.sudo(
"chown #{ssh_info[:username]} '#{guestpath}'")
"chown #{ssh_info[:username]} '#{guestpath}'"
)

# patch from https://github.com/tmatilai/vagrant-aws/commit/4a043a96076c332220ec4ec19470c4af5597dd51
def ssh_key_options(ssh_info)
Expand All @@ -81,7 +82,8 @@ def ssh_key_options(ssh_info)
*excludes.map{|e| ['--exclude', e]}.flatten,
"-e", "ssh -p #{ssh_info[:port]} -o StrictHostKeyChecking=no #{ssh_key_options(ssh_info)}",
hostpath,
"#{ssh_info[:username]}@#{ssh_info[:host]}:#{guestpath}"]
"#{ssh_info[:username]}@#{ssh_info[:host]}:#{guestpath}"
]

# we need to fix permissions when using rsync.exe on windows, see
# http://stackoverflow.com/questions/5798807/rsync-permission-denied-created-directories-have-no-permissions
Expand All @@ -90,7 +92,7 @@ def ssh_key_options(ssh_info)
end

r = Vagrant::Util::Subprocess.execute(*command)
if r.exit_code != 0
if r.exit_code.nonzero?
raise Errors::RsyncError,
:guestpath => guestpath,
:hostpath => hostpath,
Expand Down
3 changes: 2 additions & 1 deletion lib/vagrant-google/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ def merge(other)
# has it.
new_zone_specific = other.instance_variable_get(:@__zone_specific)
result.instance_variable_set(
:@__zone_specific, new_zone_specific || @__zone_specific)
:@__zone_specific, new_zone_specific || @__zone_specific
)

# Go through all the zone configs and prepend ours onto
# theirs.
Expand Down
2 changes: 1 addition & 1 deletion lib/vagrant-google/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
# limitations under the License.
module VagrantPlugins
module Google
VERSION = "0.2.4".freeze
VERSION = "0.2.5".freeze
end
end
3 changes: 2 additions & 1 deletion tasks/acceptance.rake
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ namespace :acceptance do
if File.read(ENV["GOOGLE_SSH_KEY_LOCATION"]).include?('ENCRYPTED')
unless `ssh-add -L`.include?(ENV["GOOGLE_SSH_KEY_LOCATION"])
yellow "WARNING: It looks like ssh key is encrypted and ssh-agent doesn't contain any identities."
yellow "This will likely cause the connection to the machine to fail."
puts "Trying to add identity, executing ssh-add..."
system("ssh-add #{ENV["GOOGLE_SSH_KEY_LOCATION"]}")
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions test/acceptance/skeletons/scopes/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ Vagrant.configure("2") do |config|
zone1d.name = "vagrant-acceptance-scopes-#{('a'..'z').to_a.sample(8).join}"
zone1d.scopes = ['sql-admin',
'bigquery',
'https://www.googleapis.com/auth/compute'
]
'https://www.googleapis.com/auth/compute']
zone1d.disk_type = "pd-ssd"
end
end
Expand Down
3 changes: 2 additions & 1 deletion vagrant-google.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ Gem::Specification.new do |s|
s.required_rubygems_version = ">= 1.3.6"
s.rubyforge_project = "vagrant-google"

s.add_runtime_dependency "fog-google", "~> 0.2.0"
s.add_runtime_dependency "fog-google", "~> 0.4.0"
s.add_runtime_dependency "google-api-client", "< 0.9", ">= 0.6.2"

s.add_development_dependency "pry"
s.add_development_dependency "pry-byebug"
s.add_development_dependency "rake"
s.add_development_dependency "rspec", "~> 2.14"
s.add_development_dependency "rubocop", "~> 0.35"
s.add_development_dependency "highline"

# The following block of code determines the files that should be included
# in the gem. It does this by reading all the files in the directory where
Expand Down
6 changes: 3 additions & 3 deletions vagrantfile_examples/Vagrantfile.multiple_machines
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ cat /etc/motd
SCRIPT

Vagrant.configure("2") do |config|
config.vm.box = "gce"
config.vm.box = "google/gce"
config.vm.provision :shell, :inline => $PROVISION_DEBIAN

config.vm.define :z1c do |z1c|
Expand All @@ -63,7 +63,7 @@ Vagrant.configure("2") do |config|

google.zone_config "us-central1-c" do |z1c_zone|
z1c_zone.name = "z1c"
z1c_zone.image = "debian-8-jessie-v20160511"
z1c_zone.image = "debian-8-jessie-v20160923"
z1c_zone.machine_type = "n1-standard-1"
z1c_zone.zone = "us-central1-c"
z1c_zone.metadata = {"zone" => "US Central 1c"}
Expand All @@ -83,7 +83,7 @@ Vagrant.configure("2") do |config|

google.zone_config "us-central1-f" do |z1f_zone|
z1f_zone.name = "z1f"
z1f_zone.image = "debian-8-jessie-v20160511"
z1f_zone.image = "debian-8-jessie-v20160923"
z1f_zone.machine_type = "n1-standard-2"
z1f_zone.zone = "us-central1-f"
z1f_zone.metadata = {"zone" => "US Central 1f"}
Expand Down
4 changes: 2 additions & 2 deletions vagrantfile_examples/Vagrantfile.provision_single
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ $LOCAL_SSH_KEY = "~/.ssh/id_rsa"

Vagrant.configure("2") do |config|

config.vm.box = "gce"
config.vm.box = "google/gce"

config.vm.provider :google do |google, override|
google.google_project_id = $GOOGLE_PROJECT_ID
Expand All @@ -36,7 +36,7 @@ Vagrant.configure("2") do |config|

# Override provider defaults
google.name = "testing-vagrant"
google.image = "debian-8-jessie-v20160511"
google.image = "debian-8-jessie-v20160923"
google.machine_type = "n1-standard-1"
google.zone = "us-central1-f"
google.metadata = {'custom' => 'metadata', 'testing' => 'foobarbaz'}
Expand Down
4 changes: 2 additions & 2 deletions vagrantfile_examples/Vagrantfile.zone_config
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# limitations under the License.

Vagrant.configure("2") do |config|
config.vm.box = "gce"
config.vm.box = "google/gce"

config.vm.provider :google do |google, override|
google.google_project_id = "YOUR_GOOGLE_CLOUD_PROJECT_ID"
Expand All @@ -30,7 +30,7 @@ Vagrant.configure("2") do |config|

google.zone_config "us-central1-f" do |zone1a|
zone1a.name = "testing-vagrant"
zone1a.image = "debian-8-jessie-v20160511"
zone1a.image = "debian-8-jessie-v20160923"
zone1a.machine_type = "n1-standard-4"
zone1a.zone = "us-central1-f"
zone1a.scopes = ['bigquery', 'monitoring', 'https://www.googleapis.com/auth/compute']
Expand Down

0 comments on commit 4b245e3

Please sign in to comment.