Class: Kontena::Cli::Certificate::AuthorizeCommand

Inherits:
Kontena::Command
  • Object
show all
Includes:
Kontena::Cli::Common, GridOptions, Services::ServicesHelper
Defined in:
lib/kontena/cli/certificate/authorize_command.rb

Defined Under Namespace

Classes: DeployFailedError

Instance Attribute Summary

Attributes inherited from Kontena::Command

#arguments, #exit_code, #result

Instance Method Summary collapse

Methods included from Services::ServicesHelper

#create_service, #delete_service, #deploy_service, #get_service, #health_status, #health_status_icon, #int_to_filesize, #parse_build_args, #parse_container_name, #parse_deploy_opts, #parse_health_check, #parse_image, #parse_links, #parse_log_opts, #parse_memory, #parse_ports, #parse_relative_time, #parse_secrets, #parse_service_id, #render_service_deploy_instances, #restart_service, #scale_service, #show_service, #show_service_containers, #show_service_instances, #start_service, #stop_service, #update_service, #wait_for_deploy_to_finish

Methods included from Kontena::Cli::Common

#access_token=, #add_master, #any_key_to_continue, #any_key_to_continue_with_timeout, #api_url, #api_url=, #caret, #clear_current_grid, #client, #cloud_auth?, #cloud_client, #config, #confirm, #confirm_command, #current_grid, #current_master_index, #debug?, #display_account_login_info, #display_login_info, display_logo, #display_master_login_info, #error, exit_with_error, #kontena_account, #logger, #pastel, #print, #prompt, #puts, #require_api_url, #require_token, #reset_client, #reset_cloud_client, #running_quiet?, #running_silent?, #running_verbose?, #spin_if, #spinner, #sprint, #sputs, #stdin_input, #use_refresh_token, #vfakespinner, #vputs, #vspinner, #warning

Methods included from GridOptions

included

Methods inherited from Kontena::Command

banner, callback_matcher, #help_requested?, inherited, #instance, load_subcommand, requires_current_account_token, requires_current_account_token?, requires_current_grid, requires_current_grid?, requires_current_master, requires_current_master?, requires_current_master_token, requires_current_master_token?, #run, #run_callbacks, #verify_current_account_token, #verify_current_grid, #verify_current_master, #verify_current_master_token

Instance Method Details

#executeObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/kontena/cli/certificate/authorize_command.rb', line 33

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
  when 'tls-sni-01'
    domain_auth = spinner "Waiting for tls-sni-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 "TLS-SNI challenge certificate is deployed, you can now request the actual certificate"
    end
  else
    exit_with_error "Unknown authorization type: #{self.type}"
  end
end

#requires_linked_service?Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kontena/cli/certificate/authorize_command.rb', line 20

def requires_linked_service?
  case type
  when 'dns-01'
    false
  when 'http-01'
    true
  when 'tls-sni-01'
    true
  else
    fail "Invalid authorization --type=#{type}"
  end
end

#retry_on_le_registrationObject



96
97
98
99
100
101
102
103
104
105
# File 'lib/kontena/cli/certificate/authorize_command.rb', line 96

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) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/kontena/cli/certificate/authorize_command.rb', line 88

def service_path(linked_service)
  unless linked_service.include?('/')
    "null/#{linked_service}"
  else
    linked_service
  end
end

#wait_for_domain_auth_deployed(domain_auth) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/kontena/cli/certificate/authorize_command.rb', line 77

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