| 
									
										
										
										
											2015-02-06 17:21:13 +03:00
										 |  |  | require "socket" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class DevopsConfig | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-06 13:50:26 +03:00
										 |  |  |   OBJECT_NAME = /([\w\-]+)/ | 
					
						
							| 
									
										
										
										
											2015-02-18 13:15:25 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-06 17:21:13 +03:00
										 |  |  |   @@config = nil | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   class << self | 
					
						
							|  |  |  |     def read config_file=nil | 
					
						
							|  |  |  |       if config_file.nil? | 
					
						
							| 
									
										
										
										
											2015-07-27 11:14:01 +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 | 
					
						
							| 
									
										
										
										
											2015-08-10 15:10:35 +03:00
										 |  |  |       config = { | 
					
						
							|  |  |  |         :url_prefix => "", | 
					
						
							|  |  |  |         :devops_dir => File.join(ENV["HOME"], ".devops") | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-02-06 17:21:13 +03:00
										 |  |  |       if File.exists? config_file | 
					
						
							|  |  |  |         eval File.read config_file | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         raise "No config file '#{config_file}' found" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       addr = first_public_ipv4 || first_private_ipv4 | 
					
						
							|  |  |  |       config[:address] = if addr.nil? | 
					
						
							|  |  |  |         "localhost" | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         addr.ip_address | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-01 16:31:31 +03:00
										 |  |  |       config[:report_dir_v2] = File.expand_path(File.join(config[:devops_dir], "report", "v2")) | 
					
						
							| 
									
										
										
										
											2015-09-02 11:38:58 +03:00
										 |  |  |       config[:project_info_dir] = File.expand_path(File.join(config[:devops_dir], "project_info", "v2")) | 
					
						
							| 
									
										
										
										
											2015-09-01 16:31:31 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-06 17:21:13 +03:00
										 |  |  |       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 |