Module: Cloudpatrol

Defined in:
lib/cloudpatrol.rb,
lib/cloudpatrol/task.rb,
lib/cloudpatrol/version.rb,
lib/cloudpatrol/task/ec2.rb,
lib/cloudpatrol/task/iam.rb,
lib/cloudpatrol/task/dynamo_db.rb,
lib/cloudpatrol/task/ops_works.rb,
lib/cloudpatrol/task/cloud_formation.rb

Defined Under Namespace

Modules: Task

Constant Summary collapse

VERSION =
'0.1.3'

Class Method Summary collapse

Class Method Details

.get_log(aws_credentials, table_name = "cloudpatrol-log") ⇒ Object



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
61
62
63
64
65
66
67
# File 'lib/cloudpatrol.rb', line 30

def self.get_log aws_credentials, table_name = "cloudpatrol-log"
  gate = ::AWS::DynamoDB.new(aws_credentials)
  response = {}
  table = gate.tables[table_name]
  if table.exists? and table.status == :active
    table.load_schema
    response[:log] = []
    table.items.each do |item|
      response[:log] << item.attributes.to_hash
    end
    response[:log].map! do |item|
      {
        time: Time.parse(item["time"]),
        action: item["action"],
        response: item["response"]
      }
    end
    response[:log].sort!{ |x,y| x[:time] <=> y[:time] }
  else
    response[:success] = false
    response[:error] = "Table doesn't exist"
  end
rescue AWS::Errors::Base => e
  response[:success] = false
  response[:error] = "AWS error: #{e}"
rescue
  response[:success] = false
  response[:error] = "Unknown error"
else
  response[:success] = if response[:log].empty?
    response[:error] = "Log is empty"
    false
  else
    true
  end
ensure
  return response
end

.perform(aws_credentials, table_name, klass, method, *args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cloudpatrol.rb', line 6

def self.perform aws_credentials, table_name, klass, method, *args
  response = {}
  table_name ||= "cloudpatrol-log"

  response[:task] = begin
    response[:formatted] = Task.const_get(klass).new(aws_credentials).send(method, *args)
  rescue AWS::Errors::Base => e
    response[:formatted] = "AWS error: #{e}"
    false
  rescue
    response[:formatted] = "Unknown error"
    false
  end

  response[:log] = begin
    Task::DynamoDB.new(aws_credentials, table_name).log({ class: klass, method: method, args: args }, response[:formatted])
  rescue
    puts "Failed to write log to DynamoDB"
    false
  end

  response
end