class Epimetheus::Epimetheus

Public Class Methods

new(opts={}) click to toggle source
# File lib/epimetheus.rb, line 10
def initialize(opts={})
  @paranoid = true
  log.debug "opts are #{opts}"
  require_and_set(opts,[
                        'project',
                       ])
  set(opts,[
            'access_key',
            'secret_key',
            'bucket',
            'prefix',
            'url',
            'environment',
            'role',
            'paranoid',
           ])
  log.debug "paranoid is set to #{@paranoid}"
end

Public Instance Methods

get_build_from_http(uuid,path) click to toggle source
# File lib/epimetheus.rb, line 106
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']
  uri = URI File.join(@url,build_path)
  log.debug "trying to get @{uri}"
  begin
    Net::HTTP.start(uri.host, uri.port) do |http|
      request = Net::HTTP::Get.new uri.request_uri
      http.request request do |response|
        open path, 'w' do |io|
          response.read_body do |chunk|
            io.write chunk
          end
        end
      end
    end
  rescue
    raise "could not get #{build_path} from #{@url}"
  end
  log.info "downloaded #{uri} to #{path}"
end
get_build_from_s3(uuid,path) click to toggle source
# File lib/epimetheus.rb, line 71
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/epimetheus.rb, line 59
def get_deploy_from_http()
  proper_path = deploy_path
  proper_path = "/#{proper_path}" unless proper_path[0] == '/'
  begin
    log.debug "trying to get #{proper_path} from #{@url}"
    uri = URI File.join(@url,proper_path)
    return Net::HTTP.get(uri)
  rescue
    raise "could not get deploy #{proper_path} from #{@url}"
  end
end
get_deploy_from_s3() click to toggle source
# File lib/epimetheus.rb, line 50
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 s3.objects[deploy_path].read
end
put_build(uuid,path) click to toggle source
# File lib/epimetheus.rb, line 29
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(uuid) click to toggle source
# File lib/epimetheus.rb, line 43
def put_deploy(uuid)
  verify(['environment','role'])
  log.info "releasing new deploy #{uuid} at #{deploy_path}"
  s3.objects[deploy_path].write(uuid)
  log.debug "done"
end

Private Instance Methods

archive_path(uuid) click to toggle source
# File lib/epimetheus.rb, line 148
def archive_path(uuid)
  File.join @prefix, @project, 'artifacts', uuid
end
configure_logger_for() click to toggle source
# File lib/epimetheus.rb, line 209
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/epimetheus.rb, line 160
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/epimetheus.rb, line 152
def deploy_path
  File.join @prefix, @project, 'deploys', @environment, @role
end
log() click to toggle source
# File lib/epimetheus.rb, line 205
def log
  @logger ||= configure_logger_for
end
require_and_set(opts,required_opts) click to toggle source
# File lib/epimetheus.rb, line 182
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/epimetheus.rb, line 156
def s3
  @s3 ||= configure_s3
end
set(opts,optional_opts) click to toggle source
# File lib/epimetheus.rb, line 190
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/epimetheus.rb, line 198
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