Sha256: dafd4a72dc39bc31670d9d2cf089017f87455616d16f97046b0c0d4081d53943

Contents?: true

Size: 1.03 KB

Versions: 27

Compression:

Stored size: 1.03 KB

Contents

require 'net/http'
require 'uri'

module Fog
  class CurrentMachine
    @@lock = Mutex.new
    AMAZON_AWS_CHECK_IP = 'http://checkip.amazonaws.com'

    def self.ip_address= ip_address
      @@lock.synchronize do
        @@ip_address = ip_address
      end
    end

    # Get the ip address of the machine from which this command is run. It is
    # recommended that you surround calls to this function with a timeout block
    # to ensure optimum performance in the case where the amazonaws checkip
    # service is unavailable.
    #
    # @example Get the current ip address
    #   begin
    #     Timeout::timeout(5) do
    #       puts "Your ip address is #{Fog::CurrentMachine.ip_address}"
    #     end
    #   rescue Timeout::Error
    #     puts "Service timeout"
    #   end
    #
    # @raise [Net::HTTPExceptions] if the net/http request fails.
    def self.ip_address
      @@lock.synchronize do
        @@ip_address ||= Net::HTTP \
          .get_response(URI.parse(AMAZON_AWS_CHECK_IP)) \
          .body.chomp
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 7 rubygems

Version Path
brightbox-cli-0.18.1 lib/brightbox-cli/vendor/fog/lib/fog/core/current_machine.rb
michiels-fog-1.3.1 lib/fog/core/current_machine.rb
ftl-0.2.0 vendor/bundle/gems/fog-1.3.1/lib/fog/core/current_machine.rb
brightbox-cli-0.18.0 lib/brightbox-cli/vendor/fog/lib/fog/core/current_machine.rb
fog-1.3.1 lib/fog/core/current_machine.rb
fog-1.3.0 lib/fog/core/current_machine.rb
brightbox-cli-0.17.5 lib/brightbox-cli/vendor/fog/lib/fog/core/current_machine.rb
fog-1.2.0 lib/fog/core/current_machine.rb
ktheory-fog-1.1.2 lib/fog/core/current_machine.rb
brightbox-cli-0.17.4 lib/brightbox-cli/vendor/fog/lib/fog/core/current_machine.rb
brightbox-cli-0.17.3 lib/brightbox-cli/vendor/fog/lib/fog/core/current_machine.rb
brightbox-cli-0.17.2 lib/brightbox-cli/vendor/fog/lib/fog/core/current_machine.rb
brightbox-cli-0.17.1 lib/brightbox-cli/vendor/fog/lib/fog/core/current_machine.rb
brightbox-cli-0.17.0 lib/brightbox-cli/vendor/fog/lib/fog/core/current_machine.rb
fog-1.1.2 lib/fog/core/current_machine.rb
fog_tractical-1.1.4 lib/fog/core/current_machine.rb
fog_tractical-1.1.3 lib/fog/core/current_machine.rb
brightbox-cli-0.16.0 lib/brightbox-cli/vendor/fog/lib/fog/core/current_machine.rb
fog-1.1.1 lib/fog/core/current_machine.rb
fog-1.1.0 lib/fog/core/current_machine.rb