54 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			54 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
| require "socket"
 | |
| 
 | |
| class DevopsConfig
 | |
| 
 | |
|   OBJECT_NAME = /[\w\-]+/
 | |
| 
 | |
|   @@config = nil
 | |
| 
 | |
|   class << self
 | |
|     def read config_file=nil
 | |
|       if config_file.nil?
 | |
|         config_file = ENV['DEVOPS_CONFIG'] || ENV['CONFIG'] || File.join(File.dirname(__FILE__), '../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 [](key)
 | |
|       @@config[key]
 | |
|     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
 | 
