Class: EC2::Ec2Instance
- Inherits:
-
Object
- Object
- EC2::Ec2Instance
- Defined in:
- lib/etude_for_aws/ec2/ec2_instance.rb
Instance Attribute Summary collapse
-
#instance_id ⇒ Object
readonly
Returns the value of attribute instance_id.
Instance Method Summary collapse
- #create(security_group, key_pair) ⇒ Object
-
#initialize(ec2, instance_id = nil) ⇒ Ec2Instance
constructor
A new instance of Ec2Instance.
- #reboot ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #terminate ⇒ Object
Constructor Details
#initialize(ec2, instance_id = nil) ⇒ Ec2Instance
Returns a new instance of Ec2Instance.
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/etude_for_aws/ec2/ec2_instance.rb', line 5 def initialize(ec2,instance_id=nil) @config = ec2.config @gateway = ec2.gateway script = '' @encoded_script = Base64.encode64(script) @image_id = @config.image_id @instance_type = @config.instance_type @min_count = @config.min_count @max_count = @config.max_count = @config. @instance_id = instance_id end |
Instance Attribute Details
#instance_id ⇒ Object (readonly)
Returns the value of attribute instance_id.
3 4 5 |
# File 'lib/etude_for_aws/ec2/ec2_instance.rb', line 3 def instance_id @instance_id end |
Instance Method Details
#create(security_group, key_pair) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/etude_for_aws/ec2/ec2_instance.rb', line 18 def create(security_group,key_pair) instance = @gateway.create_instances(@image_id, @min_count, @max_count, key_pair.key_pair_name, security_group, @encoded_script, @instance_type, @config) instance.empty? ? @instance_id = nil : @instance_id = instance.first.id @gateway.wait_for_instance_status_ok(@instance_id) instance.({tags: }) instance end |
#reboot ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/etude_for_aws/ec2/ec2_instance.rb', line 92 def reboot i = @gateway.find_instance_by_id(@instance_id) if i.exists? case i.state.code when 48 # terminated puts "#{instance_id} is terminated, so you cannot reboot it" else puts "#{instance_id} is rebooting" i.reboot @gateway.wait_for_instance_status_ok(instance_id) end end end |
#start ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/etude_for_aws/ec2/ec2_instance.rb', line 50 def start i = @gateway.find_instance_by_id(@instance_id) unless i.nil? if i.exists? case i.state.code when 0 # pending puts "#{instance_id} is pending, so it will be running in a bit" when 16 # started puts "#{instance_id} is already started" when 48 # terminated puts "#{instance_id} is terminated, so you cannot start it" else puts "#{instance_id} is starting" i.start @gateway.wait_for_instance_running(instance_id) end end end end |
#stop ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/etude_for_aws/ec2/ec2_instance.rb', line 71 def stop i = @gateway.find_instance_by_id(@instance_id) unless i.nil? if i.exists? case i.state.code when 48 # terminated puts "#{instance_id} is terminated, so you cannot stop it" when 64 # stopping puts "#{instance_id} is stopping, so it will be stopped in a bit" when 89 # stopped puts "#{instance_id} is already stopped" else puts "#{instance_id} is stopping" i.stop @gateway.wait_for_instance_stopped(instance_id) end end end end |
#terminate ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/etude_for_aws/ec2/ec2_instance.rb', line 34 def terminate i = @gateway.find_instance_by_id(@instance_id) unless i.nil? if i.exists? case i.state.code when 48 # terminated puts "#{@instance_id} is already terminated" else i.terminate @gateway.wait_for_instance_terminated(@instance_id) end end end end |