Class: DPL::Provider::OpsWorks

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

Instance Attribute Summary

Attributes inherited from DPL::Provider

#context, #options

Instance Method Summary collapse

Methods inherited from DPL::Provider

apt_get, #cleanup, #commit_msg, context, #create_key, #detect_encoding?, #encoding_for, #error, experimental, #initialize, #log, new, npm_g, #option, pip, requires, #run, #setup_git_credentials, #setup_git_ssh, #sha, shell, #uncleanup, #warn

Constructor Details

This class inherits a constructor from DPL::Provider

Instance Method Details

#apiObject



9
10
11
# File 'lib/dpl/provider/ops_works.rb', line 9

def api
  @api ||= AWS::OpsWorks.new
end

#check_appObject



21
22
23
# File 'lib/dpl/provider/ops_works.rb', line 21

def check_app

end

#check_authObject



29
30
31
32
# File 'lib/dpl/provider/ops_works.rb', line 29

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

#clientObject



13
14
15
# File 'lib/dpl/provider/ops_works.rb', line 13

def client
  @client ||= api.client
end

#create_deploymentObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/dpl/provider/ops_works.rb', line 71

def create_deployment
  data = client.create_deployment(
    stack_id: ops_works_app[:stack_id],
    app_id: option(:app_id),
    command: {name: 'deploy'},
    comment: travis_deploy_comment,
    custom_json: custom_json.to_json
  )
  log "Deployment created: #{data[:deployment_id]}"
  return unless options[:wait_until_deployed]
  print "Deploying "
  deployment = wait_until_deployed(data[:deployment_id])
  print "\n"
  if deployment[:status] == 'successful'
    log "Deployment successful."
  else
    error "Deployment failed."
  end
end

#current_shaObject



47
48
49
# File 'lib/dpl/provider/ops_works.rb', line 47

def current_sha
  @current_sha ||= `git rev-parse HEAD`.chomp
end

#custom_jsonObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/dpl/provider/ops_works.rb', line 34

def custom_json
  {
    deploy: {
      ops_works_app[:shortname] => {
        migrate: !!options[:migrate],
        scm: {
          revision: current_sha
        }
      }
    }
  }
end

#deployObject



107
108
109
110
111
112
113
# File 'lib/dpl/provider/ops_works.rb', line 107

def deploy
  super
rescue AWS::Errors::ClientError => error
  raise Error, "Stopping Deploy, OpsWorks error: #{error.message}", error.backtrace
rescue AWS::Errors::ServerError => error
  raise Error, "Stopping Deploy, OpsWorks server error: #{error.message}", error.backtrace
end

#fetch_ops_works_appObject



55
56
57
58
59
60
61
# File 'lib/dpl/provider/ops_works.rb', line 55

def fetch_ops_works_app
  data = client.describe_apps(app_ids: [option(:app_id)])
  unless data[:apps] && data[:apps].count == 1
    raise Error, "App #{option(:app_id)} not found.", error.backtrace
  end
  data[:apps].first
end

#needs_key?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/dpl/provider/ops_works.rb', line 17

def needs_key?
  false
end

#ops_works_appObject



51
52
53
# File 'lib/dpl/provider/ops_works.rb', line 51

def ops_works_app
  @ops_works_app ||= fetch_ops_works_app
end

#push_appObject



63
64
65
66
67
68
69
# File 'lib/dpl/provider/ops_works.rb', line 63

def push_app
  Timeout::timeout(600) do
    create_deployment
  end
rescue Timeout::Error
  error 'Timeout: Could not finish deployment in 10 minutes.'
end

#setup_authObject



25
26
27
# File 'lib/dpl/provider/ops_works.rb', line 25

def setup_auth
  AWS.config(access_key_id: option(:access_key_id), secret_access_key: option(:secret_access_key))
end

#travis_deploy_commentObject



103
104
105
# File 'lib/dpl/provider/ops_works.rb', line 103

def travis_deploy_comment
  "Deploy build #{ENV['TRAVIS_BUILD_NUMBER'] || current_sha} via Travis CI"
end

#wait_until_deployed(deployment_id) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/dpl/provider/ops_works.rb', line 91

def wait_until_deployed(deployment_id)
  deployment = nil
  loop do
    result = client.describe_deployments(deployment_ids: [deployment_id])
    deployment = result[:deployments].first
    break unless deployment[:status] == "running"
    print "."
    sleep 5
  end
  deployment
end