diff --git a/Rakefile b/Rakefile index 0b2de40a38..50867c0b28 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,6 @@ require "bundler/setup" require 'yaml' +load 'lib/tasks/s3.rake' def git_initialize(repository) unless File.exist?(".git") diff --git a/config.rb b/config.rb index d40bccfa0e..5b25f3ddc5 100644 --- a/config.rb +++ b/config.rb @@ -1,7 +1,7 @@ require 'redcarpet' require 'active_support/core_ext' -Dir['./lib/*'].each { |f| require f } +Dir['./lib/*'].reject{|path| path.split("/").include?("tasks") }.each { |f| require f } # Debugging set(:logging, ENV['RACK_ENV'] != 'production') diff --git a/lib/tasks/s3.rake b/lib/tasks/s3.rake new file mode 100644 index 0000000000..638ee23a10 --- /dev/null +++ b/lib/tasks/s3.rake @@ -0,0 +1,60 @@ +require 'shellwords' +require 'tempfile' + +def run_s3cmd(subcmd, *args) + cmd = ["s3cmd"] + cmd << subcmd + + cmd.concat(args) + + cmd = Shellwords.join(cmd) + + puts "Running command: #{cmd.inspect}" + result = system cmd + + puts "Error running command" unless result +end + +namespace :s3 do + + task :setup => [:create_bucket, :create_website, :create_policy, :deploy] + + task :create_bucket do + run_s3cmd "mb", ENV['BUCKET_URL'] + end + + task :create_website do + run_s3cmd "ws-create", ENV['BUCKET_URL'] + end + + task :create_policy do + Tempfile.open('emberjs-policy.json') do |f| + f.write BUCKET_POLICY + f.rewind + run_s3cmd "setpolicy", f.path, ENV['BUCKET_URL'] + end + end + + task :deploy do + run_s3cmd "sync", "--delete-removed", "build/", ENV['BUCKET_URL'] + end +end + +BUCKET_POLICY = <<-EOS +{ + "Version": "2008-10-17", + "Id": "Policy1337995845252", + "Statement": [ + { + "Sid": "Stmt1337995842373", + "Effect": "Allow", + "Principal": { + "AWS": "*" + }, + "Action": "s3:GetObject", + "Resource": "arn:aws:s3:::#{ENV['BUCKET_URL'].sub(%r{^s3:\/\/}, '')}/*" + } + ] +} +EOS +