Class: Chef::Provisioning::AWSDriver::AWSTagger

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/chef/provisioning/aws_driver/aws_tagger.rb

Overview

itself. Fill in the hook methods so it knows how to tag itself.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tagging_strategy, action_handler) ⇒ AWSTagger

Returns a new instance of AWSTagger.



11
12
13
14
# File 'lib/chef/provisioning/aws_driver/aws_tagger.rb', line 11

def initialize(tagging_strategy, action_handler)
  @tagging_strategy = tagging_strategy
  @action_handler = action_handler
end

Instance Attribute Details

#action_handlerObject (readonly)

Returns the value of attribute action_handler.



9
10
11
# File 'lib/chef/provisioning/aws_driver/aws_tagger.rb', line 9

def action_handler
  @action_handler
end

Instance Method Details

#converge_tagsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/chef/provisioning/aws_driver/aws_tagger.rb', line 18

def converge_tags
  if desired_tags.nil?
    Chef::Log.debug "aws_tags not provided, nothing to converge"
    return
  end

  # Duplication and normalization
  # ::Aws::EC2::Errors::InvalidParameterValue: Tag value cannot be null. Use empty string instead.
  n_desired_tags = Hash[desired_tags.map {|k,v| [k.to_s, v.to_s]}]
  n_current_tags = Hash[current_tags.map {|k,v| [k.to_s, v.to_s]}]

  tags_to_set = n_desired_tags.reject {|k,v| n_current_tags[k] && n_current_tags[k] == v}
  tags_to_delete = n_current_tags.keys - n_desired_tags.keys
  # We don't want to delete `Name`, just all other tags
  # Tag keys and values are case sensitive - `Name` is special because it
  # shows as the name in the console
  tags_to_delete.delete('Name')

  # Tagging frequently fails so we retry with an exponential backoff, a maximum of 10 seconds
  Retryable.retryable(
    :tries => 20,
    :sleep => lambda { |n| [2**n, 10].min },
    :on => [AWS::Errors::Base, Aws::Errors::ServiceError,]
  ) do |retries, exception|
    if retries > 0
      Chef::Log.info "Retrying the tagging, previous try failed with #{exception.inspect}"
    end
    unless tags_to_set.empty?
      action_handler.perform_action "creating tags #{tags_to_set}" do
        set_tags(tags_to_set)
      end
      tags_to_set = []
    end
    unless tags_to_delete.empty?
      action_handler.perform_action "deleting tags #{tags_to_delete}" do
        delete_tags(tags_to_delete)
      end
      tags_to_delete = []
    end
  end
end