Class: Switcher
- Inherits:
-
Object
- Object
- Switcher
- Defined in:
- lib/ec2_switcher.rb
Instance Attribute Summary collapse
-
#aws ⇒ Object
Returns the value of attribute aws.
-
#credentials ⇒ Object
Returns the value of attribute credentials.
-
#ec2 ⇒ Object
Returns the value of attribute ec2.
-
#region ⇒ Object
Returns the value of attribute region.
Instance Method Summary collapse
- #auto ⇒ Object
- #ec2_client_connect(id) ⇒ Object
-
#initialize(id) ⇒ Switcher
constructor
A new instance of Switcher.
- #off ⇒ Object
- #on ⇒ Object
- #reload ⇒ Object
- #running? ⇒ Boolean
Constructor Details
#initialize(id) ⇒ Switcher
Returns a new instance of Switcher.
11 12 13 14 15 16 17 |
# File 'lib/ec2_switcher.rb', line 11 def initialize(id) @region = set_region @credentials = set_credentials @aws = aws_connection @ec2 = ec2_client_connect(id) puts 'connected to ec2 server' if @ec2 end |
Instance Attribute Details
#aws ⇒ Object
Returns the value of attribute aws.
7 8 9 |
# File 'lib/ec2_switcher.rb', line 7 def aws @aws end |
#credentials ⇒ Object
Returns the value of attribute credentials.
9 10 11 |
# File 'lib/ec2_switcher.rb', line 9 def credentials @credentials end |
#ec2 ⇒ Object
Returns the value of attribute ec2.
6 7 8 |
# File 'lib/ec2_switcher.rb', line 6 def ec2 @ec2 end |
#region ⇒ Object
Returns the value of attribute region.
8 9 10 |
# File 'lib/ec2_switcher.rb', line 8 def region @region end |
Instance Method Details
#auto ⇒ Object
45 46 47 |
# File 'lib/ec2_switcher.rb', line 45 def auto self.running? ? self.off : self.on end |
#ec2_client_connect(id) ⇒ Object
19 20 21 22 |
# File 'lib/ec2_switcher.rb', line 19 def ec2_client_connect(id) @id ||= id Aws::EC2::Instance.new(credentials: @credentials, id: id) end |
#off ⇒ Object
33 34 35 36 37 |
# File 'lib/ec2_switcher.rb', line 33 def off @ec2.stop puts 'instance stopping' self.reload end |
#on ⇒ Object
39 40 41 42 43 |
# File 'lib/ec2_switcher.rb', line 39 def on @ec2.start puts 'instance starting' self.reload end |
#reload ⇒ Object
24 25 26 |
# File 'lib/ec2_switcher.rb', line 24 def reload @ec2 = self.ec2_client_connect(@id) end |
#running? ⇒ Boolean
28 29 30 31 |
# File 'lib/ec2_switcher.rb', line 28 def running? self.reload @ec2.state.to_s.include?("name=\"running\"") end |