kontena-cli 0.15.5 → 0.16.0.pre1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Dockerfile +0 -3
- data/Gemfile +3 -0
- data/LOGO +8 -0
- data/VERSION +1 -1
- data/kontena-cli.gemspec +2 -3
- data/lib/kontena/callback.rb +57 -0
- data/lib/kontena/callbacks/.gitkeep +0 -0
- data/lib/kontena/callbacks/auth/01_list_and_select_grid_after_master_auth.rb +27 -0
- data/lib/kontena/callbacks/master/01_clear_current_master_after_terminate.rb +20 -0
- data/lib/kontena/callbacks/master/deploy/01_show_logo_before_deploy.rb +15 -0
- data/lib/kontena/callbacks/master/deploy/05_before_deploy_configuration_wizard.rb +124 -0
- data/lib/kontena/callbacks/master/deploy/50_authenticate_after_deploy.rb +53 -0
- data/lib/kontena/callbacks/master/deploy/55_create_initial_grid_after_deploy.rb +32 -0
- data/lib/kontena/callbacks/master/deploy/60_configure_auth_provider_after_deploy.rb +49 -0
- data/lib/kontena/callbacks/master/deploy/90_suggest_inviting_yourself_after_deploy.rb +24 -0
- data/lib/kontena/cli/app_command.rb +2 -1
- data/lib/kontena/cli/apps/build_command.rb +1 -1
- data/lib/kontena/cli/apps/common.rb +6 -1
- data/lib/kontena/cli/apps/config_command.rb +1 -1
- data/lib/kontena/cli/apps/deploy_command.rb +1 -1
- data/lib/kontena/cli/apps/init_command.rb +3 -5
- data/lib/kontena/cli/apps/list_command.rb +1 -1
- data/lib/kontena/cli/apps/logs_command.rb +1 -1
- data/lib/kontena/cli/apps/monitor_command.rb +1 -1
- data/lib/kontena/cli/apps/remove_command.rb +2 -3
- data/lib/kontena/cli/apps/restart_command.rb +1 -1
- data/lib/kontena/cli/apps/scale_command.rb +1 -1
- data/lib/kontena/cli/apps/show_command.rb +1 -1
- data/lib/kontena/cli/apps/start_command.rb +1 -1
- data/lib/kontena/cli/apps/stop_command.rb +1 -1
- data/lib/kontena/cli/apps/yaml/reader.rb +3 -13
- data/lib/kontena/cli/apps/yaml/validator.rb +0 -4
- data/lib/kontena/cli/apps/yaml/validator_v2.rb +1 -5
- data/lib/kontena/cli/certificate/authorize_command.rb +1 -1
- data/lib/kontena/cli/certificate/get_command.rb +1 -1
- data/lib/kontena/cli/certificate/register_command.rb +1 -1
- data/lib/kontena/cli/certificate_command.rb +1 -1
- data/lib/kontena/cli/cloud/login_command.rb +128 -0
- data/lib/kontena/cli/cloud/master/add_command.rb +54 -0
- data/lib/kontena/cli/cloud/master/delete_command.rb +20 -0
- data/lib/kontena/cli/cloud/master/list_command.rb +29 -0
- data/lib/kontena/cli/cloud/master/show_command.rb +23 -0
- data/lib/kontena/cli/cloud/master/update_command.rb +58 -0
- data/lib/kontena/cli/cloud/master_command.rb +21 -0
- data/lib/kontena/cli/cloud_command.rb +10 -0
- data/lib/kontena/cli/common.rb +230 -88
- data/lib/kontena/cli/config.rb +537 -0
- data/lib/kontena/cli/container_command.rb +1 -1
- data/lib/kontena/cli/containers/exec_command.rb +1 -1
- data/lib/kontena/cli/containers/inspect_command.rb +1 -1
- data/lib/kontena/cli/etcd/get_command.rb +1 -1
- data/lib/kontena/cli/etcd/list_command.rb +1 -1
- data/lib/kontena/cli/etcd/mkdir_command.rb +1 -1
- data/lib/kontena/cli/etcd/remove_command.rb +1 -1
- data/lib/kontena/cli/etcd/set_command.rb +1 -1
- data/lib/kontena/cli/etcd_command.rb +1 -1
- data/lib/kontena/cli/external_registries/add_command.rb +1 -1
- data/lib/kontena/cli/external_registries/delete_command.rb +1 -1
- data/lib/kontena/cli/external_registries/list_command.rb +1 -1
- data/lib/kontena/cli/external_registries/remove_command.rb +1 -1
- data/lib/kontena/cli/external_registry_command.rb +1 -1
- data/lib/kontena/cli/grid_command.rb +1 -1
- data/lib/kontena/cli/grids/audit_log_command.rb +6 -5
- data/lib/kontena/cli/grids/cloud_config_command.rb +1 -1
- data/lib/kontena/cli/grids/common.rb +1 -1
- data/lib/kontena/cli/grids/create_command.rb +8 -4
- data/lib/kontena/cli/grids/current_command.rb +1 -1
- data/lib/kontena/cli/grids/env_command.rb +1 -1
- data/lib/kontena/cli/grids/list_command.rb +35 -10
- data/lib/kontena/cli/grids/logs_command.rb +1 -1
- data/lib/kontena/cli/grids/remove_command.rb +2 -2
- data/lib/kontena/cli/grids/show_command.rb +1 -1
- data/lib/kontena/cli/grids/trusted_subnet_command.rb +1 -1
- data/lib/kontena/cli/grids/trusted_subnets/add_command.rb +1 -1
- data/lib/kontena/cli/grids/trusted_subnets/list_command.rb +1 -1
- data/lib/kontena/cli/grids/trusted_subnets/remove_command.rb +1 -1
- data/lib/kontena/cli/grids/update_command.rb +1 -1
- data/lib/kontena/cli/grids/use_command.rb +11 -6
- data/lib/kontena/cli/grids/user_command.rb +1 -1
- data/lib/kontena/cli/grids/users/add_command.rb +1 -1
- data/lib/kontena/cli/grids/users/list_command.rb +1 -1
- data/lib/kontena/cli/grids/users/remove_command.rb +1 -1
- data/lib/kontena/cli/localhost_web_server.rb +93 -0
- data/lib/kontena/cli/login_command.rb +5 -118
- data/lib/kontena/cli/logout_command.rb +33 -2
- data/lib/kontena/cli/master/audit_log_command.rb +19 -0
- data/lib/kontena/cli/master/config/export_command.rb +47 -0
- data/lib/kontena/cli/master/config/get_command.rb +24 -0
- data/lib/kontena/cli/master/config/import_command.rb +69 -0
- data/lib/kontena/cli/master/config/set_command.rb +19 -0
- data/lib/kontena/cli/master/config/unset_command.rb +20 -0
- data/lib/kontena/cli/master/config_command.rb +24 -0
- data/lib/kontena/cli/master/create_command.rb +76 -0
- data/lib/kontena/cli/master/current_command.rb +10 -2
- data/lib/kontena/cli/master/join_command.rb +20 -0
- data/lib/kontena/cli/master/list_command.rb +4 -4
- data/lib/kontena/cli/master/login_command.rb +274 -0
- data/lib/kontena/cli/master/use_command.rb +8 -19
- data/lib/kontena/cli/master/users/invite_command.rb +33 -6
- data/lib/kontena/cli/master/users/list_command.rb +2 -2
- data/lib/kontena/cli/master/users/remove_command.rb +1 -1
- data/lib/kontena/cli/master/users/role_command.rb +1 -1
- data/lib/kontena/cli/master/users/roles/add_command.rb +18 -16
- data/lib/kontena/cli/master/users/roles/remove_command.rb +1 -1
- data/lib/kontena/cli/master/users_command.rb +1 -1
- data/lib/kontena/cli/master_command.rb +21 -1
- data/lib/kontena/cli/node_command.rb +1 -1
- data/lib/kontena/cli/nodes/label_command.rb +1 -1
- data/lib/kontena/cli/nodes/labels/add_command.rb +1 -1
- data/lib/kontena/cli/nodes/labels/remove_command.rb +1 -1
- data/lib/kontena/cli/nodes/list_command.rb +1 -1
- data/lib/kontena/cli/nodes/remove_command.rb +1 -1
- data/lib/kontena/cli/nodes/show_command.rb +1 -1
- data/lib/kontena/cli/nodes/ssh_command.rb +1 -1
- data/lib/kontena/cli/nodes/update_command.rb +1 -1
- data/lib/kontena/cli/plugin_command.rb +1 -1
- data/lib/kontena/cli/plugins/install_command.rb +2 -2
- data/lib/kontena/cli/plugins/list_command.rb +2 -2
- data/lib/kontena/cli/plugins/search_command.rb +1 -1
- data/lib/kontena/cli/plugins/uninstall_command.rb +2 -2
- data/lib/kontena/cli/registry/create_command.rb +2 -4
- data/lib/kontena/cli/registry/delete_command.rb +1 -1
- data/lib/kontena/cli/registry/remove_command.rb +1 -1
- data/lib/kontena/cli/registry_command.rb +1 -1
- data/lib/kontena/cli/service_command.rb +1 -1
- data/lib/kontena/cli/services/container_command.rb +1 -1
- data/lib/kontena/cli/services/containers_command.rb +1 -1
- data/lib/kontena/cli/services/create_command.rb +1 -1
- data/lib/kontena/cli/services/delete_command.rb +1 -1
- data/lib/kontena/cli/services/deploy_command.rb +1 -1
- data/lib/kontena/cli/services/env_command.rb +1 -1
- data/lib/kontena/cli/services/envs/add_command.rb +1 -1
- data/lib/kontena/cli/services/envs/list_command.rb +1 -1
- data/lib/kontena/cli/services/envs/remove_command.rb +1 -1
- data/lib/kontena/cli/services/link_command.rb +1 -1
- data/lib/kontena/cli/services/list_command.rb +1 -1
- data/lib/kontena/cli/services/logs_command.rb +1 -1
- data/lib/kontena/cli/services/monitor_command.rb +1 -1
- data/lib/kontena/cli/services/remove_command.rb +1 -1
- data/lib/kontena/cli/services/restart_command.rb +1 -1
- data/lib/kontena/cli/services/scale_command.rb +1 -1
- data/lib/kontena/cli/services/secret_command.rb +1 -1
- data/lib/kontena/cli/services/secrets/link_command.rb +1 -1
- data/lib/kontena/cli/services/secrets/unlink_command.rb +1 -1
- data/lib/kontena/cli/services/services_helper.rb +6 -3
- data/lib/kontena/cli/services/show_command.rb +1 -1
- data/lib/kontena/cli/services/start_command.rb +1 -1
- data/lib/kontena/cli/services/stats_command.rb +1 -1
- data/lib/kontena/cli/services/stop_command.rb +1 -1
- data/lib/kontena/cli/services/unlink_command.rb +1 -1
- data/lib/kontena/cli/services/update_command.rb +1 -1
- data/lib/kontena/cli/spinner.rb +122 -0
- data/lib/kontena/cli/stack_command.rb +1 -1
- data/lib/kontena/cli/stacks/create_command.rb +1 -1
- data/lib/kontena/cli/stacks/deploy_command.rb +1 -1
- data/lib/kontena/cli/stacks/list_command.rb +1 -1
- data/lib/kontena/cli/stacks/remove_command.rb +1 -1
- data/lib/kontena/cli/stacks/show_command.rb +1 -1
- data/lib/kontena/cli/stacks/update_command.rb +1 -1
- data/lib/kontena/cli/vault/list_command.rb +1 -1
- data/lib/kontena/cli/vault/read_command.rb +1 -1
- data/lib/kontena/cli/vault/remove_command.rb +1 -1
- data/lib/kontena/cli/vault/update_command.rb +1 -1
- data/lib/kontena/cli/vault/write_command.rb +1 -1
- data/lib/kontena/cli/vault_command.rb +1 -1
- data/lib/kontena/cli/version.rb +1 -1
- data/lib/kontena/cli/version_command.rb +1 -1
- data/lib/kontena/cli/vpn/config_command.rb +1 -1
- data/lib/kontena/cli/vpn/create_command.rb +2 -4
- data/lib/kontena/cli/vpn/delete_command.rb +1 -1
- data/lib/kontena/cli/vpn/remove_command.rb +1 -1
- data/lib/kontena/cli/vpn_command.rb +1 -1
- data/lib/kontena/cli/whoami_command.rb +16 -13
- data/lib/kontena/client.rb +410 -90
- data/lib/kontena/command.rb +172 -0
- data/lib/kontena/main_command.rb +7 -8
- data/lib/kontena/presets/github_auth_provider.yml +11 -0
- data/lib/kontena/presets/kontena_auth_provider.yml +11 -0
- data/lib/kontena_cli.rb +51 -1
- data/spec/kontena/cli/app/deploy_command_spec.rb +14 -44
- data/spec/kontena/cli/app/scale_spec.rb +1 -1
- data/spec/kontena/cli/app/yaml/reader_spec.rb +0 -48
- data/spec/kontena/cli/common_spec.rb +63 -59
- data/spec/kontena/cli/grids/use_command_spec.rb +43 -0
- data/spec/kontena/cli/master/current_command_spec.rb +3 -24
- data/spec/kontena/cli/master/use_command_spec.rb +2 -27
- data/spec/kontena/cli/master/users/invite_command_spec.rb +4 -18
- data/spec/kontena/cli/master/users/roles/add_command_spec.rb +2 -16
- data/spec/kontena/cli/master/users/roles/remove_command_spec.rb +2 -13
- data/spec/kontena/cli/services/restart_command_spec.rb +1 -1
- data/spec/kontena/cli/services/update_command_spec.rb +5 -5
- data/spec/kontena/client_spec.rb +104 -35
- data/spec/kontena/config_spec.rb +65 -0
- data/spec/spec_helper.rb +25 -3
- data/spec/support/client_helpers.rb +10 -3
- data/spec/support/requirements_helper.rb +32 -0
- metadata +61 -48
- data/lib/kontena/cli/register_command.rb +0 -23
- data/lib/kontena/cli/user/forgot_password_command.rb +0 -16
- data/lib/kontena/cli/user/reset_password_command.rb +0 -23
- data/lib/kontena/cli/user/verify_command.rb +0 -20
- data/lib/kontena/cli/user_command.rb +0 -13
- data/spec/fixtures/kontena-malformed-yaml.yml +0 -6
- data/spec/fixtures/kontena-not-hash-service-config.yml +0 -3
- data/spec/kontena/cli/login_command_spec.rb +0 -32
- data/spec/kontena/cli/register_command_spec.rb +0 -57
@@ -0,0 +1,24 @@
|
|
1
|
+
module Kontena
|
2
|
+
module Callbacks
|
3
|
+
class SuggestInvitingYourself < Kontena::Callback
|
4
|
+
|
5
|
+
include Kontena::Cli::Common
|
6
|
+
|
7
|
+
matches_commands 'master create'
|
8
|
+
|
9
|
+
def after
|
10
|
+
return unless current_master
|
11
|
+
return unless command.exit_code == 0
|
12
|
+
return unless current_master.username.to_s == 'admin'
|
13
|
+
|
14
|
+
puts
|
15
|
+
puts "Protip:"
|
16
|
+
puts " You are currently using Kontena Master administrator account."
|
17
|
+
puts " Consider inviting yourself as a regular user. Use: "
|
18
|
+
puts " kontena master users invite -r master_admin your@email.address.example.com"
|
19
|
+
puts " kontena master join <master_url> <invite_code>"
|
20
|
+
puts
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -12,7 +12,7 @@ require_relative 'apps/monitor_command'
|
|
12
12
|
require_relative 'apps/show_command'
|
13
13
|
require_relative 'apps/scale_command'
|
14
14
|
|
15
|
-
class Kontena::Cli::AppCommand <
|
15
|
+
class Kontena::Cli::AppCommand < Kontena::Command
|
16
16
|
|
17
17
|
subcommand "init", "Init Kontena application", Kontena::Cli::Apps::InitCommand
|
18
18
|
subcommand "build", "Build Kontena services", Kontena::Cli::Apps::BuildCommand
|
@@ -27,6 +27,7 @@ class Kontena::Cli::AppCommand < Clamp::Command
|
|
27
27
|
subcommand ["logs"], "Show service logs", Kontena::Cli::Apps::LogsCommand
|
28
28
|
subcommand "monitor", "Monitor services", Kontena::Cli::Apps::MonitorCommand
|
29
29
|
subcommand ["remove","rm"], "Remove services", Kontena::Cli::Apps::RemoveCommand
|
30
|
+
|
30
31
|
def execute
|
31
32
|
end
|
32
33
|
end
|
@@ -62,7 +62,12 @@ module Kontena::Cli::Apps
|
|
62
62
|
|
63
63
|
def project_name_from_yaml(file)
|
64
64
|
reader = YAML::Reader.new(file, true)
|
65
|
-
reader.
|
65
|
+
outcome = reader.execute
|
66
|
+
if outcome[:version].to_i == 2
|
67
|
+
outcome[:name]
|
68
|
+
else
|
69
|
+
nil
|
70
|
+
end
|
66
71
|
end
|
67
72
|
|
68
73
|
# @return [String]
|
@@ -6,7 +6,7 @@ require_relative 'kontena_yml_generator'
|
|
6
6
|
|
7
7
|
|
8
8
|
module Kontena::Cli::Apps
|
9
|
-
class InitCommand <
|
9
|
+
class InitCommand < Kontena::Command
|
10
10
|
include Kontena::Cli::Common
|
11
11
|
include Common
|
12
12
|
|
@@ -17,8 +17,6 @@ module Kontena::Cli::Apps
|
|
17
17
|
|
18
18
|
|
19
19
|
def execute
|
20
|
-
require 'highline/import'
|
21
|
-
|
22
20
|
if File.exist?('Dockerfile')
|
23
21
|
puts 'Found Dockerfile'
|
24
22
|
elsif create_dockerfile?
|
@@ -67,7 +65,7 @@ module Kontena::Cli::Apps
|
|
67
65
|
end
|
68
66
|
|
69
67
|
def create_dockerfile?
|
70
|
-
|
68
|
+
prompt.yes?('Dockerfile not found. Do you want to create it?')
|
71
69
|
end
|
72
70
|
|
73
71
|
def create_env_file(env)
|
@@ -85,7 +83,7 @@ module Kontena::Cli::Apps
|
|
85
83
|
end
|
86
84
|
|
87
85
|
def create_docker_compose_yml?
|
88
|
-
|
86
|
+
prompt.yes?("#{docker_compose_file} not found. Do you want to create it?")
|
89
87
|
end
|
90
88
|
|
91
89
|
end
|
@@ -2,7 +2,7 @@ require_relative 'common'
|
|
2
2
|
require_relative '../helpers/log_helper'
|
3
3
|
|
4
4
|
module Kontena::Cli::Apps
|
5
|
-
class LogsCommand <
|
5
|
+
class LogsCommand < Kontena::Command
|
6
6
|
include Kontena::Cli::Common
|
7
7
|
include Kontena::Cli::GridOptions
|
8
8
|
include Kontena::Cli::Helpers::LogHelper
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require_relative 'common'
|
2
2
|
|
3
3
|
module Kontena::Cli::Apps
|
4
|
-
class RemoveCommand <
|
4
|
+
class RemoveCommand < Kontena::Command
|
5
5
|
include Kontena::Cli::Common
|
6
6
|
include Kontena::Cli::GridOptions
|
7
7
|
include Common
|
@@ -41,8 +41,7 @@ module Kontena::Cli::Apps
|
|
41
41
|
|
42
42
|
def delete(name, options, async = true)
|
43
43
|
unless deleted_services.include?(name)
|
44
|
-
|
45
|
-
ShellSpinner " " do
|
44
|
+
spinner "deleting #{name.colorize(:cyan)}" do
|
46
45
|
service = get_service(token, prefixed_name(name)) rescue nil
|
47
46
|
if(service)
|
48
47
|
delete_service(token, prefixed_name(name))
|
@@ -27,17 +27,13 @@ module Kontena::Cli::Apps
|
|
27
27
|
Dir.chdir(File.dirname(File.expand_path(file))) do
|
28
28
|
result[:version] = yaml['version'] || '1'
|
29
29
|
result[:name] = yaml['name']
|
30
|
+
result[:services] = parse_services(service_name)
|
30
31
|
result[:errors] = errors
|
31
32
|
result[:notifications] = notifications
|
32
|
-
result[:services] = parse_services(service_name) unless errors.count > 0
|
33
33
|
end
|
34
34
|
result
|
35
35
|
end
|
36
36
|
|
37
|
-
def stack_name
|
38
|
-
yaml['name'] if v2?
|
39
|
-
end
|
40
|
-
|
41
37
|
##
|
42
38
|
# @return [true|false]
|
43
39
|
def v2?
|
@@ -51,11 +47,7 @@ module Kontena::Cli::Apps
|
|
51
47
|
content = content % { project: ENV['project'], grid: ENV['grid'] }
|
52
48
|
interpolate(content)
|
53
49
|
replace_dollar_dollars(content)
|
54
|
-
|
55
|
-
@yaml = ::YAML.load(content)
|
56
|
-
rescue Psych::SyntaxError => e
|
57
|
-
abort("Error while parsing #{file}".colorize(:red)+ " "+e.message)
|
58
|
-
end
|
50
|
+
@yaml = ::YAML.load(content)
|
59
51
|
end
|
60
52
|
|
61
53
|
# @return [Array] array of validation errors
|
@@ -88,9 +80,7 @@ module Kontena::Cli::Apps
|
|
88
80
|
# @return [Hash]
|
89
81
|
def parse_services(service_name = nil)
|
90
82
|
if service_name.nil?
|
91
|
-
services.each
|
92
|
-
services[name] = process_config(config)
|
93
|
-
end
|
83
|
+
services.each { |name, config| services[name] = process_config(config) }
|
94
84
|
services
|
95
85
|
else
|
96
86
|
abort("Service '#{service_name}' not found in #{file}".colorize(:red)) unless services.key?(service_name)
|
@@ -43,10 +43,6 @@ module Kontena::Cli::Apps
|
|
43
43
|
}
|
44
44
|
|
45
45
|
yaml.each do |service, options|
|
46
|
-
unless options.is_a?(Hash)
|
47
|
-
result[:errors] << { service => { 'options' => 'must be a mapping not a string'} }
|
48
|
-
next
|
49
|
-
end
|
50
46
|
key_errors = validate_keys(options)
|
51
47
|
option_errors = validate_options(options)
|
52
48
|
result[:errors] << { service => option_errors.messages } if option_errors.failure?
|
@@ -53,11 +53,7 @@ module Kontena::Cli::Apps
|
|
53
53
|
notifications: []
|
54
54
|
}
|
55
55
|
if yaml.key?('services')
|
56
|
-
yaml['services'].each do |service, options|
|
57
|
-
unless options.is_a?(Hash)
|
58
|
-
result[:errors] << { service => { 'options' => 'must be a mapping not a string'} }
|
59
|
-
next
|
60
|
-
end
|
56
|
+
yaml['services'].each do |service, options|
|
61
57
|
key_errors = validate_keys(options)
|
62
58
|
option_errors = validate_options(options)
|
63
59
|
result[:errors] << { service => option_errors.messages } if option_errors.failure?
|
@@ -2,7 +2,7 @@ require_relative 'certificate/register_command'
|
|
2
2
|
require_relative 'certificate/authorize_command'
|
3
3
|
require_relative 'certificate/get_command'
|
4
4
|
|
5
|
-
class Kontena::Cli::CertificateCommand <
|
5
|
+
class Kontena::Cli::CertificateCommand < Kontena::Command
|
6
6
|
|
7
7
|
|
8
8
|
subcommand "register", "Register to LetsEncrypt", Kontena::Cli::Certificate::RegisterCommand
|
@@ -0,0 +1,128 @@
|
|
1
|
+
require 'uri'
|
2
|
+
|
3
|
+
module Kontena::Cli::Cloud
|
4
|
+
class LoginCommand < Kontena::Command
|
5
|
+
include Kontena::Cli::Common
|
6
|
+
|
7
|
+
option ['-t', '--token'], '[TOKEN]', 'Use a pre-generated access token'
|
8
|
+
option ['-c', '--code'], '[CODE]', 'Use an authorization code'
|
9
|
+
option ['-v', '--verbose'], :flag, 'Increase output verbosity'
|
10
|
+
|
11
|
+
def execute
|
12
|
+
require_relative '../localhost_web_server'
|
13
|
+
require 'launchy'
|
14
|
+
|
15
|
+
if self.code
|
16
|
+
kontena_account.token ||= Kontena::Cli::Config::Token.new(access_token: self.token, parent_type: :account, parent_name: kontena_account.name)
|
17
|
+
client = Kontena::Client.new(kontena_account.token_endpoint, kontena_account.token)
|
18
|
+
response = nil
|
19
|
+
begin
|
20
|
+
vspinner "Exchanging authorization code to access token" do
|
21
|
+
response = client.exchange_code(self.code)
|
22
|
+
raise Kontena::Errors::StandardError.new(400, 'Code exchange failed') unless response
|
23
|
+
end
|
24
|
+
end
|
25
|
+
if response && response.kind_of?(Hash) && response['access_token']
|
26
|
+
kontena_account.token.access_token = response['access_token']
|
27
|
+
kontena_account.token.refresh_token = response['refresh_token']
|
28
|
+
kontena_account.token.expires_at = response['expires_in'].to_i > 0 ? Time.now.utc.to_i + response['expires_in'].to_i : nil
|
29
|
+
logger.debug "Code exchanged succesfully"
|
30
|
+
else
|
31
|
+
puts "Code exchange failed".colorize(:red)
|
32
|
+
exit 1
|
33
|
+
end
|
34
|
+
elsif self.token.nil?
|
35
|
+
token = kontena_account.token ||= Kontena::Cli::Config::Token.new(parent_type: :account, parent_name: ENV['KONTENA_ACCOUNT'] || 'kontena')
|
36
|
+
elsif self.token
|
37
|
+
kontena_account.token = Kontena::Cli::Config::Token.new(access_token: self.token, parent_type: :account, parent_name: ENV['KONTENA_ACCOUNT'] || 'kontena')
|
38
|
+
end
|
39
|
+
|
40
|
+
client = Kontena::Client.new(kontena_account.userinfo_endpoint, kontena_account.token, prefix: '')
|
41
|
+
|
42
|
+
if kontena_account.token.access_token
|
43
|
+
auth_ok = false
|
44
|
+
vspinner "Verifying current access token" do
|
45
|
+
auth_ok = client.authentication_ok?(kontena_account.userinfo_endpoint)
|
46
|
+
end
|
47
|
+
|
48
|
+
if auth_ok
|
49
|
+
config.write
|
50
|
+
display_logo
|
51
|
+
display_login_info(only: :account)
|
52
|
+
exit 0
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
uri = URI.parse(kontena_account.authorization_endpoint)
|
57
|
+
uri.host ||= kontena_account.url
|
58
|
+
|
59
|
+
web_server = Kontena::LocalhostWebServer.new
|
60
|
+
|
61
|
+
params = {
|
62
|
+
client_id: kontena_account.client_id || Kontena::Client::CLIENT_ID,
|
63
|
+
response_type: 'code',
|
64
|
+
redirect_uri: "http://localhost:#{web_server.port}/cb"
|
65
|
+
}
|
66
|
+
|
67
|
+
uri.query = URI.encode_www_form(params)
|
68
|
+
|
69
|
+
puts "Opening a browser to #{uri.scheme}://#{uri.host}"
|
70
|
+
puts
|
71
|
+
puts "If you are running this command over an ssh connection or it's"
|
72
|
+
puts "otherwise not possible to open a browser from this terminal"
|
73
|
+
puts "then you must use a pregenerated access token using the --token"
|
74
|
+
puts "option : kontena cloud login --token <access_token>"
|
75
|
+
puts
|
76
|
+
puts "Once the authentication is complete you can close the browser"
|
77
|
+
puts "window or tab and return to this window to continue."
|
78
|
+
puts
|
79
|
+
any_key_to_continue
|
80
|
+
|
81
|
+
puts "If the browser does not open, try visiting this URL manually:"
|
82
|
+
puts "<#{uri.to_s}>"
|
83
|
+
puts
|
84
|
+
|
85
|
+
server_thread = Thread.new { Thread.main['response'] = web_server.serve_one }
|
86
|
+
browser_thread = Thread.new { Launchy.open(uri.to_s) }
|
87
|
+
|
88
|
+
server_thread.join
|
89
|
+
browser_thread.join
|
90
|
+
|
91
|
+
response = Thread.main['response']
|
92
|
+
|
93
|
+
# If the master responds with a code, then exchange it to a token
|
94
|
+
if response && response.kind_of?(Hash) && response['code']
|
95
|
+
logger.debug 'Account responded with code, exchanging to token'
|
96
|
+
response = client.exchange_code(response['code'])
|
97
|
+
end
|
98
|
+
|
99
|
+
if response && response.kind_of?(Hash) && response['access_token']
|
100
|
+
kontena_account.token = Kontena::Cli::Config::Token.new
|
101
|
+
kontena_account.token.access_token = response['access_token']
|
102
|
+
kontena_account.token.refresh_token = response['refresh_token']
|
103
|
+
kontena_account.token.expires_at = response['expires_in'].to_i > 0 ? Time.now.utc.to_i + response['expires_in'].to_i : nil
|
104
|
+
else
|
105
|
+
puts "Authentication failed".colorize(:red)
|
106
|
+
exit 1
|
107
|
+
end
|
108
|
+
|
109
|
+
uri = URI.parse(kontena_account.userinfo_endpoint)
|
110
|
+
path = uri.path
|
111
|
+
uri.path = '/'
|
112
|
+
|
113
|
+
client = Kontena::Client.new(uri.to_s, kontena_account.token)
|
114
|
+
|
115
|
+
response = client.get(path) rescue nil
|
116
|
+
if response && response.kind_of?(Hash)
|
117
|
+
kontena_account.username = response['username']
|
118
|
+
config.write
|
119
|
+
display_logo
|
120
|
+
display_login_info(only: :account)
|
121
|
+
exit 0
|
122
|
+
else
|
123
|
+
puts "Authentication failed".colorize(:red)
|
124
|
+
exit 1
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|