class KnifeTopo::TopoExport

knife topo export

Public Instance Methods

default_strategy_data() click to toggle source
# File lib/chef/knife/topo_export.rb, line 112
def default_strategy_data
  {
    'cookbook' => @topo_name || 'topo1',
    'filename' => 'topology'
  }
end
empty_node(name) click to toggle source
# File lib/chef/knife/topo_export.rb, line 119
def empty_node(name)
  {
    'name' => name,
    'run_list' => [],
    'ssh_host' => name,
    'ssh_port' => '22',
    'normal' => {},
    'tags' => []
  }
end
empty_topology() click to toggle source

give user a template to get started

# File lib/chef/knife/topo_export.rb, line 100
def empty_topology
  {
    'id' => @topo_name || 'topo1',
    'name' => @topo_name || 'topo1',
    'chef_environment' => '_default',
    'tags' => [],
    'strategy' => 'via_cookbook',
    'strategy_data' => default_strategy_data,
    'nodes' => @node_names.empty? ? [empty_node('node1')] : []
  }
end
load_or_initialize_topo() click to toggle source
# File lib/chef/knife/topo_export.rb, line 69
def load_or_initialize_topo
  topo = load_topo_from_server(@topo_name)
  if topo
    export = topo.raw_data
    update_nodes!(export['nodes'])
  else
    export = new_topo
  end
  export
end
new_topo() click to toggle source
# File lib/chef/knife/topo_export.rb, line 80
def new_topo
  topo = empty_topology
  update_nodes!(topo['nodes'])

  # pick an topo environment based on the nodes
  return topo if @node_names.empty?
  env = pick_env(topo['nodes'])
  topo['chef_environment'] = env if env
  topo
end
node_export(node_name) click to toggle source

get actual node properties for export

# File lib/chef/knife/topo_export.rb, line 131
def node_export(node_name)
  load_node_data(node_name, config[:min_priority])
rescue Net::HTTPServerException => e
  raise unless e.to_s =~ /^404/
  empty_node(node_name)
end
pick_env(nodes) click to toggle source
# File lib/chef/knife/topo_export.rb, line 91
def pick_env(nodes)
  envs = []
  nodes.each do |node|
    envs << node['chef_environment'] if node['chef_environment']
  end
  most_common(envs)
end
run() click to toggle source
# File lib/chef/knife/topo_export.rb, line 59
def run
  unless KnifeTopo::PRIORITIES.include?(config[:min_priority])
    ui.warn('--min-priority should be one of ' \
      "#{KnifeTopo::PRIORITIES.join(', ')}")
  end
  @topo_name = config[:topo]
  @node_names = @name_args
  output(Chef::JSONCompat.to_json_pretty(load_or_initialize_topo))
end
update_nodes!(nodes) click to toggle source

put node details in node array, overwriting existing details

# File lib/chef/knife/topo_export.rb, line 139
def update_nodes!(nodes)
  @node_names.each do |node_name|
    # find out if the node is already in the array
    found = nodes.index { |n| n['name'] == node_name }
    if found.nil?
      nodes.push(node_export(node_name))
    else
      nodes[found] = node_export(node_name)
    end
  end
end