Class: CustomChef::KnifeTagBatch::TagBatch
- Inherits:
-
Chef::Knife
- Object
- Chef::Knife
- CustomChef::KnifeTagBatch::TagBatch
- Defined in:
- lib/chef/knife/tagbatch.rb
Instance Method Summary collapse
- #add_tag(node, tag) ⇒ Object
- #log_node(str) ⇒ Object
- #remove_tag(node, tag) ⇒ Object
- #run ⇒ Object
- #validate_arg(arg, type, pattern) ⇒ Object
Instance Method Details
#add_tag(node, tag) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/chef/knife/tagbatch.rb', line 48 def add_tag(node, tag) if node..include?(tag) log = "#{Time.now.getutc}:#{node.name} already has tag \"#{@tag}\"" else node. << 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) ⇒ Object
78 79 80 81 82 83 |
# 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) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/chef/knife/tagbatch.rb', line 63 def remove_tag(node, tag) if node..include?(tag) node..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 ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# 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) ⇒ Object
85 86 87 88 89 90 91 92 |
# 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 |