Class: KnifeTopo::TopoExport

Inherits:
Chef::Knife
  • Object
show all
Includes:
CommandHelper, Loader
Defined in:
lib/chef/knife/topo_export.rb

Overview

knife topo export

Instance Attribute Summary

Attributes included from Loader

#loader, #ui

Instance Method Summary collapse

Methods included from CommandHelper

#check_chef_env, #initialize_cmd_args, #most_common, #resource_exists?, #run_cmd

Methods included from Loader

#auto_detect_format, #check_file, #create_topo_bag, #get_local_topo_path, #list_topo_bag, #load_local_topo_or_exit, #load_node_data, #load_topo_from_file_or_exit, #load_topo_from_server, #load_topo_from_server_or_exit, #priority_attrs, #topo_bag_name, #topologies_path

Instance Method Details

#default_strategy_dataObject



112
113
114
115
116
117
# File 'lib/chef/knife/topo_export.rb', line 112

def default_strategy_data
  {
    'cookbook' => @topo_name || 'topo1',
    'filename' => 'topology'
  }
end

#empty_node(name) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# 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_topologyObject

give user a template to get started



100
101
102
103
104
105
106
107
108
109
110
# 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_topoObject



69
70
71
72
73
74
75
76
77
78
# 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_topoObject



80
81
82
83
84
85
86
87
88
89
# 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) ⇒ Object

get actual node properties for export



131
132
133
134
135
136
# 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) ⇒ Object



91
92
93
94
95
96
97
# 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

#runObject



59
60
61
62
63
64
65
66
67
# 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) ⇒ Object

put node details in node array, overwriting existing details



139
140
141
142
143
144
145
146
147
148
149
# 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