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



29
30
31
32
33
# File 'lib/letsencrypt_heroku/process/update_certificates.rb', line 29

def has_already_cert(herokuapp)
  execute("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
23
24
25
26
27
# 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)
    privkey_name = "privkey_#{herokuapp}.pem"
    fullchain_name = "fullchain_#{herokuapp}.pem"
    File.write(privkey_name, certificate.request.private_key.to_pem)
    File.write(fullchain_name, certificate.fullchain_to_pem)

    if has_already_cert(herokuapp)
      execute "heroku certs:update #{fullchain_name} #{privkey_name} --confirm #{herokuapp} --app #{herokuapp}"
    else
      execute "heroku certs:add #{fullchain_name} #{privkey_name} --app #{herokuapp}"
    end

    unless context.config.keep_certs
      FileUtils.rm [privkey_name, fullchain_name]
    end
  end
  puts # finish the output with a nice newline ;)
end