Class: ConfigureS3Website::CloudFrontClient

Inherits:
Object
  • Object
show all
Defined in:
lib/configure-s3-website/cloudfront_client.rb

Class Method Summary collapse

Class Method Details

.create_distribution_if_user_agrees(options, standard_input) ⇒ Object



6
7
8
9
10
11
# File 'lib/configure-s3-website/cloudfront_client.rb', line 6

def self.create_distribution_if_user_agrees(options, 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 do_create_distribution options
  end
end

.default_cloudfront_settings(config_source) ⇒ Object



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
# File 'lib/configure-s3-website/cloudfront_client.rb', line 76

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' => (60 * 60 * 24)
    },
    'cache_behaviors' => {
      'quantity' => '0'
    },
    'price_class' => 'PriceClass_All'
  }
end

.distribution_config_xml(config_source, custom_cf_settings = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/configure-s3-website/cloudfront_client.rb', line 52

def self.distribution_config_xml(config_source, custom_cf_settings = {})
  %|
  <DistributionConfig xmlns="http://cloudfront.amazonaws.com/doc/2012-07-01/">
    <Origins>
      <Quantity>1</Quantity>
      <Items>
        <Origin>
          <Id>#{origin_id config_source}</Id>
          <DomainName>#{config_source.s3_bucket_name}.#{Endpoint.by_config_source(config_source).hostname}</DomainName>
          <S3OriginConfig>
            <OriginAccessIdentity></OriginAccessIdentity>
          </S3OriginConfig>
        </Origin>
      </Items>
    </Origins>
    #{
      XmlHelper.hash_to_api_xml(
        default_cloudfront_settings(config_source).merge custom_cf_settings
      )
    }
  </DistributionConfig>
  |
end

.do_create_distribution(options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/configure-s3-website/cloudfront_client.rb', line 15

def self.do_create_distribution(options)
  config_source = options[:config_source]
  response = HttpHelper.call_cloudfront_api(
    path = '/2012-07-01/distribution',
    method = Net::HTTP::Post,
    body = (distribution_config_xml config_source),
    config_source = config_source
  )
  response_xml = REXML::Document.new(response.body)
  dist_id = REXML::XPath.first(response_xml, '/Distribution/Id').get_text
  print_report_on_new_dist response_xml, dist_id, options
  config_source.cloudfront_distribution_id = dist_id.to_s
  puts "  Added setting 'cloudfront_distribution_id: #{dist_id}' into #{config_source.description}"
end

.origin_id(config_source) ⇒ Object



113
114
115
# File 'lib/configure-s3-website/cloudfront_client.rb', line 113

def self.origin_id(config_source)
  "#{config_source.s3_bucket_name}-S3-origin"
end


30
31
32
33
34
35
36
37
38
39
40
# File 'lib/configure-s3-website/cloudfront_client.rb', line 30

def self.print_report_on_new_dist(response_xml, dist_id, options)
  config_source = options[:config_source]
  domain_name = REXML::XPath.first(response_xml, '/Distribution/DomainName').get_text
  puts "  The distribution #{dist_id} at #{domain_name} now delivers the bucket #{config_source.s3_bucket_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 options[:verbose]
    puts '  Below is the response from the CloudFront API:'
    print_verbose(response_xml, left_padding = 4)
  end
end


42
43
44
45
46
47
48
49
50
# File 'lib/configure-s3-website/cloudfront_client.rb', line 42

def self.print_verbose(response_xml, left_padding)
  lines = []
  response_xml.write(lines, 2)
  padding = ""
  left_padding.times { padding << " " }
  puts lines.join().
    gsub(/^/, "" + padding).
    gsub(/\s$/, "")
end