Commit d565e1d9 authored by Praveen Arimbrathodiyil's avatar Praveen Arimbrathodiyil
Browse files

Imported Upstream version 1.21.1

parents
module Fog
module Account
def self.[](provider)
self.new(:provider => provider)
end
def self.new(attributes)
attributes = attributes.dup
provider = attributes.delete(:provider).to_s.downcase.to_sym
if provider == :stormondemand
require 'fog/storm_on_demand/account'
Fog::Account::StormOnDemand.new(attributes)
else
raise ArgumentError.new("#{provider} has no account service")
end
end
def self.providers
Fog.services[:account]
end
end
end
module Fog
module Billing
def self.[](provider)
self.new(:provider => provider)
end
def self.new(attributes)
attributes = attributes.dup
provider = attributes.delete(:provider).to_s.downcase.to_sym
if provider == :stormondemand
require 'fog/storm_on_demand/billing'
Fog::Billing::StormOnDemand.new(attributes)
else
raise ArgumentError.new("#{provider} has no billing service")
end
end
def self.providers
Fog.services[:billing]
end
end
end
module Fog
module CDN
def self.[](provider)
self.new(:provider => provider)
end
def self.new(attributes)
attributes = attributes.dup # prevent delete from having side effects
provider = attributes.delete(:provider).to_s.downcase.to_sym
if self.providers.include?(provider)
require "fog/#{provider}/cdn"
return Fog::CDN.const_get(Fog.providers[provider]).new(attributes)
end
raise ArgumentError.new("#{provider} is not a recognized cdn provider")
end
def self.providers
Fog.services[:cdn]
end
end
end
module Fog
module Compute
def self.[](provider)
self.new(:provider => provider)
end
def self.new(attributes)
attributes = attributes.dup # prevent delete from having side effects
provider = attributes.delete(:provider).to_s.downcase.to_sym
case provider
when :gogrid
require 'fog/go_grid/compute'
Fog::Compute::GoGrid.new(attributes)
when :hp
version = attributes.delete(:version)
version = version.to_s.downcase.to_sym unless version.nil?
if version == :v2
require 'fog/hp/compute_v2'
Fog::Compute::HPV2.new(attributes)
else
Fog::Logger.deprecation "HP Cloud Compute V1 service will be soon deprecated. Please use `:version => v2` attribute to use HP Cloud Compute V2 service."
require 'fog/hp/compute'
Fog::Compute::HP.new(attributes)
end
when :new_servers
require 'fog/bare_metal_cloud/compute'
Fog::Logger.deprecation "`new_servers` is deprecated. Please use `bare_metal_cloud` instead."
Fog::Compute::BareMetalCloud.new(attributes)
when :baremetalcloud
require 'fog/bare_metal_cloud/compute'
Fog::Compute::BareMetalCloud.new(attributes)
when :rackspace
version = attributes.delete(:version)
version = version.to_s.downcase.to_sym unless version.nil?
if version == :v1
Fog::Logger.deprecation "First Gen Cloud Servers are deprecated. Please use `:version => :v2` attribute to use Next Gen Cloud Servers."
require 'fog/rackspace/compute'
Fog::Compute::Rackspace.new(attributes)
else
require 'fog/rackspace/compute_v2'
Fog::Compute::RackspaceV2.new(attributes)
end
when :stormondemand
require 'fog/storm_on_demand/compute'
Fog::Compute::StormOnDemand.new(attributes)
when :vcloud
require 'fog/vcloud/compute'
Fog::Vcloud::Compute.new(attributes)
when :vclouddirector
require 'fog/vcloud_director/compute'
Fog::Compute::VcloudDirector.new(attributes)
else
if self.providers.include?(provider)
require "fog/#{provider}/compute"
return Fog::Compute.const_get(Fog.providers[provider]).new(attributes)
end
raise ArgumentError.new("#{provider} is not a recognized compute provider")
end
end
def self.providers
Fog.services[:compute]
end
def self.servers
servers = []
for provider in self.providers
begin
servers.concat(self[provider].servers)
rescue # ignore any missing credentials/etc
end
end
servers
end
end
end
require 'fog/core/model'
module Fog
module Compute
class Server < Fog::Model
attr_writer :username, :private_key, :private_key_path, :public_key, :public_key_path, :ssh_port, :ssh_options
def username
@username ||= 'root'
end
def private_key_path
@private_key_path ||= Fog.credentials[:private_key_path]
@private_key_path &&= File.expand_path(@private_key_path)
end
def private_key
@private_key ||= private_key_path && File.read(private_key_path)
end
def public_key_path
@public_key_path ||= Fog.credentials[:public_key_path]
@public_key_path &&= File.expand_path(@public_key_path)
end
def public_key
@public_key ||= public_key_path && File.read(public_key_path)
end
# Port used for ssh/scp interactions with server.
# @return [Integer] IP port
# @note By default this returns 22
def ssh_port
@ssh_port ||= 22
end
# Sets the proc used to determine the IP Address used for ssh/scp interactions.
# @example
# service.servers.bootstrap :name => 'bootstrap-server',
# :flavor_id => service.flavors.first.id,
# :image_id => service.images.find {|img| img.name =~ /Ubuntu/}.id,
# :public_key_path => '~/.ssh/fog_rsa.pub',
# :private_key_path => '~/.ssh/fog_rsa',
# :ssh_ip_address => Proc.new {|server| server.private_ip_address }
#
# @note By default scp/ssh will use the public_ip_address if this proc is not set.
def ssh_ip_address=(proc)
@ssh_ip_address = proc
end
# IP Address used for ssh/scp interactions with server.
# @return [String] IP Address
# @note By default this returns the public_ip_address
def ssh_ip_address
return public_ip_address unless @ssh_ip_address
return @ssh_ip_address.call(self) if @ssh_ip_address.is_a?(Proc)
@ssh_ip_address
end
def ssh_options
@ssh_options ||= {}
ssh_options = @ssh_options.merge({:port => ssh_port})
if private_key
ssh_options[:key_data] = [private_key]
ssh_options[:auth_methods] = ["publickey"]
end
ssh_options
end
def scp(local_path, remote_path, upload_options = {})
require 'net/scp'
requires :ssh_ip_address, :username
Fog::SCP.new(ssh_ip_address, username, ssh_options).upload(local_path, remote_path, upload_options)
end
alias_method :scp_upload, :scp
def scp_download(remote_path, local_path, download_options = {})
require 'net/scp'
requires :ssh_ip_address, :username
Fog::SCP.new(ssh_ip_address, username, ssh_options).download(remote_path, local_path, download_options)
end
def ssh(commands, options={}, &blk)
require 'net/ssh'
requires :ssh_ip_address, :username
options = ssh_options.merge(options)
Fog::SSH.new(ssh_ip_address, username, options).run(commands, &blk)
end
def sshable?(options={})
ready? && !ssh_ip_address.nil? && !!Timeout::timeout(8) { ssh('pwd', options) }
rescue SystemCallError, Net::SSH::AuthenticationFailed, Net::SSH::Disconnect, Timeout::Error
false
end
end
end
end
# external core dependencies
require 'base64'
require 'cgi'
require 'uri'
require 'excon'
require 'fileutils'
require 'formatador'
require 'openssl'
require 'time'
require 'timeout'
require 'ipaddr'
# internal core dependencies
require "fog/version"
require 'fog/core/attributes'
require 'fog/core/collection'
require 'fog/core/connection'
require 'fog/core/credentials'
require 'fog/core/current_machine'
require 'fog/core/deprecation'
require 'fog/core/errors'
require 'fog/core/hmac'
require 'fog/core/logger'
require 'fog/core/model'
require 'fog/core/mock'
require 'fog/core/provider'
require 'fog/core/service'
require 'fog/core/ssh'
require 'fog/core/scp'
require 'fog/core/time'
require 'fog/core/wait_for'
require 'fog/core/wait_for_defaults'
require 'fog/core/class_from_string'
require 'fog/core/uuid'
# service wrappers
require 'fog/account'
require 'fog/billing'
require 'fog/cdn'
require 'fog/compute'
require 'fog/dns'
require 'fog/identity'
require 'fog/image'
require 'fog/metering'
require 'fog/monitoring'
require 'fog/network'
require 'fog/orchestration'
require 'fog/storage'
require 'fog/support'
require 'fog/volume'
require 'fog/vpn'
module Fog
module Attributes
module ClassMethods
def _load(marshalled)
new(Marshal.load(marshalled))
end
def aliases
@aliases ||= {}
end
def attributes
@attributes ||= []
end
def attribute(name, options = {})
class_eval <<-EOS, __FILE__, __LINE__
def #{name}
attributes[:#{name}]
end
EOS
case options[:type]
when :boolean
class_eval <<-EOS, __FILE__, __LINE__
def #{name}=(new_#{name})
attributes[:#{name}] = case new_#{name}
when true,'true'
true
when false,'false'
false
end
end
EOS
when :float
class_eval <<-EOS, __FILE__, __LINE__
def #{name}=(new_#{name})
attributes[:#{name}] = new_#{name}.to_f
end
EOS
when :integer
class_eval <<-EOS, __FILE__, __LINE__
def #{name}=(new_#{name})
attributes[:#{name}] = new_#{name}.to_i
end
EOS
when :string
class_eval <<-EOS, __FILE__, __LINE__
def #{name}=(new_#{name})
attributes[:#{name}] = new_#{name}.to_s
end
EOS
when :time
class_eval <<-EOS, __FILE__, __LINE__
def #{name}=(new_#{name})
attributes[:#{name}] = if new_#{name}.nil? || new_#{name} == "" || new_#{name}.is_a?(Time)
new_#{name}
else
Time.parse(new_#{name})
end
end
EOS
when :timestamp
class_eval <<-EOS, __FILE__, __LINE__
def #{name}=(new_#{name})
attributes[:#{name}] = Time.at(new_#{name}.to_i)
end
EOS
when :array
class_eval <<-EOS, __FILE__, __LINE__
def #{name}=(new_#{name})
attributes[:#{name}] = [*new_#{name}]
end
EOS
else
if squash = options[:squash]
class_eval <<-EOS, __FILE__, __LINE__
def #{name}=(new_data)
if new_data.is_a?(Hash)
if new_data.has_key?(:'#{squash}')
attributes[:#{name}] = new_data[:'#{squash}']
elsif new_data.has_key?("#{squash}")
attributes[:#{name}] = new_data["#{squash}"]
else
attributes[:#{name}] = [ new_data ]
end
else
attributes[:#{name}] = new_data
end
end
EOS
else
class_eval <<-EOS, __FILE__, __LINE__
def #{name}=(new_#{name})
attributes[:#{name}] = new_#{name}
end
EOS
end
end
@attributes ||= []
@attributes |= [name]
for new_alias in [*options[:aliases]]
aliases[new_alias] = name
end
end
def identity(name, options = {})
@identity = name
self.attribute(name, options)
end
def ignore_attributes(*args)
@ignored_attributes = args.collect {|attr| attr.to_s }
end
def ignored_attributes
@ignored_attributes ||= []
end
end
module InstanceMethods
def _dump(level)
Marshal.dump(attributes)
end
def attributes
@attributes ||= {}
end
def dup
copy = super
copy.dup_attributes!
copy
end
def identity
send(self.class.instance_variable_get('@identity'))
end
def identity=(new_identity)
send("#{self.class.instance_variable_get('@identity')}=", new_identity)
end
def merge_attributes(new_attributes = {})
for key, value in new_attributes
unless self.class.ignored_attributes.include?(key)
if aliased_key = self.class.aliases[key]
send("#{aliased_key}=", value)
elsif self.respond_to?("#{key}=",true)
send("#{key}=", value)
else
attributes[key] = value
end
end
end
self
end
# Returns true if a remote resource has been assigned an
# identity and we can assume it has been persisted.
#
# @return [Boolean]
def persisted?
!!identity
end
# Returns true if a remote resource has not been assigned an
# identity.
#
# This was added for a ActiveRecord like feel but has been
# outdated by ActiveModel API using {#persisted?}
#
# @deprecated Use inverted form of {#persisted?}
# @return [Boolean]
def new_record?
Fog::Logger.deprecation("#new_record? is deprecated, use !persisted? instead [light_black](#{caller.first})[/]")
!persisted?
end
# check that the attributes specified in args exist and is not nil
def requires(*args)
missing = missing_attributes(args)
if missing.length == 1
raise(ArgumentError, "#{missing.first} is required for this operation")
elsif missing.any?
raise(ArgumentError, "#{missing[0...-1].join(", ")} and #{missing[-1]} are required for this operation")
end
end
def requires_one(*args)
missing = missing_attributes(args)
if missing.length == args.length
raise(ArgumentError, "#{missing[0...-1].join(", ")} or #{missing[-1]} are required for this operation")
end
end
protected
def missing_attributes(args)
missing = []
for arg in [:service] | args
unless send("#{arg}") || attributes.has_key?(arg)
missing << arg
end
end
missing
end
def dup_attributes!
@attributes = @attributes.dup if @attributes
end
private
def remap_attributes(attributes, mapping)
for key, value in mapping
if attributes.key?(key)
attributes[value] = attributes.delete(key)
end
end
end
end
end
end
module Fog
# get class by string or nil
def self.class_from_string classname, defaultpath=""
if classname and classname.is_a? String then
chain = classname.split("::")
klass = Kernel
chain.each do |klass_string|
klass = klass.const_get klass_string
end
if klass.is_a? Class then
klass
elsif defaultpath != nil then
Fog.class_from_string((defaultpath.split("::")+chain).join("::"), nil)
else
nil
end
elsif classname and classname.is_a? Class then
classname
else
nil
end
rescue NameError
defaultpath != nil ? Fog.class_from_string((defaultpath.split("::")+chain).join("::"), nil) : nil
end
end
require "fog/core/deprecated_connection_accessors"
module Fog
class Collection < Array
extend Fog::Attributes::ClassMethods
include Fog::Attributes::InstanceMethods
include Fog::Core::DeprecatedConnectionAccessors
attr_reader :service
Array.public_instance_methods(false).each do |method|
unless [:reject, :select, :slice, :clear, :inspect].include?(method.to_sym)
class_eval <<-EOS, __FILE__, __LINE__
def #{method}(*args)
unless @loaded
lazy_load
end
super
end
EOS
end
end
%w[reject select slice].each do |method|
class_eval <<-EOS, __FILE__, __LINE__
def #{method}(*args)
unless @loaded
lazy_load
end
data = super
self.clone.clear.concat(data)
end
EOS
end
def self.model(new_model=nil)
if new_model == nil
@model
else
@model = new_model
end
end
def clear
@loaded = true
super
end
def create(attributes = {})
object = new(attributes)
object.save