Class: Dpl::Providers::Codedeploy

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

Overview

split this up to CodeDeploy::Github and CodeDeploy::S3 using the revision_type, in order to make opts more strict

Constant Summary

Constants inherited from Dpl::Provider

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

Instance Attribute Summary

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 Method Details

#bucketObject



154
155
156
# File 'lib/dpl/providers/codedeploy.rb', line 154

def bucket
  super || error(:missing_bucket)
end

#build_numberObject



170
171
172
# File 'lib/dpl/providers/codedeploy.rb', line 170

def build_number
  ENV['TRAVIS_BUILD_NUMBER']
end

#bundle_typeObject



162
163
164
# File 'lib/dpl/providers/codedeploy.rb', line 162

def bundle_type
  super || key =~ /\.(tar|tgz|zip)$/ && ::Regexp.last_match(1) || error(:unknown_bundle_type)
end

#client_optionsObject



182
183
184
# File 'lib/dpl/providers/codedeploy.rb', line 182

def client_options
  compact(region:, credentials:, endpoint:)
end

#code_deployObject



174
175
176
# File 'lib/dpl/providers/codedeploy.rb', line 174

def code_deploy
  @code_deploy ||= Aws::CodeDeploy::Client.new(client_options)
end

#commit_idObject



146
147
148
# File 'lib/dpl/providers/codedeploy.rb', line 146

def commit_id
  super || git_sha
end

#create_deploymentObject



76
77
78
79
80
81
82
83
84
85
# File 'lib/dpl/providers/codedeploy.rb', line 76

def create_deployment
  deployment = code_deploy.create_deployment(
    revision:,
    application_name: application,
    deployment_group_name: deployment_group,
    description:,
    file_exists_behavior: file_exists_behavior.upcase
  )
  deployment.deployment_id
end

#credentialsObject



186
187
188
# File 'lib/dpl/providers/codedeploy.rb', line 186

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

#deployObject



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

def deploy
  register_revision if revision_info[:version]
  id = create_deployment
  info :deploy_triggered, id
  wait_until_deployed(id) if wait_until_deployed?
rescue Aws::CodeDeploy::Errors::DeploymentLimitExceededException => e
  error e.message
end

#descriptionObject



166
167
168
# File 'lib/dpl/providers/codedeploy.rb', line 166

def description
  interpolate(super || msg(:description), vars:)
end

#github_revisionObject



136
137
138
139
140
141
142
143
144
# File 'lib/dpl/providers/codedeploy.rb', line 136

def github_revision
  {
    revision_type: 'GitHub',
    git_hub_location: {
      commit_id:,
      repository:
    }
  }
end

#keyObject



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

def key
  super || error(:missing_key)
end

#loginObject



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

def 
  info :login
end

#poll(id) ⇒ Object



98
99
100
101
102
# File 'lib/dpl/providers/codedeploy.rb', line 98

def poll(id)
  sleep 5
  print '.'
  code_deploy.get_deployment(deployment_id: id)[:deployment_info][:status]
end

#register_revisionObject



67
68
69
70
71
72
73
74
# File 'lib/dpl/providers/codedeploy.rb', line 67

def register_revision
  info :register_revision, revision_info[:version], revision_info[:e_tag]
  code_deploy.register_application_revision(
    revision:,
    application_name: application,
    description:
  )
end

#repositoryObject



150
151
152
# File 'lib/dpl/providers/codedeploy.rb', line 150

def repository
  super || ENV['TRAVIS_REPO_SLUG'] || File.basename(Dir.pwd)
end

#revisionObject



108
109
110
111
112
113
114
115
# File 'lib/dpl/providers/codedeploy.rb', line 108

def revision
  @revision ||= case revision_type
                when 's3'     then s3_revision
                when 'github' then github_revision
                when nil      then bucket? ? s3_revision : github_revision
                else error :unknown_revision_type, revision_type
                end
end

#revision_infoObject



104
105
106
# File 'lib/dpl/providers/codedeploy.rb', line 104

def revision_info
  revision[:s3_location] || {}
end

#revision_version_infoObject



130
131
132
133
134
# File 'lib/dpl/providers/codedeploy.rb', line 130

def revision_version_info
  s3.head_object(bucket:, key:)
rescue Aws::Errors::ServiceError => e
  error e.message
end

#s3Object



178
179
180
# File 'lib/dpl/providers/codedeploy.rb', line 178

def s3
  @s3 ||= Aws::S3::Client.new(client_options)
end

#s3_revisionObject



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/dpl/providers/codedeploy.rb', line 117

def s3_revision
  {
    revision_type: 'S3',
    s3_location: compact(
      bucket:,
      bundle_type:,
      version: revision_version_info[:version_id],
      e_tag: revision_version_info[:etag],
      key:
    )
  }
end

#wait_until_deployed(id) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/dpl/providers/codedeploy.rb', line 87

def wait_until_deployed(id)
  print :waiting_for_deploy
  status = poll(id) until %w[Succeeded Failed Stopped].include?(status)
  case status
  when 'Succeeded'
    info :finished_deploy, status
  else
    error :finished_deploy, status
  end
end