Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 43 additions & 23 deletions lib/god.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,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

Expand Down Expand Up @@ -193,6 +198,7 @@ class << self
:allow,
:log_buffer_size,
:pid_file_directory,
:socket_file_directory,
:log_file,
:log_level,
:use_events,
Expand Down Expand Up @@ -221,6 +227,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
Expand Down Expand Up @@ -634,51 +641,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.
Expand Down
2 changes: 1 addition & 1 deletion lib/god/cli/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/god/socket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,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
Expand Down
22 changes: 22 additions & 0 deletions test/test_god.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/test_socket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down