Class: Switcher

Inherits:
Object
  • Object
show all
Defined in:
lib/ec2_switcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#awsObject

Returns the value of attribute aws.



7
8
9
# File 'lib/ec2_switcher.rb', line 7

def aws
  @aws
end

#credentialsObject

Returns the value of attribute credentials.



9
10
11
# File 'lib/ec2_switcher.rb', line 9

def credentials
  @credentials
end

#ec2Object

Returns the value of attribute ec2.



6
7
8
# File 'lib/ec2_switcher.rb', line 6

def ec2
  @ec2
end

#regionObject

Returns the value of attribute region.



8
9
10
# File 'lib/ec2_switcher.rb', line 8

def region
  @region
end

Instance Method Details

#autoObject



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

#offObject



33
34
35
36
37
# File 'lib/ec2_switcher.rb', line 33

def off
  @ec2.stop
  puts 'instance stopping'
  self.reload
end

#onObject



39
40
41
42
43
# File 'lib/ec2_switcher.rb', line 39

def on
  @ec2.start
  puts 'instance starting'
  self.reload
end

#reloadObject



24
25
26
# File 'lib/ec2_switcher.rb', line 24

def reload
  @ec2 = self.ec2_client_connect(@id)
end

#running?Boolean

Returns:

  • (Boolean)


28
29
30
31
# File 'lib/ec2_switcher.rb', line 28

def running?
  self.reload
  @ec2.state.to_s.include?("name=\"running\"")
end