Class: S3deploy
- Inherits:
-
Object
- Object
- S3deploy
- Defined in:
- lib/s3deploy.rb,
lib/s3deploy/version.rb
Constant Summary collapse
- AWS_REGIONS =
%W(us-west-2 us-west-1 eu-west-1 ap-southeast-1 ap-northeast-1 sa-east-1)
- VERSION =
"0.1.2"
Class Method Summary collapse
Instance Method Summary collapse
- #all_files(path = ".") ⇒ Object
- #deploy(simulate = false) ⇒ Object
- #empty(simulate) ⇒ Object
- #has_gzip_version?(file, files) ⇒ Boolean
- #import_settings(file) ⇒ Object
-
#initialize(options = {}) ⇒ S3deploy
constructor
A new instance of S3deploy.
- #is_gzip_smaller?(full_path) ⇒ Boolean
- #only_keep(active_files, simulate) ⇒ Object
- #s3_file_exists?(local, remote) ⇒ Boolean
- #set_aws_region(region) ⇒ Object
- #upload_file(local, remote, simulate, options = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ S3deploy
Returns a new instance of S3deploy.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/s3deploy.rb', line 14 def initialize( = {}) defaults = { "path" => ".", "remote_path" => "" } @options = defaults.merge() @options.merge! import_settings(File.("~/.s3deploy/.s3deploy.yml")) @options.merge! import_settings(File.("./.s3deploy.yml")) @options["extras"] ||= [] raise "No AWS credentials given." unless @options["aws_key"] && @options["aws_secret"] set_aws_region(@options["aws_region"]) if @options["aws_region"] AWS::S3::Base.establish_connection!( :access_key_id => @options["aws_key"], :secret_access_key => @options["aws_secret"] ) raise "No bucket selected." unless @options["aws_bucket"] @bucket = AWS::S3::Bucket.find @options["aws_bucket"] end |
Class Method Details
.config_files? ⇒ Boolean
10 11 12 |
# File 'lib/s3deploy.rb', line 10 def self.config_files? File.exists?( File. "~/.s3deploy/.s3deploy.yml" ) || File.exists?( File. "./.s3deploy.yml" ) end |
.install_config(default = false) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/s3deploy.rb', line 133 def self.install_config(default = false) install_path = default ? File.("~/.s3deploy/.s3deploy.yml") : File.("./.s3deploy.yml") raise "Configuration file already exist." if File.exist? install_path if default `mkdir -p #{File.dirname(install_path)}` unless File.directory? File.dirname(install_path) end puts `cp #{File.dirname(File.(__FILE__)) + "/.s3deploy.yml"} #{install_path}` puts "A configuration file has been created at #{install_path}" end |
Instance Method Details
#all_files(path = ".") ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/s3deploy.rb', line 147 def all_files(path = ".") files = [] Dir.new(path).each do |row| next if %W{. .. .git}.include? row full_path = "#{path}/#{row}" if File.directory?(full_path) files |= all_files(full_path) else files << full_path end end files end |
#deploy(simulate = false) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/s3deploy.rb', line 35 def deploy(simulate = false) path = File.(@options["path"]) raise "#{@options["path"]} is not a path." unless File.directory? path puts simulate ? "Simulating deployment to #{@options["aws_bucket"]}" : "Deploying to #{@options["aws_bucket"]}" files = all_files(path).map { |f| f.gsub(/^#{path}\//, "")} if @options["skip_regex"] files.delete_if { |f| f =~ Regexp.new(@options["skip_regex"]) } end skip_files = [] active_files = [] files.each do |file| next if skip_files.include? file full_path = (path + "/" + file).gsub( /\/\//, "/") full_remote_path = (@options["remote_path"] + "/" + file).gsub( /\/\//, "/").gsub( /(^\/)|(\/$)/, "") if @options["extras"].include?("replace_with_gzip") && has_gzip_version?(file, files) full_path.gsub!(/.gz$/i, "") file.gsub!(/.gz$/i, "") full_remote_path.gsub!(/.gz$/i, "") skip_files << file << file + ".gz" if is_gzip_smaller?(full_path) file = file + ".gz" full_path = full_path + ".gz" end end active_files << full_remote_path if s3_file_exists?(full_path, full_remote_path) puts "Skipped\t\t#{full_remote_path}" next end upload_file(full_path, full_remote_path, simulate) end only_keep(active_files, simulate) if @options["extras"].include?("delete_old_files") end |
#empty(simulate) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/s3deploy.rb', line 98 def empty(simulate) puts "Emptying #{@options["aws_bucket"]} #{"(Simulating)" if simulate}" @bucket.each do |o| AWS::S3::S3Object.delete(o.key, @options["aws_bucket"]) unless simulate puts "Deleted\t\t#{o.key}" end end |
#has_gzip_version?(file, files) ⇒ Boolean
81 82 83 |
# File 'lib/s3deploy.rb', line 81 def has_gzip_version?(file, files) (file !~ /.+.gz$/i && files.include?("#{file}.gz")) || ( file =~ /.+.gz$/i && files.include?( file.gsub(/.gz$/i, "") ) ) end |
#import_settings(file) ⇒ Object
163 164 165 166 167 168 169 170 |
# File 'lib/s3deploy.rb', line 163 def import_settings(file) settings = YAML::load_file(file) if File.file? file if settings && settings.class == Hash settings.delete_if { |k,v| k != "aws_region" && v.nil? } else Hash.new end end |
#is_gzip_smaller?(full_path) ⇒ Boolean
85 86 87 |
# File 'lib/s3deploy.rb', line 85 def is_gzip_smaller?(full_path) open("#{full_path}.gz").size < open("#{full_path}").size end |
#only_keep(active_files, simulate) ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/s3deploy.rb', line 89 def only_keep(active_files, simulate) @bucket.each do |o| unless active_files.include?( o.key ) AWS::S3::S3Object.delete(o.key, @options["aws_bucket"]) unless simulate puts "Deleted\t\t#{o.key}" end end end |
#s3_file_exists?(local, remote) ⇒ Boolean
106 107 108 109 |
# File 'lib/s3deploy.rb', line 106 def s3_file_exists?(local, remote) md5 = Digest::MD5.hexdigest(open(local).read) !@bucket.objects.select{|o| o.key == remote && o.etag == md5 }.empty? end |
#set_aws_region(region) ⇒ Object
172 173 174 175 176 177 178 179 180 |
# File 'lib/s3deploy.rb', line 172 def set_aws_region(region) unless AWS_REGIONS.include? region.downcase raise "#{region} is not a valid region, please select from #{AWS_REGIONS.join(", ")} or leave it blank for US Standard." end AWS::S3::DEFAULT_HOST.replace "s3-#{region}.amazonaws.com" end |
#upload_file(local, remote, simulate, options = {}) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/s3deploy.rb', line 111 def upload_file(local, remote, simulate, = {}) [:access] = :public_read if @options["cache_regex"] && remote =~ Regexp.new(@options["cache_regex"]) [:"Cache-Control"] = "public, max-age=31557600" end [:content_type] = "text/html; charset=#{@options["html_charset"]}" if @options["html_charset"] && remote =~ /.+\.(html|htm)(\.gz)?$/i [:"Content-Encoding"] = "gzip" if local =~ /.+.gz$/i extra_info = [] extra_info << "cache-headers" if [:"Cache-Control"] extra_info << "gzip" if [:"Content-Encoding"] extra_info << "charset=#{@options["html_charset"]}" if [:content_type] AWS::S3::S3Object.store(remote, open(local), @options["aws_bucket"], ) unless simulate = "Uploaded\t#{remote}" += " (#{extra_info.join(", ")})" unless extra_info.empty? puts end |