diff --git a/lib/launcher.rb b/lib/launcher.rb index 97eb62377..020968e7e 100644 --- a/lib/launcher.rb +++ b/lib/launcher.rb @@ -1,8 +1,8 @@ require 'proxy/log' -require 'proxy/sd_notify' require 'proxy/settings' require 'proxy/signal_handler' require 'proxy/log_buffer/trace_decorator' +require 'sd_notify' CIPHERS = ['ECDHE-RSA-AES128-GCM-SHA256', 'ECDHE-RSA-AES256-GCM-SHA384', 'AES128-GCM-SHA256', 'AES256-GCM-SHA384', 'AES128-SHA256', @@ -212,7 +212,7 @@ def install_webrick_callback!(*apps) def launched(apps) logger.info("Smart proxy has launched on #{apps.size} socket(s), waiting for requests") - Proxy::SdNotify.new.tap { |sd| sd.ready if sd.active? } + SdNotify.ready end end end diff --git a/lib/proxy/sd_notify.rb b/lib/proxy/sd_notify.rb deleted file mode 100644 index 7c267b509..000000000 --- a/lib/proxy/sd_notify.rb +++ /dev/null @@ -1,30 +0,0 @@ -require 'socket' - -# Implementation of libsystemd's sd_notify API, sends current state via socket -module Proxy - class SdNotify - def active? - !ENV['NOTIFY_SOCKET'].nil? - end - - def notify(message) - create_socket.tap do |socket| - socket.sendmsg(message.chomp + "\n") # ensure trailing \n - socket.close - end - end - - def ready(state = 1) - notify("READY=#{state}") - end - - private - - def create_socket - raise 'Missing NOTIFY_SOCKET environment variable, is this process running under systemd?' unless active? - Socket.new(Socket::AF_UNIX, Socket::SOCK_DGRAM, 0).tap do |socket| - socket.connect(Socket.pack_sockaddr_un(ENV['NOTIFY_SOCKET'])) - end - end - end -end diff --git a/smart_proxy.gemspec b/smart_proxy.gemspec index e8dddf921..c7df536c3 100644 --- a/smart_proxy.gemspec +++ b/smart_proxy.gemspec @@ -16,6 +16,7 @@ Gem::Specification.new do |s| s.add_dependency 'json' s.add_dependency 'logging' s.add_dependency 'rack', '>= 1.3' + s.add_dependency 'sd_notify', '~> 0.1' s.add_dependency 'sinatra' s.description = <<~EOF Foreman Proxy is used via The Foreman Project, it allows Foreman to manage diff --git a/test/launcher_test.rb b/test/launcher_test.rb index 8c1bff25e..ef9cc7621 100644 --- a/test/launcher_test.rb +++ b/test/launcher_test.rb @@ -52,19 +52,7 @@ def test_install_webrick_callback def test_launched_with_sdnotify @launcher.logger.expects(:info).with(includes('2 socket(s)')) - sd_notify = mock('SdNotify') - sd_notify.expects(:active?).returns(true) - sd_notify.expects(:ready) - Proxy::SdNotify.expects(:new).returns(sd_notify) - @launcher.launched([:app1, :app2]) - end - - def test_launched_with_sdnotify_inactive - @launcher.logger.expects(:info).with(includes('2 socket(s)')) - sd_notify = mock('SdNotify') - sd_notify.expects(:active?).returns(false) - sd_notify.expects(:ready).never - Proxy::SdNotify.expects(:new).returns(sd_notify) + ::SdNotify.expects(:ready) @launcher.launched([:app1, :app2]) end end diff --git a/test/sd_notify_test.rb b/test/sd_notify_test.rb deleted file mode 100644 index 57cad6837..000000000 --- a/test/sd_notify_test.rb +++ /dev/null @@ -1,58 +0,0 @@ -require 'test_helper' -require 'proxy/sd_notify' - -class SdNotifyTest < Test::Unit::TestCase - def test_active_with_notify_socket - with_notify_socket('/mock/systemd/notify') do - assert Proxy::SdNotify.new.active? - end - end - - def test_active_without_notify_socket - with_notify_socket(nil) do - refute Proxy::SdNotify.new.active? - end - end - - def test_notify - assert_equal("TEST=42\n", with_test_socket { Proxy::SdNotify.new.notify('TEST=42') }) - end - - def test_notify_when_inactive - assert_raises(RuntimeError) { with_notify_socket(nil) { Proxy::SdNotify.new.notify('TEST=42') } } - end - - def test_ready - assert_equal("READY=1\n", with_test_socket { Proxy::SdNotify.new.ready }) - end - - private - - def with_notify_socket(socket) - old_socket = ENV.delete('NOTIFY_SOCKET') - begin - ENV['NOTIFY_SOCKET'] = socket unless socket.nil? - yield - ensure - if old_socket.nil? - ENV.delete('NOTIFY_SOCKET') - else - ENV['NOTIFY_SOCKET'] = old_socket - end - end - end - - def with_test_socket(&block) - socket_path = File.expand_path('tmp/systemd.socket', __dir__) - File.delete(socket_path) if File.exist?(socket_path) - - socket = Socket.new(Socket::AF_UNIX, Socket::SOCK_DGRAM, 0) - begin - socket.bind(Socket.pack_sockaddr_un(socket_path)) - with_notify_socket(socket_path, &block) - socket.recv_nonblock(256) - ensure - socket.close - end - end -end