lib/hillary/slug.rb in hillary-0.0.3 vs lib/hillary/slug.rb in hillary-0.0.4
- old
+ new
@@ -9,10 +9,11 @@
include Shellable
attr_reader :branch, :version
SlugArchiveError = Class.new(StandardError)
+ SlugNotFoundError = Class.new(StandardError)
class << self
def build(path = File.expand_path('.'), version = Repo::Version.create!, options = {})
return unless version.sluggable?
@@ -21,20 +22,28 @@
end
def initialize(path, version, options = {})
@path = path
@version = version
- @logger = options[:logger] || Logger.new($stdout)
@bucket = options[:bucket] || Bucket.new
- logger.formatter = proc{|_, _, _, msg| msg + "\n"}
+ @logger = if options[:logger]
+ options[:logger]
+ else
+ Logger.new($stdout).tap do |logger|
+ logger.formatter = proc do |severity, time, _, msg|
+ "#{time.strftime('%Y-%m-%d %H:%M:%S')} #{severity} - #{msg}\n"
+ end
+ end
+ end
end
def build
if version.master?
+ create_slug_if_missing
copy_slug
- else
+ elsif version.dev?
create_slug
upload_slug
end
end
@@ -47,18 +56,33 @@
end
private
attr_reader :logger, :bucket, :path
+ def create_slug_if_missing
+ return if bucket.exists?(canonical_storage_slug_file)
+
+ if version.production?
+ raise SlugNotFoundError, "Attempted to create production slug from RC, but canonical slug (#{bucket.name}:/#{canonical_storage_slug_file}) couldn't be found"
+ else
+ logger.warn("Unable to find slug #{bucket.name}:/#{canonical_storage_slug_file}")
+ create_slug
+ upload_slug
+ end
+ end
+
def copy_slug
logger.info("Copying #{bucket.name}:/#{canonical_storage_slug_file} to #{bucket.name}:/#{storage_slug_file}")
bucket.copy(canonical_storage_slug_file, storage_slug_file)
end
def create_slug
Dir.chdir(project_parent_directory) do
logger.info("Creating slug #{canonical_slug_file}")
- shell("tar --exclude \".git\" --exclude \"vendor/bundle\" -zcvf #{canonical_slug_file} #{project_name}")
+
+ command = "tar --exclude \".git\" --exclude \"vendor/bundle\" -zcvf #{canonical_slug_file} #{project_name}"
+
+ shell(command, logger: logger)
end
end
def upload_slug
logger.info("Uploading #{canonical_slug_file} to #{bucket.name}:/#{canonical_storage_slug_file}")