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_TIME =
15
- DEFAULT_WAIT_ATTEMPTS =
10 minutes
600 / MAX_SLEEP_TIME
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, options) ⇒ 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, #default_ephemeral_disk_mapping, #ebs_ephemeral_disk_mapping, #extract_security_groups
Constructor Details
#initialize ⇒ ResourceWait
Returns a new instance of ResourceWait.
163 164 165 |
# File 'lib/cloud/aws/resource_wait.rb', line 163 def initialize @started_at = Time.now end |
Class Method Details
.for_attachment(args) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/cloud/aws/resource_wait.rb', line 42 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
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/cloud/aws/resource_wait.rb', line 65 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
13 14 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 |
# File 'lib/cloud/aws/resource_wait.rb', line 13 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 = [ Bosh::Retryable::ErrorMatcher.new( AWS::Errors::ServerError, /The service is unavailable. Please try again shortly./, ), ] if target_state == :running ignored_errors << AWS::EC2::Errors::InvalidInstanceID::NotFound ignored_errors << AWS::Core::Resource::NotFound end 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
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/cloud/aws/resource_wait.rb', line 124 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
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/cloud/aws/resource_wait.rb', line 100 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) do |current_state| current_state == target_state end end |
.for_subnet(args) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/cloud/aws/resource_wait.rb', line 111 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
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/cloud/aws/resource_wait.rb', line 85 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
159 160 161 |
# File 'lib/cloud/aws/resource_wait.rb', line 159 def self.logger Bosh::Clouds::Config.logger end |
.sleep_callback(description, options) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/cloud/aws/resource_wait.rb', line 141 def self.sleep_callback(description, ) max_sleep_time = .fetch(:max, MAX_SLEEP_TIME) lambda do |num_tries, error| if [:tries_before_max] && num_tries >= [:tries_before_max] time = max_sleep_time else if [:exponential] time = [[:interval] ** num_tries, max_sleep_time].min else time = [1 + [:interval] * num_tries, max_sleep_time].min end end Bosh::AwsCloud::ResourceWait.logger.debug("#{error.class}: `#{error.}'") if error Bosh::AwsCloud::ResourceWait.logger.debug("#{description}, retrying in #{time} seconds (#{num_tries}/#{[:total]})") time end end |
.validate_states(valid_states, target_state) ⇒ Object
135 136 137 138 139 |
# File 'lib/cloud/aws/resource_wait.rb', line 135 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
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/cloud/aws/resource_wait.rb', line 167 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}", { interval: 2, total: tries, max: 32, exponential: true } ) 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 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
203 204 205 |
# File 'lib/cloud/aws/resource_wait.rb', line 203 def time_passed Time.now - @started_at end |