Created
January 26, 2014 00:49
-
-
Save draffensperger/8626222 to your computer and use it in GitHub Desktop.
S3 Deployment Code (initial start)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def each_file() | |
Dir.glob("**/*") do |file| | |
yield file unless File.directory? file | |
end | |
end | |
def file_md5_name(file) | |
ext = File.extname file | |
digest = Digest::MD5.base64digest File.read file | |
name = 'a' + digest.gsub("=","").gsub("+", "_").gsub("/", '-') + ext | |
end | |
def should_cache_file(file) | |
!['.html', '.gz'].include?(File.extname(file)) | |
end | |
def is_text_file(file) | |
['.js', '.html', '.css'].include? File.extname file | |
end | |
def get_file_rename_map(assets_dir) | |
rename_map = {} | |
system "pwd" | |
each_file do |file| | |
if should_cache_file file | |
rename_map[file] = file_md5_name(file) | |
end | |
end | |
rename_map | |
end | |
def sub_all_file_references(replace_map, assets_host) | |
each_file do |file| | |
if is_text_file file | |
sub_file_references file, replace_map, assets_host | |
end | |
end | |
end | |
def sub_file_references(file, replace_map, assets_host) | |
contents = File.read file | |
replace_map.each do |k, v| | |
contents.gsub!(k,assets_host + '/' + v) | |
end | |
File.open(file, 'w') {|f| f.write(contents) } | |
end | |
def s3_deploy_file(file, s3_bucket) | |
params = '--acl-public' | |
suffix = "" | |
if is_text_file file | |
# Compress the file and add gzip encoding | |
suffix = '.gz' | |
system "zopfli #{file}" unless File.exists?(file + suffix) | |
# Not quite to HTTP standard because it doesn't wait for the request | |
# to accept gzip encoding. Could mess with certain proxy caches. | |
# I'll take the risk. | |
params += ' --add-header "Content-Encoding:gzip"' | |
end | |
if File.extname(file) == ".html" | |
params += ' --mime-type="text/html;charset=utf-8"' | |
end | |
if should_cache_file file | |
# Expire one year from now | |
cache_secs = 365*24*60*60 | |
params += ' --add-header "Cache-Control:public,max-age=' + cache_secs.to_s + '"' | |
params += ' --add-header "Expires:' + (Time.now + cache_secs).httpdate + '"' | |
end | |
cmd = "s3cmd #{params} sync #{file}#{suffix} s3://#{s3_bucket}/#{file}" | |
puts cmd | |
system cmd | |
end | |
desc "Deploy website via s3cmd" | |
task :s3_custom do | |
puts "## Deploying website via s3cmd" | |
#system("s3cmd --acl-public --mime-type='text/html;charset=utf-8' put #{public_dir}/index.html s3://#{s3_bucket}/index.html") | |
# Just straight up sync the pre-compressed media directory to an S3 bucket. | |
#system("s3cmd sync --acl-public #{media_dir}/* s3://#{media_s3_bucket}/") | |
# Sync the files | |
#system "rm -rf #{deploy_dir}" | |
system "mkdir -p #{deploy_dir}" | |
system "mkdir -p #{assets_dir}" | |
system "cp -R #{public_dir}/* #{deploy_dir}" | |
Dir.chdir deploy_dir | |
# Move asset files to new headings | |
rename_file_map = get_file_rename_map assets_dir | |
sub_all_file_references rename_file_map, assets_host | |
rename_file_map.each do |file, move_to| | |
move_to = '../' + assets_dir + '/' + move_to | |
system "mv #{file} #{move_to}" unless file == move_to | |
end | |
each_file do |file| | |
s3_deploy_file file, s3_bucket | |
end | |
Dir.chdir '..' | |
Dir.chdir assets_dir | |
each_file do |file| | |
s3_deploy_file file, assets_s3_bucket | |
end | |
Dir.chdir '..' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment