Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CDN support #451

Merged
merged 1 commit into from Sep 6, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/nanoc/extra/deployers/fog.rb
Expand Up @@ -9,6 +9,10 @@ module Nanoc::Extra::Deployers
# deploy:
# public:
# kind: fog
# bucket: nanoc-site
# cdn_id: XXXXXX
# preprod:
# kind: fog
# provider: local
# local_root: ~/myCloud
# bucket: nanoc-site
Expand All @@ -27,6 +31,7 @@ def run
src = File.expand_path(source_path)
bucket = config.delete(:bucket) || config.delete(:bucket_name)
path = config.delete(:path)
cdn_id = config.delete(:cdn_id)

config.delete(:kind)

Expand All @@ -35,6 +40,7 @@ def run

# Mock if necessary
if self.dry_run?
puts 'Dry run - simulation'
::Fog.mock!
end

Expand All @@ -53,6 +59,7 @@ def run

# Create bucket if necessary
if should_create_bucket
puts 'Creating bucket'
directory = connection.directories.create(:key => bucket, :prefix => path)
end

Expand All @@ -65,6 +72,7 @@ def run
files = files + set
end
keys_to_destroy = files.all.map { |file| file.key }
keys_to_invalidate = []

# Upload all the files in the output folder to the clouds
puts 'Uploading local files'
Expand All @@ -77,6 +85,7 @@ def run
:body => File.open(file_path),
:public => true)
keys_to_destroy.delete(key)
keys_to_invalidate.push(key)
end
end

Expand All @@ -86,6 +95,21 @@ def run
directory.files.get(key).destroy
end

# invalidate CDN objects
if cdn_id
puts 'Invalidating CDN distribution'
keys_to_invalidate.concat(keys_to_destroy)
cdn = ::Fog::CDN.new(config)
# fog cannot mock CDN requests
unless self.dry_run?
distribution = cdn.get_distribution(cdn_id)
# usual limit per invalidation: 1000 objects
keys_to_invalidate.each_slice(1000) do |paths|
resp = cdn.post_invalidation(distribution, paths)
end
end
end

puts 'Done!'
end

Expand Down
32 changes: 32 additions & 0 deletions test/extra/deployers/test_fog.rb
Expand Up @@ -62,6 +62,38 @@ def test_run_with_dry_run
end
end

def test_run_cdn_with_dry_run
if_have 'fog' do
begin
# Create deployer
fog = Nanoc::Extra::Deployers::Fog.new(
'output/',
{
:provider => 'aws',
:cdn_id => 'id-cdn',
# FIXME bucket is necessary for deployer but fog doesn't like it
:bucket_name => 'doesntmatter',
:aws_access_key_id => 'meh',
:aws_secret_access_key => 'dontcare'},
:dry_run => true)

# Create site
FileUtils.mkdir_p('output')
File.open('output/meow', 'w') { |io| io.write "I am a cat!" }
File.open('output/bark', 'w') { |io| io.write "I am a dog!" }

# Create local cloud (but not bucket)
FileUtils.mkdir_p('mylocalcloud')

# Run
fog.run
ensure
# Hack :(
::Fog.instance_eval { @mocking = false }
end
end
end

def test_run_delete_stray
if_have 'fog' do
# Create deployer
Expand Down