ec2-blackout 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -10,6 +10,9 @@ If you don't, `ec2-blackout` could save you money
10
10
 
11
11
  Use ec2-blackout to shutdown EC2 instances when they are idle, for example when you are not in the office.
12
12
 
13
+ If an instance has an Elastic IP address, `ec2-blackout` will reassociate the EIP when the instance is started.
14
+ Note: When an instance with an EIP is stopped AWS will automatically disassociate the EIP. AWS charge a small hourly fee for an unattached EIP.
15
+
13
16
  Certinaly not suitable for production instances but development and test instances can generally be shutdown overnight to save money.
14
17
 
15
18
  ## Installation
@@ -30,7 +33,9 @@ It is recommended you create an access policy using Amazon IAM
30
33
  "ec2:CreateTags",
31
34
  "ec2:DeleteTags",
32
35
  "ec2:DescribeInstances",
33
- "ec2:DescribeRegions"
36
+ "ec2:DescribeRegions",
37
+ "ec2:DescribeAddresses",
38
+ "ec2:AssociateAddress"
34
39
  ],
35
40
  "Effect": "Allow",
36
41
  "Resource": "\*"
@@ -11,14 +11,11 @@ class Ec2::Blackout::Startup
11
11
  AWS.memoize do
12
12
  @aws.regions[region].instances.each do |instance|
13
13
  if startable? instance
14
- @ui.say "-> Starting instance: #{instance.id}, name: #{instance.tags['Name']}"
14
+ @ui.say "-> Starting instance: #{instance.id}, name: #{instance.tags['Name']}."
15
15
  unless dry_run?
16
16
  instance.tags.delete('ec2:blackout:on')
17
17
  instance.start
18
- if instance.tags['ec2:blackout:eip']
19
- instance.associate_elastic_ip(instance.tags['ec2:blackout:eip']) rescue nil
20
- instance.tags.delete('ec2:blackout:eip')
21
- end
18
+ associate_eip(instance)
22
19
  end
23
20
  elsif instance.status == :stopped
24
21
  @ui.say "-> Skipping instance: #{instance.id}, name: #{instance.tags['Name'] || 'N/A'}"
@@ -43,4 +40,20 @@ class Ec2::Blackout::Startup
43
40
  def dry_run?
44
41
  @options[:dry_run]
45
42
  end
43
+
44
+ def associate_eip instance
45
+ eip = instance.tags['ec2:blackout:eip']
46
+ if eip
47
+ @ui.say("Associating Elastic IP #{eip} to #{instance.id}")
48
+ attempts = 0
49
+ begin
50
+ instance.associate_elastic_ip(eip)
51
+ rescue
52
+ sleep 5
53
+ attempts += 1
54
+ retry if attempts < 60 # 5 mins
55
+ end
56
+ instance.tags.delete('ec2:blackout:eip')
57
+ end
58
+ end
46
59
  end
@@ -1,6 +1,6 @@
1
1
  module Ec2
2
2
  module Blackout
3
- VERSION = "0.0.5"
3
+ VERSION = "0.0.6"
4
4
 
5
5
  def self.name
6
6
  'ec2-blackout'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ec2-blackout
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: