fluke/devops-service/devops_config.rb

49 lines
1.0 KiB
Ruby
Raw Normal View History

2015-02-06 17:21:13 +03:00
require "socket"
class DevopsConfig
@@config = nil
class << self
def read config_file=nil
if config_file.nil?
root = File.dirname(__FILE__)
config_file = File.join(root, "config.rb")
end
config = {:url_prefix => ""}
if File.exists? config_file
eval File.read config_file
else
raise "No config file '#{config_file}' found"
end
config[:devops_dir] = File.join(ENV["HOME"], ".devops") if config[:devops_dir].nil?
addr = first_public_ipv4 || first_private_ipv4
config[:address] = if addr.nil?
"localhost"
else
addr.ip_address
end
DevopsConfig.config = config
end
def config= config
@@config = config
end
def config
@@config
end
def first_private_ipv4
Socket.ip_address_list.detect{|intf| intf.ipv4_private?}
end
def first_public_ipv4
Socket.ip_address_list.detect{|intf| intf.ipv4? and !intf.ipv4_loopback? and !intf.ipv4_multicast? and !intf.ipv4_private?}
end
end
end