ec2_switcher 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8fd93fa72656c21779f979493b066d1dbaf86ec3
4
+ data.tar.gz: 2faf4f04c8cbbcc422434dcbac2efd59c7c9778a
5
+ SHA512:
6
+ metadata.gz: 930f1ad137b7c9a9ad62496e33c0d718b576ba85263c00791ecbb8a3d1b091a6fca314ca1982d191c03c3f1df067c5035748cf2bdc0b33a4499bb7617a971379
7
+ data.tar.gz: c515f515af4f65df442a3198f389dfc5d9f0b36288a3c50a28a82445b9ce35d38273cb6227f0722017275ce7c3a6091194700f6dc44fc2ce6ba8fe0e14085268
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.12.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ec2_switcher.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # Ec2Switcher
2
+
3
+ Welcome to Ec2Switcher.
4
+ The purpose of this gem is to avoid you to pay for an AWS EC2 instance that you don't need running all the time.
5
+
6
+ Just use the gem when you want to switch off/on the instance.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'ec2_switcher'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install ec2_switcher
23
+
24
+
25
+ ## Configuration
26
+
27
+ **First Method**
28
+
29
+ Add as environment variables:
30
+
31
+ `AMAZON_REGION = 'your-region-1'`
32
+
33
+ `AMAZON_ACCESS_KEY = 'your-access-key'`
34
+
35
+ `AMAZON_SECRET_ACCESS = 'your-secret-access-key'`
36
+
37
+
38
+ **Second method**
39
+
40
+ If you have your credentials stored here `~/.aws/credentials`
41
+ create a profile called `ec2`
42
+
43
+ And prototype it like this ->
44
+ ```
45
+ [ec2]
46
+ aws_access_key_id = youraccesskey
47
+ aws_secret_access_key = yoursecretaccesskey
48
+ ```
49
+ **If your ec2 server is located somewhere else than `eu-west-1`, please fill up `AMAZON_REGION` variable**
50
+
51
+ ## Usage
52
+
53
+ Make sure you have auto-scaling disabled on your ec2 instance.
54
+
55
+ Initialize a new switcher with your instance-id `switcher = Switcher.new('i-yourid')`
56
+
57
+ Then juste use `switcher.auto` to turn on your instance if it's down / off if it's up
58
+
59
+
60
+ ### Pro-Tips
61
+
62
+ You can also use any ec2-instance command you want by using `switcher.ec2` variable.
63
+ Usable methods are here -> http://docs.aws.amazon.com/sdkforruby/api/Aws/EC2/Instance.html
64
+
65
+
66
+ ## Development
67
+
68
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
69
+
70
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
71
+
72
+ ## Contributing
73
+
74
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ec2_switcher.
75
+
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
80
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ec2_switcher"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ec2_switcher/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ec2_switcher"
8
+ spec.version = Ec2Switcher::VERSION
9
+ spec.authors = ["Kevin LEIBA"]
10
+ spec.email = ["kleiba@student.42.fr"]
11
+
12
+ spec.summary = %q{Turns on/off an ec2 instace}
13
+ spec.description = %q{Use this gem to simply turn on your ec2 instance when it's off and off when it's on}
14
+ spec.homepage = "http://bubblz.net"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.12"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec", "~> 3.0"
33
+
34
+ spec.add_dependency 'aws-sdk', '~> 2'
35
+ end
@@ -0,0 +1,3 @@
1
+ module Ec2Switcher
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,74 @@
1
+ require "ec2_switcher/version"
2
+ require "aws-sdk"
3
+
4
+ class Switcher
5
+
6
+ attr_accessor :ec2
7
+ attr_accessor :region
8
+
9
+ def initialize(id)
10
+ @region = set_region
11
+ @credentials = set_credentials
12
+ @aws = aws_connection
13
+ @ec2 = ec2_client_connect(id)
14
+ puts 'connected to ec2 server' if @ec2
15
+ end
16
+
17
+ def ec2_client_connect(id)
18
+ @id ||= id
19
+ Aws::EC2::Instance.new(credentials: @credentials, id: id)
20
+ end
21
+
22
+ def reload
23
+ @ec2 = self.ec2_client_connect(@id)
24
+ end
25
+
26
+ def running?
27
+ self.reload
28
+ @ec2.state.to_s.include?("name=\"running\"")
29
+ end
30
+
31
+ def off
32
+ @ec2.stop
33
+ puts 'instance stopping'
34
+ self.reload
35
+ end
36
+
37
+ def on
38
+ @ec2.start
39
+ puts 'instance starting'
40
+ self.reload
41
+ end
42
+
43
+ def auto
44
+ self.running? ? self.off : self.on
45
+ end
46
+
47
+ private
48
+
49
+ def constants_not_defined?
50
+ defined?(AMAZON_ACCESS_KEY).nil? || defined?(AMAZON_SECRET_ACCESS).nil?
51
+ end
52
+
53
+ def set_region
54
+ region = 'eu-west-1' if defined?(AMAZON_REGION).nil?
55
+ end
56
+
57
+ def set_credentials
58
+ if constants_not_defined?
59
+ puts 'profile connection'
60
+ Aws::SharedCredentials.new(profile_name: 'ec2')
61
+ else
62
+ puts 'global variables connection'
63
+ Aws::Credentials.new(AMAZON_ACCESS_KEY, AMAZON_SECRET_ACCESS)
64
+ end
65
+ end
66
+
67
+ def aws_connection
68
+ Aws.config.update({
69
+ region: @region,
70
+ credentials: @credentials
71
+ })
72
+ end
73
+
74
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ec2_switcher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Kevin LEIBA
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: aws-sdk
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2'
69
+ description: Use this gem to simply turn on your ec2 instance when it's off and off
70
+ when it's on
71
+ email:
72
+ - kleiba@student.42.fr
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - ec2_switcher.gemspec
87
+ - lib/ec2_switcher.rb
88
+ - lib/ec2_switcher/version.rb
89
+ homepage: http://bubblz.net
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.5.1
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Turns on/off an ec2 instace
113
+ test_files: []