lib/chef/knife/topo_export.rb in knife-topo-0.0.8 vs lib/chef/knife/topo_export.rb in knife-topo-0.0.9

- old
+ new

@@ -18,11 +18,11 @@ require 'chef/knife' require_relative 'topology_helper' -# NOTE: This command exports to stdout - do not add ui messages (other than fatal) to this command +# NOTE: This command exports to stdout class Chef class Knife class TopoExport < Chef::Knife @@ -33,18 +33,33 @@ option :data_bag, :short => '-D DATA_BAG', :long => "--data-bag DATA_BAG", :description => "The data bag the topologies are stored in" + + option :min_priority, + :long => "--min-priority PRIORITY", + :default => "default", + :description => "Export attributes with this priority or above" + def most_common (vals) + vals.group_by do |val| + val + end.values.max_by(&:size).first + end + def run @bag_name = topo_bag_name(config[:data_bag]) @topo_name = @name_args[0] @node_names = @name_args[1..-1] + unless ['default', 'normal', 'override'].include?(config[:min_priority]) + ui.warn("--min-priority should be one of 'default', 'normal' or 'override'") + end + if @topo_name if topo = load_from_server(@bag_name, @topo_name) export = topo.raw_data else export = empty_topology @@ -53,10 +68,19 @@ # merge in data for nodes that user explicitly specified @node_names.each do |node_name| merge_node_properties!(export['nodes'], node_name) end + + # if a new topo, pick an topo environment based on the nodes + if !topo && @node_names.length != 0 + envs = [] + export['nodes'].each do |node| + envs << node['chef_environment'] if node['chef_environment'] + end + export['chef_environment'] = most_common(envs) if envs.length > 0 + end else # export all topologies export = [] if dbag = load_from_server(@bag_name) @@ -76,11 +100,10 @@ { "id" => @topo_name || "topo1", "name" => @topo_name || "topo1", "chef_environment" => "_default", "tags" => [ ], - "normal" => { }, "nodes" => [ ], "cookbook_attributes" => [{ "cookbook" => @topo_name || "topo1", "filename" => "topology" }] @@ -107,11 +130,15 @@ node = Chef::Node.load(node_name) node_data['name'] = node.name node_data['tags'] = node.tags node_data['chef_environment'] = node.chef_environment - node_data['normal'] = node.normal node_data['run_list'] = node.run_list + + pri = config[:min_priority] + node_data['default'] = node.default if pri == "default" + node_data['normal'] = node.normal if pri == "default" || pri == "normal" + node_data['override'] = node.override rescue Net::HTTPServerException => e raise unless e.to_s =~ /^404/ node_data = empty_node(node_name) end