class Kontena::Cli::Certificate::AuthorizeCommand

Public Instance Methods

execute() click to toggle source
# File lib/kontena/cli/certificate/authorize_command.rb, line 31
def execute
  exit_with_error "--linked-service is required with --type=#{type}" if requires_linked_service? && !self.linked_service

  data = {
    domain: domain,
    authorization_type: self.type
  }
  data[:linked_service] = service_path(self.linked_service) if self.linked_service
  retried = false

  response = nil
  retry_on_le_registration do
    response = client.post("grids/#{current_grid}/domain_authorizations", data)
  end

  case self.type
  when 'dns-01'
    puts "Authorization successfully created. Use the following details to create necessary validations:"
    puts "Record name: #{response.dig('challenge_opts', 'record_name')}.#{domain}"
    puts "Record type: #{response.dig('challenge_opts', 'record_type')}"
    puts "Record content: #{response.dig('challenge_opts', 'record_content')}"
  when 'http-01'
    domain_auth = spinner "Waiting for http-01 challenge to be deployed into #{response.dig('linked_service', 'id').colorize(:cyan)} " do
      wait_for_domain_auth_deployed(response)
    end
    if domain_auth['state'] == 'deploy_error'
      exit_with_error "Linked services deploy failed. Check service events for details"
    else
      puts "HTTP challenge is deployed, you can now request the actual certificate"
    end
  else
    exit_with_error "Unknown authorization type: #{self.type}"
  end
end
requires_linked_service?() click to toggle source
# File lib/kontena/cli/certificate/authorize_command.rb, line 20
def requires_linked_service?
  case type
  when 'dns-01'
    false
  when 'http-01'
    true
  else
    fail "Invalid authorization --type=#{type}"
  end
end
retry_on_le_registration() { || ... } click to toggle source
# File lib/kontena/cli/certificate/authorize_command.rb, line 85
def retry_on_le_registration
  yield
rescue Kontena::Errors::StandardErrorHash => exc
  raise unless exc.errors.has_key?('le_registration')
  # Run through registration
  puts "Let's Encrypt registration missing, creating one."
  email = prompt.ask("Email for Let's Encrypt:")
  Kontena.run!(['certificate', 'register', email])
  yield
end
service_path(linked_service) click to toggle source
# File lib/kontena/cli/certificate/authorize_command.rb, line 77
def service_path(linked_service)
  unless linked_service.include?('/')
    "null/#{linked_service}"
  else
    linked_service
  end
end
wait_for_domain_auth_deployed(domain_auth) click to toggle source
# File lib/kontena/cli/certificate/authorize_command.rb, line 66
def wait_for_domain_auth_deployed(domain_auth)
  Timeout.timeout(300) {
    while domain_auth['status'] == 'deploying' do
      sleep 1

      domain_auth = client.get("domain_authorizations/#{domain_auth['id']}")
    end
    return domain_auth
  }
end