Class: Dpl::Providers::Elasticbeanstalk

Inherits:
Dpl::Provider show all
Defined in:
lib/dpl/providers/elasticbeanstalk.rb

Constant Summary collapse

NON_PRINTABLE_CHARS =

We do not actually know what characters are valid on AWS EB’s side, see: github.com/aws/aws-sdk-ruby/issues/1502

Reference: www.w3.org/TR/xml/#charsets

"\u0009\u000A\u000D\u0020-\uD7FF\uE000-\uFFFD\u10000-\u10FFFF"

Constants inherited from Dpl::Provider

Dpl::Provider::FOLDS, Dpl::Provider::STAGES

Instance Attribute Summary collapse

Attributes inherited from Dpl::Provider

#key_name, #repo_name

Instance Method Summary collapse

Methods inherited from Dpl::Provider

#before_finish, #before_init, #before_install, #before_prepare, #before_setup, #chmod, #cleanup, #cmd, #compact, #err, #error, #escape, examples, #expand, #file?, #finish?, #fold, #fold?, #info, #initialize, install_deps, install_deps?, #mkdir_p, move_files, #msg, #mv, #only, #open, #opt_for, #opt_key, #opts_for, #print, #quote, #read, #remove_dpl_dir, #rm_rf, #run, #run_cmd, #run_cmds, #run_stage, #run_stage?, #script, #setup_dpl_dir, #setup_git_config, #setup_git_http_user_agent, #setup_git_ssh, #setup_ssh_key, #shell, #sq, #ssh_keygen, #symbolize, #try_ssh_access, #uncleanup, unmove_files, validate_runtimes, #wait_for_ssh_access, #warn

Methods included from Dpl::Provider::Dsl

#apt, #apt?, #cmds, #env, #errs, #full_name, #gem, #gem?, #keep, #move, #msgs, #needs, #needs?, #node_js, #npm, #npm?, #opt, #path, #pip, #pip?, #python, #ruby_pre?, #ruby_version, #runtimes, #status, #strs, #summary, #user_agent, #vars

Methods included from Squiggle

#sq

Methods included from Assets

#asset

Methods included from Env

included, #opts

Methods included from ConfigFile

included, #opts

Methods included from Interpolate

#interpolate, #obfuscate, #vars

Methods included from Memoize

included

Constructor Details

This class inherits a constructor from Dpl::Provider

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



48
49
50
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 48

def object
  @object
end

#startedObject (readonly)

Returns the value of attribute started.



48
49
50
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 48

def started
  @started
end

#versionObject (readonly)

Returns the value of attribute version.



48
49
50
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 48

def version
  @version
end

Instance Method Details

#archive_nameObject



72
73
74
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 72

def archive_name
  "#{label}.zip"
end

#bucketObject



181
182
183
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 181

def bucket
  @bucket ||= s3.bucket(super)
end

#bucket_pathObject



84
85
86
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 84

def bucket_path
  bucket_path? ? "#{super.gsub(/\/*$/, '')}/#{archive_name}" : archive_name
end

#check_deployment(msgs) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 138

def check_deployment(msgs)
  sleep 5
  events.each do |event|
    msg = "#{event.event_date} [#{event.severity}] #{event.message}"
    error "Deployment failed: #{msg}" if event.severity == 'ERROR'
    info msg unless msgs.include?(msg)
    msgs << msg
  end
  environment[:status] == 'Ready'
rescue Aws::Errors::ServiceError => e
  info "Caught #{e}: #{e.message}. Retrying ..."
end

#clean(str) ⇒ Object



195
196
197
198
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 195

def clean(str)
  str.gsub!(/[^#{NON_PRINTABLE_CHARS}]/, '') && info(:clean_description)
  str
end

#create_versionObject



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 111

def create_version
  @version = eb.create_application_version(
    application_name: app,
    version_label: label,
    description: clean(description[0, 200]),
    source_bundle: {
      s3_bucket: bucket.name,
      s3_key: object.key
    },
    auto_create_application: false
  )
end

#create_zipObject



96
97
98
99
100
101
102
103
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 96

def create_zip
  ::Zip::File.open(zip_file, ::Zip::File::CREATE) do |zip|
    files.each do |path|
      debug :zip_add, path
      zip.add(path.sub(cwd, ''), path)
    end
  end
end

#credentialsObject



173
174
175
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 173

def credentials
  Aws::Credentials.new(access_key_id, secret_access_key)
end

#cwdObject



88
89
90
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 88

def cwd
  @cwd ||= "#{Dir.pwd}/"
end

#debug(*args) ⇒ Object



200
201
202
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 200

def debug(*args)
  info *args if debug?
end

#deployObject



59
60
61
62
63
64
65
66
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 59

def deploy
  @started = Time.now
  bucket.create unless bucket.exists?
  create_zip unless zip_exists?
  upload
  create_version
  update_app if env?
end

#descriptionObject



80
81
82
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 80

def description
  super || git_commit_msg
end

#ebObject



185
186
187
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 185

def eb
  @eb ||= Aws::ElasticBeanstalk::Client.new(retry_limit: 10)
end

#environmentObject



168
169
170
171
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 168

def environment
  args = { application_name: app, environment_names: [env] }
  eb.describe_environments(args)[:environments].first
end

#eventsObject



163
164
165
166
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 163

def events
  args = { environment_name: env, start_time: started.utc.iso8601 }
  eb.describe_events(args)[:events].reverse
end

#filesObject



151
152
153
154
155
156
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 151

def files
  files = Dir.glob('**/*', File::FNM_DOTMATCH)
  ignore = %w(.ebignore .gitignore).detect { |file| file?(file) }
  files = filter(files, ignore) if ignore
  files
end

#filter(files, spec) ⇒ Object



158
159
160
161
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 158

def filter(files, spec)
  spec = PathSpec.from_filename(spec)
  files.reject { |file| spec.match(file) }
end

#labelObject



76
77
78
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 76

def label
  @label ||= super || "travis-#{git_sha}-#{Time.now.to_i}"
end

#loginObject



50
51
52
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 50

def 
  info :login
end

#s3Object



177
178
179
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 177

def s3
  @s3 ||= Aws::S3::Resource.new
end

#setupObject



54
55
56
57
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 54

def setup
  info :login
  Aws.config.update(credentials: credentials, region: region)
end

#update_appObject



124
125
126
127
128
129
130
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 124

def update_app
  eb.update_environment(
    environment_name: env,
    version_label: version[:application_version][:version_label]
  )
  wait_until_deployed if wait_until_deployed?
end

#uploadObject



105
106
107
108
109
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 105

def upload
  @object = bucket.object(bucket_path)
  object.put(body: File.open(zip_file))
  sleep 5 # s3 eventual consistency
end

#wait_until_deployedObject



132
133
134
135
136
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 132

def wait_until_deployed
  msgs = []
  1.upto(wait_until_deployed_timeout / 5) { return if check_deployment(msgs) }
  error 'Deploy status unknown due to timeout. Increase the wait_until_deployed_timeout option'
end

#zip_exists?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 92

def zip_exists?
  File.exists?(zip_file)
end

#zip_fileObject



68
69
70
# File 'lib/dpl/providers/elasticbeanstalk.rb', line 68

def zip_file
  zip_file? ? expand(super) : archive_name
end