Class: KnifeTopo::ViaCookbookProcessor

Inherits:
Processor
  • Object
show all
Includes:
ViaCookbookPrint
Defined in:
lib/chef/knife/topo/processor/via_cookbook.rb

Overview

Process attributes via a cookbook

Instance Attribute Summary collapse

Attributes inherited from Processor

#input

Instance Method Summary collapse

Methods included from ViaCookbookPrint

#cookbook_contents, #copyright, #print_attr, #print_attr_header, #print_attrs, #print_attrs_for_node, #print_node_name_attr_header, #print_node_type_attr_header

Methods inherited from Processor

for_topo, load_processor, processor, register_processor

Constructor Details

#initialize(topo) ⇒ ViaCookbookProcessor

Returns a new instance of ViaCookbookProcessor.



40
41
42
43
44
45
46
# 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

Instance Attribute Details

#cookbookObject

Returns the value of attribute cookbook.



36
37
38
# File 'lib/chef/knife/topo/processor/via_cookbook.rb', line 36

def cookbook
  @cookbook
end

#filenameObject

Returns the value of attribute filename.



36
37
38
# File 'lib/chef/knife/topo/processor/via_cookbook.rb', line 36

def filename
  @filename
end

Instance Method Details

#cookbook_pathObject



91
92
93
94
95
# File 'lib/chef/knife/topo/processor/via_cookbook.rb', line 91

def cookbook_path
  paths = @config['cookbook_path']
  return unless paths
  paths.first
end

#create_attr_file(dir, contents) ⇒ Object



83
84
85
86
87
88
89
# 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 = {}) ⇒ Object

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



57
58
59
60
61
62
63
64
65
66
67
# 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_nodesObject



50
51
52
# File 'lib/chef/knife/topo/processor/via_cookbook.rb', line 50

def generate_nodes
  super
end

#run_create_cookbookObject



69
70
71
72
73
74
75
76
77
78
79
80
81
# 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_cookbookObject



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/chef/knife/topo/processor/via_cookbook.rb', line 104

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 = {}) ⇒ Object



97
98
99
100
101
102
# File 'lib/chef/knife/topo/processor/via_cookbook.rb', line 97

def upload_artifacts(context = {})
  @cmd = context['cmd']
  @cmd_args = context['cmd_args'] || []
  return unless @cmd && !@cmd.config[:disable_upload]
  run_upload_cookbook
end