Module: CloudCrooner

Defined in:
lib/cloud_crooner/storage.rb,
lib/cloud_crooner/version.rb,
lib/cloud_crooner/cloud_crooner.rb

Defined Under Namespace

Classes: FogSettingError, Storage

Constant Summary collapse

VERSION =
"0.0.3"
VALID_AWS_REGIONS =
%w(
  us-west-2
  us-west-1
  eu-west-1
  ap-southeast-1
  ap-southeast-2
  ap-northeast-1
  sa-east-1
)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.asset_pathsObject

Logical paths to assets for use with Sprockets. Defaults to “prefix” dir. If serve_assets is set to “remote” or “local_static”, paths will be added, but the link helpers bypass Sprockets and grab the manifest links, and will only fall back to the paths if the asset is not found in the manifest.



214
215
216
# File 'lib/cloud_crooner/cloud_crooner.rb', line 214

def asset_paths
  @asset_paths ||= [prefix] 
end

.assets_to_compileObject

The list of assets to compile, given by their Sprocket’s load path. Defaults to every file under the prefix directory.



130
131
132
133
134
# File 'lib/cloud_crooner/cloud_crooner.rb', line 130

def assets_to_compile  
  return @assets_to_compile if @assets_to_compile 
  files = Dir.glob(prefix + "**/*").select {|f| File.file?(f)}
  files.collect! { |f| f.gsub(/^#{prefix}\//, "") }
end

.aws_access_key_idObject

AWS access id key given by Amazon. Defaults to ENV but can be overridden in config block. Required if using S3.

Do not set value directly in app, store outside in a non-source controlled location.



178
179
180
181
182
183
184
185
# File 'lib/cloud_crooner/cloud_crooner.rb', line 178

def aws_access_key_id
  if @aws_access_key_id
    return @aws_access_key_id
  elsif !ENV.has_key?('AWS_ACCESS_KEY_ID')
    raise FogSettingError, "access key id must be set in ENV or configure block"
  end
  @aws_access_key_id ||= ENV['AWS_ACCESS_KEY_ID']
end

.aws_secret_access_keyObject

AWS secret access key given by Amazon. Defaults to ENV but can be overridden in config block. Required if using S3.

Do not set value directly in app, store outside in a non-source controlled location.



196
197
198
199
200
201
202
203
# File 'lib/cloud_crooner/cloud_crooner.rb', line 196

def aws_secret_access_key 
  if @aws_secret_access_key
    return @aws_secret_access_key
  elsif !ENV.has_key?('AWS_SECRET_ACCESS_KEY')
    raise FogSettingError, "secret access key must be set in ENV or configure block"
  end
  @aws_secret_access_key ||= ENV['AWS_SECRET_ACCESS_KEY']
end

.backups_to_keepObject

Number of compiled assets to keep as backups. Default is 2.



222
223
224
# File 'lib/cloud_crooner/cloud_crooner.rb', line 222

def backups_to_keep
  @backups_to_keep ||= 2
end

.bucket_nameObject

AWS bucket name. Defaults to ENV but can be overridden in config block. Required if using S3.



141
142
143
144
145
146
147
148
# File 'lib/cloud_crooner/cloud_crooner.rb', line 141

def bucket_name 
  if @bucket_name
    return @bucket_name
  elsif !ENV.has_key?('AWS_BUCKET_NAME')
    raise FogSettingError, "Bucket name must be set in ENV or configure block"
  end
  @bucket_name = ENV['AWS_BUCKET_NAME'] 
end

.manifestObject

Returns or creates the Sprockets manifest in public_folder/prefix.



92
93
94
# File 'lib/cloud_crooner/cloud_crooner.rb', line 92

def manifest
  @manifest ||= Sprockets::Manifest.new(sprockets, File.join(public_folder, prefix))
end

.sprocketsObject

Returns or creates the Sprockets instance and adds asset_paths to the Sprockets load path.



80
81
82
83
84
85
86
# File 'lib/cloud_crooner/cloud_crooner.rb', line 80

def sprockets
  if @sprockets.nil?
    @sprockets = Sprockets::Environment.new { |env| env.logger = Logger.new($stdout) }
    asset_paths.each {|path| @sprockets.append_path(path)}
  end
  return @sprockets
end

Class Method Details

.clean_sprockets_assetsObject

If there are more than the set number of compiled asset backups, the oldest asset will be deleted locally and removed from the manifest.



251
252
253
# File 'lib/cloud_crooner/cloud_crooner.rb', line 251

def clean_sprockets_assets
  manifest.clean(backups_to_keep)
end

.clobber_sprockets_assetsObject

Delete the manifest’s containing directory and everything in it.



258
259
260
# File 'lib/cloud_crooner/cloud_crooner.rb', line 258

def clobber_sprockets_assets
  manifest.clobber
end

.compile_sprockets_assetsObject

Compile the assets given in “assets_to_compile”. Outputs to the directory of the manifest. Updates the manifest.



243
244
245
# File 'lib/cloud_crooner/cloud_crooner.rb', line 243

def compile_sprockets_assets
  manifest.compile(*self.assets_to_compile)
end

.configure {|_self| ... } ⇒ Object

Convinience method for setting options and configuring helpers

Yields:

  • (_self)

Yield Parameters:

  • _self (CloudCrooner)

    the object that the method was called on



23
24
25
26
# File 'lib/cloud_crooner/cloud_crooner.rb', line 23

def configure(&proc)
  yield self
  configure_sprockets_helpers
end

.configure_sprockets_helpersObject

Set up the helpers to properly link to assets in current environment.

If you’re running fully on defaults, you must call this explicitly in config.ru if you want to use the helpers



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cloud_crooner/cloud_crooner.rb', line 34

def configure_sprockets_helpers
  Sprockets::Helpers.configure do |config|
    case serve_assets
    when "remote"
      config.manifest = manifest
      config.debug = false
      config.asset_host = asset_host
    when "local_static" 
      config.manifest = manifest
      config.debug = false
      config.public_path = public_folder 
    end
    config.environment = sprockets
    config.prefix = '/' + prefix
    config.digest = digest 
  end
end

.fog_optionsObject

Passed to Fog gem when creating a Storage instance



230
231
232
233
234
235
236
237
# File 'lib/cloud_crooner/cloud_crooner.rb', line 230

def fog_options
  options = { 
              :provider => provider, 
              :aws_access_key_id => aws_access_key_id, 
              :aws_secret_access_key => aws_secret_access_key, 
              :region => region 
            }
end

.log(msg) ⇒ Object



277
278
279
# File 'lib/cloud_crooner/cloud_crooner.rb', line 277

def log(msg)
  $stdout.puts msg
end

.prefixObject

Path from root to dynamic assets base folder. By default ‘/assets’.

Also serves as the prefix under which S3 assets will be stored. Paths to assets from the helpers will look something like ‘bucket-name.s3.amazonaws.com/prefix/filename’.



104
105
106
# File 'lib/cloud_crooner/cloud_crooner.rb', line 104

def prefix
  @prefix ||= 'assets'
end

.prefix=(val) ⇒ Object



108
109
110
111
# File 'lib/cloud_crooner/cloud_crooner.rb', line 108

def prefix=(val)
  #remove any slashes at beginning or end 
  @prefix = val.chomp('/').gsub(/^\//, "")
end

.public_folderObject

The public folder of the application. By default ‘/public’. If a

different folder is set, it must also be set on the Sinatra app.



117
118
119
# File 'lib/cloud_crooner/cloud_crooner.rb', line 117

def public_folder
  @public_folder ||= 'public'
end

.public_folder=(val) ⇒ Object



121
122
123
124
# File 'lib/cloud_crooner/cloud_crooner.rb', line 121

def public_folder=(val)
  #remove any slashes at beginning or end 
  @public_folder = val.chomp('/').gsub(/^\//, "")
end

.regionObject

Region of AWS bucket. Defaults to ENV but can be overridden in config block. Must be a valid AWS region. Required if using S3.



156
157
158
159
160
161
162
163
164
165
# File 'lib/cloud_crooner/cloud_crooner.rb', line 156

def region
  if @region
    return @region
  elsif !ENV.has_key?('AWS_REGION')
    raise FogSettingError, "AWS Region must be set in ENV or in configure block"
  elsif !VALID_AWS_REGIONS.include?(ENV['AWS_REGION'])
    raise FogSettingError, "Invalid region"
  end
  @region = ENV['AWS_REGION']
end

.region=(val) ⇒ Object



167
168
169
# File 'lib/cloud_crooner/cloud_crooner.rb', line 167

def region=(val)
  VALID_AWS_REGIONS.include?(val) ? @region = val : (raise FogSettingError, "Invalid region") 
end

.serve_assetsObject

Where to serve assets from

  • “local_dynamic” : default in dev and test - serves assets from sprockets

  • “local_static” : serves compiled assets from public_folder

  • “remote” : default in prod - serves compiled assets from S3



63
64
65
66
67
68
# File 'lib/cloud_crooner/cloud_crooner.rb', line 63

def serve_assets
  if @serve_assets.nil?
    ENV['RACK_ENV'] == 'production' ? (@serve_assets = 'remote') : (@serve_assets = 'local_dynamic')
  end
  @serve_assets
end

.serve_assets=(val) ⇒ Object



70
71
72
73
74
# File 'lib/cloud_crooner/cloud_crooner.rb', line 70

def serve_assets= (val)
  (@serve_assets =  val) if [ 'local_dynamic', 
                            'local_static', 
                            'remote' ].include?(val)
end

.storageObject



52
53
54
# File 'lib/cloud_crooner/cloud_crooner.rb', line 52

def storage
  @storage ||= Storage.new
end

.syncObject

Compile assets locally and remove old local backups. If serve_assets is

“remote”, it will upload changed files and delete old remote backups.



266
267
268
269
270
271
272
273
274
275
# File 'lib/cloud_crooner/cloud_crooner.rb', line 266

def sync
  compile_sprockets_assets
  clean_sprockets_assets

  if serve_assets == "remote" 
    storage.upload_files
    storage.clean_remote
  end

end