Class: LetsencryptHeroku::Process::UpdateCertificates

Inherits:
Object
  • Object
show all
Includes:
Tools
Defined in:
lib/letsencrypt_heroku/process/update_certificates.rb

Instance Method Summary collapse

Methods included from Tools

#banner, #error, #execute, #log, #output

Instance Method Details

#has_already_cert(herokuapp) ⇒ Object



24
25
26
27
28
# File 'lib/letsencrypt_heroku/process/update_certificates.rb', line 24

def has_already_cert(herokuapp)
  Open3.popen3("heroku certs:info --app #{herokuapp}") do |stdin, stdout, stderr, wait_thr|
    return wait_thr.value.success?
  end
end

#perform(context) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/letsencrypt_heroku/process/update_certificates.rb', line 5

def perform(context)
  herokuapp = context.config.heroku_certificate_app

  output 'Update certificates' do
    csr = Acme::Client::CertificateRequest.new(names: context.config.domains)
    certificate = context.client.new_certificate(csr)
    File.write('privkey.pem', certificate.request.private_key.to_pem)
    File.write('fullchain.pem', certificate.fullchain_to_pem)

    if has_already_cert(herokuapp)
      execute "heroku certs:update fullchain.pem privkey.pem --confirm #{herokuapp} --app #{herokuapp}"
    else
      execute "heroku certs:add fullchain.pem privkey.pem --app #{herokuapp}"
    end
    FileUtils.rm %w(privkey.pem fullchain.pem)
  end
  puts # finish the output with a nice newline ;)
end