Class: Chef::Knife::TagBulkDelete

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

Instance Method Summary collapse

Instance Method Details

#nodes_found(node_count) ⇒ Object



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

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



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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/chef/knife/tag_bulk_delete.rb', line 46

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
      if config[:regex]
        unless config[:yes]
          confirm = ui.ask_question(
            ui.color("\u2192  Deleting tags based on regulard expressions is a potentially dangerous operation. Are you sure you want to continue? ", [:bold, :red]),
            {:default => 'Y'}
          )
          if not confirm.match(/[Yy][Ee]?[Ss]?/)
            ui.fatal('Did not confirm destructive operation. Exiting...')
            exit 1
          end
        end
        rtags = @tags.map { |rtag| Regexp.new(rtag) }
      end
      node_items.each do |node_name|
        node = Chef::Node.load(node_name)
        tags_to_remove = Array.new              
        if config[:regex]                
          tags_to_remove = node.tags.map do |ntag|
            rtags.map do |utag|
              break ntag if ntag.match(utag)
            end
          end.flatten.compact.uniq
        else
          tags_to_remove = @tags
        end
        if (node.tags & tags_to_remove).size == 0
          tags_not_present(node_name, @tags)
          next
        end
        tags_to_remove.each { |tag| node.tags.delete(tag) }
        node.save
        tags_successfully_deleted(node_name, tags_to_remove)
        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
  rescue RegexpError => e
    ui.fatal("Invalid Regular expression given: #{e.message}")
  end      
end

#tags_not_present(node_name, tags) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/chef/knife/tag_bulk_delete.rb', line 120

def tags_not_present(node_name, tags)
  ui.info(
    ui.color("\u2714  ", [:bold, :green]) +
    ui.color("Tag(s) ", [:bold, :white]) + 
    ui.color("[#{tags.join(', ')}]", [:bold, :yellow]) + 
    ui.color(" not present on node ", [:bold, :white]) +
    ui.color("[#{node_name}]", [:bold, :yellow]) +
    ui.color(". Skipping...", [:bold, :white])
  )      
end

#tags_successfully_deleted(node_name, tags) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/chef/knife/tag_bulk_delete.rb', line 131

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

#verbose_node_tag_information(node_name, tags) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/chef/knife/tag_bulk_delete.rb', line 141

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