Class: Cumulus::CloudFront::Manager

Inherits:
Cumulus::Common::Manager show all
Defined in:
lib/cloudfront/manager/Manager.rb

Instance Method Summary collapse

Methods inherited from Cumulus::Common::Manager

#diff, #diff_one, #filter_local, #list, #sync, #sync_one

Constructor Details

#initializeManager

Returns a new instance of Manager.



14
15
16
17
# File 'lib/cloudfront/manager/Manager.rb', line 14

def initialize
  super()
  @cloudfront = Aws::CloudFront::Client.new(Configuration.instance.client)
end

Instance Method Details

#added_diff(local) ⇒ Object



41
42
43
# File 'lib/cloudfront/manager/Manager.rb', line 41

def added_diff(local)
  DistributionDiff.added(local)
end

#aws_resourcesObject



27
28
29
# File 'lib/cloudfront/manager/Manager.rb', line 27

def aws_resources
  @aws_resources ||= CloudFront::id_distributions
end

#create(local) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/cloudfront/manager/Manager.rb', line 116

def create(local)
  create_config = {
    distribution_config: {
      caller_reference: local.name,
      aliases: {
        quantity: local.aliases.size,
        items: if local.aliases.empty? then nil else local.aliases end
      },
      origins: {
        quantity: local.origins.size,
        items: if local.origins.empty? then nil else local.origins.map(&:to_aws) end
      },
      default_cache_behavior: local.default_cache_behavior.to_aws,
      cache_behaviors: {
        quantity: local.cache_behaviors.size,
        items: if local.cache_behaviors.empty? then nil else local.cache_behaviors.map(&:to_aws) end
      },
      comment: local.comment,
      enabled: local.enabled
    }
  }

  local.id = @cloudfront.create_distribution(create_config).distribution.id

  # Save the updated local config with id
  File.open("#{Configuration.instance.cloudfront.distributions_directory}/#{local.name}.json", "w") { |f| f.write(local.pretty_json) }
  puts "Distribution #{local.name} created with id #{local.id}"

rescue Aws::CloudFront::Errors::InvalidArgument => e
  if e.message =~ /OriginSslProtocols is required/
    puts Colors.red("Distribution #{local.name} must specify $.custom-origin-config.origin-ssl-protocols when \"protocol-policy\" is \"https-only\". Distribution not created")
    StatusCodes.set_status(StatusCodes::EXCEPTION)
  end
rescue => e
  puts "Failed to create distribution #{local.name}\n#{e}"
end

#diff_resource(local, aws) ⇒ Object



45
46
47
# File 'lib/cloudfront/manager/Manager.rb', line 45

def diff_resource(local, aws)
  local.diff(full_distribution(aws.id).distribution_config)
end

#full_distribution(distribution_id) ⇒ Object



31
32
33
34
35
# File 'lib/cloudfront/manager/Manager.rb', line 31

def full_distribution(distribution_id)
  @full_aws_configs ||= Hash.new

  @full_aws_configs[distribution_id] ||= CloudFront::load_distribution_config(distribution_id)
end

#invalidate(invalidation_name) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/cloudfront/manager/Manager.rb', line 161

def invalidate(invalidation_name)

  invalidation = invalidations[invalidation_name]

  # Use a combination of the current time and md5 of paths to prevent
  # identical invalidations from being ran too often
  time_throttle = (Time.now.to_i / 60 / 5)
  md5 = Digest::MD5.hexdigest(invalidation.paths.join)[0..5]

  @cloudfront.create_invalidation({
    distribution_id: invalidation.distribution_id,
    invalidation_batch: {
      paths: {
        quantity: invalidation.paths.size,
        items: if !invalidation.paths.empty? then invalidation.paths end
      },
      caller_reference: "#{invalidation_name}-#{md5}-#{time_throttle}"
    }
  })

end

#invalidationsObject



153
154
155
# File 'lib/cloudfront/manager/Manager.rb', line 153

def invalidations
  @invalidations ||= Hash[Loader.invalidations.map { |local| [local.name, local] }]
end

#list_invalidationsObject



157
158
159
# File 'lib/cloudfront/manager/Manager.rb', line 157

def list_invalidations
  puts invalidations.keys.join(" ")
end

#local_resourcesObject



23
24
25
# File 'lib/cloudfront/manager/Manager.rb', line 23

def local_resources
  @local_resources ||= Hash[Loader.distributions.map { |local| [local.id, local] }]
end

#migrateObject

Migrate AWS CloudFront distributions to local config



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cloudfront/manager/Manager.rb', line 50

def migrate
  distributions_dir = "#{@migration_root}/distributions"

  if !Dir.exists?(@migration_root)
    Dir.mkdir(@migration_root)
  end
  if !Dir.exists?(distributions_dir)
    Dir.mkdir(distributions_dir)
  end

  aws_resources.each_key do |dist_id|
    puts "Processing #{dist_id}..."
    full_config = full_distribution(dist_id).distribution_config

    config = DistributionConfig.new(dist_id)
    config.populate!(dist_id, full_config)

    puts "Writing #{dist_id} configuration to file"
    File.open("#{distributions_dir}/#{dist_id}.json", "w") { |f| f.write(config.pretty_json) }
  end
end

#resource_nameObject



19
20
21
# File 'lib/cloudfront/manager/Manager.rb', line 19

def resource_name
  "CloudFront Distribution"
end

#unmanaged_diff(aws) ⇒ Object



37
38
39
# File 'lib/cloudfront/manager/Manager.rb', line 37

def unmanaged_diff(aws)
  DistributionDiff.unmanaged(aws)
end

#update(local, diffs) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cloudfront/manager/Manager.rb', line 72

def update(local, diffs)
  if !diffs.empty?
    full_aws_response = full_distribution(local.id)

    aws_config = full_aws_response.distribution_config

    updated_config = {
      aliases: {
        quantity: local.aliases.size,
        items: if local.aliases.empty? then nil else local.aliases end
      },
      origins: {
        quantity: local.origins.size,
        items: if local.origins.empty? then nil else local.origins.map(&:to_aws) end
      },
      default_cache_behavior: local.default_cache_behavior.to_aws,
      cache_behaviors: {
        quantity: local.cache_behaviors.size,
        items: if local.cache_behaviors.empty? then nil else local.cache_behaviors.map(&:to_aws) end
      },
      comment: local.comment,
      enabled: local.enabled
    }

    update_params = {
      id: local.id,
      if_match: full_aws_response.etag,
      distribution_config: aws_config.to_h.merge(updated_config),
    }

    begin
      @cloudfront.update_distribution(update_params)
    rescue Aws::CloudFront::Errors::InvalidArgument => e
      if e.message =~ /OriginSslProtocols is required/
        puts Colors.red("Distribution #{local.name} must specify $.custom-origin-config.origin-ssl-protocols when \"protocol-policy\" is \"https-only\". Distribution not updated")
        StatusCodes.set_status(StatusCodes::EXCEPTION)
      else
        throw e
      end
    end
  end

end