Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rework log4r configuration #108

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ require 'test/unit'
class TestPuppetRun < Test::Unit::TestCase

def setup
@ppm = Rouster.new(:name => 'ppm', :verbose => 4)
@ppm = Rouster.new(:name => 'ppm')
@app = Rouster.new(:name => 'app')
end

Expand Down
3 changes: 0 additions & 3 deletions examples/aws.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
:instance => 'your-instance-id',
:key => sprintf('%s/.ssh/id_rsa-aws', ENV['HOME'])
},
:verbosity => 1,
)

a = aws_already_running.run('ls -l /etc/hosts; who')
Expand Down Expand Up @@ -40,7 +39,6 @@
:secret_key => ENV['AWS_SECRET_ACCESS_KEY'],
},
:sshtunnel => false,
:verbosity => 1,
)

p "up(): #{aws.up}"
Expand All @@ -52,7 +50,6 @@
:key => sprintf('%s/.ssh/id_rsa-aws', ENV['HOME']),
:instance => aws.aws_get_instance,
},
:verbosity => 1,
)

[ aws, aws_already_running, aws_clone ].each do |a|
Expand Down
8 changes: 4 additions & 4 deletions examples/bootstrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
require 'rouster/puppet'
require 'rouster/tests'

app = Rouster.new(:name => 'app', :verbosity => 4, :sudo => true)
ppm = Rouster.new(:name => 'ppm', :verbosity => 1, :sudo => true)
app = Rouster.new(:name => 'app', :sudo => true)
ppm = Rouster.new(:name => 'ppm', :sudo => true)

# passthrough boxes do not need to specify a name
# commented out currently because passthrough is not MVP
#lpt = Rouster.new(:passthrough => 'local', :verbosity => 4)
#rpt = Rouster.new(:passthrough => 'remote', :verbosity => 4, :sshkey => '~/.ssh/id_dsa')
#lpt = Rouster.new(:passthrough => 'local')
#rpt = Rouster.new(:passthrough => 'remote', :sshkey => '~/.ssh/id_dsa')

workers = [app]

Expand Down
2 changes: 1 addition & 1 deletion examples/demo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'rouster'
require 'rouster/tests'

@app = Rouster.new(:name => 'app', :sudo => true, :verbosity => 3)
@app = Rouster.new(:name => 'app', :sudo => true)
@app.up()

# get list of packages
Expand Down
4 changes: 2 additions & 2 deletions examples/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'rouster'

begin
badkey = Rouster.new(:name => 'whatever', :verbosity => 4, :sshkey => __FILE__)
badkey = Rouster.new(:name => 'whatever', :sshkey => __FILE__)
rescue Rouster::InternalError => e
p "caught #{e.class}: #{e.message}"
rescue => e
Expand All @@ -22,7 +22,7 @@
p badvagrantfile

begin
good = Rouster.new(:name => 'app', verbosity => 4)
good = Rouster.new(:name => 'app')
rescue => e
p "caught unexpected exception #{e.class}: #{e.message}"
end
Expand Down
2 changes: 0 additions & 2 deletions examples/openstack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
:key => '/path/to/ssh_keys.pem', # SSH key filename
},
:sshtunnel => false,
:verbosity => 1,
)

p "UP(): #{ostack.up}"
Expand All @@ -41,7 +40,6 @@
:instance => ostack.ostack_get_instance_id, # ID of a running OpenStack instance.
},
:sshtunnel => false,
:verbosity => 1,
)

[ ostack, ostack_copy ].each do |o|
Expand Down
6 changes: 0 additions & 6 deletions examples/passthrough.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@
require 'rouster/puppet'
require 'rouster/tests'

verbosity = ENV['VERBOSE'].nil? ? 4 : 0

# .inspect of this is blank for sshkey and status, looks ugly, but is ~accurate.. fix this?
local = Rouster.new(
:name => 'local',
:sudo => false,
:passthrough => { :type => :local },
:verbosity => verbosity,
)

remote = Rouster.new(
Expand All @@ -23,20 +20,17 @@
:user => ENV['USER'],
:key => sprintf('%s/.ssh/id_dsa', ENV['HOME']),
},
:verbosity => verbosity,
)

sudo = Rouster.new(
:name => 'sudo',
:sudo => true,
:passthrough => { :type => :local },
:verbosity => verbosity,
)

vagrant = Rouster.new(
:name => 'ppm',
:sudo => true,
:verbosity => verbosity,
)

workers = [ local, remote, vagrant ]
Expand Down
46 changes: 8 additions & 38 deletions lib/rouster.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'rubygems'
require 'log4r'
require 'log4r/yamlconfigurator'
require 'json'
require 'net/scp'
require 'net/ssh'
Expand Down Expand Up @@ -42,10 +43,8 @@ class SSHConnectionError < StandardError; end # thrown by available_via_ssh()
# * [vagrantfile] - the full or relative path to the Vagrantfile to use, if not specified, will look for one in 5 directories above current location
# * [vagrant_concurrency] - boolean controlling whether Rouster will attempt to run `vagrant *` if another vagrant process is already running, default is false
# * [vagrant_reboot] - particularly sticky systems restart better if Vagrant does it for us, default is false
# * [verbosity] - an integer representing console level logging, or an array of integers representing console,file level logging - DEBUG (0) < INFO (1) < WARN (2) < ERROR (3) < FATAL (4)
def initialize(opts = nil)
@cache_timeout = opts[:cache_timeout].nil? ? false : opts[:cache_timeout]
@logfile = opts[:logfile].nil? ? false : opts[:logfile]
@name = opts[:name]
@passthrough = opts[:passthrough].nil? ? false : opts[:passthrough]
@retries = opts[:retries].nil? ? 0 : opts[:retries]
Expand All @@ -56,28 +55,6 @@ def initialize(opts = nil)
@vagrant_concurrency = opts[:vagrant_concurrency].nil? ? false : opts[:vagrant_concurrency]
@vagrant_reboot = opts[:vagrant_reboot].nil? ? false : opts[:vagrant_reboot]

# TODO kind of want to invert this, 0 = trace, 1 = debug, 2 = info, 3 = warning, 4 = error
# could do `fixed_ordering = [4, 3, 2, 1, 0]` and use user input as index instead, so an input of 4 (which should be more verbose), yields 0
if opts[:verbosity]
# TODO decide how to handle this case -- currently #2 is implemented
# - option 1, if passed a single integer, use that level for both loggers
# - option 2, if passed a single integer, use that level for stdout, and a hardcoded level (probably INFO) to logfile

# kind of want to do if opts[:verbosity].respond_to?(:[]), but for 1.87 compatability, going this way..
if ! opts[:verbosity].is_a?(Array) or opts[:verbosity].is_a?(Integer)
@verbosity_console = opts[:verbosity].to_i
@verbosity_logfile = 2
elsif opts[:verbosity].is_a?(Array)
# TODO more error checking here when we are sure this is the right way to go
@verbosity_console = opts[:verbosity][0].to_i
@verbosity_logfile = opts[:verbosity][1].to_i
@logfile = true if @logfile.eql?(false) # overriding the default setting
end
else
@verbosity_console = 3
@verbosity_logfile = 2 # this is kind of arbitrary, but won't actually be created unless opts[:logfile] is also passed
end

@ostype = nil
@osversion = nil

Expand All @@ -90,20 +67,14 @@ def initialize(opts = nil)
@ssh_info = nil # hash containing connection information

# set up logging
require 'log4r/config'
Log4r.define_levels(*Log4r::Log4rConfig::LogLevels)

@logger = Log4r::Logger.new(sprintf('rouster:%s', @name))
@logger.outputters << Log4r::Outputter.stderr
#@log.outputters << Log4r::Outputter.stdout

if @logfile
@logfile = @logfile.eql?(true) ? sprintf('/tmp/rouster-%s.%s.%s.log', @name, Time.now.to_i, $$) : @logfile
@logger.outputters << Log4r::FileOutputter.new(sprintf('rouster:%s', @name), :filename => @logfile, :level => @verbosity_logfile)
Log4r::YamlConfigurator.load_yaml_file(File.expand_path(sprintf('%s/../log4r.yaml', File.dirname(__FILE__))))
@logger = Log4r::Logger.get('rouster')
begin
@logger.instance_variable_set(:@fullname, sprintf('rouster:%s', @name)) # want to use the same configs for everyone, but want to distinguish between VMs
rescue => e
@logger.error(sprintf('unadvised use of instance variables failed[%s], please file an issue', e.message))
end

@logger.outputters[0].level = @verbosity_console # can't set this when instantiating a .std* logger, and want the FileOutputter at a different level

if opts.has_key?(:sudo)
@sudo = opts[:sudo]
elsif @passthrough.class.eql?(Hash)
Expand Down Expand Up @@ -280,8 +251,7 @@ def inspect
sshkey[#{@sshkey}],
status[#{s}],
sudo[#{@sudo}],
vagrantfile[#{@vagrantfile}],
verbosity console[#{@verbosity_console}] / log[#{@verbosity_logfile} - #{@logfile}]\n"
vagrantfile[#{@vagrantfile}]"
end

## internal methods
Expand Down
22 changes: 22 additions & 0 deletions log4r.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
log4r_config:
loggers:
- name: 'rouster'
outputters:
- stdout
- stderr
outputters:
- type: StdoutOutputter
level: INFO
name: stdout
formatter:
type: PatternFormatter
date_pattern: '%Y/%m/%d %H:%M.%S'
pattern: '%d | %C | %l | %m'
- type: StderrOutputter
level: ERROR
name: stderr
formatter:
type: PatternFormatter
date_pattern: '%Y/%m/%d %H:%M.%S'
pattern: '%d | %C | %l | %m'
8 changes: 4 additions & 4 deletions test/basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
require 'rouster/testing'
require 'rouster/tests'

#a = Rouster.new(:name => 'app', :verbosity => 1, :vagrantfile => '../piab/Vagrantfile', :retries => 3)
#p = Rouster.new(:name => 'ppm', :verbosity => 1, :vagrantfile => '../piab/Vagrantfile')
#r = Rouster.new(:name => 'app', :verbosity => 1, :vagrantfile => 'Vagrantfile')
l = Rouster.new(:name => 'local', :passthrough => { :type => :local }, :verbosity => 3)
#a = Rouster.new(:name => 'app', :vagrantfile => '../piab/Vagrantfile', :retries => 3)
#p = Rouster.new(:name => 'ppm', :vagrantfile => '../piab/Vagrantfile')
#r = Rouster.new(:name => 'app', :vagrantfile => 'Vagrantfile')
l = Rouster.new(:name => 'local', :passthrough => { :type => :local })

p 'DBGZ' if nil?
1 change: 0 additions & 1 deletion test/functional/test_inspect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def test_happy_path
assert_match(/status/, res)
assert_match(/sudo\[true\]/, res)
assert_match(/vagrantfile/, res)
assert_match(/verbosity console\[3\] \/ log\[2/, res)
end

def teardown
Expand Down
40 changes: 14 additions & 26 deletions test/functional/test_new.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ def test_defaults
assert_equal(true, @app.instance_variable_get(:@sshtunnel))
assert_equal(false, @app.instance_variable_get(:@unittest))
assert_equal(false, @app.instance_variable_get(:@vagrant_concurrency))
assert_equal(3, @app.instance_variable_get(:@verbosity_console))
assert_equal(2, @app.instance_variable_get(:@verbosity_logfile))

end

def test_good_openssh_tunnel
Expand All @@ -66,7 +63,6 @@ def test_good_advanced_instantiation
:name => 'app',
:passthrough => false,
:sudo => false,
:verbosity => [4,0],
#:vagrantfile => traverse_up(Dir.pwd, 'Vagrantfile'), # this is what happens anyway..
:sshkey => ENV['VAGRANT_HOME'].nil? ? sprintf('%s/.vagrant.d/insecure_private_key', ENV['HOME']) : sprintf('%s/insecure_private_key', ENV['VAGRANT_HOME']),
:cache_timeout => 10,
Expand All @@ -78,8 +74,6 @@ def test_good_advanced_instantiation
assert_equal('app', @app.name)
assert_equal(false, @app.is_passthrough?())
assert_equal(false, @app.uses_sudo?())
assert_equal(4, @app.instance_variable_get(:@verbosity_console))
assert_equal(0, @app.instance_variable_get(:@verbosity_logfile))
assert_equal(true, File.file?(@app.vagrantfile))
assert_equal(true, File.file?(@app.sshkey))
assert_equal(10, @app.cache_timeout)
Expand Down Expand Up @@ -130,7 +124,7 @@ def test_bad_sshkey_instantiation
def test_good_local_passthrough

assert_nothing_raised do
@app = Rouster.new(:name => 'local', :passthrough => { :type => :local }, :verbosity => 4)
@app = Rouster.new(:name => 'local', :passthrough => { :type => :local })
end

assert_equal('local', @app.name)
Expand Down Expand Up @@ -159,7 +153,6 @@ def test_good_remote_passthrough
:key => @@user_sshkey,
:paranoid => false,
},
:verbosity => 4,
)
end

Expand All @@ -180,16 +173,15 @@ def test_paranoia_remote_passthrough

assert_nothing_raised do
@app = Rouster.new(
:name => 'remote',
:sudo => false,
:passthrough => {
:type => :remote,
:host => host,
:user => ENV['USER'],
:key => @@user_sshkey,
:paranoid => :very,
},
:verbosity => 4,
:name => 'remote',
:sudo => false,
:passthrough => {
:type => :remote,
:host => host,
:user => ENV['USER'],
:key => @@user_sshkey,
:paranoid => :very,
},
)
end

Expand All @@ -207,19 +199,19 @@ def test_invalid_passthrough
# missing required parameters

assert_raise Rouster::ArgumentError do
@app = Rouster.new(:name => 'fizzy', :passthrough => {}, :verbosity => 4)
@app = Rouster.new(:name => 'fizzy', :passthrough => {})
end

assert_raise Rouster::ArgumentError do
@app = Rouster.new(:name => 'fizzy', :passthrough => { :type => 'invalid' }, :verbosity => 4)
@app = Rouster.new(:name => 'fizzy', :passthrough => { :type => 'invalid' })
end

assert_raise Rouster::ArgumentError do
@app = Rouster.new(:name => 'fizzy', :passthrough => { :type => :remote }, :verbosity => 4)
@app = Rouster.new(:name => 'fizzy', :passthrough => { :type => :remote })
end

assert_raise Rouster::ArgumentError do
@app = Rouster.new(:name => 'fizzy', :passthrough => { :type => :remote, :user => 'foo' }, :verbosity => 4)
@app = Rouster.new(:name => 'fizzy', :passthrough => { :type => :remote, :user => 'foo' })
end

end
Expand All @@ -236,7 +228,6 @@ def test_bad_passthrough
:user => 'foo',
:host => 'bar',
},
:verbosity => 4,
)
end

Expand All @@ -250,7 +241,6 @@ def test_bad_passthrough
:user => 'foo',
:host => 'bar',
},
:verbosity => 4,
)
end

Expand All @@ -270,7 +260,6 @@ def test_bad_passthrough
},

:sshtunnel => true,
:verbosity => 4,
)
end

Expand All @@ -289,7 +278,6 @@ def test_bad_passthrough
},

:sshtunnel => true,
:verbosity => 4,
)
end

Expand Down
1 change: 0 additions & 1 deletion test/functional/test_passthroughs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ def test_functional_remote_passthrough
:user => ENV['USER'],
:key => @@user_sshkey,
},
:verbosity => 4,
)
end

Expand Down
Loading