class DPL::Provider::ElasticBeanstalk

Constants

DEFAULT_REGION

Public Instance Methods

access_key_id() click to toggle source
# File lib/dpl/provider/elastic_beanstalk.rb, line 16
def access_key_id
  options[:access_key_id] || context.env['AWS_ACCESS_KEY_ID'] || raise(Error, "missing access_key_id")
end
check_app() click to toggle source
# File lib/dpl/provider/elastic_beanstalk.rb, line 32
def check_app
end
check_auth() click to toggle source
# File lib/dpl/provider/elastic_beanstalk.rb, line 24
def check_auth
  options = {
    :region      => region,
    :credentials => Aws::Credentials.new(access_key_id, secret_access_key)
  }
  Aws.config.update(options)
end
needs_key?() click to toggle source
# File lib/dpl/provider/elastic_beanstalk.rb, line 12
def needs_key?
  false
end
only_create_app_version() click to toggle source
# File lib/dpl/provider/elastic_beanstalk.rb, line 35
def only_create_app_version
  options[:only_create_app_version]
end
push_app() click to toggle source
# File lib/dpl/provider/elastic_beanstalk.rb, line 39
def push_app
  @start_time = Time.now
  create_bucket unless bucket_exists?

  if options[:zip_file]
    zip_file = File.expand_path(options[:zip_file])
  else
    zip_file = create_zip
  end

  s3_object = upload(archive_name, zip_file)
  sleep 5 #s3 eventual consistency
  version = create_app_version(s3_object)
  if !only_create_app_version
    update_app(version)
    wait_until_deployed if options[:wait_until_deployed]
  end
end
secret_access_key() click to toggle source
# File lib/dpl/provider/elastic_beanstalk.rb, line 20
def secret_access_key
  options[:secret_access_key] || context.env['AWS_SECRET_ACCESS_KEY'] || raise(Error, "missing secret_access_key")
end

Private Instance Methods

app_name() click to toggle source
# File lib/dpl/provider/elastic_beanstalk.rb, line 60
def app_name
  option(:app)
end
archive_name() click to toggle source
# File lib/dpl/provider/elastic_beanstalk.rb, line 76
def archive_name
  "#{version_label}.zip"
end
bucket_exists?() click to toggle source
# File lib/dpl/provider/elastic_beanstalk.rb, line 100
def bucket_exists?
  s3.bucket(bucket_name).exists?
end
bucket_name() click to toggle source
# File lib/dpl/provider/elastic_beanstalk.rb, line 84
def bucket_name
  option(:bucket_name, :bucket)
end
bucket_path() click to toggle source
# File lib/dpl/provider/elastic_beanstalk.rb, line 88
def bucket_path
  @bucket_path ||= options[:bucket_path] ? option(:bucket_path).gsub(/\/*$/,'/') : nil
end
create_app_version(s3_object) click to toggle source
# File lib/dpl/provider/elastic_beanstalk.rb, line 135
def create_app_version(s3_object)
  # Elastic Beanstalk doesn't support descriptions longer than 200 characters
  description = version_description[0, 200]
  options = {
    :application_name  => app_name,
    :version_label     => version_label,
    :description       => description,
    :source_bundle     => {
      :s3_bucket => bucket_name,
      :s3_key    => s3_object.key
    },
    :auto_create_application => false
  }
  eb.create_application_version(options)
end
create_bucket() click to toggle source
# File lib/dpl/provider/elastic_beanstalk.rb, line 104
def create_bucket
  s3.bucket(bucket_name).create
end
create_zip() click to toggle source
# File lib/dpl/provider/elastic_beanstalk.rb, line 112
def create_zip
  directory = Dir.pwd
  zipfile_name = File.join(directory, archive_name)

  Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
    files_to_pack.each do |file|
      relative_archive_path = File.join(directory, '/')
      zipfile.add(file.sub(relative_archive_path, ''), file)
    end
  end
  zipfile_name
end
eb() click to toggle source
# File lib/dpl/provider/elastic_beanstalk.rb, line 96
def eb
  @eb ||= Aws::ElasticBeanstalk::Client.new(retry_limit: 10)
end
env_name() click to toggle source
# File lib/dpl/provider/elastic_beanstalk.rb, line 64
def env_name
  options[:env] || context.env['ELASTIC_BEANSTALK_ENV'] || raise(Error, "missing env")
end
files_to_pack() click to toggle source
# File lib/dpl/provider/elastic_beanstalk.rb, line 108
def files_to_pack
  `git ls-files -z`.split("\x0")
end
region() click to toggle source
# File lib/dpl/provider/elastic_beanstalk.rb, line 80
def region
  options[:region] || DEFAULT_REGION
end
s3() click to toggle source
# File lib/dpl/provider/elastic_beanstalk.rb, line 92
def s3
  @s3 ||= Aws::S3::Resource.new
end
update_app(version) click to toggle source
# File lib/dpl/provider/elastic_beanstalk.rb, line 199
def update_app(version)
  options = {
    :environment_name  => env_name,
    :version_label     => version[:application_version][:version_label]
  }
  eb.update_environment(options)
end
upload(key, file) click to toggle source
# File lib/dpl/provider/elastic_beanstalk.rb, line 125
def upload(key, file)
  options = {
    :body => Pathname.new(file).open
  }
  bucket = s3.bucket(bucket_name)
  obj = bucket_path ? bucket.object("#{bucket_path}#{key}") : bucket.object(key)
  obj.put(options)
  obj
end
version_description() click to toggle source
# File lib/dpl/provider/elastic_beanstalk.rb, line 72
def version_description
  context.env['ELASTIC_BEANSTALK_DESCRIPTION'] || commit_msg
end
version_label() click to toggle source
# File lib/dpl/provider/elastic_beanstalk.rb, line 68
def version_label
  context.env['ELASTIC_BEANSTALK_LABEL'] || "travis-#{sha}-#{Time.now.to_i}"
end
wait_until_deployed() click to toggle source

Wait until EB environment update finishes

# File lib/dpl/provider/elastic_beanstalk.rb, line 152
def wait_until_deployed
  errorEvents = 0 # errors counter, should remain 0 for successful deployment
  events = []
  tries = 0
  max_tries = 10

  loop do
    if tries >= max_tries
      log "Too many failures"
      break
    end

    begin
      environment = eb.describe_environments({
        :application_name  => app_name,
        :environment_names => [env_name]
      })[:environments].first

      eb.describe_events({
        :environment_name  => env_name,
        :start_time        => @start_time.utc.iso8601,
      })[:events].reverse.each do |event|
        message = "#{event[:event_date]} [#{event[:severity]}] #{event[:message]}"
        unless events.include?(message)
          events.push(message)
          if event[:severity] == "ERROR"
            errorEvents += 1
            warn(message)
          else
            log(message)
          end
        end
      end

      break if environment[:status] == "Ready"
      sleep 5
    rescue Aws::Errors::ServiceError => e
      log "Caught #{e}: #{e.message}"
      log "Continuing..."
    ensure
      tries += 1
    end
  end

  if errorEvents > 0 then error("Deployment failed.") end
end