Class: DPL::Provider::OpsWorks

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

Instance Method Summary collapse

Instance Method Details

#access_key_idObject



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

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

#check_appObject



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

def check_app

end

#check_authObject



40
41
42
# File 'lib/dpl/provider/ops_works.rb', line 40

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

#create_deploymentObject



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
# File 'lib/dpl/provider/ops_works.rb', line 81

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
  }
  if !options[:instance_ids].nil?
    deployment_config[:instance_ids] = Array(option(:instance_ids))
  end
  if !options[:layer_ids].nil?
    deployment_config[:layer_ids] = Array(option(:layer_ids))
  end
  log "creating deployment #{deployment_config.to_json}"
  data = opsworks.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."
    update = options[:update_app_on_success] || options[:update_on_success]
    return unless update.to_s.squeeze.downcase == 'true'
    update_app
  else
    error "Deployment failed."
  end
end

#current_shaObject



57
58
59
# File 'lib/dpl/provider/ops_works.rb', line 57

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

#custom_jsonObject



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

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

#deployObject



139
140
141
142
143
# File 'lib/dpl/provider/ops_works.rb', line 139

def deploy
  super
rescue Aws::Errors::ServiceError => error
  raise Error, "Stopping Deploy, OpsWorks service error: #{error.message}", error.backtrace
end

#fetch_ops_works_appObject



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

def fetch_ops_works_app
  data = opsworks.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)


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

def needs_key?
  false
end

#ops_works_appObject



61
62
63
# File 'lib/dpl/provider/ops_works.rb', line 61

def ops_works_app
  @ops_works_app ||= fetch_ops_works_app
end

#opsworksObject



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

def opsworks
  @opsworks ||= Aws::OpsWorks::Client.new(opsworks_options)
end

#opsworks_optionsObject



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

def opsworks_options
  {
    region:      region || 'us-east-1',
    credentials: ::Aws::Credentials.new(access_key_id, secret_access_key)
  }
end

#push_appObject



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

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

#regionObject



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

def region
  options[:region] || context.env['AWS_DEFAULT_REGION']
end

#secret_access_keyObject



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

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

#travis_deploy_commentObject



135
136
137
# File 'lib/dpl/provider/ops_works.rb', line 135

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

#update_appObject



112
113
114
115
116
117
118
119
120
121
# File 'lib/dpl/provider/ops_works.rb', line 112

def update_app
  update_config = {
    app_id: option(:app_id),
    app_source: {
      revision: current_sha,
    }
  }
  opsworks.update_app(update_config)
  log "Application Source branch/revision setting updated."
end

#wait_until_deployed(deployment_id) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/dpl/provider/ops_works.rb', line 123

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