Class: DPL::Provider::CodeDeploy

Inherits:
DPL::Provider show all
Defined in:
lib/dpl/provider/code_deploy.rb

Instance Method Summary collapse

Instance Method Details

#access_key_idObject



24
25
26
# File 'lib/dpl/provider/code_deploy.rb', line 24

def access_key_id
  options[:access_key_id] || context.env['AWS_ACCESS_KEY_ID'] || raise(Error, "missing access_key_id")
end

#bundle_typeObject



126
127
128
129
130
131
132
# File 'lib/dpl/provider/code_deploy.rb', line 126

def bundle_type
  if s3_key =~ /\.(tar|tgz|zip)$/
    options[:bundle_type] || $1
  else
    option(:bundle_type)
  end
end

#check_authObject



142
143
144
# File 'lib/dpl/provider/code_deploy.rb', line 142

def check_auth
  log "Logging in with Access Key: #{access_key_id[-4..-1].rjust(20, '*')}"
end

#cleanupObject



146
147
# File 'lib/dpl/provider/code_deploy.rb', line 146

def cleanup
end

#code_deployObject



7
8
9
# File 'lib/dpl/provider/code_deploy.rb', line 7

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

#code_deploy_optionsObject



15
16
17
18
19
20
21
22
# File 'lib/dpl/provider/code_deploy.rb', line 15

def code_deploy_options
  code_deploy_options = {
    region:      options[:region] || 'us-east-1',
    credentials: Aws::Credentials.new(access_key_id, secret_access_key)
  }
  code_deploy_options[:endpoint] = options[:endpoint] if options[:endpoint]
  code_deploy_options
end

#default_descriptionObject



138
139
140
# File 'lib/dpl/provider/code_deploy.rb', line 138

def default_description
  "Deploy build #{context.env['TRAVIS_BUILD_NUMBER']} via Travis CI"
end

#github_revisionObject



71
72
73
74
75
76
77
78
79
# File 'lib/dpl/provider/code_deploy.rb', line 71

def github_revision
  {
    revision_type: 'GitHub',
    git_hub_location: {
      commit_id:  options[:commit_id]  || context.env['TRAVIS_COMMIT']    || `git rev-parse HEAD`.strip,
      repository: options[:repository] || context.env['TRAVIS_REPO_SLUG'] || option(:repository)
    }
  }
end

#needs_key?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/dpl/provider/code_deploy.rb', line 32

def needs_key?
  false
end

#push_appObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/dpl/provider/code_deploy.rb', line 81

def push_app
  rev = revision()
  rev_info = rev[:s3_location]

  if rev_info && rev_info[:version]
    log "Registering app revision with version=#{rev_info[:version]}, etag=#{rev_info[:e_tag]}"
    code_deploy.register_application_revision({
      revision:               rev,
      application_name:       options[:application] || option(:application_name),
      description:            options[:description] || default_description
    })
  end

  data = code_deploy.create_deployment({
    revision:               revision,
    application_name:       options[:application]      || option(:application_name),
    deployment_group_name:  options[:deployment_group] || option(:deployment_group_name),
    description:            options[:description]      || default_description
  })
  log "Triggered deployment #{data.deployment_id.inspect}."
  return unless options[:wait_until_deployed]
  print "Deploying "
  deployment = wait_until_deployed(data[:deployment_id])
  print "\n"
  if deployment[:status] == 'Succeeded'
    log "Deployment successful."
  else
    error "Deployment failed."
  end
rescue Aws::CodeDeploy::Errors::DeploymentLimitExceededException => exception
  error(exception.message)
end

#revisionObject



36
37
38
39
40
41
42
43
# File 'lib/dpl/provider/code_deploy.rb', line 36

def revision
  case options[:revision_type].to_s.downcase
  when "s3"     then s3_revision
  when "github" then github_revision
  when ""       then options[:bucket] ? s3_revision : github_revision
  else error("unknown revision type %p" % options[:revision_type])
  end
end

#revision_version_infoObject



45
46
47
48
49
50
51
52
# File 'lib/dpl/provider/code_deploy.rb', line 45

def revision_version_info
  s3api.head_object({
    bucket: option(:bucket),
    key: s3_key
  })
rescue Aws::Errors::ServiceError
  {}
end

#s3_keyObject



134
135
136
# File 'lib/dpl/provider/code_deploy.rb', line 134

def s3_key
  options[:key] || option(:s3_key)
end

#s3_revisionObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dpl/provider/code_deploy.rb', line 54

def s3_revision
  info = {
    revision_type: 'S3',
    s3_location: {
      bucket:      option(:bucket),
      bundle_type: bundle_type,
      key:         s3_key,
    }
  }
  unless revision_version_info.empty?
    info[:s3_location][:version] = revision_version_info[:version_id]
    info[:s3_location][:e_tag]   = revision_version_info[:etag]
  end

  info
end

#s3apiObject



11
12
13
# File 'lib/dpl/provider/code_deploy.rb', line 11

def s3api
  @s3api ||= Aws::S3::Client.new(code_deploy_options)
end

#secret_access_keyObject



28
29
30
# File 'lib/dpl/provider/code_deploy.rb', line 28

def secret_access_key
  options[:secret_access_key] || context.env['AWS_SECRET_ACCESS_KEY'] || raise(Error, "missing secret_access_key")
end

#uncleanupObject



149
150
# File 'lib/dpl/provider/code_deploy.rb', line 149

def uncleanup
end

#wait_until_deployed(deployment_id) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/dpl/provider/code_deploy.rb', line 114

def wait_until_deployed(deployment_id)
  deployment = {}
  loop do
    result = code_deploy.get_deployment(deployment_id: deployment_id)
    deployment = result[:deployment_info]
    break if deployment[:status] == "Succeeded" || deployment[:status] == "Failed" || deployment[:status] == "Stopped"
    print "."
    sleep 5
  end
  deployment
end