From 7992280fb3cc8081219fe834ebaa20a567a54a58 Mon Sep 17 00:00:00 2001 From: Peter L Date: Thu, 26 Jul 2018 13:50:15 +0200 Subject: [PATCH 1/3] Fixes #709. Add Standard- and StandardError log configuration for Systemd --- data/export/systemd/process.service.erb | 4 ++-- lib/foreman/export/systemd.rb | 8 ++++++++ spec/foreman/export/systemd_spec.rb | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/data/export/systemd/process.service.erb b/data/export/systemd/process.service.erb index 148cbe79..50214199 100644 --- a/data/export/systemd/process.service.erb +++ b/data/export/systemd/process.service.erb @@ -11,8 +11,8 @@ Environment="<%= var %>=<%= env %>" ExecStart=/bin/bash -lc 'exec <%= process.command %>' Restart=always StandardInput=null -StandardOutput=syslog -StandardError=syslog +StandardOutput=<%= log %> +StandardError=<%= error_log %> SyslogIdentifier=%n KillMode=mixed TimeoutStopSec=<%= engine.options[:timeout] %> diff --git a/lib/foreman/export/systemd.rb b/lib/foreman/export/systemd.rb index 0f828d55..85af68bb 100644 --- a/lib/foreman/export/systemd.rb +++ b/lib/foreman/export/systemd.rb @@ -37,4 +37,12 @@ def export write_template "systemd/master.target.erb", "#{app}.target", binding end + + def log + options[:log] || "syslog" + end + + def error_log + options[:error_log] || "syslog" + end end diff --git a/spec/foreman/export/systemd_spec.rb b/spec/foreman/export/systemd_spec.rb index 768137ab..86643933 100644 --- a/spec/foreman/export/systemd_spec.rb +++ b/spec/foreman/export/systemd_spec.rb @@ -66,6 +66,24 @@ expect(File.read("/tmp/init/app-alpha@.service")).to match(/^ExecStart=/) end + context "with a log setting" do + it "includes StandardOutput line with default value" do + systemd.export + expect(File.read("/tmp/init/app-alpha@.service")).to match("StandardOutput=syslog") + expect(File.read("/tmp/init/app-alpha@.service")).to match("StandardError=syslog") + end + + context "with custom options" do + let(:options) { { :log => "/var/log/app-alpha", :error_log => "/var/log/app-alpha_error" } } + + it "includes StandardOutput line with given value" do + systemd.export + expect(File.read("/tmp/init/app-alpha@.service")).to match("StandardOutput=/var/log/app-alpha") + expect(File.read("/tmp/init/app-alpha@.service")).to match("StandardError=/var/log/app-alpha_error") + end + end + end + context "with a formation" do let(:formation) { "alpha=2" } From c4b2bf2dee5169f1b1e83829afab2353b553ca81 Mon Sep 17 00:00:00 2001 From: Peter L Date: Mon, 13 Aug 2018 14:25:45 +0200 Subject: [PATCH 2/3] Fix --- Changelog.md | 4 ++++ lib/foreman/cli.rb | 1 + 2 files changed, 5 insertions(+) diff --git a/Changelog.md b/Changelog.md index 259b8b2b..9039ddb1 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,3 +1,7 @@ +## 0.86.0 (2018-08-13) + +* Add options for systemd logging [Lichtamberg] + ## 0.85.0 (2018-06-18) * updates rubies in travis - ensures only running with PID would be killed - updates dependencies; runs `codeclimate-test-reporter` after successful build - updates rubies for travis - squashes commits [LeFnord] diff --git a/lib/foreman/cli.rb b/lib/foreman/cli.rb index 49f0377d..9391f40d 100644 --- a/lib/foreman/cli.rb +++ b/lib/foreman/cli.rb @@ -46,6 +46,7 @@ def start(process=nil) method_option :app, :type => :string, :aliases => "-a" method_option :log, :type => :string, :aliases => "-l" + method_option :error_log, :type => :string, :aliases => "-el" method_option :run, :type => :string, :aliases => "-r", :desc => "Specify the pid file directory, defaults to /var/run/" method_option :env, :type => :string, :aliases => "-e", :desc => "Specify an environment file to load, defaults to .env" method_option :port, :type => :numeric, :aliases => "-p" From bee7c15f69f91d6b6616440ad312835c6931a2c3 Mon Sep 17 00:00:00 2001 From: Peter L Date: Wed, 12 Sep 2018 15:42:47 +0200 Subject: [PATCH 3/3] Fix file:/ and cli --- lib/foreman/cli.rb | 2 +- lib/foreman/export/systemd.rb | 4 ++-- spec/foreman/cli_spec.rb | 13 +++++++++++++ spec/foreman/export/systemd_spec.rb | 6 +++--- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/foreman/cli.rb b/lib/foreman/cli.rb index 9391f40d..52d62fb8 100644 --- a/lib/foreman/cli.rb +++ b/lib/foreman/cli.rb @@ -46,7 +46,7 @@ def start(process=nil) method_option :app, :type => :string, :aliases => "-a" method_option :log, :type => :string, :aliases => "-l" - method_option :error_log, :type => :string, :aliases => "-el" + method_option :error_log, :type => :string, :aliases => "-x" method_option :run, :type => :string, :aliases => "-r", :desc => "Specify the pid file directory, defaults to /var/run/" method_option :env, :type => :string, :aliases => "-e", :desc => "Specify an environment file to load, defaults to .env" method_option :port, :type => :numeric, :aliases => "-p" diff --git a/lib/foreman/export/systemd.rb b/lib/foreman/export/systemd.rb index 85af68bb..d293185f 100644 --- a/lib/foreman/export/systemd.rb +++ b/lib/foreman/export/systemd.rb @@ -39,10 +39,10 @@ def export end def log - options[:log] || "syslog" + options[:log].nil? ? "syslog" : "file:#{options[:log]}" end def error_log - options[:error_log] || "syslog" + options[:error_log].nil? ? "syslog" : "file:#{options[:error_log]}" end end diff --git a/spec/foreman/cli_spec.rb b/spec/foreman/cli_spec.rb index b8c3cfee..c92fe3fe 100644 --- a/spec/foreman/cli_spec.rb +++ b/spec/foreman/cli_spec.rb @@ -108,4 +108,17 @@ end end + describe "export" do + describe "with a valid Procfile" do + it "logs to specified file" do + without_fakefs do + output = foreman("export systemd -f #{resource_path("Procfile")} -l /var/log/foreman.log -x /var/log/foreman.error.log /tmp/foreman-systemd-export") + Dir.glob("/tmp/foreman-systemd-export/*.service").each do |file| + expect(File.read(file)).to match("StandardOutput=file:/var/log/foreman.log") + expect(File.read(file)).to match("StandardError=file:/var/log/foreman.error.log") + end + end + end + end + end end diff --git a/spec/foreman/export/systemd_spec.rb b/spec/foreman/export/systemd_spec.rb index 86643933..64140288 100644 --- a/spec/foreman/export/systemd_spec.rb +++ b/spec/foreman/export/systemd_spec.rb @@ -74,12 +74,12 @@ end context "with custom options" do - let(:options) { { :log => "/var/log/app-alpha", :error_log => "/var/log/app-alpha_error" } } + let(:options) { { :log => "/var/log/foreman.log", :error_log => "/var/log/foreman.error.log" } } it "includes StandardOutput line with given value" do systemd.export - expect(File.read("/tmp/init/app-alpha@.service")).to match("StandardOutput=/var/log/app-alpha") - expect(File.read("/tmp/init/app-alpha@.service")).to match("StandardError=/var/log/app-alpha_error") + expect(File.read("/tmp/init/app-alpha@.service")).to match("StandardOutput=file:/var/log/foreman.log") + expect(File.read("/tmp/init/app-alpha@.service")).to match("StandardError=file:/var/log/foreman.error.log") end end end