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, #user_agent, #warn

Constructor Details

This class inherits a constructor from DPL::Provider

Instance Method Details

#access_key_idObject



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

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

#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



37
38
39
40
# File 'lib/dpl/provider/ops_works.rb', line 37

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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/dpl/provider/ops_works.rb', line 79

def create_deployment
  deployment_config = {
    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
  }
  if !options[:instance_ids].nil?
    deployment_config[:instance_ids] = option(:instance_ids)
  end
  data = client.create_deployment(deployment_config)
  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



55
56
57
# File 'lib/dpl/provider/ops_works.rb', line 55

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

#custom_jsonObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dpl/provider/ops_works.rb', line 42

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

#deployObject



119
120
121
122
123
124
125
# File 'lib/dpl/provider/ops_works.rb', line 119

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



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

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



59
60
61
# File 'lib/dpl/provider/ops_works.rb', line 59

def ops_works_app
  @ops_works_app ||= fetch_ops_works_app
end

#push_appObject



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

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

#secret_access_keyObject



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

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

#setup_authObject



33
34
35
# File 'lib/dpl/provider/ops_works.rb', line 33

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

#travis_deploy_commentObject



115
116
117
# File 'lib/dpl/provider/ops_works.rb', line 115

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

#wait_until_deployed(deployment_id) ⇒ Object



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

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