Class: Kontena::Callbacks::AuthenticateAfterDeploy

Inherits:
Kontena::Callback show all
Includes:
Kontena::Cli::Common
Defined in:
lib/kontena/callbacks/master/deploy/50_authenticate_after_deploy.rb

Instance Attribute Summary

Attributes inherited from Kontena::Callback

#command

Instance Method Summary collapse

Methods included from Kontena::Cli::Common

#access_token=, #add_master, #any_key_to_continue, #any_key_to_continue_with_timeout, #api_url, #api_url=, #api_url_version, #ask, #clear_current_grid, #client, #cloud_auth?, #cloud_client, #config, #confirm, #confirm_command, #current_account, #current_grid, #current_grid=, #current_master, #current_master=, #current_master_index, #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_current_account, #require_current_grid, #require_current_master, #require_token, #reset_client, #reset_cloud_client, #running_silent?, #running_verbose?, #settings, #settings_filename, #spinner, #sprint, #sputs, #use_refresh_token, #vfakespinner, #vputs, #vspinner, #warning, #yes?

Methods inherited from Kontena::Callback

callbacks, #initialize, matches_commands, run_callbacks

Constructor Details

This class inherits a constructor from Kontena::Callback

Instance Method Details

#afterObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
# File 'lib/kontena/callbacks/master/deploy/50_authenticate_after_deploy.rb', line 10

def after
  ENV["DEBUG"] && puts("Command result: #{command.result.inspect}")
  ENV["DEBUG"] && puts("Command exit code: #{command.exit_code.inspect}")
  return unless command.exit_code == 0
  return unless command.result.kind_of?(Hash)
  return unless command.result.has_key?(:public_ip)
  return unless command.result.has_key?(:code)
  return unless command.result.has_key?(:name)

  # If plugins generate self-signed cert, most of the upcoming callbacks will
  # fail without this. This can be made bit more clever once all the plugins
  # return the generated self-signed certificate.
  ENV['SSL_IGNORE_ERRORS'] = 'true'

  # In case there already is a server with the same name add random characters to name
  if config.find_server(command.result[:name])
    command.result[:name] = "#{command.result[:name]}-#{SecureRandom.hex(2)}"
  end

  new_master = Kontena::Cli::Config::Server.new(
    url: "https://#{command.result[:public_ip]}",
    name: command.result[:name]
  )

  retried = false

  # Figure out if HTTPS works, if not, try HTTP
  begin
    ENV["DEBUG"] && puts("Trying to request / from #{new_master.url}")
    client = Kontena::Client.new(new_master.url, nil, ignore_ssl_errors: true)
    client.get('/')
  rescue
    ENV["DEBUG"] && puts("HTTPS test failed: #{$!} #{$!.message}")
    unless retried
      new_master.url = "http://#{command.result[:public_ip]}"
      retried = true
      retry
    end
    return
  end

  require 'shellwords'
  cmd = "master login --no-login-info --skip-grid-auto-select --verbose --name #{command.result[:name].shellescape} --code #{command.result[:code].shellescape} #{new_master.url.shellescape}"
  Retriable.retriable do
    ENV["DEBUG"] && puts("Running: #{cmd}")
    Kontena.run(cmd)
  end
end