Class: Cloudpatrol::Task::EC2

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudpatrol/task/ec2.rb

Instance Method Summary collapse

Constructor Details

#initialize(cred) ⇒ EC2

Returns a new instance of EC2.



6
7
8
# File 'lib/cloudpatrol/task/ec2.rb', line 6

def initialize cred
  @gate = ::AWS::EC2.new(cred)
end

Instance Method Details

#clean_elastic_ipsObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/cloudpatrol/task/ec2.rb', line 69

def clean_elastic_ips
  deleted = []
  @gate.elastic_ips.each do |ip|
    unless ip.instance
      deleted << ip.inspect
      ip.release
    end
  end
  deleted
end

#clean_instances(allowed_age) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/cloudpatrol/task/ec2.rb', line 28

def clean_instances allowed_age
  deleted = []
  @gate.instances.each do |instance|
    if (Time.now - instance.launch_time).to_i > allowed_age.days and instance.status != :terminated
      deleted << instance.inspect
      instance.delete
    end
  end
  deleted
end

#clean_ports_in_defaultObject



58
59
60
61
62
63
64
65
66
67
# File 'lib/cloudpatrol/task/ec2.rb', line 58

def clean_ports_in_default
  deleted = []
  @gate.security_groups.filter("group-name", "default").each do |sg|
    sg.ingress_ip_permissions.each do |perm|
      deleted << { port_range: perm.port_range }
      perm.revoke
    end
  end
  deleted
end

#clean_security_groupsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cloudpatrol/task/ec2.rb', line 39

def clean_security_groups
  deleted = []
  protected_groups = []
  @gate.security_groups.each do |sg|
    sg.ip_permissions.each do |perm|
      perm.groups.each do |dependent_sg|
        protected_groups << dependent_sg
      end
    end
  end
  @gate.security_groups.each do |sg|
    if !protected_groups.include?(sg) and sg.exists? and sg.instances.count == 0 and sg.name != "default"
      deleted << sg.inspect
      sg.delete
    end
  end
  deleted
end

#start_instancesObject



10
11
12
13
14
15
16
17
# File 'lib/cloudpatrol/task/ec2.rb', line 10

def start_instances
  result = []
  @gate.instances.each do |instance|
    result << instance.inspect
    instance.start
  end
  result
end

#stop_instancesObject



19
20
21
22
23
24
25
26
# File 'lib/cloudpatrol/task/ec2.rb', line 19

def stop_instances
  result = []
  @gate.instances.each do |instance|
    result << instance.inspect
    instance.stop
  end
  result
end