Class: Bosh::AwsCloud::ResourceWait
- Inherits:
-
Object
- Object
- Bosh::AwsCloud::ResourceWait
- Includes:
- Helpers
- Defined in:
- lib/cloud/aws/resource_wait.rb
Constant Summary collapse
- DEFAULT_TRIES =
a sane amount of retries on AWS (~25 minutes), as things can take anywhere between a minute and forever
54
- MAX_SLEEP_EXPONENT =
5
Class Method Summary collapse
- .for_attachment(args) ⇒ Object
- .for_image(args) ⇒ Object
- .for_instance(args) ⇒ Object
- .for_sgroup(args) ⇒ Object
- .for_snapshot(args) ⇒ Object
- .for_subnet(args) ⇒ Object
- .for_volume(args) ⇒ Object
- .logger ⇒ Object
- .sleep_callback(description, tries) ⇒ Object
- .task_checkpoint ⇒ Object
- .validate_states(valid_states, target_state) ⇒ Object
Instance Method Summary collapse
- #for_resource(args, &blk) ⇒ Object
-
#initialize ⇒ ResourceWait
constructor
A new instance of ResourceWait.
- #time_passed ⇒ Object
Methods included from Helpers
#cloud_error, #extract_security_group_names
Constructor Details
#initialize ⇒ ResourceWait
Returns a new instance of ResourceWait.
150 151 152 |
# File 'lib/cloud/aws/resource_wait.rb', line 150 def initialize @started_at = Time.now end |
Class Method Details
.for_attachment(args) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/cloud/aws/resource_wait.rb', line 34 def self.(args) = args.fetch(:attachment) { raise ArgumentError, 'attachment object required' } target_state = args.fetch(:state) { raise ArgumentError, 'state symbol required' } valid_states = [:attached, :detached] validate_states(valid_states, target_state) ignored_errors = [] if target_state == :attached ignored_errors << AWS::Core::Resource::NotFound end description = "volume %s to be %s to instance %s as device %s" % [ .volume.id, target_state, .instance.id, .device ] new.for_resource(resource: , errors: ignored_errors, target_state: target_state, description: description) do |current_state| current_state == target_state end rescue AWS::Core::Resource::NotFound # if an attachment is detached, AWS can reap the object and the reference is no longer found, # so consider this exception a success condition if we are detaching raise unless target_state == :detached end |
.for_image(args) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/cloud/aws/resource_wait.rb', line 57 def self.for_image(args) image = args.fetch(:image) { raise ArgumentError, 'image object required' } target_state = args.fetch(:state) { raise ArgumentError, 'state symbol required' } valid_states = [:available, :deleted] validate_states(valid_states, target_state) ignored_errors = [] if target_state == :available ignored_errors = [AWS::EC2::Errors::InvalidAMIID::NotFound] end new.for_resource(resource: image, errors: ignored_errors, target_state: target_state, state_method: :state) do |current_state| current_state == target_state end rescue AWS::Core::Resource::NotFound # if an AMI is deleted, AWS can reap the object and the reference is no longer found, # so consider this exception a success condition if we are deleting raise unless target_state == :deleted end |
.for_instance(args) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/cloud/aws/resource_wait.rb', line 12 def self.for_instance(args) raise ArgumentError, "args should be a Hash, but `#{args.class}' given" unless args.is_a?(Hash) instance = args.fetch(:instance) { raise ArgumentError, 'instance object required' } target_state = args.fetch(:state) { raise ArgumentError, 'state symbol required' } valid_states = [:running, :terminated] validate_states(valid_states, target_state) ignored_errors = [ AWS::EC2::Errors::InvalidInstanceID::NotFound, AWS::Core::Resource::NotFound ] new.for_resource(resource: instance, errors: ignored_errors, target_state: target_state) do |current_state| if target_state == :running && current_state == :terminated logger.error("instance #{instance.id} terminated while starting") raise Bosh::Clouds::VMCreationFailed.new(true) else current_state == target_state end end end |
.for_sgroup(args) ⇒ Object
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/cloud/aws/resource_wait.rb', line 116 def self.for_sgroup(args) sgroup = args.fetch(:sgroup) { raise ArgumentError, 'sgroup object required' } target_state = args.fetch(:state) { raise ArgumentError, 'state symbol required' } valid_states = [true, false] validate_states(valid_states, target_state) new.for_resource(resource: sgroup, target_state: true, state_method: :exists?) do |current_state| current_state == target_state end end |
.for_snapshot(args) ⇒ Object
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/cloud/aws/resource_wait.rb', line 92 def self.for_snapshot(args) snapshot = args.fetch(:snapshot) { raise ArgumentError, 'snapshot object required' } target_state = args.fetch(:state) { raise ArgumentError, 'state symbol required' } valid_states = [:completed] validate_states(valid_states, target_state) new.for_resource(resource: snapshot, target_state: target_state, tries: 18) do |current_state| current_state == target_state end end |
.for_subnet(args) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/cloud/aws/resource_wait.rb', line 103 def self.for_subnet(args) subnet = args.fetch(:subnet) { raise ArgumentError, 'subnet object required' } target_state = args.fetch(:state) { raise ArgumentError, 'state symbol required' } valid_states = [:available] validate_states(valid_states, target_state) ignored_errors = [AWS::EC2::Errors::InvalidSubnetID::NotFound] new.for_resource(resource: subnet, target_state: target_state, errors: ignored_errors, state_method: :state) do |current_state| current_state == target_state end end |
.for_volume(args) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/cloud/aws/resource_wait.rb', line 77 def self.for_volume(args) volume = args.fetch(:volume) { raise ArgumentError, 'volume object required' } target_state = args.fetch(:state) { raise ArgumentError, 'state symbol required' } valid_states = [:available, :deleted] validate_states(valid_states, target_state) new.for_resource(resource: volume, target_state: target_state) do |current_state| current_state == target_state end rescue AWS::EC2::Errors::InvalidVolume::NotFound # if an volume is deleted, AWS can reap the object and the reference is no longer found, # so consider this exception a success condition if we are deleting raise unless target_state == :deleted end |
.logger ⇒ Object
142 143 144 |
# File 'lib/cloud/aws/resource_wait.rb', line 142 def self.logger Bosh::Clouds::Config.logger end |
.sleep_callback(description, tries) ⇒ Object
133 134 135 136 137 138 139 140 |
# File 'lib/cloud/aws/resource_wait.rb', line 133 def self.sleep_callback(description, tries) lambda do |num_tries, error| sleep_time = 2**[num_tries, MAX_SLEEP_EXPONENT].min # Exp backoff: 1, 2, 4, 8 ... up to max 32 Bosh::AwsCloud::ResourceWait.logger.debug("#{error.class}: `#{error.}'") if error Bosh::AwsCloud::ResourceWait.logger.debug("#{description}, retrying in #{sleep_time} seconds (#{num_tries}/#{tries})") sleep_time end end |
.task_checkpoint ⇒ Object
146 147 148 |
# File 'lib/cloud/aws/resource_wait.rb', line 146 def self.task_checkpoint Bosh::Clouds::Config.task_checkpoint end |
.validate_states(valid_states, target_state) ⇒ Object
127 128 129 130 131 |
# File 'lib/cloud/aws/resource_wait.rb', line 127 def self.validate_states(valid_states, target_state) unless valid_states.include?(target_state) raise ArgumentError, "target state must be one of #{valid_states.join(', ')}, `#{target_state}' given" end end |
Instance Method Details
#for_resource(args, &blk) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/cloud/aws/resource_wait.rb', line 154 def for_resource(args, &blk) resource = args.fetch(:resource) state_method = args.fetch(:state_method, :status) errors = args.fetch(:errors, []) desc = args.fetch(:description) { resource.id } tries = args.fetch(:tries, DEFAULT_TRIES).to_i target_state = args.fetch(:target_state) sleep_cb = self.class.sleep_callback("Waiting for #{desc} to be #{target_state}", tries) errors << AWS::EC2::Errors::RequestLimitExceeded ensure_cb = Proc.new do |retries| cloud_error("Timed out waiting for #{desc} to be #{target_state}, took #{time_passed}s") if retries == tries end state = nil Bosh::Retryable.new(tries: tries, sleep: sleep_cb, on: errors, ensure: ensure_cb).retryer do Bosh::AwsCloud::ResourceWait.task_checkpoint state = resource.method(state_method).call if state == :error || state == :failed raise Bosh::Clouds::CloudError, "#{desc} state is #{state}, expected #{target_state}, took #{time_passed}s" end # the yielded block should return true if we have reached the target state blk.call(state) end Bosh::AwsCloud::ResourceWait.logger.info("#{desc} is now #{state}, took #{time_passed}s") rescue Bosh::Common::RetryCountExceeded => e Bosh::AwsCloud::ResourceWait.logger.error( "Timed out waiting for #{desc} state is #{state}, expected to be #{target_state}, took #{time_passed}s") raise e end |
#time_passed ⇒ Object
188 189 190 |
# File 'lib/cloud/aws/resource_wait.rb', line 188 def time_passed Time.now - @started_at end |