aws-cloudfront 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in aws-cloudfront.gemspec
4
+ gemspec
@@ -0,0 +1,50 @@
1
+ = Amazon CloudFront Management Tool
2
+
3
+ This tool enables you to manage your CloudFront distributions via Ruby or command line.
4
+ It is driven by functions you need to host complete static websites on CloudFront. At
5
+ the moment you can set the root object of a distribution and invalidate objects to trigger
6
+ a re-deployment of certain files.
7
+
8
+ == Requirements
9
+ gem install xml-simple
10
+
11
+ == Usage
12
+
13
+ === Setting a root object
14
+
15
+ Set the root object that gets returned when the root url of the distribution is called:
16
+ ./cloudfront_cmd.rb --distribution_id <id> --access-key <access key> --secret-access-key <secret key> set_default_root_object <object>
17
+
18
+ <object> - a file name that exists on the distribution
19
+
20
+ Example:
21
+ ./cloudfront_cmd.rb --distribution_id AB12FWEEXAMPLE --access-key 15B4D3461BDF1EXAMPLE --secret-access-key "AQE0diMbLRepdf3YBDF/EXAMPLE" set_default_root_object index.html
22
+
23
+ === Invalidating objects
24
+
25
+ When a object on S3 gets overwritten by a newer version CloudFront doesn't trigger a distribution
26
+ automatically. Therefore you have to invalidate to objects after overwriting.
27
+
28
+ ./cloudfront_cmd.rb --distribution_id <id> --access-key <access key> --secret-access-key <secret key> invalidate_objects <list of objects>
29
+
30
+ <object list> - a list of
31
+
32
+ Example:
33
+ ./cloudfront_cmd.rb --distribution_id AB12FWEEXAMPLE --access-key 15B4D3461BDF1EXAMPLE --secret-access-key "AQE0diMbLRepdf3YBDF/EXAMPLE" invalidate_objects "/index.html" "/styles.css"
34
+
35
+ <id> - the AWS CloudFront distribution id. It can be found on the
36
+ {AWS Management Console}[https://console.aws.amazon.com/cloudfront/home].
37
+
38
+ <access key> - your AWS access key id.
39
+
40
+ <secret key> - the corresponding AWS secret access key
41
+
42
+
43
+ ==Author
44
+
45
+ {Alexander Greim}[http://github.com/iltempo]
46
+
47
+ == Copyright
48
+
49
+ Copyright (c) 2010 {il tempo}[http://github.com/iltempo] -
50
+ {Alexander Greim}[http://github.com/iltempo], released under the MIT license
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "aws-cloudfront/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "aws-cloudfront"
7
+ s.version = Aws::Cloudfront::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Alexander Greim", "Luke van der Hoeven", "TJ Singleton"]
10
+ s.email = ["alexxx@iltempo.de", "hungerandthirst@gmail.com", "tj@salescrunch.com"]
11
+ s.homepage = "https://github.com/crunchy/aws-cloudfront"
12
+ s.summary = %q{Library and command line tool for managing CloudFront.}
13
+ s.description = %q{This tool enables you to manage your CloudFront distributions via Ruby or command line. It is
14
+ driven by functions you need to host complete static websites on CloudFront. At the moment you can
15
+ set the root object of a distribution and invalidate objects to trigger a re-deployment of certain
16
+ files.}
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+
23
+ s.add_dependency "xml-simple"
24
+ end
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+ require 'optparse'
3
+ require File.expand_path File.join(File.dirname(__FILE__), '..', '/lib/aws-cloudfront')
4
+
5
+ options = {}
6
+ OptionParser.new do |opts|
7
+ opts.banner = "Usage: #{__FILE__}.rb [options] command"
8
+
9
+ opts.on("-d", "--distribution-id [ID]", "CloudFront Distribution ID") do |v|
10
+ options[:distribution_id] = v
11
+ end
12
+ opts.on("-k", "--access-key [KEY]", "AWS Access Key") do |v|
13
+ options[:access_key] = v
14
+ end
15
+ opts.on("-s", "--secret-access-key [KEY]", "AWS Secret Access Key") do |v|
16
+ options[:secret_key] = v
17
+ end
18
+ opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
19
+ options[:verbose] = v
20
+ end
21
+ end.parse!(ARGV)
22
+
23
+ unless ARGV.length > 0
24
+ puts "Please specify command: set_default_root_object [object]"
25
+ exit 1
26
+ end
27
+
28
+ distribution = AWS::Cloudfront::Distribution.new(options[:distribution_id],
29
+ options[:access_key], options[:secret_key], options[:verbose])
30
+
31
+ distribution.__send__(ARGV.shift, ARGV)
@@ -0,0 +1,14 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+
3
+ require 'openssl'
4
+ require 'uri'
5
+ require 'net/http'
6
+ require 'net/https'
7
+ require 'rubygems'
8
+ require 'yaml'
9
+
10
+ require "rubygems"
11
+ require "bundler/setup"
12
+
13
+ require "xmlsimple"
14
+ require 'aws-cloudfront/distribution'
@@ -0,0 +1,110 @@
1
+ module AWS
2
+ module Cloudfront
3
+ class Distribution
4
+
5
+ def initialize(id, aws_access_key, aws_secret_key, verbose=false)
6
+ @id = id
7
+ @aws_access_key = aws_access_key
8
+ @aws_secret_key = aws_secret_key
9
+ @verbose = verbose
10
+ end
11
+
12
+ def set_default_root_object(objects)
13
+ object = objects.to_s
14
+ get_config unless @config
15
+ @config['DefaultRootObject'] = [object]
16
+ put_config
17
+ end
18
+
19
+ def invalidate_objects(objects)
20
+ get_config unless @config
21
+ post_invalidation(objects)
22
+ end
23
+
24
+ private
25
+
26
+ def resource_uri(path)
27
+ URI.parse("https://cloudfront.amazonaws.com/2010-11-01/distribution/#{@id}/#{path}")
28
+ end
29
+
30
+ def config
31
+ get_config unless @config
32
+ @config
33
+ end
34
+
35
+ def etag
36
+ get_config unless @etag
37
+ @etag
38
+ end
39
+
40
+ def get_config
41
+ uri = resource_uri('config')
42
+ http = Net::HTTP.new(uri.host, uri.port)
43
+ http.set_debug_output $stderr if @verbose
44
+ http.use_ssl = true
45
+ response, body = http.get(uri.path, build_authorization_headers)
46
+
47
+ raise 'Distribution could not be loaded' unless response.kind_of?(Net::HTTPSuccess)
48
+
49
+ @etag = response.fetch('Etag')
50
+ @config = XmlSimple.xml_in(body)
51
+ end
52
+
53
+ def put_config
54
+ uri = resource_uri('config')
55
+ http = Net::HTTP.new(uri.host, uri.port)
56
+ http.set_debug_output $stderr if @verbose
57
+ http.use_ssl = true
58
+ headers = build_authorization_headers
59
+ headers['If-Match'] = @etag
60
+ body = XmlSimple.xml_out(@config, {'RootName' => 'DistributionConfig'})
61
+ response = http.send_request('PUT', uri.path, body, headers)
62
+
63
+ unless response.kind_of?(Net::HTTPSuccess)
64
+ raise XmlSimple.xml_in(response.body)['Error'].to_yaml
65
+ end
66
+
67
+ response.body
68
+ end
69
+
70
+ def post_invalidation(objects)
71
+ uri = resource_uri('invalidation')
72
+ http = Net::HTTP.new(uri.host, uri.port)
73
+ http.set_debug_output $stderr if @verbose
74
+ http.use_ssl = true
75
+ headers = build_authorization_headers
76
+ body = XmlSimple.xml_out(build_invalidation_batch(objects),
77
+ {'RootName' => 'InvalidationBatch'})
78
+ response = http.send_request('POST', uri.path, body, headers)
79
+
80
+ unless response.kind_of?(Net::HTTPSuccess)
81
+ raise XmlSimple.xml_in(response.body)['Error'].to_yaml
82
+ end
83
+
84
+ response.body
85
+ end
86
+
87
+ def build_signature(date_string)
88
+ digest = OpenSSL::Digest::Digest.new('sha1')
89
+ [OpenSSL::HMAC.digest(digest, @aws_secret_key, date_string)].pack("m").strip
90
+ end
91
+
92
+ def build_authorization_headers
93
+ date_string = build_date_string
94
+
95
+ headers = {}
96
+ headers['Authorization'] = "AWS #{@aws_access_key}:#{build_signature(date_string)}"
97
+ headers['Date'] = date_string
98
+ headers
99
+ end
100
+
101
+ def build_date_string
102
+ Time.now.strftime('%a, %d %b %Y %k:%M:%S %Z')
103
+ end
104
+
105
+ def build_invalidation_batch(objects)
106
+ {'Path' => Array(objects), 'CallerReference' => [build_date_string] }
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,5 @@
1
+ module Aws
2
+ module Cloudfront
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aws-cloudfront
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Alexander Greim
13
+ - Luke van der Hoeven
14
+ - TJ Singleton
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-12-08 00:00:00 -05:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: xml-simple
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: |-
36
+ This tool enables you to manage your CloudFront distributions via Ruby or command line. It is
37
+ driven by functions you need to host complete static websites on CloudFront. At the moment you can
38
+ set the root object of a distribution and invalidate objects to trigger a re-deployment of certain
39
+ files.
40
+ email:
41
+ - alexxx@iltempo.de
42
+ - hungerandthirst@gmail.com
43
+ - tj@salescrunch.com
44
+ executables:
45
+ - cloudfront_cmd.rb
46
+ extensions: []
47
+
48
+ extra_rdoc_files: []
49
+
50
+ files:
51
+ - .gitignore
52
+ - Gemfile
53
+ - README.rdoc
54
+ - Rakefile
55
+ - aws-cloudfront.gemspec
56
+ - bin/cloudfront_cmd.rb
57
+ - lib/aws-cloudfront.rb
58
+ - lib/aws-cloudfront/distribution.rb
59
+ - lib/aws-cloudfront/version.rb
60
+ has_rdoc: true
61
+ homepage: https://github.com/crunchy/aws-cloudfront
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options: []
66
+
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ requirements: []
86
+
87
+ rubyforge_project:
88
+ rubygems_version: 1.3.7
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Library and command line tool for managing CloudFront.
92
+ test_files: []
93
+