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

Make knife verbose if logging level is DEBUG or INFO #104

Merged
merged 1 commit into from
Aug 21, 2019
Merged
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
46 changes: 31 additions & 15 deletions lib/between_meals/knife.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ def initialize(opts = {})
@config = opts[:config] ||
"#{@home}/.chef/knife-#{@user}-taste-tester.rb"
@knife = opts[:bin] || 'knife'
@knife_verb_option = ''
unless @logger.nil?
if @logger.level == Logger::DEBUG
@knife_verb_option = '-VV'
elsif @logger.level == Logger::INFO
ifel marked this conversation as resolved.
Show resolved Hide resolved
@knife_verb_option = '-V'
end
end
@berks = opts[:berks_bin] || 'berks'
@berks_config = opts[:berks_config]
@pem = opts[:pem] ||
Expand All @@ -55,7 +63,8 @@ def initialize(opts = {})
def role_upload_all
if File.exists?(@role_dir)
roles = File.join(@role_dir, "*.#{@role_type}")
exec!("#{@knife} role from file #{roles} -c #{@config}", @logger)
exec!("#{@knife} role from file #{roles} #{@knife_verb_option} " +
ifel marked this conversation as resolved.
Show resolved Hide resolved
"-c #{@config}", @logger)
end
end

Expand All @@ -64,22 +73,23 @@ def role_upload(roles)
roles = roles.map do |x|
File.join(@role_dir, "#{x.name}.#{@role_type}")
end.join(' ')
exec!("#{@knife} role from file #{roles} -c #{@config}", @logger)
exec!("#{@knife} role from file #{roles} #{@knife_verb_option} " +
"-c #{@config}", @logger)
end
end

def role_delete(roles)
if roles.any?
roles.each do |role|
exec!(
"#{@knife} role delete #{role.name} --yes -c #{@config}", @logger
)
exec!("#{@knife} role delete #{role.name} #{@knife_verb_option} " +
"--yes -c #{@config}", @logger)
end
end
end

def cookbook_upload_all
exec!("#{@knife} cookbook upload -a -c #{@config}", @logger)
exec!("#{@knife} cookbook upload -a #{@knife_verb_option} " +
"-c #{@config}", @logger)
end

def berks_cookbook_upload_all
Expand All @@ -99,7 +109,8 @@ def berks_cookbook_upload_all
def cookbook_upload(cookbooks)
if cookbooks.any?
cookbooks = cookbooks.map(&:name).join(' ')
exec!("#{@knife} cookbook upload #{cookbooks} -c #{@config}", @logger)
exec!("#{@knife} cookbook upload #{cookbooks} #{@knife_verb_option} " +
"-c #{@config}", @logger)
end
end

Expand All @@ -126,7 +137,8 @@ def cookbook_delete(cookbooks)
if cookbooks.any?
cookbooks.each do |cookbook|
exec!("#{@knife} cookbook delete #{cookbook.name}" +
" --purge -a --yes -c #{@config}", @logger)
" --purge -a --yes #{@knife_verb_option} -c #{@config}",
@logger)
end
end
end
Expand All @@ -149,7 +161,8 @@ def databag_upload(databags)
File.join(@databag_dir, dbname, "#{x.item}.json")
end.join(' ')
exec!(
"#{@knife} data bag from file #{dbname} #{dbitems} -c #{@config}",
"#{@knife} data bag from file #{dbname} #{dbitems} " +
"#{@knife_verb_option} -c #{@config}",
@logger,
)
end
Expand All @@ -161,7 +174,7 @@ def databag_delete(databags)
databags.group_by(&:name).each do |dbname, dbs|
dbs.each do |db|
exec!("#{@knife} data bag delete #{dbname} #{db.item}" +
" --yes -c #{@config}", @logger)
" --yes #{@knife_verb_option} -c #{@config}", @logger)
ifel marked this conversation as resolved.
Show resolved Hide resolved
end
delete_databag_if_empty(dbname)
end
Expand Down Expand Up @@ -242,22 +255,25 @@ def write_user_config

def create_databag_if_missing(databag)
s = Mixlib::ShellOut.new("#{@knife} data bag list" +
" --format json -c #{@config}").run_command
" --format json #{@knife_verb_option} " +
"-c #{@config}").run_command
s.error!
db = JSON.parse(s.stdout)
unless db.include?(databag)
exec!("#{@knife} data bag create #{databag} -c #{@config}", @logger)
exec!("#{@knife} data bag create #{databag} #{@knife_verb_option} " +
"-c #{@config}", @logger)
end
end

def delete_databag_if_empty(databag)
s = Mixlib::ShellOut.new("#{@knife} data bag show #{databag}" +
" --format json -c #{@config}").run_command
" --format json #{@knife_verb_option} " +
"-c #{@config}").run_command
s.error!
db = JSON.parse(s.stdout)
if db.empty?
exec!("#{@knife} data bag delete #{databag} --yes -c #{@config}",
@logger)
exec!("#{@knife} data bag delete #{databag} --yes " +
"#{@knife_verb_option} -c #{@config}", @logger)
end
end
end
Expand Down