| 
									
										
										
										
											2015-02-06 17:21:13 +03:00
										 |  |  | require "socket" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class DevopsConfig | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-18 13:15:25 +03:00
										 |  |  |   OBJECT_NAME = /[\w\-]+/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-06 17:21:13 +03:00
										 |  |  |   @@config = nil | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   class << self | 
					
						
							|  |  |  |     def read config_file=nil | 
					
						
							|  |  |  |       if config_file.nil? | 
					
						
							| 
									
										
										
										
											2015-07-08 18:42:36 +03:00
										 |  |  |         config_file = ENV['DEVOPS_CONFIG'] || ENV['CONFIG'] || File.join(File.dirname(__FILE__), 'config.rb') | 
					
						
							| 
									
										
										
										
											2015-02-06 17:21:13 +03:00
										 |  |  |       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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |     def [](key) | 
					
						
							|  |  |  |       @@config[key] | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-06 17:21:13 +03:00
										 |  |  |     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 |