Class: Chef::Knife::TagBulkCreate

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/tag_bulk_create.rb

Instance Method Summary collapse

Instance Method Details

#nodes_found(node_count) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/chef/knife/tag_bulk_create.rb', line 73

def nodes_found(node_count)
  ui.info(
    ui.color("\u2192  ", [:bold, :green]) +
    ui.color(node_count.to_s, [:bold, :yellow]) +
    ui.color(' node(s) found.', [:bold, :white])
  )      
end

#runObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/chef/knife/tag_bulk_create.rb', line 32

def run
  @query = @name_args.shift
  @tags = @name_args

  if @query.nil? || @query.empty? || @tags.nil? || @tags.empty?
    show_usage
    ui.fatal('You must specify a valid Chef node search query and at least one tag.')
    exit 1
  end

  escaped_query = URI.escape(@query,Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
  query = Chef::Search::Query.new
  node_items = []
  node_count = 0
  begin
    res = query.search(:node, escaped_query)
    node_items = res.first.map { |node| node.name }.sort
    node_count = res.last
    nodes_found(node_count)
    if node_count > 0
      node_items.each do |node_name|              
        node = Chef::Node.load(node_name)
        if (node.tags & @tags).size == @tags.size
          tags_already_present(node_name, @tags)
          next
        end
        @tags.each { |tag| (node.tags << tag).uniq! }
        node.save
        tags_successfully_added(node_name, @tags)
        if config[:verbosity] > 0
          verbose_node_tag_information(node_name, node.tags)
        end
      end
    end
  rescue Net::HTTPServerException => e
    msg = Chef::JSONCompat.from_json(e.response.body)['error'].first
    ui.fatal("An error occurred: #{msg}")
    exit 1
  end      
end

#tags_already_present(node_name, tags) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/chef/knife/tag_bulk_create.rb', line 81

def tags_already_present(node_name, tags)
  ui.info(
    ui.color("\u2714  ", [:bold, :green]) +
    ui.color('Node ', [:bold, :white]) +
    ui.color("[#{node_name}]", [:bold, :yellow]) +
    ui.color(' is already tagged with ', [:bold, :white]) +
    ui.color("[#{tags.join(', ')}]", [:bold, :yellow]) +
    ui.color(". Skipping...", [:bold, :white])
  )      
end

#tags_successfully_added(node_name, tags) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/chef/knife/tag_bulk_create.rb', line 92

def tags_successfully_added(node_name, tags)
  ui.info(
    ui.color("\u2714  ", [:bold, :green]) +
    ui.color("Successfully added tag(s) ", [:bold, :white]) + 
    ui.color("[#{tags.join(', ')}]", [:bold, :yellow]) + 
    ui.color(" to node ", [:bold, :white]) +
    ui.color("[#{node_name}]", [:bold, :yellow])
  )
end

#verbose_node_tag_information(node_name, tags) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/chef/knife/tag_bulk_create.rb', line 102

def verbose_node_tag_information(node_name, tags)
  ui.info(
    ui.color("\u2192  ", [:bold, :green]) +
    ui.color('Node ', [:bold, :white]) +
    ui.color("[#{node_name}]", [:bold, :yellow]) +
    ui.color(' is now tagged with ', [:bold, :white]) +
    ui.color("[#{tags.join(', ')}]", [:bold, :yellow])
  )
end