Class: AWSTracker::Tracker

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

Overview

Tracks one or more AwsAccounts in an ActiveRecord database

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Tracker

Creates an object for tracking AWS accounts Tracked info is stored in an ActiveRecord database with config db_config



12
13
14
15
16
# File 'lib/aws_tracker/tracker.rb', line 12

def initialize(options={})
  @log   = options[:logger] || AWSTracker.default_logger
  @delay = options[:delay]  || 300 # default delay is 5 minutes
  load_aws_acounts
end

Instance Attribute Details

#accountsObject (readonly)

Returns the value of attribute accounts.



8
9
10
# File 'lib/aws_tracker/tracker.rb', line 8

def accounts
  @accounts
end

#delayObject

How many seconds to wait between status updates



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

def delay
  @delay
end

#logObject (readonly)

Returns the value of attribute log.



8
9
10
# File 'lib/aws_tracker/tracker.rb', line 8

def log
  @log
end

Instance Method Details

#load_aws_acountsObject



18
19
20
21
22
23
24
25
# File 'lib/aws_tracker/tracker.rb', line 18

def load_aws_acounts
  @log.debug "Reading AWS accounts from database..."
  @accounts = AWSAccount.find(:all)
  if @accounts.empty?
    @log.warn "No accounts found in database"
  end
  @accounts.each {|acc| acc.log = @log }  # pass logger to all resources
end

#running?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/aws_tracker/tracker.rb', line 35

def running?
  @timer != nil
end

#startObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/aws_tracker/tracker.rb', line 39

def start
  if not running?
    @log.info "Tracking for #{accounts.count} accounts..."
    @timer = Thread.new do
      while true do
        update_status
        sleep @delay
      end
    end
  else
    @log.info "Automatic status updates already running"
  end
end

#stopObject



53
54
55
56
57
58
59
60
61
# File 'lib/aws_tracker/tracker.rb', line 53

def stop
  if running?
    @log.info "Stopping automatic updates"
    @timer.kill
    @timer = nil
  else
    @log.info "Automatic updates already stopped"
  end
end

#update_status(selected_accounts = accounts) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/aws_tracker/tracker.rb', line 27

def update_status(selected_accounts = accounts)
  selected_accounts.each do ||
    @log.info "Updating account #{.name}..."
    .update_status
    @log.info "Status update complete for account #{.name}..."
  end
end