From 98aa27861e2d2019de010a9ac44f088035149d92 Mon Sep 17 00:00:00 2001 From: Max Lansing Date: Tue, 28 Feb 2012 23:44:53 -0800 Subject: [PATCH 1/2] change default socket file dir to /var/run/god or ~/.god/sockets. custom setting via class attribute available as well. --- lib/god.rb | 66 +++++++++++++++++++++++++++--------------- lib/god/cli/command.rb | 2 +- lib/god/socket.rb | 2 +- 3 files changed, 45 insertions(+), 25 deletions(-) diff --git a/lib/god.rb b/lib/god.rb index eb8318f4..93f8d012 100644 --- a/lib/god.rb +++ b/lib/god.rb @@ -161,6 +161,11 @@ module God # permissions will be used. PID_FILE_DIRECTORY_DEFAULTS = ['/var/run/god', '~/.god/pids'] + # Array of directory paths to be used for the socket file. + # This list will be searched in order and the first one that has write + # permission will be used. + SOCKET_FILE_DIRECTORY_DEFAULTS = ['/var/run/god', '~/.god/sockets'] + # The default Integer port number for the DRb communcations channel. DRB_PORT_DEFAULT = 17165 @@ -188,6 +193,7 @@ class << self :allow, :log_buffer_size, :pid_file_directory, + :socket_file_directory, :log_file, :log_level, :use_events, @@ -216,6 +222,7 @@ class << self self.allow = nil self.log_buffer_size = nil self.pid_file_directory = nil + self.socket_file_directory = nil self.log_level = nil self.terminate_timeout = nil self.socket_user = nil @@ -629,51 +636,64 @@ def self.load(glob) Kernel.load f end end + + # Setup pid and socket file directories, and log system. + # + # Returns nothing + def self.setup + self.setup_special_file_directory(PID_FILE_DIRECTORY_DEFAULTS, :pid_file_directory) + self.setup_special_file_directory(SOCKET_FILE_DIRECTORY_DEFAULTS, :socket_file_directory) + + if God::Logger.syslog + LOG.info("Syslog enabled.") if self.log_level + else + LOG.info("Syslog disabled.") if self.log_level + end + end - # Setup pid file directory and log system. + # Setup special file directory (pid and socket at this point) + # Requires an array of directories to try, and a symbol for the + # corresponding attribute on this class object. # # Returns nothing. - def self.setup - if self.pid_file_directory - # Pid file dir was specified, ensure it is created and writable. - unless File.exist?(self.pid_file_directory) + def self.setup_special_file_directory(default_directories, special_file_directory_attribute) + special_file_directory = self.send(special_file_directory_attribute) + nice_directory_text = special_file_directory_attribute.to_s.gsub("_"," ") + + if special_file_directory + # Special file dir was specified, ensure it is created and writable. + unless File.exist?(special_file_directory) begin - FileUtils.mkdir_p(self.pid_file_directory) + FileUtils.mkdir_p(special_file_directory) rescue Errno::EACCES => e - abort "Failed to create pid file directory: #{e.message}" + abort "Failed to create #{nice_directory_text}: #{e.message}" end end - unless File.writable?(self.pid_file_directory) - abort "The pid file directory (#{self.pid_file_directory}) is not writable by #{Etc.getlogin}" + unless File.writable?(special_file_directory) + abort "The pid file directory (#{special_file_directory}) is not writable by #{Etc.getlogin}" end else - # No pid file dir specified, try defaults. - PID_FILE_DIRECTORY_DEFAULTS.each do |idir| + # No special file dir specified, try defaults. + default_directories.each do |idir| dir = File.expand_path(idir) begin FileUtils.mkdir_p(dir) if File.writable?(dir) - self.pid_file_directory = dir + self.send("#{special_file_directory_attribute}=".to_sym,dir) break end rescue Errno::EACCES => e end end - unless self.pid_file_directory - dirs = PID_FILE_DIRECTORY_DEFAULTS.map { |x| File.expand_path(x) } - abort "No pid file directory exists, could be created, or is writable at any of #{dirs.join(', ')}" + unless self.send(special_file_directory_attribute) + dirs = default_directories.map { |x| File.expand_path(x) } + abort "No #{nice_directory_text} exists, could be created, or is writable at any of #{dirs.join(', ')}" end end - - if God::Logger.syslog - LOG.info("Syslog enabled.") - else - LOG.info("Syslog disabled.") - end - - applog(nil, :info, "Using pid file directory: #{self.pid_file_directory}") + + applog(nil, :info, "Using #{nice_directory_text}: #{self.send(special_file_directory_attribute)}") if self.log_level end # Initialize and startup the machinery that makes god work. diff --git a/lib/god/cli/command.rb b/lib/god/cli/command.rb index a7dc2449..27bcfcf9 100644 --- a/lib/god/cli/command.rb +++ b/lib/god/cli/command.rb @@ -11,10 +11,10 @@ def initialize(command, options, args) end def setup + God.setup #necessary to set the proper socket file. # connect to drb unix socket DRb.start_service("druby://127.0.0.1:0") @server = DRbObject.new(nil, God::Socket.socket(@options[:port])) - # ping server to ensure that it is responsive begin @server.ping diff --git a/lib/god/socket.rb b/lib/god/socket.rb index d9ab12a1..09948b59 100644 --- a/lib/god/socket.rb +++ b/lib/god/socket.rb @@ -11,7 +11,7 @@ class Socket # # Returns String (file location) def self.socket_file(port) - "/tmp/god.#{port}.sock" + "#{God.socket_file_directory}/god.#{port}.sock" end # The address of the socket for a given port From 2ee721435bc220dff801e426ab184f48f169e849 Mon Sep 17 00:00:00 2001 From: Max Lansing Date: Tue, 28 Feb 2012 23:45:21 -0800 Subject: [PATCH 2/2] new tests and updates for socket file changes --- test/helper.rb | 1 + test/test_god.rb | 22 ++++++++++++++++++++++ test/test_socket.rb | 2 +- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/test/helper.rb b/test/helper.rb index 063924bb..c8ea2dc7 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -81,6 +81,7 @@ def self.reset self.host = nil self.port = nil self.pid_file_directory = nil + self.socket_file_directory = nil self.registry.reset end end diff --git a/test/test_god.rb b/test/test_god.rb index 43cf7fe0..24e61278 100644 --- a/test/test_god.rb +++ b/test/test_god.rb @@ -7,6 +7,7 @@ def setup God.stubs(:validater).returns(true) God.reset God.pid_file_directory = '/var/run/god' + God.socket_file_directory = '/var/run/god' end def teardown @@ -41,6 +42,14 @@ def test_pid_file_directory_should_abort_if_called_after_watch God.pid_file_directory = 'foo' end end + + def test_socket_file_directory_should_abort_if_called_after_watch + God.watch { |w| w.name = 'ayo'; w.start = 'yayo' } + + assert_abort do + God.socket_file_directory = 'yayo' + end + end # pid_file_directory @@ -54,6 +63,19 @@ def test_pid_file_directory_equals_should_set God.internal_init assert_equal '/foo', God.pid_file_directory end + + # socket_file_directory + + def test_socket_file_directory_should_return_default_if_not_set_explicitly + God.internal_init + assert_equal '/var/run/god', God.socket_file_directory + end + + def test_socket_file_directory_equals_should_set + God.socket_file_directory = '/foo' + God.internal_init + assert_equal '/foo', God.socket_file_directory + end # watch diff --git a/test/test_socket.rb b/test/test_socket.rb index e51c5999..13396ad8 100644 --- a/test/test_socket.rb +++ b/test/test_socket.rb @@ -13,7 +13,7 @@ def test_should_start_a_drb_server end def test_should_use_supplied_port_and_host - DRb.expects(:start_service).with { |uri, object| uri == "drbunix:///tmp/god.9999.sock" && object.is_a?(God::Socket) } + DRb.expects(:start_service).with { |uri, object| uri == God::Socket.socket(9999) && object.is_a?(God::Socket) } server = God::Socket.new(9999) end