Module: KnifeTopo::ViaCookbookPrint

Included in:
ViaCookbookProcessor
Defined in:
lib/chef/knife/topo/processor/via_cookbook_print.rb

Overview

Prints attribute file contents to string

Instance Method Summary collapse

Instance Method Details

#cookbook_contentsObject



83
84
85
86
87
88
89
90
91
# File 'lib/chef/knife/topo/processor/via_cookbook_print.rb', line 83

def cookbook_contents
  contents = ''
  print_attr_header(contents, @cookbook, @filename)
  print_attrs(contents, @topo['attributes'])
  @topo.nodes.each do |n|
    print_attrs_for_node(contents, n)
  end
  contents
end


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

def copyright
  Chef::Config.copyright || @cmd.config['copyright']
end


40
41
42
43
44
45
46
47
48
49
# File 'lib/chef/knife/topo/processor/via_cookbook_print.rb', line 40

def print_attr(contents, lhs, value1)
  if value1.is_a?(Hash)
    value1.each do |key, value2|
      print_attr(contents, "#{lhs}['#{key}']", value2)
    end
  else
    ruby_str = value1.nil? ? 'nil' : Chef::JSONCompat.to_json(value1)
    contents << "#{lhs} = #{ruby_str}\n"
  end
end


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/chef/knife/topo/processor/via_cookbook_print.rb', line 23

def print_attr_header(contents, cookbook_name, filename)
  copyright = @config['cookbook_copyright']
  contents << "
#
# THIS FILE IS GENERATED BY KNIFE TOPO - MANUAL CHANGES WILL BE OVERWRITTEN
#
# Cookbook Name:: #{cookbook_name}
# Attribute File:: #{filename}
#
"
  return unless copyright
  contents << "
# Copyright (c) #{Time.now.year} #{copyright}
#
"
end


51
52
53
54
55
56
57
58
59
60
# File 'lib/chef/knife/topo/processor/via_cookbook_print.rb', line 51

def print_attrs(contents, attrs, indent = 0)
  return unless attrs
  KnifeTopo::PRIORITIES.each do |priority|
    next unless attrs[priority]
    lhs = ''
    indent.times { lhs << ' ' }
    lhs << priority
    print_attr(contents, lhs, attrs[priority])
  end
end


62
63
64
65
66
67
68
69
70
# File 'lib/chef/knife/topo/processor/via_cookbook_print.rb', line 62

def print_attrs_for_node(contents, n)
  if n['node_type']
    print_node_type_attr_header(contents, n['node_type'])
  else
    print_node_name_attr_header(contents, n['name'])
  end
  print_attrs(contents, n, 2)
  contents << "end\n"
end


78
79
80
81
# File 'lib/chef/knife/topo/processor/via_cookbook_print.rb', line 78

def print_node_name_attr_header(contents, node_name)
  contents << "# Attributes for node name #{node_name}\n"
  contents << "if node.name == '#{node_name}'\n"
end


72
73
74
75
76
# File 'lib/chef/knife/topo/processor/via_cookbook_print.rb', line 72

def print_node_type_attr_header(contents, node_type)
  contents << "# Attributes for node type #{node_type}\n"
  contents << "if node['topo'] && node['topo']['node_type']" \
   " == '#{node_type}'\n"
end