lib/meroku/aws.rb in meroku-2.0.22 vs lib/meroku/aws.rb in meroku-2.0.23

- old
+ new

@@ -1,14 +1,77 @@ -require 'meroku/aws/ec2' +# frozen_string_literal: true module Meroku - # Stuff related to Amazon Web Services + # Mostly wrappers around aws library functions module Aws - IP = '34.239.241.218' + include Meroku::Shared - KEY_NAME = "#{Dir.home}/.meroku/meroku.id_rsa" + def make_instance + @instance_id = Meroku::Shared.ec2_client.run_instances( + image_id: 'ami-841f46ff', + min_count: 1, + max_count: 1, + key_name: 'meroku.id_rsa', + instance_type: 't2.micro', + tag_specifications: [tag] + ).instances.first.instance_id + end - def self.bucket_url - "https://s3.amazonaws.com/#{ENV['SECRET']}" + def associate_address + retries ||= 0 + sleep 10 + begin + puts Meroku::Shared.ec2_client.associate_address( + allocation_id: allocation_id, instance_id: instance_id + ).class + sleep 20 + rescue ::Aws::EC2::Errors::InvalidInstanceID + (retries += 1) < 10 ? retry : raise + end + end + + # Private S3 Bucket + # The only security here is not no one knows the bucket name, + # which is a uuid + def bucket + "http://s3.amazonaws.com/#{Meroku::Shared.secrets.meroku_secret}/" + end + + def self.terminate_all(tag: nil) + Meroku::Shared.ec2_client.describe_instances( + filters: running_filter(tag) + ).reservations.map do |r| + id = r.instances.first.instance_id + puts "Will terminate #{id}" + Meroku::Shared.ec2_client.terminate_instances(instance_ids: [id]) + end + end + + # Our elastic ip allocation + def allocation_id + 'eipalloc-139f7823' + end + + def ip_address + '34.239.241.218' + end + + def tag + { + resource_type: 'instance', + tags: [ + { + key: 'Name', + value: 'node' + } + ] + } + end + + def self.running_filter(tag) + [ + { name: 'tag:Name', values: [tag] }, + { name: 'instance-state-name', values: %w[running pending] } + ] end end end