Sha256: 51969425efe292ab9a0ac49dcb40772eb2ecc02e3fc3f56e5035e0d7c8dba680

Contents?: true

Size: 2 KB

Versions: 14

Compression:

Stored size: 2 KB

Contents

# Copyright 2013 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require "log4r"

module VagrantPlugins
  module Google
    module Action
      # This action reads the state of the machine and puts it in the
      # `:machine_state_id` key in the environment.
      class ReadState
        def initialize(app, env)
          @app    = app
          @logger = Log4r::Logger.new("vagrant_google::action::read_state")
        end

        def call(env)
          env[:machine_state_id] = read_state(env[:google_compute], env[:machine])

          @app.call(env)
        end

        def read_state(google, machine)
          return :not_created if machine.id.nil?

          # Find the machine
          zone = machine.provider_config.zone
          # TODO(erjohnso): not sure why this is necessary, 'server' should be nil
          begin
            server = google.servers.get(machine.id, zone)
          rescue Exception => e
            @logger.info("TODO: this shouldn't be happening. Call should return nil")
            @logger.info(e.message)
            server = nil
          end
          if server.nil? || [:"shutting-down", :terminated].include?(server.status.to_sym)
            # The machine can't be found
            @logger.info("Machine '#{zone}:#{machine.id}' not found or terminated, assuming it got destroyed.")
            machine.id = nil
            return :not_created
          end

          # Return the state
          return server.status.to_sym
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
vagrant-google-2.7.0 lib/vagrant-google/action/read_state.rb
vagrant-google-2.6.0 lib/vagrant-google/action/read_state.rb
vagrant-google-2.5.0 lib/vagrant-google/action/read_state.rb
vagrant-google-2.4.0 lib/vagrant-google/action/read_state.rb
vagrant-google-2.4.0.rc0 lib/vagrant-google/action/read_state.rb
vagrant-google-2.3.0 lib/vagrant-google/action/read_state.rb
vagrant-google-2.3.0.rc0 lib/vagrant-google/action/read_state.rb
vagrant-google-2.2.1 lib/vagrant-google/action/read_state.rb
vagrant-google-2.2.0 lib/vagrant-google/action/read_state.rb
vagrant-google-2.2.0.rc0 lib/vagrant-google/action/read_state.rb
vagrant-google-2.1.0 lib/vagrant-google/action/read_state.rb
vagrant-google-2.0.0 lib/vagrant-google/action/read_state.rb
vagrant-google-2.0.0.rc1 lib/vagrant-google/action/read_state.rb
vagrant-google-2.0.0.rc0 lib/vagrant-google/action/read_state.rb