class KnifeTopo::ViaCookbookProcessor

Process attributes via a cookbook

Attributes

cookbook[RW]
filename[RW]

Public Class Methods

new(topo) click to toggle source
Calls superclass method KnifeTopo::Processor::new
# File lib/chef/knife/topo/processor/via_cookbook.rb, line 40
def initialize(topo)
  super
  data = @topo['strategy_data'] || {}
  @cookbook = data['cookbook'] || topo.topo_name
  @filename = data['filename'] || 'topology'
  @helper = KnifeHelper.new
end

Public Instance Methods

cookbook_path() click to toggle source
# File lib/chef/knife/topo/processor/via_cookbook.rb, line 91
def cookbook_path
  paths = @config['cookbook_path']
  return unless paths
  # cookbook path can be an array or a string
  paths.is_a?(Array) ? (paths.first || './cookbooks') : paths
end
create_attr_file(dir, contents) click to toggle source
# File lib/chef/knife/topo/processor/via_cookbook.rb, line 83
def create_attr_file(dir, contents)
  @helper.ui.info("** Creating attribute file: #{@filename}")

  name = @filename << '.rb' unless File.extname(@filename) == '.rb'
  filepath = File.join(dir, @cookbook, 'attributes', name)
  File.open(filepath, 'w') { |file| file.write(contents) }
end
generate_artifacts(context = {}) click to toggle source

generate attributes to cookbook context must be calling command context must be calling command's args

# File lib/chef/knife/topo/processor/via_cookbook.rb, line 57
def generate_artifacts(context = {})
  @cmd = context['cmd']
  @cmd_args = context['cmd_args'] || []
  @config = Chef::Config.merge!(@cmd.config)
  return unless @cmd && cookbook_path
  run_create_cookbook
  create_attr_file(
    cookbook_path,
    cookbook_contents
  )
end
generate_nodes() click to toggle source
Calls superclass method KnifeTopo::Processor#generate_nodes
# File lib/chef/knife/topo/processor/via_cookbook.rb, line 50
def generate_nodes
  super
end
run_create_cookbook() click to toggle source
# File lib/chef/knife/topo/processor/via_cookbook.rb, line 69
def run_create_cookbook
  create_args = @helper.initialize_cmd_args(
    @cmd_args, @cmd.name_args, %w(cookbook create)
  )
  create_args[2] = @cookbook
  # set options from calling command, so validation does not fail
  Chef::Knife::CookbookCreate.options = @cmd.class.options
  @helper.run_cmd(Chef::Knife::CookbookCreate, create_args)
rescue StandardError => e
  raise if Chef::Config[:verbosity] == 2
  @helper.ui.warn "Create of cookbook #{@cookbook} exited with error"
  @helper.humanize_exception(e)
end
run_upload_cookbook() click to toggle source
# File lib/chef/knife/topo/processor/via_cookbook.rb, line 105
def run_upload_cookbook
  upload_args = @helper.initialize_cmd_args(
    @cmd_args, @cmd.name_args, %w(cookbook upload)
  )
  upload_args[2] = @cookbook
  # set options from calling command, so validation does not fail
  Chef::Knife::CookbookUpload.options = @cmd.class.options
  @helper.run_cmd(Chef::Knife::CookbookUpload, upload_args)
rescue StandardError => e
  raise if Chef::Config[:verbosity] == 2
  @helper.ui.warn "Upload of cookbook #{@cookbook} exited with error"
  @helper.humanize_exception(e)
end
upload_artifacts(context = {}) click to toggle source
# File lib/chef/knife/topo/processor/via_cookbook.rb, line 98
def upload_artifacts(context = {})
  @cmd = context['cmd']
  @cmd_args = context['cmd_args'] || []
  return unless @cmd && !@cmd.config[:disable_upload]
  run_upload_cookbook
end