Class: Spaux::Chef::Knife

Inherits:
Chef::Application::Knife
  • Object
show all
Defined in:
lib/spaux/chef/knife.rb

Constant Summary collapse

DEFAULT_KNIFE_CONFIG =
{
  config_file: {
    flags: %w(--config -c),
    value: ::File.join('@work_dir', 'knife.rb')
  }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(work_dir, args) ⇒ Knife

Returns a new instance of Knife.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/spaux/chef/knife.rb', line 19

def initialize(work_dir, args)
  @work_dir = work_dir
  @args = args

  DEFAULT_KNIFE_CONFIG.each do |_,v|
    v[:value].is_a?(String) && v[:value].gsub!(/@work_dir/, @work_dir)
  end

  cf_flags = DEFAULT_KNIFE_CONFIG[:config_file][:flags]
  unless @args.include?(cf_flags.first) || @args.include?(cf_flags.last)
    @args << DEFAULT_KNIFE_CONFIG[:config_file][:flags].first
    @args << DEFAULT_KNIFE_CONFIG[:config_file][:value]
  end

  config_file = DEFAULT_KNIFE_CONFIG[:config_file][:value]
  FileUtils.touch config_file
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



17
18
19
# File 'lib/spaux/chef/knife.rb', line 17

def args
  @args
end

#work_dirObject

Returns the value of attribute work_dir.



16
17
18
# File 'lib/spaux/chef/knife.rb', line 16

def work_dir
  @work_dir
end

Instance Method Details

#runObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/spaux/chef/knife.rb', line 37

def run
  Mixlib::Log::Formatter.show_time = false
  quiet_traps
  # I'm not sure why I have to do this if this class is child
  # of Chef::Application::Knife, so options should be set already
  knife = ::Chef::Application::Knife.new
  options = knife.options
  begin
    ::Chef::Knife.run(@args, options)
  rescue SystemExit => e
    # just ignore the exit of knife tool
  end
end