Skip to content

A Gem for Syncing assets to Windows Azure (CDN)

License

Notifications You must be signed in to change notification settings

ypadlyak/waz-sync

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Windows Azure Sync library — Simple gem for syncing assets to Windows Azure CDN

A simple client library aim to sync assets to Windows Azure, using a modified version of the great waz-storage gem

Install

Add to your gemfile :

gem 'waz-storage', :git=>"https://github.com/gmontard/waz-storage.git"
gem 'waz-sync'

Create a config file to /app/config/azure.yml to hold your Azure credentials

development:
  account_name: NAME
  access_key: KEY

staging:
  account_name: NAME
  access_key: KEY

production:
  account_name: NAME
  access_key: KEY

Usage

azure = WazSync.new()
["images", "javascripts", "stylesheets"].each{|folder|    
  container = azure.find_or_create_container(folder)    
  Dir.glob("#{Rails.root.to_s}/public/#{folder}/**/*").each do |file|
    if File.file?(file)              
      filename = file.gsub("/public/#{folder}/", "").gsub("#{Rails.root.to_s}", "")
      azure.send_or_update(container, file, filename)
    end
  end
}

This simple code will sync all your assets unders “images”, “javascripts” and “stylesheets” to Windows Azure CDN

Rake task

There is a rake task included to this Gem. To use it you first have to include this line into your Rakefile

require 'waz_sync/tasks'

Then you can use the rake task

rake waz:sync

By default it does sync your images, javascripts and stylesheets folders, but you can specify which folder to sync

rake waz:sync folders=images,foo,bar

Capistrano recipe

If you use capistrano to deploy you app it’s a good idea to sync you assets during each deploy. Add this line to your deploy.rb

require "waz_sync/recipes"

Then you can add these callbacks

before "deploy:stop",    "waz_sync:sync"
before "deploy:start",   "waz_sync:sync"
before "deploy:restart", "waz_sync:sync"

There is also a variable :waz_sync_args where you can specify which folder to sync

set :waz_sync_args, "images,foo,bar"

Remarks

This Gem use a modified version of the waz-storage gem that you can find there : github.com/johnnyhalife/waz-storage

You can find my modified version here : github.com/gmontard/waz-storage

Extra

If you also want to automatically sync your assets cached file (javascripts && stylesheets) created using the include rails helper with :cache condition,

javascript_include_tag :all, :cache => true

you can take a look at this override lib file (tested on Rails 2.3.14) :

#####
## Add this line to your environment.rb file
# require 'lib/override_assetfile_cache.rb'
####

module ActionView
  module Helpers
    module AssetTagHelper

      def write_asset_file_contents(joined_asset_path, asset_paths)

        FileUtils.mkdir_p(File.dirname(joined_asset_path))
        File.open(joined_asset_path, "w+") { |cache| cache.write(join_asset_file_contents(asset_paths)) }

        # Set mtime to the latest of the combined files to allow for
        # consistent ETag without a shared filesystem.
        mt = asset_paths.map { |p| File.mtime(asset_file_path(p)) }.max
        File.utime(mt, mt, joined_asset_path)

        ######################
        ### Azure CDN Sync ###
        ######################

        begin
          azure = WazSync.new()
          folder = joined_asset_path.match(/^.*public\/([^\/]*)\/(.*)$/)[1]
          filename = joined_asset_path.match(/^.*public\/([^\/]*)\/(.*)$/)[2]
          container = azure.find_or_create_container(folder)    
          azure.send_or_update(container, joined_asset_path, filename)          
        rescue
          Rails.logger.info("Azure Sync Failed")
        end

        ######################

      end
    end
  end
end

(gist.github.com/2428488)

TODO’s

Tests !!!!

Meta

Written by Guillaume Montard for Vodeclic SAS (www.vodeclic.com)

Released under the MIT License: www.opensource.org/licenses/mit-license.php

github.com/gmontard/waz-sync

About

A Gem for Syncing assets to Windows Azure (CDN)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 100.0%