class CustomChef::KnifeTagBatch::TagBatch

Public Instance Methods

add_tag(node, tag) click to toggle source
# File lib/chef/knife/tagbatch.rb, line 48
def add_tag(node, tag)
  if node.tags.include?(tag)
    log       = "#{Time.now.getutc}:#{node.name} already has tag \"#{@tag}\""
  else
    node.tags << tag
    unless config[:dry_run]
      node.save                       
    end
    log = "#{Time.now.getutc}:Created tag \"#{@tag}\" on #{node.name}"
  end
  if config[:batch_log] 
    log_node(log) 
  end         
end
log_node(str) click to toggle source
# File lib/chef/knife/tagbatch.rb, line 78
def log_node(str)
    log_filename = 'tagBatch.log'
  File.open(log_filename, 'a') do |file|
    file.puts str
            end
end
remove_tag(node, tag) click to toggle source
# File lib/chef/knife/tagbatch.rb, line 63
def remove_tag(node, tag)
  if node.tags.include?(tag)        
      node.tags.delete(tag)
    unless config[:dry_run]
      node.save                              
    end
    log = "#{Time.now.getutc}:Removed tag \"#{@tag}\" on #{node.name}"    
  else
    log = "#{Time.now.getutc}:The tag \"#{@tag}\" does not exist on #{node.name}"  
  end       
    if config[:batch_log] 
            log_node(log) 
    end            
end
run() click to toggle source
# File lib/chef/knife/tagbatch.rb, line 28
def run
  @command = validate_arg(@name_args[0], 'command', /^add$|^remove$/)
  @tag = validate_arg(@name_args[1],'tag',/[\w-]*/)
  @search_query = validate_arg(@name_args[2], 'search', /[\w]*:[\w*-]*(\sAND\s|\sOR\s|\sNOT\s)*/)
  @query_nodes = Chef::Search::Query.new.search('node', @search_query)
  ui.msg "Search returned #{@query_nodes.last} node(s)"
  if @query_nodes.last > 0
                      ui.confirm("Execute command \"#{@command}\" with tag #{@tag} on #{@query_nodes.last} node(s)?", append_instructions=true)
  elsif @query_nodes.last == 0
    ui.warn('No nodes returned from search')
  else
    ui.fatal('Invalid return from search')
  end

  @query_nodes[0].each do |node|    
    self.send("#{@command}_tag",node,@tag)
  end 
  ui.msg('Operation completed')
end
validate_arg(arg, type, pattern) click to toggle source
# File lib/chef/knife/tagbatch.rb, line 85
def validate_arg(arg, type, pattern)
    unless arg =~ pattern
            show_usage
            ui.fatal "Argument #{type}:#{arg} is not valid"
            exit 1
    end
    arg
end