lib/meroku/infrastructure.rb in meroku-2.0.0 vs lib/meroku/infrastructure.rb in meroku-2.0.1
- old
+ new
@@ -1,24 +1,17 @@
+require "meroku/infrastructure/node"
+
module Meroku
module Infrastructure
- def self.spawn
- credentials = Aws::Credentials.new(ENV['AWS_ACCESS_KEY'], ENV['AWS_SECRET'])
- ec2_client = Aws::EC2::Client.new(region: 'us-east-1', credentials: credentials)
- ec2_client.run_instances(
- image_id: 'ami-cd0f5cb6',
- min_count: 1,
- max_count: 1,
- key_name: 'meroku.id_rsa',
- instance_type: 't2.micro',
- ).instances.first
- end
+ extend Meroku::Aws
+
def self.despawn
- resp = nil
- credentials = Aws::Credentials.new(ENV['AWS_ACCESS_KEY'], ENV['AWS_SECRET'])
- ec2 = Aws::EC2::Resource.new(region: 'us-east-1', credentials: credentials)
- running_instance = ec2.instances.detect { |i| i.exists? && i.state.code!=48 }
- resp = running_instance.terminate if running_instance
- resp
+ instances = ec2_client.describe_instances(filters:[{ name: "tag:Name", values: ['node'] }, { name: 'instance-state-name', values: ['running','pending'] }]).reservations.map { |xx| xx.instances.first.instance_id }
+ puts "will despawn #{instances.inspect}"
+ ec2_client.terminate_instances({
+ instance_ids: instances
+ }) if instances.size > 0
end
end
+
end