Class: DPL::Provider::Heroku::API

Inherits:
Generic show all
Defined in:
lib/dpl/provider/heroku/api.rb

Instance Attribute Summary collapse

Attributes inherited from Generic

#app, #user

Instance Method Summary collapse

Methods inherited from Generic

#check_app, #check_auth, #faraday, #handle_error_response, #needs_key?, #restart, #run

Instance Attribute Details

#build_idObject (readonly)

Returns the value of attribute build_id.



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

def build_id
  @build_id
end

Instance Method Details

#archive_fileObject



18
19
20
# File 'lib/dpl/provider/heroku/api.rb', line 18

def archive_file
  Shellwords.escape("#{context.env['HOME']}/.dpl.#{option(:app)}.tgz")
end

#curl_optionsObject



95
96
97
# File 'lib/dpl/provider/heroku/api.rb', line 95

def curl_options
  $stdout.isatty ? '' : ' -sS'
end

#get_urlObject



71
72
73
# File 'lib/dpl/provider/heroku/api.rb', line 71

def get_url
  source_blob.fetch("get_url")
end

#pack_archiveObject



22
23
24
25
# File 'lib/dpl/provider/heroku/api.rb', line 22

def pack_archive
  log "creating application archive"
  context.shell "tar -zcf #{archive_file} --exclude .git ."
end

#push_appObject



11
12
13
14
15
16
# File 'lib/dpl/provider/heroku/api.rb', line 11

def push_app
  pack_archive
  upload_archive
  trigger_build
  verify_build
end

#put_urlObject



75
76
77
# File 'lib/dpl/provider/heroku/api.rb', line 75

def put_url
  source_blob.fetch("put_url")
end

#source_blobObject



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/dpl/provider/heroku/api.rb', line 79

def source_blob
  return @source_blob if @source_blob

  response = faraday.post('/sources')

  if response.success?
    @source_blob = JSON.parse(response.body)["source_blob"]
  else
    handle_error_response(response)
  end
end

#trigger_buildObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dpl/provider/heroku/api.rb', line 32

def trigger_build
  log "triggering new deployment"
  response = faraday.post("/apps/#{option(:app)}/builds") do |req|
    req.headers['Content-Type'] = 'application/json'
    req.body = {
      "source_blob" => {
        "url" => get_url,
        "version" => version
      }
      }.to_json
  end

  if response.success?
    @build_id  = JSON.parse(response.body)['id']
    output_stream_url = JSON.parse(response.body)['output_stream_url']
    context.shell "curl#{curl_options} #{Shellwords.escape(output_stream_url)} -H 'Accept: application/vnd.heroku+json; version=3' -H 'User-Agent: #{user_agent}'"
  else
    handle_error_response(response)
  end
end

#upload_archiveObject



27
28
29
30
# File 'lib/dpl/provider/heroku/api.rb', line 27

def upload_archive
  log "uploading application archive"
  context.shell "curl#{curl_options} #{Shellwords.escape(put_url)} -X PUT -H 'Content-Type:' -H 'Accept: application/vnd.heroku+json; version=3' -H 'User-Agent: #{user_agent}' --data-binary @#{archive_file}"
end

#verify_buildObject



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

def verify_build
  loop do
    response = faraday.get("/apps/#{option(:app)}/builds/#{build_id}")
    body = JSON.parse(response.body)

    case body['status']
    when 'pending'
      log "heroku build still pending"
      sleep 5
      next
    when 'succeeded'
      break
    else
      error "deploy failed"
    end
  end
end

#versionObject



91
92
93
# File 'lib/dpl/provider/heroku/api.rb', line 91

def version
  @version ||= options[:version] || context.env['TRAVIS_COMMIT'] || `git rev-parse HEAD`.strip
end