Class: FogTracker::Tracker

Inherits:
Object
  • Object
show all
Defined in:
lib/fog_tracker/tracker.rb

Overview

Tracks one or more Fog accounts and exposes a #query on the results

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(accounts = {}, options = {}) ⇒ Tracker

Creates an object for tracking multiple Fog accounts

Parameters:

  • accounts (Hash) (defaults to: {})

    a Hash of account information (see accounts.example.yml)

  • options (Hash) (defaults to: {})

    optional additional parameters:

    • :delay (Integer) - Default time between polling of accounts

    • :callback (Proc) - A Method or Proc to call each time an account is polled. (should take an Array of resources as its only required parameter)

    • :error_callback (Proc) - A Method or Proc to call if polling errors occur. (should take a single Exception as its only required parameter)

    • :logger - a Ruby Logger-compatible object



19
20
21
22
23
24
25
26
27
28
# File 'lib/fog_tracker/tracker.rb', line 19

def initialize(accounts = {}, options = {})
  @delay      = options[:delay]
  @callback   = options[:callback]
  @log        = options[:logger] || FogTracker.default_logger
  @error_proc = options[:error_callback]
  @running    = false
  self.accounts= accounts
  # Create a Hash that maps account names to AccountTrackers
  create_trackers
end

Instance Attribute Details

#accountsObject

a Hash of account information (see accounts.example.yml)



7
8
9
# File 'lib/fog_tracker/tracker.rb', line 7

def accounts
  @accounts
end

Instance Method Details

#allArray <Fog::Model>

Returns an Array of all Resources currenty tracked

Returns:



89
90
91
92
93
# File 'lib/fog_tracker/tracker.rb', line 89

def all
  results = query '*::*::*::*'
  (results.each {|r| yield r}) if block_given?
  results
end

#loggerObject

Returns this tracker’s logger, for changing logging dynamically



103
# File 'lib/fog_tracker/tracker.rb', line 103

def logger ; @log end

#preceeding_update_time(account_name) ⇒ Time

Returns the time of given account’s most recent successful update

Parameters:

  • account_name (String)

    the name of the account that was polled

Returns:

  • (Time)

    the time the account finished updating



98
99
100
# File 'lib/fog_tracker/tracker.rb', line 98

def preceeding_update_time()
  @trackers[].preceeding_update_time
end

#query(query_string) ⇒ Array <Fog::Model> Also known as: []

Returns an array of Resources matching the query_string. Calls any block passed for each resulting resource.

Parameters:

  • query_string (String)

    a string used to filter for matching resources it might look like: “Account Name::Compute::AWS::servers”

Returns:

  • (Array <Fog::Model>)

    an Array of Resources, filtered by query



78
79
80
81
82
83
84
# File 'lib/fog_tracker/tracker.rb', line 78

def query(query_string)
  results = FogTracker::Query::QueryProcessor.new(
    @trackers, :logger => @log
  ).execute(query_string)
  (results.each {|r| yield r}) if block_given?
  results
end

#running?true, false

Returns true or false, depending on whether this tracker is polling

Returns:

  • (true, false)


64
# File 'lib/fog_tracker/tracker.rb', line 64

def running? ; @running end

#startObject

Starts periodically polling all this tracker’s accounts for all their Fog resource collections



32
33
34
35
36
37
38
39
40
# File 'lib/fog_tracker/tracker.rb', line 32

def start
  if not running?
    @log.info "Tracking #{@trackers.keys.count} accounts..."
    @trackers.each_value {|tracker| tracker.start}
    @running = true
  else
    @log.info "Already tracking #{@trackers.keys.count} accounts"
  end
end

#stopObject

Stops polling for all this tracker’s accounts



43
44
45
46
47
48
49
50
51
# File 'lib/fog_tracker/tracker.rb', line 43

def stop
  if running?
    @log.info "Stopping tracker..."
    @trackers.each_value {|tracker| tracker.stop}
    @running = false
  else
    @log.info "Tracking already stopped"
  end
end

#types_for_account(account_name) ⇒ Array<String>

Returns an Array of resource types for a given account

Parameters:

  • name (String)

    the name of the account

Returns:

  • (Array<String>)

    an array of Resource types



69
70
71
# File 'lib/fog_tracker/tracker.rb', line 69

def ()
  @trackers[].tracked_types
end

#updateArray <Fog::Model>

Polls all accounts once in the calling thread (synchronously)

Returns:

  • (Array <Fog::Model>)

    an Array of all discovered resources



55
56
57
58
59
60
# File 'lib/fog_tracker/tracker.rb', line 55

def update
  @trackers.each_value {|tracker| tracker.update}
  results = all
  (results.each {|r| yield r}) if block_given?
  results
end