ec2-blackout 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ vendor/bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ec2-sleeper.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Stephen Bartlett
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,25 @@
1
+ # Ec2::Blackout
2
+
3
+ Want to reduce your EC2 costs?
4
+
5
+ ec2-blackout is a command-line tool to stop running EC2 instances.
6
+
7
+ Use ec2-blackout to shutdown EC2 instances when they are idle, for example when you are not in the office.
8
+
9
+ Definitely not suitable for production instances but development and test instances can generally be shutdown overnight to save money.
10
+
11
+ ## Installation
12
+
13
+ $ gem install ec2-blackout
14
+
15
+ ## Usage
16
+
17
+ $ ec2-blackout --help
18
+
19
+ ## Contributing
20
+
21
+ 1. Fork it
22
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
23
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
24
+ 4. Push to the branch (`git push origin my-new-feature`)
25
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ begin
4
+ require 'ec2-blackout/commands'
5
+ rescue LoadError
6
+ require 'rubygems'
7
+ require 'ec2-blackout/commands'
8
+ end
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/ec2-blackout/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Stephen Bartlett"]
6
+ gem.email = ["stephenb@rtlett.org"]
7
+ gem.description = Ec2::Blackout.description
8
+ gem.summary = Ec2::Blackout.summary
9
+ gem.homepage = "https://github.com/srbartlett/ec2-blackout"
10
+ gem.add_dependency 'commander'
11
+ gem.add_dependency 'aws-sdk'
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.name = "ec2-blackout"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = Ec2::Blackout::VERSION
18
+ end
@@ -0,0 +1,6 @@
1
+ require "ec2-blackout/version"
2
+ require 'commander'
3
+ require 'aws-sdk'
4
+ require 'ec2-blackout/shutdown'
5
+ require 'ec2-blackout/startup'
6
+
@@ -0,0 +1,44 @@
1
+ require 'ec2-blackout'
2
+ require 'commander/import'
3
+
4
+ program :name, Ec2::Blackout.name
5
+ program :version, Ec2::Blackout::VERSION
6
+ program :description, Ec2::Blackout.summary
7
+
8
+ default_command :help
9
+
10
+ def aws
11
+ AWS::EC2.new(:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
12
+ :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'])
13
+ end
14
+
15
+ DEFAULT_REGIONS = ['us-east-1', 'us-west-1', 'us-west-2', 'eu-west-1', 'ap-southeast-1',
16
+ 'ap-northeast-1', 'sa-east-1'].join(',')
17
+
18
+ command "on" do |c|
19
+ c.syntax = '[options]'
20
+ c.summary = 'Shutdown EC2 instances'
21
+ c.description = 'For each AWS Region, find running EC2 instances and shut them down.'
22
+
23
+ c.option '-r', '--regions STRING', 'Comma separated list of regions to search. Defaults to all regions'
24
+ c.option '-x', '--exclude-by-tag STRING', 'Exclude instances with the specified tag.'
25
+
26
+ c.action do |args, options|
27
+ options.default :regions => DEFAULT_REGIONS
28
+ Ec2::Blackout::Shutdown.new(self, aws, options.__hash__).execute
29
+ end
30
+ end
31
+
32
+ command "off" do |c|
33
+ c.syntax = '[options]'
34
+ c.summary = 'Start up EC2 instances'
35
+ c.description = 'For each AWS Region, find EC2 instances previously shutdown by ec2-blackout and start them up.'
36
+
37
+ c.option '-r', '--regions STRING', 'Comma separated list of regions to search. Defaults to all regions'
38
+ c.option '-f', '--force', 'Force start up regardless of who shut them dowm'
39
+
40
+ c.action do |args, options|
41
+ options.default :regions => DEFAULT_REGIONS
42
+ Ec2::Blackout::Startup.new(self, aws, options.__hash__).execute
43
+ end
44
+ end
@@ -0,0 +1,40 @@
1
+
2
+ class Ec2::Blackout::Shutdown
3
+
4
+ def initialize(ui, aws, options)
5
+ @ui, @aws, @options = ui, aws, options
6
+ end
7
+
8
+ def execute
9
+ regions.each do |region|
10
+ @ui.say "Checking region: #{region}"
11
+ AWS.memoize do
12
+ @aws.regions[region].instances.each do |instance|
13
+ if stoppable? instance
14
+ @ui.say "-> Stopping instance: #{instance.id}, name: #{instance.tags['Name']}"
15
+ instance.add_tag('ec2::blackout::on', Date.now.utc)
16
+ #instance.stop
17
+ elsif instance.status == :running
18
+ @ui.say "-> Skipping instance: #{instance.id}, name: #{instance.tags['Name']}, region: #{region}"
19
+ end
20
+ end
21
+ end
22
+ end
23
+ @ui.say 'Done!'
24
+ end
25
+
26
+ private
27
+
28
+ def regions
29
+ @options[:regions] ? @options[:regions].split(',') : Ec2::Blackout.regions
30
+ end
31
+
32
+ def stoppable? instance
33
+ (instance.status == :running && exclude_tag && !instance.tags[exclude_tag].nil?) or
34
+ (instance.status == :running && exclude_tag.nil?)
35
+ end
36
+
37
+ def exclude_tag
38
+ @options[:exclude_by_tag]
39
+ end
40
+ end
@@ -0,0 +1,37 @@
1
+
2
+ class Ec2::Blackout::Startup
3
+
4
+ def initialize(ui, aws, options)
5
+ @ui, @aws, @options = ui, aws, options
6
+ end
7
+
8
+ def execute
9
+ regions.each do |region|
10
+ @ui.say "Checking region: #{region}"
11
+ AWS.memoize do
12
+ @aws.regions[region].instances.each do |instance|
13
+ if startable? instance
14
+ @ui.say "-> Starting instance: #{instance.id}, name: #{instance.tags['Name']}"
15
+ instance.tags.delete('ec2::blackout::on')
16
+ instance.start
17
+ elsif instance.status == :stopped
18
+ @ui.say "-> Skipping instance: #{instance.id}, name: #{instance.tags['Name'] || 'N/A'}"
19
+ end
20
+ end
21
+ end
22
+ end
23
+ @ui.say 'Done!'
24
+ end
25
+
26
+ private
27
+
28
+ def regions
29
+ @options[:regions] ? @options[:regions].split(',') : Ec2::Blackout.regions
30
+ end
31
+
32
+ def startable? instance
33
+ (instance.status == :stopped && instance.tags['ec2::blackout::on']) ||
34
+ (instance.status == :stopped && @options[:force])
35
+ end
36
+
37
+ end
@@ -0,0 +1,23 @@
1
+ module Ec2
2
+ module Blackout
3
+ VERSION = "0.0.1"
4
+
5
+ def self.name
6
+ 'ec2-blackout'
7
+ end
8
+
9
+ def self.summary
10
+ 'A command-line tool to shutdown EC2 instances.'
11
+ end
12
+
13
+ def self.description
14
+ %q{
15
+ ec2-blackout is a command line tool to shutdown EC2 instances.
16
+
17
+ It's main purpose is to save you money by stopping EC2 instances during
18
+ out of office hours.
19
+ }
20
+ end
21
+
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ec2-blackout
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Stephen Bartlett
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: commander
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: aws-sdk
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: ! "\n ec2-blackout is a command line tool to shutdown EC2 instances.\n\n
47
+ \ It's main purpose is to save you money by stopping EC2 instances during\n
48
+ \ out of office hours.\n "
49
+ email:
50
+ - stephenb@rtlett.org
51
+ executables:
52
+ - ec2-blackout
53
+ extensions: []
54
+ extra_rdoc_files: []
55
+ files:
56
+ - .gitignore
57
+ - Gemfile
58
+ - LICENSE
59
+ - README.md
60
+ - Rakefile
61
+ - bin/ec2-blackout
62
+ - ec2-blackout.gemspec
63
+ - lib/ec2-blackout.rb
64
+ - lib/ec2-blackout/commands.rb
65
+ - lib/ec2-blackout/shutdown.rb
66
+ - lib/ec2-blackout/startup.rb
67
+ - lib/ec2-blackout/version.rb
68
+ homepage: https://github.com/srbartlett/ec2-blackout
69
+ licenses: []
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 1.8.23
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: A command-line tool to shutdown EC2 instances.
92
+ test_files: []