You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by mf...@redhat.com on 2011/11/07 13:41:29 UTC

[PATCH core] Core: Added option to specify where Deltacloud API should log (-L mock.log)

From: Michal Fojtik <mf...@redhat.com>


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/bin/deltacloudd          |   24 ++++++++++++++----------
 server/config.ru                |    6 ++++++
 server/lib/deltacloud/server.rb |   12 +++++++-----
 3 files changed, 27 insertions(+), 15 deletions(-)

diff --git a/server/bin/deltacloudd b/server/bin/deltacloudd
index e8fca6e..289ed9a 100755
--- a/server/bin/deltacloudd
+++ b/server/bin/deltacloudd
@@ -46,14 +46,14 @@ deltacloudd -i <driver> [options]
 
 Options:
 BANNER
-  opts.on( '-i', '--driver DRIVER', 'Driver to use') do |driver|
+  opts.on( '-i', '--driver [DRIVER]', 'Driver to use') do |driver|
     ENV["API_DRIVER"] = driver
   end
-  opts.on( '-r', '--hostname HOSTNAME',
+  opts.on( '-r', '--hostname [HOSTNAME]',
            'Bind to HOST address (default: localhost)') do |host|
     ENV["API_HOST"] = host
   end
-  opts.on( '-p', '--port PORT', 'Use PORT (default: 3001)') do |port|
+  opts.on( '-p', '--port [PORT]', 'Use PORT (default: 3001)') do |port|
     ENV["API_PORT"] = port
   end
   opts.on( '-P', '--provider PROVIDER', 'Use PROVIDER (default is set in the driver)') do |provider|
@@ -65,17 +65,20 @@ BANNER
   opts.on( '-c', '--config [FILE]', 'Read provider and other config from FILE (default: ~/.deltacloud/config)') do |config|
     options[:config] = File::expand_path(config || DEFAULT_CONFIG)
   end
-  opts.on( '-e', '--env ENV', 'Environment (default: "development")') { |env| options[:env] = env }
+  opts.on( '-e', '--env [ENV]', 'Environment (default: "development")') { |env| options[:env] = env }
   opts.on( '-d', '--daemon', 'Run daemonized in the background, logging to SYSLOG (default: "disabled")') do
     options[:daemon] = true
   end
-  opts.on( '-u', '--user USER', 'User to run daemon as. Use with -d (default: "nobody")') { |user| options[:user] = user }
-  opts.on( '', '--pid PID', 'File to store PID (default: tmp/pids/thin.pid)') { |pid| options[:pid] = pid }
+  opts.on('-L', '--log [FILE]', 'Log everything to given file (default: "syslog or console"') do |f|
+    options[:log] = f.strip
+  end
+  opts.on( '-u', '--user [USER]', 'User to run daemon as. Use with -d (default: "nobody")') { |user| options[:user] = user }
+  opts.on( '-P', '--pid [PID]', 'File to store PID (default: tmp/pids/thin.pid)') { |pid| options[:pid] = pid }
   opts.on( '-l', '--drivers', 'List available drivers') { |env| options[:drivers] = true }
   opts.on( '-s', '--ssl', 'Enable SSL (default: disabled)') { |ssl| options[:ssl] = true }
-  opts.on( '-k', '--ssl-key KEY', 'SSL key file to use') { |key| options[:ssl_key] = key }
-  opts.on( '-C', '--ssl-cert CERT', 'SSL certificate file to use') { |cert| options[:ssl_cert] = cert }
-  opts.on( '-t', '--timeout TIMEOUT', 'Timeout for single request (default: 60)') do |timeout|
+  opts.on( '-k', '--ssl-key [KEY]', 'SSL key file to use') { |key| options[:ssl_key] = key }
+  opts.on( '-C', '--ssl-cert [CERT]', 'SSL certificate file to use') { |cert| options[:ssl_cert] = cert }
+  opts.on( '-t', '--timeout [TIMEOUT]', 'Timeout for single request (default: 60)') do |timeout|
     ENV["API_TIMEOUT"] = timeout
   end
   opts.on( '-V', '--verbose', 'Set verbose logging on') do |verbose|
@@ -127,6 +130,7 @@ if options[:drivers]
   exit(0)
 end
 
+ENV['DELTACLOUD_LOG'] = options[:log] if options[:log]
 
 if options[:ssl]
   unless options[:ssl_key]
@@ -161,7 +165,6 @@ unless options[:daemon]
     msg << ":: http://#{ENV["API_HOST"]}:#{ENV["API_PORT"]}/#{api_uri}"
   end
   puts msg
-  puts
 end
 
 if ENV['API_USER'] && ENV['API_PASSWORD']
@@ -216,6 +219,7 @@ else
     options[:env] = "production"
     argv_opts << [ "--daemonize", "--user", options[:user] || 'nobody', "--tag", "deltacloud-#{ENV['API_DRIVER']}"]
     argv_opts << [ "--pid", options[:pid]] if options[:pid]
+    argv_opts << ['--log', ENV['DELTACLOUD_LOG']] if ENV['DELTACLOUD_LOG']
   end
   argv_opts.flatten!
 
diff --git a/server/config.ru b/server/config.ru
index e3eb817..9d817ff 100644
--- a/server/config.ru
+++ b/server/config.ru
@@ -22,6 +22,12 @@ $:.unshift File.join($top_srcdir, 'lib')
 
 server_dir = ENV['API_FRONTEND'] == 'cimi' ? 'cimi' : 'deltacloud'
 
+if ENV['DELTACLOUD_LOG']
+  log = File.new(ENV['DELTACLOUD_LOG'], "a+")
+  $stdout.reopen(log)
+  $stderr.reopen(log)
+end
+
 load File.join($top_srcdir, 'lib', server_dir, 'server.rb')
 
 run Sinatra::Application
diff --git a/server/lib/deltacloud/server.rb b/server/lib/deltacloud/server.rb
index 2a5d481..6eb69fa 100644
--- a/server/lib/deltacloud/server.rb
+++ b/server/lib/deltacloud/server.rb
@@ -56,20 +56,22 @@ configure do
 end
 
 configure :production do
-  use Rack::SyslogLogger
+  # Log to syslog by default if in production, except user explicitely set
+  # a file where to log using -L
+  unless ENV['DELTACLOUD_LOG']
+    use Rack::SyslogLogger
+    $stdout = SyslogFile.new
+    $stderr = $stdout
+  end
   disable :logging
   enable :show_errors
   set :dump_errors, false
-  $stdout = SyslogFile.new
-  $stderr = $stdout
 end
 
 configure :development do
   # So we can just use puts for logging
   set :raise_errors => false
   set :show_exceptions, false
-  $stdout.sync = true
-  $stderr.sync = true
 end
 
 # You could use $API_HOST environment variable to change your hostname to
-- 
1.7.4.4