class Titanic::Titanic
Public Class Methods
new(opts={})
click to toggle source
# File lib/titanic.rb, line 11 def initialize(opts={}) @paranoid = true log.debug "opts are #{opts}" require_and_set(opts,[ 'project', 'prefix', ]) set(opts,[ 'access_key', 'secret_key', 'bucket', 'url', 'environment', 'role', 'paranoid', 'uuid', 'deploy_id', ]) log.debug "paranoid is set to #{@paranoid}" end
Public Instance Methods
get_build_from_http(uuid,path)
click to toggle source
# File lib/titanic.rb, line 115 def get_build_from_http(uuid,path) unless Dir.exists? File.dirname(path) if @paranoid log.error "the parent directory of #{path} does not exist and paranoid is set." log.error "consider unsetting paranoid if you want to take a walk on the wild side" log.error "exiting..." exit 6 else FileUtils.mkdir_p File.dirname(path) end end if File.exists?(path) and @paranoid log.error "refusing to clobbler #{path} because paranoid is set" log.error "unset paranoid if your in the danger zone" log.error "exiting..." exit 7 end build_path = archive_path uuid verify ['url', 'prefix'] full_path = File.join(@url,build_path) begin log.debug "trying to download #{full_path}" log.debug "full_path.inspect is #{full_path.inspect}" log.debug "full_path.class is #{full_path.class}" log.debug "writing file to #{path}" File.open(path, 'w') do |target_file| IO.copy_stream(open(full_path), target_file) end rescue Object => e log.error "could not get #{build_path} from #{@url} because #{e.inspect}" raise e end log.info "downloaded #{full_path} to #{path}" end
get_build_from_s3(uuid,path)
click to toggle source
# File lib/titanic.rb, line 80 def get_build_from_s3(uuid,path) build_path = archive_path uuid unless s3.objects[build_path].exists? log.error "tring to get build #{build_path} from #{@bucket} which does not exist" log.error "exiting..." exit 3 end log.debug "getting build #{build_path} from #{@bucket}" unless Dir.exists? File.dirname(path) if @paranoid log.error "looks like the parent dir for #{path} does not exist and paranoid is set" log.error "set paranoid to false to automatically create this and do other potentially dangerous things" log.error "exiting..." exit 4 else FileUtils.mkdir_p File.dirname(path) end end if File.exists?(path) and @paranoid log.error "you are trying to clober #{path} and paranoid is set" log.error "unset this if you don't care about these types of things" log.error "exiting..." exit 5 end log.info "downloading s3://#{@bucket}/#{build_path} to #{path} " # streaming download from S3 to a file on disk File.open(path, 'w') do |file| s3.objects[build_path].read do |chunk| file.write(chunk) end end end
get_deploy_from_http()
click to toggle source
# File lib/titanic.rb, line 66 def get_deploy_from_http() proper_path = deploy_path proper_path = "/#{proper_path}" unless proper_path[0] == '/' full_path = File.join(@url,proper_path) begin log.debug "trying to get #{full_path}" body = URI.parse(full_path).read return JSON.parse(body) rescue Object => e log.error "could not get deploy #{proper_path} from #{@url} because of #{e.inspect}" raise e, "could not get deploy #{proper_path} from #{@url}" end end
get_deploy_from_s3()
click to toggle source
# File lib/titanic.rb, line 57 def get_deploy_from_s3() unless s3.objects[deploy_path].exists? log.error "deployment #{deploy_path} does not exist" log.error "exiting" exit 8 end return JSON.parse(s3.objects[deploy_path].read) end
put_build(uuid,path)
click to toggle source
# File lib/titanic.rb, line 32 def put_build(uuid,path) build_path = archive_path uuid if s3.objects[build_path].exists? and @paranoid log.error "refusing to overwrite build because paranoid is set" log.error "unset paranoid to remove this and other saftey features" log.error "exiting..." exit 2 else log.info "putting #{path} at #{build_path}" s3.objects[build_path].write(Pathname.new(path)) log.debug "successfully put #{File.basename path}" end end
put_deploy()
click to toggle source
# File lib/titanic.rb, line 46 def put_deploy verify(['environment','role','uuid','deploy_id']) log.info "releasing new deploy #{@uuid} at #{deploy_path}" deploy_contents = { 'uuid' => @uuid, 'deploy_id' => @deploy_id } s3.objects[deploy_path].write(deploy_contents.to_json) log.debug "done" end
Private Instance Methods
archive_path(uuid)
click to toggle source
# File lib/titanic.rb, line 154 def archive_path(uuid) File.join @prefix, @project, 'artifacts', uuid end
configure_logger_for()
click to toggle source
# File lib/titanic.rb, line 222 def configure_logger_for logger = Logger.new(STDOUT) logger.info "set the DEBUG environment to get debug messages" unless ENV['DEBUG'] logger.level = Logger::INFO unless ENV['DEBUG'] logger.progname = self.class.name logger end
configure_s3()
click to toggle source
# File lib/titanic.rb, line 173 def configure_s3 verify(['access_key', 'secret_key', 'bucket', 'prefix']) AWS.config( :access_key_id => @access_key, :secret_access_key => @secret_key, ) bucket = AWS::S3.new.buckets[@bucket] log.debug "checking if bucket #{bucket} exists" unless bucket.exists? log.error "bucket #{bucket} does not exist" if @paranoid log.error "unset paranoid to create bucket and do other non-paranoid type things" log.error "exiting" exit 1 else log.error "paranoid is not set, so we will create the bucket" bucket = AWS::S3.new.buckets.create(@bucket) end end return bucket end
deploy_path()
click to toggle source
# File lib/titanic.rb, line 158 def deploy_path log.debug "in deploy_path" %w{prefix project environment role}.each do |var| log.debug "var #{var} is:" value = instance_variable_get "@#{var}" log.debug " #{value}" end File.join @prefix, @project, 'deploys', @environment, @role end
log()
click to toggle source
# File lib/titanic.rb, line 218 def log @logger ||= configure_logger_for end
require_and_set(opts,required_opts)
click to toggle source
# File lib/titanic.rb, line 195 def require_and_set(opts,required_opts) required_opts.each do |required| raise "you need to provide #{required}" unless opts[required] log.debug "setting instance variable #{required} to #{opts[required]}" instance_variable_set "@#{required}", opts[required] end end
s3()
click to toggle source
# File lib/titanic.rb, line 169 def s3 @s3 ||= configure_s3 end
set(opts,optional_opts)
click to toggle source
# File lib/titanic.rb, line 203 def set(opts,optional_opts) log.debug "in set with opts #{opts}" optional_opts.each do |optional| log.debug "in set with #{optional}" instance_variable_set("@#{optional}", opts[optional]) if opts.include? optional end end
verify(required_opts)
click to toggle source
# File lib/titanic.rb, line 211 def verify(required_opts) required_opts.each do |required| value = instance_variable_get "@#{required}" raise "you need to specify #{required}" unless instance_variable_get "@#{required}" end end