Class: AutomateIt::TagManager

Inherits:
Plugin::Manager show all
Defined in:
lib/automateit/tag_manager.rb

Overview

TagManager

The TagManager provides a way of querying tags. Tags are keywords associated with a specific hostname or group. These are useful for grouping together hosts and defining common behavior for them.

Basics

The tags are typically stored in a Project’s config/tags.yml file.

For example, consider this config/tags.yml file:

desktops:
  - satori
  - sunyata
  - michiru
notebooks:
  - rheya
  - avijja

With the above file, if we’re on the host called “satori”, we can query the fields like this:

tags # => ["satori", "desktops", "localhost", ...]

tagged?("desktops") # => true
tagged?("notebooks") # => false
tagged?(:satori) # => true
tagged?("satori") # => true
tagged?("satori || desktops") # => true
tagged?("(satori || desktops) && !notebooks") # => true

Traits

Your system may also automatically add tags that describe your system’s traits, such as the name of the operating system, distribution release, hardware architecture, hostnames, IP addresses, etc.

For example, here is a full set of tags for a system:

ai> pp tags.sort                # Pretty print the tags in sorted order
["10.0.0.6",                    # IPv4 addresses
 "127.0.0.1",                   # ...
 "192.168.130.1",               # ...
 "::1/128",                     # IPv6 addresses
 "fe80::250:56ff:fec0:8/64",    # ...
 "fe80::250:8dff:fe95:8fe9/64", # ...
 "i686",                        # Hardware architecture
 "linux",                       # OS
 "linux_i686",                  # OS and architecture
 "localhost",                   # Variants of hostname
 "localhost.localdomain",       # ...
 "michiru",                     # ...
 "michiru.koshevoy",            # ...
 "michiru.koshevoy.net",        # ...
 "myapp_servers",               # User defined tags
 "rails_servers",               # ...
 "ubuntu",                      # OS distribution name
 "ubuntu_6.06"]                 # OS distribution name and release version

To execute code only on an Ubuntu system:

if tagged?("ubuntu")
  # Code will only be run on Ubuntu systems
end

These additional tags are retrieved from the PlatformManager and AddressManager. If your platform does not provide drivers for these, you will not get these tags. If you’re on an unsupported platform and do not want to write drivers, you can work around this by manually declaring the missing tags in config/tags.yml on a host-by-host basis.

Inclusion and negation

You can include and negate tags declaratively by giving “@” and “!” prefixes to arguments.

For example, consider this config/tags.yml file:

apache_servers:
  - kurou
  - shirou
apache_servers_except_kurou:
  - @apache_servers
  - !kurou

This will produce the following results:

ai> hosts_tagged_with("apache_servers")
=> ["kurou", "shirou"]
ai> hosts_tagged_with("apache_servers_except_kurou")
=> ["shirou"]

Defined Under Namespace

Classes: BaseDriver, Struct, TagParser, YAML

Constant Summary

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::Manager

#drivers

Attributes inherited from Common

#interpreter

Instance Method Summary collapse

Methods inherited from Plugin::Manager

#[], abstract_manager, alias_methods, #available?, #default, #default=, #dispatch, #dispatch_safely, #dispatch_safely_to, #dispatch_to, driver_classes, #driver_for, #driver_suitability_levels_for, inherited, #instantiate_drivers, #setup

Methods inherited from Plugin::Base

#setup, #token, token

Methods inherited from Common

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

Constructor Details

This class inherits a constructor from AutomateIt::Common

Instance Method Details

#hosts_tagged_with(query) ⇒ Object

Return a list of hosts that match the query. See #tagged? for information on query syntax.



96
# File 'lib/automateit/tag_manager.rb', line 96

def hosts_tagged_with(query) dispatch(query) end

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

Is this host tagged with the query?

Examples:

tags # => ["localhost", "foo", "bar", ...]

tagged?(:localhost) # => true
tagged?("localhost") # => true
tagged?("localhost && foo") # => true
tagged?("localhost || foo") # => true
tagged?("!foo") # => false
tagged?("(localhost || foo) && bar") # => true

Returns:

  • (Boolean)


112
# File 'lib/automateit/tag_manager.rb', line 112

def tagged?(query, hostname=nil) dispatch(query, hostname) end

#tagsObject

Return a list of tags for this host.



99
# File 'lib/automateit/tag_manager.rb', line 99

def tags() dispatch() end

#tags_for(hostname) ⇒ Object

Return a list of tags for the host.



115
# File 'lib/automateit/tag_manager.rb', line 115

def tags_for(hostname) dispatch(hostname) end