Class: Kontena::Callbacks::BeforeDeployConfigurationWizard

Inherits:
Kontena::Callback show all
Defined in:
lib/kontena/callbacks/master/deploy/05_before_deploy_configuration_wizard.rb

Instance Attribute Summary

Attributes inherited from Kontena::Callback

#command

Instance Method Summary collapse

Methods inherited from Kontena::Callback

callbacks, #initialize, matches_commands, run_callbacks

Constructor Details

This class inherits a constructor from Kontena::Callback

Instance Method Details

#after_loadObject



7
8
9
10
11
12
13
14
15
16
# File 'lib/kontena/callbacks/master/deploy/05_before_deploy_configuration_wizard.rb', line 7

def after_load
  extend Kontena::Cli::Common

  command.class_eval do
    option ['--no-prompt'], :flag, "Don't ask questions"
    option ['--skip-auth-provider'], :flag, "Skip auth provider configuration (single user mode)"
    option ['--use-kontena-cloud'], :flag, "Use Kontena Cloud as authentication provider"
    option ['--cloud-master-id'], '[ID]', "Use Kontena Cloud Master ID for auth provider configuration"
  end
end

#beforeObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/kontena/callbacks/master/deploy/05_before_deploy_configuration_wizard.rb', line 52

def before
  extend Kontena::Cli::Common
  unless_param(:name) do
    if command.cloud_master_id
      response = spinner "Receiving Master information from Kontena Cloud" do
        cloud_client.get("user/masters/#{command.cloud_master_id}")
      end
      if response.kind_of?(Hash) && response.has_key?('data')
        command.name = response['data']['attributes']['name']
      else
        exit_with_error "Unable to receive Master information using id"
      end
    elsif command.no_prompt?
      command.name = next_default_name
    else
      command.name = prompt.ask("Enter a name for this Kontena Master: ", default: next_default_name, required: true) do |q|
        q.validate /^[a-z0-9\_\-\.]+$/, 'Name should only include lower case letters, numbers and -._, example: "master-4"'
      end
    end
  end

  unless_param(:skip_auth_provider) do
    unless command.no_prompt? || command.use_kontena_cloud? || command.cloud_master_id
      answer = prompt.select("Select OAuth2 authentication provider: ") do |menu|
        menu.choice 'Kontena Cloud (recommended)', :kontena_new
        menu.choice 'Custom', :custom
        menu.choice 'None (single user mode)', :none
      end
      case answer
      when :kontena_new
         || abort('You must login to Kontena Cloud')
        command.skip_auth_provider = false
      when :custom
        puts
        puts 'Learn how to configure custom user authentication provider after installation at: www.kontena.io/docs/advanced/authentication'
        puts
        command.cloud_master_id = nil
        command.skip_auth_provider = true
      when :none
        puts
        puts "You have selected to use Kontena Master in single user mode. You can configure an authentication provider later. For more information, see here: www.kontena.io/docs/advanced/authentication"
        puts
        command.cloud_master_id = nil
        command.skip_auth_provider = true
      else
        abort 'Should never be here'
      end
    end
  end
  true
end

#login_to_kontenaObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/kontena/callbacks/master/deploy/05_before_deploy_configuration_wizard.rb', line 35

def 
  if cloud_auth?
    return true if cloud_client.authentication_ok?(.userinfo_endpoint)
  end
  puts
  puts "You don't seem to be logged in to Kontena Cloud"
  puts
  Kontena.run!(%w(cloud login --verbose))
  config.reset_instance
  reset_cloud_client
  result = false
  Retriable.retriable do
    result = cloud_client.authentication_ok?(.userinfo_endpoint)
  end
  result
end

#next_default_nameObject

Scans config server names and returns default-2 if default exists, default-3 if default-2 exists, etc.



26
27
28
29
30
31
32
33
# File 'lib/kontena/callbacks/master/deploy/05_before_deploy_configuration_wizard.rb', line 26

def next_default_name
  last_default = config.servers.map(&:name).select{ |n| n =~ /kontena\-master(?:\-\d+)?$/ }.sort.last
  return "kontena-master" unless last_default
  unless last_default =~ /\d$/
    last_default << "-1"
  end
  last_default.succ
end

#unless_param(param, &block) ⇒ Object



18
19
20
21
22
# File 'lib/kontena/callbacks/master/deploy/05_before_deploy_configuration_wizard.rb', line 18

def unless_param(param, &block)
  return if command.respond_to?(param) && !command.send(param).nil?
  return if command.respond_to?("#{param}?".to_sym) && command.send("#{param}?".to_sym)
  yield
end