Class: AutomateIt::TagManager::Struct

Inherits:
BaseDriver show all
Defined in:
lib/automateit/tag_manager/struct.rb

Overview

TagManager::Struct

A TagManager driver for querying a data structure. It’s not useful on its own, but can be subclassed by other drivers that actually load tags.

Direct Known Subclasses

YAML

Constant Summary

Constants inherited from Plugin::Driver

Plugin::Driver::BASE_DRIVER_NAME

Constants included from Constants

Constants::PERROR, Constants::PEXEC, Constants::PNOTE, Constants::VERSION, Constants::WARNING_BOILERPLATE

Instance Attribute Summary

Attributes inherited from Common

#interpreter

Instance Method Summary collapse

Methods inherited from Plugin::Driver

#_raise_unless_available, abstract_driver, #available?, base_driver, base_driver?, depends_on, inherited, manager_token

Methods inherited from Plugin::Base

#token, token

Methods inherited from Common

#initialize, #log, #nitpick, #noop, #noop=, #noop?, #preview, #preview=, #preview?, #preview_for, #superuser?, #writing, #writing=, #writing?

Constructor Details

This class inherits a constructor from AutomateIt::Common

Instance Method Details

#hosts_tagged_with(query) ⇒ Object

See TagManager#hosts_tagged_with



48
49
50
51
# File 'lib/automateit/tag_manager/struct.rb', line 48

def hosts_tagged_with(query)
  hosts = @struct.values.flatten.uniq
  return hosts.select{|hostname| tagged?(query, hostname)}
end

#setup(opts = {}) ⇒ Object

Options:

  • :struct – Hash to use for queries.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/automateit/tag_manager/struct.rb', line 14

def setup(opts={})
  super(opts)

  if opts[:struct]
    @struct = AutomateIt::TagManager::TagParser.expand(opts[:struct])
    @tags   = Set.new
  else
    @struct ||= {}
    @tags   ||= Set.new
  end
end

#suitability(method, *args) ⇒ Object

:nodoc:



8
9
10
# File 'lib/automateit/tag_manager/struct.rb', line 8

def suitability(method, *args) # :nodoc:
  return 1
end

#tagged?(query, hostname = nil) ⇒ Boolean

See TagManager#tagged?

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/automateit/tag_manager/struct.rb', line 54

def tagged?(query, hostname=nil)
  query = query.to_s
  selected_tags = hostname ? tags_for(hostname) : tags
  # XXX This tokenization process discards unknown characters, which may hide errors in the query
  tokens = query.scan(%r{\!|\(|\)|\&+|\|+|!?[\.\w]+})
  if tokens.size > 1
    booleans = tokens.map do |token|
      if matches = token.match(/^(!?)([\.\w]+)$/)
        selected_tags.include?(matches[2]) && matches[1].empty?
      else
        token
      end
    end
    code = booleans.join(" ")

    begin
      return eval(code) # XXX What could possibly go wrong?
    rescue Exception => e
      raise ArgumentError.new("Invalid query -- #{query}")
    end
  else
    return selected_tags.include?(query)
  end
end

#tagsObject

Return tags, populate them if necessary.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/automateit/tag_manager/struct.rb', line 27

def tags
  if @tags.empty?
    begin
      hostnames = interpreter.address_manager.hostnames # SLOW 0.4s
      @tags.merge(hostnames)
      @tags.merge(tags_for(hostnames))
      @tags.merge(interpreter.address_manager.addresses)
    rescue NotImplementedError => e
      log.debug("Can't find AddressManager for this platform: #{e}")
    end

    begin
      @tags.merge(interpreter.platform_manager.tags)
    rescue NotImplementedError => e
      log.debug("Can't find PlatformManager for this platform: #{e}")
    end
  end
  @tags
end

#tags_for(hostnames) ⇒ Object

See TagManager#tags_for



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/automateit/tag_manager/struct.rb', line 80

def tags_for(hostnames)
  hostnames = \
    case hostnames
    when String
      interpreter.address_manager.hostnames_for(hostnames)
    when Array, Set
      hostnames.inject(Set.new) do |sum, hostname|
        sum.merge(interpreter.address_manager.hostnames_for(hostname)); sum
      end
    else
      raise TypeError.new("invalid hostnames argument type: #{hostnames.class}")
    end
  return @struct.inject(Set.new) do |sum, role_and_members|
    role, members = role_and_members
    members_aliases = members.inject(Set.new) do |aliases, member|
      aliases.merge(interpreter.address_manager.hostnames_for(member)); aliases
    end.to_a
    sum.add(role) unless (hostnames & members_aliases).empty?
    sum
  end
end