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 collapse

TAG_NEGATION =
%r{!?}
TAG_WORD =
%r{[\w\.\-]+}
TAG_TOKENIZER =
%r{\!|\(|\)|\&{1,2}|\|{1,2}|#{TAG_NEGATION}#{TAG_WORD}}

Constants inherited from Plugin::Driver

Plugin::Driver::BASE_DRIVER_NAME

Constants included from Constants

Constants::HELPERS_DIR, Constants::INSTALL_DIR, Constants::PERROR, Constants::PEXEC, Constants::PNOTE, Constants::WARNING_BOILERPLATE

Instance Attribute Summary

Attributes inherited from Plugin::Driver

#manager

Attributes inherited from Common

#interpreter

Instance Method Summary collapse

Methods inherited from Plugin::Driver

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



63
64
65
66
# File 'lib/automateit/tag_manager/struct.rb', line 63

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
25
26
# File 'lib/automateit/tag_manager/struct.rb', line 14

def setup(opts={})
  super(opts)
  @struct ||= {}
  @tags   ||= Set.new
  @struct_updated = false
  @our_hostname_tags ||= Set.new
  @our_platform_tags ||= Set.new

  if opts[:struct]
    @struct_updated = true
    @struct.merge!(AutomateIt::TagManager::TagParser.expand(opts[:struct]))
  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)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/automateit/tag_manager/struct.rb', line 73

def tagged?(query, hostname=nil)
  query = query.to_s
  selected_tags = hostname ? tags_for(hostname) : tags
  # XXX Tokenizer discards unknown characters, which may hide errors in the query
  tokens = query.scan(TAG_TOKENIZER)
  if tokens.size > 1
    booleans = tokens.map do |token|
      if matches = token.match(/^(#{TAG_NEGATION})(#{TAG_WORD})$/)
        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.



29
30
31
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
# File 'lib/automateit/tag_manager/struct.rb', line 29

def tags
  if @our_hostname_tags.empty?
    @struct_updated = true

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

    @tags.merge(@our_hostname_tags)
  end

  if @our_platform_tags.empty?
    @struct_updated = true

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

  if @struct_updated
    @tags.merge(tags_for(@our_hostname_tags))
    @struct_updated = false
  end

  @tags
end

#tags_for(hostnames) ⇒ Object

See TagManager#tags_for



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/automateit/tag_manager/struct.rb', line 99

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
  result = @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
  return result.to_a.sort
end