Class: Kontena::Cli::Cloud::Master::AddCommand

Inherits:
Kontena::Command
  • Object
show all
Includes:
Kontena::Cli::Common
Defined in:
lib/kontena/cli/cloud/master/add_command.rb

Instance Attribute Summary

Attributes inherited from Kontena::Command

#arguments, #exit_code, #result

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=, #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 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

#cloud_mastersObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/kontena/cli/cloud/master/add_command.rb', line 46

def cloud_masters
  masters = []
  spinner "Retrieving a list of your registered Kontena Masters in Kontena Cloud" do |spin|
    begin
      masters = Kontena.run!(%w(cloud master list --return --quiet))
    rescue SystemExit
      spin.fail
    end
  end
  masters
end

#executeObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/kontena/cli/cloud/master/add_command.rb', line 132

def execute
  unless cloud_client.authentication_ok?(.userinfo_endpoint)
    Kontena.run!(%w(cloud login))
    config.reset_instance
    reset_cloud_client
  end

  return register_current if self.current?

  exit_with_error 'Master name is required' unless self.name

  response = register(self.name, self.url, self.provider, self.redirect_uri, self.version, self.owner)
  if self.return?
    return response['data']['id']
  elsif self.id?
    puts response['data']['id']
  else
    puts pastel.green("Registered master.")
    puts "ID: #{response['data']['id']}"
    puts "Client ID: #{response['data']['attributes']['client-id']}"
    puts "Client Secret: #{response['data']['attributes']['client-secret']}"
  end
end

#get_existing(id) ⇒ Object



42
43
44
# File 'lib/kontena/cli/cloud/master/add_command.rb', line 42

def get_existing(id)
  cloud_client.get("user/masters/#{id}")
end

#new_cloud_master_name(master_name) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/kontena/cli/cloud/master/add_command.rb', line 58

def new_cloud_master_name(master_name)
  masters = cloud_masters
  return master_name if masters.empty?

  existing_master = masters.find { |m| m['attributes']['name'] == master_name }
  return master_name unless existing_master

  new_name = "#{master_name}-2"
  new_name.succ! until masters.find { |m| m['attributes']['name'] == new_name }.nil?
  new_name
end

#register(name, url = nil, provider = nil, redirect_uri = nil, version = nil, owner = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/kontena/cli/cloud/master/add_command.rb', line 26

def register(name, url = nil, provider = nil, redirect_uri = nil, version = nil, owner = nil)
  attributes = {}
  attributes['name']         = name
  attributes['url']          = url if url
  attributes['provider']     = provider if provider
  attributes['redirect-uri'] = redirect_uri if redirect_uri
  attributes['version']      = version if version
  attributes['owner']        = owner if owner

  response = cloud_client.post('user/masters', { data: { attributes: attributes } })
  exit_with_error "Failed (invalid response)" unless response.kind_of?(Hash)
  exit_with_error "Failed: #{response['error']}" if response['error']
  exit_with_error "Failed (no data)" unless response['data']
  response
end

#register_currentObject



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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/kontena/cli/cloud/master/add_command.rb', line 70

def register_current
  require_api_url
  require_token

  unless self.force?
    puts "Proceeding will:"
    puts " * Register the Kontena Master #{current_master.name} to Kontena Cloud"
    puts " * Configure the Kontena Master to use Kontena Cloud as the"
    puts "   authentication provider"
    puts
    puts "After this:"
    puts " * Users will not be able to reauthenticate without authorizing the"
    puts "   Master to access their Kontena Cloud user information"
    puts " * Users that have registered a different email address to Kontena"
    puts "   Cloud than the one they currently have as their username in the"
    puts "   master will not be able to authenticate before an administrator"
    puts "   of the Kontena Master creates an invitation code for them"
    puts "   (kontena master user invite [email protected])"
    exit_with_error "Aborted" unless prompt.yes?("Proceed?")
  end

  new_name = new_cloud_master_name(current_master.name)

  if self.cloud_master_id
    response = spinner "Retrieving Master information from Kontena Cloud using id" do
      get_existing(self.cloud_master_id)
    end
    if response && response.kind_of?(Hash) && response.has_key?('data') && response['data']['attributes']
      if (self.provider && response['data']['attributes']['provider'] != self.provider) || (self.version && response['data']['attributes']['version'] != self.version)
        spinner "Updating provider and version attributes to Kontena Cloud master" do |spin|
          args = []
          args += ['--provider', self.provider] if self.provider
          args += ['--version', self.version] if self.version
          args << self.cloud_master_id
          spin.fail! unless Kontena.run(['cloud', 'master', 'update'] + args)
        end
      end
    end
  else
    response = spinner "Registering current Kontena Master '#{current_master.name}' #{" as '#{new_name}' " unless new_name == current_master.name}to Kontena Cloud" do
      register(new_name, current_master.url, self.provider, current_master.url.gsub(/\/$/, '') + "/cb", self.version)
    end
  end

  spinner "Loading Kontena Cloud auth provider base configuration to Kontena Master" do |spin|
    spin.fail! unless Kontena.run(%w(master config import --force --preset kontena_auth_provider))
  end

  spinner "Updating OAuth2 client-id and client-secret to Kontena Master" do |spin|
    spin.fail! unless Kontena.run(
      [
        'master', 'config', 'set',
        "oauth2.client_id=#{response['data']['attributes']['client-id'].shellescape}",
        "oauth2.client_secret=#{response['data']['attributes']['client-secret'].shellescape}",
        "server.root_url=#{current_master.url.shellescape}",
        "server.name=#{current_master.name.shellescape}",
        "cloud.provider_is_kontena=true"
      ]
    )
  end
end