Class: Octopress::Deploy::S3
- Inherits:
-
Object
- Object
- Octopress::Deploy::S3
- Defined in:
- lib/octopress-deploy/s3.rb
Class Method Summary collapse
-
.default_config(options = {}) ⇒ Object
Return default configuration options for this deployment type.
Instance Method Summary collapse
-
#add_bucket ⇒ Object
Create a new S3 bucket.
- #configure_bucket ⇒ Object
-
#connect ⇒ Object
Connect to S3 using the AWS SDK Retuns an aws bucket.
-
#deletable_files ⇒ Object
Files from the bucket which are deletable Only deletes files beneath the remote_path if specified.
-
#delete_files ⇒ Object
Delete files from the bucket, to ensure a 1:1 match with site files.
- #delete_files? ⇒ Boolean
- #get_file_with_metadata(file, s3_filename) ⇒ Object
-
#initialize(options) ⇒ S3
constructor
A new instance of S3.
- #pluralize(str, num) ⇒ Object
-
#progress(str) ⇒ Object
Print consecutive characters.
- #pull ⇒ Object
- #push ⇒ Object
-
#remote_path(file) ⇒ Object
Replace local path with remote path.
-
#site_files ⇒ Object
local site files.
-
#site_files_dest ⇒ Object
Destination paths for local site files.
-
#status_message ⇒ Object
List written and deleted file counts.
-
#write_files ⇒ Object
Write site files to the selected bucket.
Constructor Details
#initialize(options) ⇒ S3
Returns a new instance of S3.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/octopress-deploy/s3.rb', line 8 def initialize() begin require 'aws-sdk' rescue LoadError abort "Deploying to S3 requires the aws-sdk gem. Install with `gem install aws-sdk`." end = @local = [:site_dir] || '_site' @bucket_name = [:bucket_name] @access_key = [:access_key_id] || ENV['AWS_ACCESS_KEY_ID'] @secret_key = [:secret_access_key] || ENV['AWS_SECRET_ACCESS_KEY'] @region = [:region] || ENV['AWS_DEFAULT_REGION'] || 'us-east-1' @remote_path = ([:remote_path] || '/').sub(/^\//,'') @verbose = [:verbose] @incremental = [:incremental] @delete = [:delete] @headers = [:headers] || [] @remote_path = @remote_path.sub(/^\//,'') # remove leading slash @pull_dir = [:dir] connect end |
Class Method Details
.default_config(options = {}) ⇒ Object
Return default configuration options for this deployment type
235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/octopress-deploy/s3.rb', line 235 def self.default_config(={}) "\#{\"bucket_name: \#{options[:bucket_name]}\".ljust(40)} # Name of the S3 bucket where these files will be stored.\n\#{\"access_key_id: \#{options[:access_key_id]}\".ljust(40)} # Get this from your AWS console at aws.amazon.com.\n\#{\"secret_access_key: \#{options[:secret_access_key]}\".ljust(40)} # Keep it safe; keep it secret. Keep this file in your .gitignore.\n\#{\"remote_path: \#{options[:remote_path] || '/'}\".ljust(40)} # relative path on bucket where files should be copied.\n\#{\"region: \#{options[:remote_path] || 'us-east-1'}\".ljust(40)} # Region where your bucket is located.\n\#{\"verbose: \#{options[:verbose] || 'false'}\".ljust(40)} # Print out all file operations.\n\#{\"incremental: \#{options[:incremental] || 'false'}\".ljust(40)} # Only upload new/changed files\n\#{\"delete: \#{options[:delete] || 'false'}\".ljust(40)} # Remove files from destination which do not match source files.\n" end |
Instance Method Details
#add_bucket ⇒ Object
Create a new S3 bucket
162 163 164 165 166 167 |
# File 'lib/octopress-deploy/s3.rb', line 162 def add_bucket puts @bucket_name @bucket = @s3.buckets.create(@bucket_name) puts "Created new bucket '#{@bucket_name}' in region '#{@region}'." configure_bucket end |
#configure_bucket ⇒ Object
169 170 171 172 173 174 175 176 177 178 |
# File 'lib/octopress-deploy/s3.rb', line 169 def configure_bucket error_page = ['error_page'] || remote_path('404.html') index_page = ['index_page'] || remote_path('index.html') config = @bucket.configure_website do |cfg| cfg.index_document_suffix = index_page cfg.error_document_key = error_page end puts "Bucket configured with index_document: #{index_page} and error_document: #{error_page}." end |
#connect ⇒ Object
Connect to S3 using the AWS SDK Retuns an aws bucket
67 68 69 70 |
# File 'lib/octopress-deploy/s3.rb', line 67 def connect AWS.config(access_key_id: @access_key, secret_access_key: @secret_key, region: @region) @s3 = AWS.s3 end |
#deletable_files ⇒ Object
Files from the bucket which are deletable Only deletes files beneath the remote_path if specified
203 204 205 206 207 208 209 210 |
# File 'lib/octopress-deploy/s3.rb', line 203 def deletable_files return [] unless delete_files? unless @deletable @deletable = @bucket.objects.map(&:key) - site_files_dest @deletable.reject!{|f| (f =~ /^#{@remote_path}/).nil? } end @deletable end |
#delete_files ⇒ Object
Delete files from the bucket, to ensure a 1:1 match with site files
146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/octopress-deploy/s3.rb', line 146 def delete_files if deletable_files.size > 0 puts "Deleting #{pluralize('file', deletable_files.size)}:" if @verbose deletable_files.each do |file| @bucket.objects.delete(file) if @verbose puts "- #{file}" else progress('-') end end end end |
#delete_files? ⇒ Boolean
180 181 182 |
# File 'lib/octopress-deploy/s3.rb', line 180 def delete_files? !!@delete end |
#get_file_with_metadata(file, s3_filename) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/octopress-deploy/s3.rb', line 99 def (file, s3_filename) = { :file => file, :acl => :public_read } @headers.each do |conf| if conf.has_key? 'filename' and s3_filename.match(conf['filename']) if @verbose puts "+ #{remote_path(file)} matched pattern #{conf['filename']}" end if conf.has_key? 'expires' expireDate = conf['expires'] relative_years = /^\+(\d+) year(s)?$/.match(conf['expires']) if relative_years expireDate = (Time.now + (60 * 60 * 24 * 365 * relative_years[1].to_i)).httpdate end relative_days = /^\+(\d+) day(s)?$/.match(conf['expires']) if relative_days expireDate = (Time.now + (60 * 60 * 24 * relative_days[1].to_i)).httpdate end [:expires] = expireDate end if conf.has_key? 'content_type' [:content_type] = conf['content_type'] end if conf.has_key? 'cache_control' [:cache_control] = conf['cache_control'] end if conf.has_key? 'content_encoding' [:content_encoding] = conf['content_encoding'] end end end return end |
#pluralize(str, num) ⇒ Object
229 230 231 232 |
# File 'lib/octopress-deploy/s3.rb', line 229 def pluralize(str, num) str << 's' if num != 1 str end |
#progress(str) ⇒ Object
Print consecutive characters
224 225 226 227 |
# File 'lib/octopress-deploy/s3.rb', line 224 def progress(str) print str $stdout.flush end |
#pull ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/octopress-deploy/s3.rb', line 43 def pull @bucket = @s3.buckets[@bucket_name] if !@bucket.exists? abort "Bucket not found: '#{@bucket_name}'. Check your configuration or create a bucket using: `octopress deploy add-bucket`" else puts "Syncing from S3 bucket: '#{@bucket_name}' to #{@pull_dir}." @bucket.objects.each do |object| path = File.join(@pull_dir, object.key) # Path is a directory, not a file if path =~ /\/$/ FileUtils.mkdir_p(path) unless File.directory?(path) else dir = File.dirname(path) FileUtils.mkdir_p(dir) unless File.directory?(dir) File.open(path, 'w') { |f| f.write(object.read) } end end end end |
#push ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/octopress-deploy/s3.rb', line 30 def push #abort "Seriously, you should. Quitting..." unless Deploy.check_gitignore @bucket = @s3.buckets[@bucket_name] if !@bucket.exists? abort "Bucket not found: '#{@bucket_name}'. Check your configuration or create a bucket using: `octopress deploy add-bucket`" else puts "Syncing #{@local} files to #{@bucket_name} on S3." write_files delete_files if delete_files? end end |
#remote_path(file) ⇒ Object
Replace local path with remote path
197 198 199 |
# File 'lib/octopress-deploy/s3.rb', line 197 def remote_path(file) File.join(@remote_path, file.sub(@local, '')).sub(/^\//, '') end |
#site_files ⇒ Object
local site files
185 186 187 188 189 |
# File 'lib/octopress-deploy/s3.rb', line 185 def site_files @site_files ||= Find.find(@local).to_a.reject do |f| File.directory?(f) end end |
#site_files_dest ⇒ Object
Destination paths for local site files.
192 193 194 |
# File 'lib/octopress-deploy/s3.rb', line 192 def site_files_dest @site_files_dest ||= site_files.map{|f| remote_path(f) } end |
#status_message ⇒ Object
List written and deleted file counts
213 214 215 216 217 218 219 220 221 |
# File 'lib/octopress-deploy/s3.rb', line 213 def uploaded = site_files.size deleted = deletable_files.size = "\nSuccess:".green + " #{uploaded} #{pluralize('file', uploaded)} uploaded" << ", #{deleted} #{pluralize('file', deleted)} deleted." puts configure_bucket unless @bucket.website? end |
#write_files ⇒ Object
Write site files to the selected bucket
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/octopress-deploy/s3.rb', line 74 def write_files puts "Writing #{pluralize('file', site_files.size)}:" if @verbose site_files.each do |file| s3_filename = remote_path(file) o = @bucket.objects[s3_filename] = (file, s3_filename); s3sum = o.etag.tr('"','') if @incremental && (s3sum == Digest::MD5.file(file).hexdigest) if @verbose puts "= #{remote_path(file)}" else progress('=') end else o.write() if @verbose puts "+ #{remote_path(file)}" else progress('+') end end end end |