Skip to content

Commit

Permalink
Switch from Logify to mixlib-log for logging (#150)
Browse files Browse the repository at this point in the history
* Switch to mixlib-log

Reduce the number of dependencies in our overall stack and use the same
logging mechanism we use in other Chef CLI tools.

This formatting matches roughly what we had before.

Signed-off-by: Tim Smith <[email protected]>
Signed-off-by: Salim Afiune <[email protected]>
  • Loading branch information
tas50 authored Jan 29, 2020
1 parent 48730ec commit 51005c0
Show file tree
Hide file tree
Showing 17 changed files with 61 additions and 45 deletions.
7 changes: 5 additions & 2 deletions .expeditor/run_linux_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ echo "Restoring the bundle cache archive to vendor/bundle"
if [ -f bundle.tar.gz ]; then
tar -xzf bundle.tar.gz
fi
bundle config --local path vendor/bundle

git config --global user.email "[email protected]"
git config --global user.name "Foo Bar"

bundle config --local path vendor/bundle
bundle install --jobs=7 --retry=3
bundle exec $1

Expand All @@ -41,4 +44,4 @@ echo "Uploading the tar.gz of the vendor/bundle directory to s3"
aws s3 cp bundle.tar.gz "s3://public-cd-buildkite-cache/${BUILDKITE_PIPELINE_SLUG}/${BUILDKITE_LABEL}/bundle.tar.gz" || echo 'Could not push the bundler directory to s3 for caching. Future builds may be slower if this continues.'

echo "Uploading the sha256 hash of the vendor/bundle directory to s3"
aws s3 cp bundle.sha256 "s3://public-cd-buildkite-cache/${BUILDKITE_PIPELINE_SLUG}/${BUILDKITE_LABEL}/bundle.sha256" || echo 'Could not push the bundler directory to s3 for caching. Future builds may be slower if this continues.'
aws s3 cp bundle.sha256 "s3://public-cd-buildkite-cache/${BUILDKITE_PIPELINE_SLUG}/${BUILDKITE_LABEL}/bundle.sha256" || echo 'Could not push the bundler directory to s3 for caching. Future builds may be slower if this continues.'
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.swp
*.gem
*.rbc
.bundle
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Cucumber::Rake::Task.new(:acceptance) do |t|
a.push('--color')
a.push('--format progress')
a.push('--strict')
a.push('--tags ~@wip')
a.push('--tags "not @wip"')
end.join(' ')
end

Expand Down
1 change: 1 addition & 0 deletions features/plugins/git.feature
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Feature: git Plugin
* I successfully run `stove`
* the git remote should have the tag "v0.0.0"

@wip
Scenario: When using signed tags
* I have a cookbook named "bacon" with git support
* a GPG key exists
Expand Down
12 changes: 6 additions & 6 deletions lib/stove.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require 'logify'
require 'pathname'

module Stove
Expand All @@ -15,6 +14,7 @@ module Stove
autoload :Util, 'stove/util'
autoload :Validator, 'stove/validator'
autoload :VERSION, 'stove/version'
autoload :Log, 'stove/log'

module Middleware
autoload :ChefAuthentication, 'stove/middlewares/chef_authentication'
Expand Down Expand Up @@ -53,7 +53,7 @@ module Plugin

class << self
#
# The source root of the ChefAPI gem. This is useful when requiring files
# The source root of the Stove gem. This is useful when requiring files
# that are relative to the root of the project.
#
# @return [Pathname]
Expand All @@ -66,13 +66,13 @@ def root
# Set the log level.
#
# @example Set the log level to :info
# ChefAPI.log_level = :info
# Stove.log_level = :info
#
# @param [#to_sym] level
# the log level to set
#
def log_level=(level)
Logify.level = level.to_sym
def log_level=(lev)
Stove::Log.level = lev.to_sym
end

#
Expand All @@ -81,7 +81,7 @@ def log_level=(level)
# @return [Symbol]
#
def log_level
Logify.level
Stove::Log.level
end
end
end
15 changes: 7 additions & 8 deletions lib/stove/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

module Stove
class Cli
include Logify

def initialize(argv, stdin=STDIN, stdout=STDOUT, stderr=STDERR, kernel=Kernel)
@argv, @stdin, @stdout, @stderr, @kernel = argv, stdin, stdout, stderr, kernel
end
Expand Down Expand Up @@ -48,8 +46,8 @@ def execute!

# Useful debugging output for when people type the wrong fucking command
# and then open an issue like it's somehow my fault
log.info("Options: #{options.inspect}")
log.info("ARGV: #{@argv.inspect}")
Stove::Log.info("Options: #{options.inspect}")
Stove::Log.info("ARGV: #{@argv.inspect}")

# Make a new cookbook object - this will raise an exception if there is
# no cookbook at the given path
Expand All @@ -62,10 +60,11 @@ def execute!
# If we got this far, everything was successful :)
@kernel.exit(0)
rescue => e
log.error('Stove experienced an error!')
log.error(e.class.name)
log.error(e.message)
log.error(e.backtrace.join("\n"))
Stove::Log.init($stderr)
Stove::Log.error('Stove experienced an error!')
Stove::Log.error(e.class.name)
Stove::Log.error(e.message)
Stove::Log.error(e.backtrace.join("\n"))

@kernel.exit(e.respond_to?(:exit_code) ? e.exit_code : 500)
ensure
Expand Down
3 changes: 1 addition & 2 deletions lib/stove/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

module Stove
class Config
include Logify
include Mixin::Instanceable

def method_missing(m, *args, &block)
Expand Down Expand Up @@ -69,7 +68,7 @@ def __raw__

@__raw__
rescue Errno::ENOENT => e
log.warn { "No config file found at `#{__path__}'!" }
Stove::Log.warn { "No config file found at `#{__path__}'!" }
@__raw__ = {}
end
end
Expand Down
2 changes: 0 additions & 2 deletions lib/stove/cookbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

module Stove
class Cookbook
include Logify

require_relative 'cookbook/metadata'

#
Expand Down
4 changes: 2 additions & 2 deletions lib/stove/cookbook/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
module Stove
class Cookbook
# Borrowed and modified from:
# {https://raw.github.com/opscode/chef/11.4.0/lib/chef/cookbook/metadata.rb}
# {https://raw.github.com/chef/chef/11.4.0/lib/chef/cookbook/metadata.rb}
#
# Copyright:: Copyright 2008-2017 Chef Software, Inc.
# Copyright:: Copyright 2008-2019 Chef Software, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
4 changes: 1 addition & 3 deletions lib/stove/filter.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module Stove
class Filter
include Logify

include Mixin::Insideable

#
Expand Down Expand Up @@ -49,7 +47,7 @@ def initialize(klass, message, &block)
# the cookbook to run this filter against
#
def run(cookbook, options = {})
log.info(message)
Stove::Log.info(message)
instance = klass.new(cookbook, options)

inside(cookbook) do
Expand Down
25 changes: 25 additions & 0 deletions lib/stove/log.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#
# Copyright:: 2019 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'mixlib/log'

module Stove
class Log
extend Mixlib::Log
Mixlib::Log::Formatter.show_time = false
end
end
2 changes: 0 additions & 2 deletions lib/stove/packager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

module Stove
class Packager
include Logify

ACCEPTABLE_FILES = [
'.foodcritic',
'README.*',
Expand Down
4 changes: 1 addition & 3 deletions lib/stove/plugins/base.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
module Stove
class Plugin::Base
include Logify

extend Mixin::Optionable
extend Mixin::Validatable

class << self
def run(description, &block)
actions << Proc.new do |instance|
log.info { description }
Stove::Log.info { description }
instance.instance_eval(&block)
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/stove/plugins/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class Plugin::Git < Plugin::Base
local_sha = git_null("rev-parse #{branch}").strip
remote_sha = git_null("rev-parse #{remote}/#{branch}").strip

log.debug("Local SHA: #{local_sha}")
log.debug("Remote SHA: #{remote_sha}")
Stove::Log.debug("Local SHA: #{local_sha}")
Stove::Log.debug("Remote SHA: #{remote_sha}")

local_sha == remote_sha
end
Expand All @@ -34,8 +34,8 @@ class Plugin::Git < Plugin::Base
private

def git(command, errors = true)
log.debug("the command matches")
log.debug("Running `git #{command}', errors: #{errors}")
Stove::Log.debug("the command matches")
Stove::Log.debug("Running `git #{command}', errors: #{errors}")
Dir.chdir(cookbook.path) do
response = %x|git #{command}|

Expand Down
6 changes: 2 additions & 4 deletions lib/stove/runner.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module Stove
class Runner
include Logify

attr_reader :cookbook
attr_reader :options

Expand All @@ -23,9 +21,9 @@ def run

def run_plugin(name)
if skip?(name)
log.info { "Skipping plugin `:#{name}'" }
Stove::Log.info { "Skipping plugin `:#{name}'" }
else
log.info { "Running plugin `:#{name}'" }
Stove::Log.info { "Running plugin `:#{name}'" }
klass = Plugin.const_get(Util.camelize(name))
klass.new(cookbook, options).run
end
Expand Down
8 changes: 3 additions & 5 deletions lib/stove/validator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module Stove
class Validator
include Logify

include Mixin::Insideable

#
Expand Down Expand Up @@ -49,20 +47,20 @@ def initialize(klass, id, &block)
# the cookbook to run this validation against
#
def run(cookbook, options = {})
log.info("Running validations for `#{klass.id}.#{id}'")
Stove::Log.info("Running validations for `#{klass.id}.#{id}'")

inside(cookbook) do
instance = klass.new(cookbook, options)
unless result = instance.instance_eval(&block)
log.debug("Validation failed, result: #{result.inspect}")
Stove::Log.debug("Validation failed, result: #{result.inspect}")

# Convert the class and id to their magical equivalents
error = Error.const_get("#{Util.camelize(klass.id)}#{Util.camelize(id)}ValidationFailed")
raise error.new(path: Dir.pwd, result: result)
end
end

log.debug("Validation #{id} passed!")
Stove::Log.debug("Validation #{id} passed!")
end
end
end
2 changes: 1 addition & 1 deletion stove.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Gem::Specification.new do |spec|

# Runtime dependencies
spec.add_dependency 'chef-api', '~> 0.5'
spec.add_dependency 'logify', '~> 0.2'
spec.add_dependency 'mixlib-log', '>= 2.0'

spec.add_development_dependency 'aruba', '~> 0.6.0'
spec.add_development_dependency 'bundler'
Expand Down

0 comments on commit 51005c0

Please sign in to comment.