Class: ConfigureS3Website::CloudFrontClient
- Inherits:
-
Object
- Object
- ConfigureS3Website::CloudFrontClient
- Defined in:
- lib/configure-s3-website/cloudfront_client.rb
Class Method Summary collapse
- .apply_distribution_config(options) ⇒ Object
- .create_distribution(options) ⇒ Object
- .create_distribution_if_user_agrees(options, standard_input) ⇒ Object
-
.default_cloudfront_settings(config_source) ⇒ Object
Changing these default settings probably necessitates a backward incompatible release.
- .distribution_config_xml(config_source, custom_cf_settings) ⇒ Object
- .origin_id(config_source) ⇒ Object
- .padding(amount) ⇒ Object
- .print_report_on_custom_distribution_config(custom_distribution_config, left_padding = 4) ⇒ Object
- .print_report_on_new_dist(response_xml, dist_id, options, config_source) ⇒ Object
- .print_verbose_response_from_cloudfront(response_xml, left_padding = 4) ⇒ Object
Class Method Details
.apply_distribution_config(options) ⇒ Object
6 7 8 9 10 11 12 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 41 42 43 44 |
# File 'lib/configure-s3-website/cloudfront_client.rb', line 6 def self.apply_distribution_config() config_source = [:config_source] puts "Detected an existing CloudFront distribution (id #{config_source.cloudfront_distribution_id}) ..." # Get caller reference and ETag (will be required by the PUT config resource) response = HttpHelper.call_cloudfront_api( path = "/2012-07-01/distribution/#{config_source.cloudfront_distribution_id}/config", method = Net::HTTP::Get, body = '', config_source ) etag = response['ETag'] caller_reference = REXML::XPath.first( REXML::Document.new(response.body), '/DistributionConfig/CallerReference' ).get_text.to_s # Call the PUT config resource with the caller reference and ETag custom_distribution_config = config_source.cloudfront_distribution_config || {} custom_distribution_config_with_caller_ref = custom_distribution_config.merge({ 'caller_reference' => caller_reference, 'comment' => 'Updated by the configure-s3-website gem' }) HttpHelper.call_cloudfront_api( path = "/2012-07-01/distribution/#{[:config_source].cloudfront_distribution_id}/config", method = Net::HTTP::Put, body = distribution_config_xml( config_source, custom_distribution_config_with_caller_ref ), config_source, headers = { 'If-Match' => etag } ) # Report unless custom_distribution_config.empty? print_report_on_custom_distribution_config custom_distribution_config end end |
.create_distribution(options) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/configure-s3-website/cloudfront_client.rb', line 55 def self.create_distribution() config_source = [:config_source] custom_distribution_config = config_source.cloudfront_distribution_config || {} response_xml = REXML::Document.new( HttpHelper.call_cloudfront_api( '/2012-07-01/distribution', Net::HTTP::Post, distribution_config_xml(config_source, custom_distribution_config), config_source ).body ) dist_id = REXML::XPath.first(response_xml, '/Distribution/Id').get_text print_report_on_new_dist response_xml, dist_id, , config_source config_source.cloudfront_distribution_id = dist_id.to_s puts " Added setting 'cloudfront_distribution_id: #{dist_id}' into #{config_source.description}" unless custom_distribution_config.empty? print_report_on_custom_distribution_config custom_distribution_config end end |
.create_distribution_if_user_agrees(options, standard_input) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/configure-s3-website/cloudfront_client.rb', line 46 def self.create_distribution_if_user_agrees(, standard_input) puts 'Do you want to deliver your website via CloudFront, the CDN of Amazon? [y/N]' case standard_input.gets.chomp when /(y|Y)/ then create_distribution end end |
.default_cloudfront_settings(config_source) ⇒ Object
Changing these default settings probably necessitates a backward incompatible release.
If you change these settings, remember to update also the README.md.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/configure-s3-website/cloudfront_client.rb', line 140 def self.default_cloudfront_settings(config_source) { 'caller_reference' => 'configure-s3-website gem ' + Time.now.to_s, 'default_root_object' => 'index.html', 'logging' => { 'enabled' => 'false', 'include_cookies' => 'false', 'bucket' => '', 'prefix' => '' }, 'enabled' => 'true', 'comment' => 'Created by the configure-s3-website gem', 'aliases' => { 'quantity' => '0' }, 'default_cache_behavior' => { 'target_origin_id' => (origin_id config_source), 'trusted_signers' => { 'enabled' => 'false', 'quantity' => '0' }, 'forwarded_values' => { 'query_string' => 'true', 'cookies' => { 'forward' => 'all' } }, 'viewer_protocol_policy' => 'allow-all', 'min_TTL' => '86400' }, 'cache_behaviors' => { 'quantity' => '0' }, 'price_class' => 'PriceClass_All' } end |
.distribution_config_xml(config_source, custom_cf_settings) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/configure-s3-website/cloudfront_client.rb', line 108 def self.distribution_config_xml(config_source, custom_cf_settings) domain_name = "#{config_source.s3_bucket_name}.#{Endpoint.by_config_source(config_source).website_hostname}" %| <DistributionConfig xmlns="http://cloudfront.amazonaws.com/doc/2012-07-01/"> <Origins> <Quantity>1</Quantity> <Items> <Origin> <Id>#{origin_id config_source}</Id> <DomainName>#{domain_name}</DomainName> <CustomOriginConfig> <HTTPPort>80</HTTPPort> <HTTPSPort>443</HTTPSPort> <OriginProtocolPolicy>match-viewer</OriginProtocolPolicy> </CustomOriginConfig> </Origin> </Items> </Origins> #{ require 'deep_merge' settings = default_cloudfront_settings config_source settings.deep_merge! custom_cf_settings XmlHelper.hash_to_api_xml(settings) } </DistributionConfig> | end |
.origin_id(config_source) ⇒ Object
177 178 179 |
# File 'lib/configure-s3-website/cloudfront_client.rb', line 177 def self.origin_id(config_source) "#{config_source.s3_bucket_name}-S3-origin" end |
.padding(amount) ⇒ Object
181 182 183 184 185 |
# File 'lib/configure-s3-website/cloudfront_client.rb', line 181 def self.padding(amount) padding = '' amount.times { padding << " " } padding end |
.print_report_on_custom_distribution_config(custom_distribution_config, left_padding = 4) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/configure-s3-website/cloudfront_client.rb', line 75 def self.print_report_on_custom_distribution_config(custom_distribution_config, left_padding = 4) puts ' Applied custom distribution settings:' puts custom_distribution_config. to_yaml. to_s. gsub("---\n", ''). gsub(/^/, padding(left_padding)) end |
.print_report_on_new_dist(response_xml, dist_id, options, config_source) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/configure-s3-website/cloudfront_client.rb', line 84 def self.print_report_on_new_dist(response_xml, dist_id, , config_source) config_source = [:config_source] dist_domain_name = REXML::XPath.first(response_xml, '/Distribution/DomainName').get_text s3_website_domain_name = REXML::XPath.first( response_xml, '/Distribution/DistributionConfig/Origins/Items/Origin/DomainName' ).get_text puts " The distribution #{dist_id} at #{dist_domain_name} now delivers the origin #{s3_website_domain_name}" puts ' Please allow up to 15 minutes for the distribution to initialise' puts ' For more information on the distribution, see https://console.aws.amazon.com/cloudfront' if [:verbose] puts ' Below is the response from the CloudFront API:' print_verbose_response_from_cloudfront(response_xml) end end |
.print_verbose_response_from_cloudfront(response_xml, left_padding = 4) ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/configure-s3-website/cloudfront_client.rb', line 100 def self.print_verbose_response_from_cloudfront(response_xml, left_padding = 4) lines = [] response_xml.write(lines, 2) puts lines.join(). gsub(/^/, "" + padding(left_padding)). gsub(/\s$/, "") end |