Skip to content
This repository was archived by the owner on Jun 5, 2020. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 39 additions & 22 deletions lib/puppet/provider/ec2_instance/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,49 @@ def initialize(value={})
end

def self.instances
regions.collect do |region|
begin
instances = []
subnets = Hash.new()

subnets_response = ec2_client(region).describe_subnets()
subnets_response.data.subnets.each do |subnet|
subnet_name = name_from_tag(subnet)
subnets[subnet.subnet_id] = subnet_name if subnet_name
end

ec2_client(region).describe_instances(filters: [
{name: 'instance-state-name', values: ['pending', 'running', 'stopping', 'stopped']}
]).each do |response|
response.data.reservations.each do |reservation|
reservation.instances.each do |instance|
hash = instance_to_hash(region, instance, subnets)
instances << new(hash) if has_name?(hash)
mutex = Mutex.new
results = []
threads = []
ex = nil

regions.each do |region|
threads << Thread.new(region, results) do |region, results|
begin
instances = []
subnets = Hash.new()
subnets_response = ec2_client(region).describe_subnets()
subnets_response.data.subnets.each do |subnet|
subnet_name = name_from_tag(subnet)
subnets[subnet.subnet_id] = subnet_name if subnet_name
end
ec2_client(region).describe_instances(filters: [
{name: 'instance-state-name', values: ['pending', 'running', 'stopping', 'stopped']}
]).each do |response|
response.data.reservations.each do |reservation|
reservation.instances.each do |instance|
hash = instance_to_hash(region, instance, subnets)
instances << new(hash) if has_name?(hash)
end
end
mutex.synchronize { results << instances }
end
rescue Timeout::Error, StandardError => e
err = PuppetX::Puppetlabs::FetchingAWSDataError.new(region, self.resource_type.name.to_s, e.message)
mutex.synchronize {
if !ex
ex = err
end
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: is this the best way to propagate errors from the spawned threads? The downside I see here is that if an error occurs, we are still waiting for all threads to finish before we can raise it.

end
instances
rescue Timeout::Error, StandardError => e
raise PuppetX::Puppetlabs::FetchingAWSDataError.new(region, self.resource_type.name.to_s, e.message)
end
end.flatten
end

if ex
raise ex
end

threads.each(&:join)
results.flatten
end

read_only(:instance_id, :instance_type, :image_id, :region, :user_data,
Expand Down