Class: Rebi::EC2
- Inherits:
-
Object
- Object
- Rebi::EC2
- Defined in:
- lib/rebi/ec2.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
- #authorize_ssh(instance_id, &blk) ⇒ Object
- #describe_instance(instance_id) ⇒ Object
-
#initialize(client = Aws::EC2::Client.new) ⇒ EC2
constructor
A new instance of EC2.
- #log(mes) ⇒ Object
Constructor Details
#initialize(client = Aws::EC2::Client.new) ⇒ EC2
Returns a new instance of EC2.
6 7 8 |
# File 'lib/rebi/ec2.rb', line 6 def initialize client=Aws::EC2::Client.new @client = client end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
4 5 6 |
# File 'lib/rebi/ec2.rb', line 4 def client @client end |
Instance Method Details
#authorize_ssh(instance_id, &blk) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/rebi/ec2.rb', line 15 def instance_id, &blk group_id = describe_instance(instance_id).security_groups.map(&:group_id).sort.first my_ip = `dig +short myip.opendns.com @resolver1.opendns.com`.chomp cidr_ip = my_ip.present? ? "#{my_ip}/32" : "0.0.0.0/0" begin log "Attempting to open port 22." client.({ group_id: group_id, ip_protocol: "tcp", to_port: 22, from_port: 22, cidr_ip: cidr_ip }) log "SSH port 22 open." rescue Aws::EC2::Errors::InvalidPermissionDuplicate log "Opened already" rescue Exception => e raise e end yield if block_given? ensure begin log "Attempting to close port 22." client.revoke_security_group_ingress({ group_id: group_id, ip_protocol: "tcp", to_port: 22, from_port: 22, cidr_ip: cidr_ip }) rescue Exception => e raise e end end |
#describe_instance(instance_id) ⇒ Object
10 11 12 13 |
# File 'lib/rebi/ec2.rb', line 10 def describe_instance instance_id res = client.describe_instances instance_ids: [instance_id] return res.reservations.first.instances.first end |