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

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

Instance Attribute Summary collapse

Attributes inherited from DPL::Provider

#context, #options

Instance Method Summary collapse

Methods inherited from Generic

#api, #api_options, #check_app, #check_auth, #deploy, #info, #needs_key?, #restart, #run, #user

Methods inherited from DPL::Provider

apt_get, #check_app, #cleanup, #commit_msg, context, #create_key, #deploy, #detect_encoding?, #encoding_for, #error, experimental, #initialize, #log, #needs_key?, 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 Attribute Details

#build_idObject (readonly)

Returns the value of attribute build_id.



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

def build_id
  @build_id
end

Instance Method Details

#archive_fileObject



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

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

#get(subpath, options = {}) ⇒ Object



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

def get(subpath, options = {})
  options = {
    method: :get,
    path: "/apps/#{option(:app)}/#{subpath}",
    headers: { "Accept" => "application/vnd.heroku+json; version=3" },
    expects: [200]
  }.merge(options)

  api.request(options).body
end

#get_urlObject



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

def get_url
  source_blob.fetch("get_url")
end

#pack_archiveObject



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

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

#post(subpath, body = nil, options = {}) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/dpl/provider/heroku/api.rb', line 82

def post(subpath, body = nil, options = {})
  options = {
    method: :post,
    path: "/apps/#{option(:app)}/#{subpath}",
    headers: { "Accept" => "application/vnd.heroku+json; version=3" },
    expects: [200, 201]
  }.merge(options)

  if body
    options[:body]                    = JSON.dump(body)
    options[:headers]['Content-Type'] = 'application/json'
  end

  api.request(options).body
end

#push_appObject



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

def push_app
  pack_archive
  upload_archive
  trigger_build
  verify_build
end

#put_urlObject



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

def put_url
  source_blob.fetch("put_url")
end

#source_blobObject



63
64
65
# File 'lib/dpl/provider/heroku/api.rb', line 63

def source_blob
  @source_blog ||= post(:sources).fetch("source_blob")
end

#trigger_buildObject



31
32
33
34
35
36
37
# File 'lib/dpl/provider/heroku/api.rb', line 31

def trigger_build
  log "triggering new deployment"
  response   = post(:builds, source_blob: { url: get_url, version: version })
  @build_id  = response.fetch('id')
  output_stream_url = response.fetch('output_stream_url')
  context.shell "curl #{Shellwords.escape(output_stream_url)}"
end

#upload_archiveObject



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

def upload_archive
  log "uploading application archive"
  context.shell "curl #{Shellwords.escape(put_url)} -X PUT -H 'Content-Type:' --data-binary @#{archive_file}"
end

#verify_buildObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dpl/provider/heroku/api.rb', line 39

def verify_build
  loop do
    response = get("builds/#{build_id}/result")
    exit_code = response.fetch('exit_code')
    if exit_code.nil?
      log "heroku build still pending"
      sleep 5
      next
    elsif exit_code == 0
      break
    else
      error "deploy failed, build exited with code #{exit_code}"
    end
  end
end

#versionObject



67
68
69
# File 'lib/dpl/provider/heroku/api.rb', line 67

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