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,93 @@
|
|
1
|
+
require 'socket'
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
module Kontena
|
5
|
+
class LocalhostWebServer
|
6
|
+
# Serves one request to http://localhost:<random_port>/cb
|
7
|
+
#
|
8
|
+
# Used for local webserver browser authentication flow.
|
9
|
+
#
|
10
|
+
# @example
|
11
|
+
# server = LocalhostWebServer.new
|
12
|
+
# server.url
|
13
|
+
# => "http://localhost:1234/cb"
|
14
|
+
# response = server.serve_one
|
15
|
+
# # <visit server.url?foo=bar&bar=123 on browser>
|
16
|
+
# => { "foo" => "bar", "bar" => 123 } # (it converts integers!)
|
17
|
+
attr_accessor :server, :success_response, :error_response, :port
|
18
|
+
|
19
|
+
DEFAULT_SUCCESS_MESSAGE = "<html><head><title>Success</title></head><body><h3>Success!</h3><p>You can now close this browser window and return to the terminal application.</p></body></html>".freeze
|
20
|
+
DEFAULT_ERROR_MESSAGE = "Bad request"
|
21
|
+
|
22
|
+
# Get new server instance
|
23
|
+
#
|
24
|
+
# @param [String] success_response Returned for successful callback
|
25
|
+
# @param [String] error_response Returned for unsuccessful callback
|
26
|
+
def initialize(success_response: nil, error_response: nil, port: nil)
|
27
|
+
@success_response = success_response || DEFAULT_SUCCESS_MESSAGE
|
28
|
+
@error_response = error_response || DEFAULT_ERROR_MESSAGE
|
29
|
+
@server = TCPServer.new('localhost', port || 0)
|
30
|
+
@port = @server.addr[1]
|
31
|
+
end
|
32
|
+
|
33
|
+
# The url to this service, send this as redirect_uri to auth provider.
|
34
|
+
def url
|
35
|
+
"http://localhost:#{port}/cb"
|
36
|
+
end
|
37
|
+
|
38
|
+
# Serve one request and return query params.
|
39
|
+
#
|
40
|
+
# @return [Hash] query_params
|
41
|
+
def serve_one
|
42
|
+
ENV["DEBUG"] && puts("Waiting for connection on port #{port}..")
|
43
|
+
socket = server.accept
|
44
|
+
|
45
|
+
content = socket.recvfrom(2048).first.split(/(?:\r)?\n/)
|
46
|
+
|
47
|
+
request = content.shift
|
48
|
+
|
49
|
+
headers = {}
|
50
|
+
while line = content.shift
|
51
|
+
break if line.nil?
|
52
|
+
break if line == ''
|
53
|
+
header, value = line.chomp.split(/:\s{0,}/, 2)
|
54
|
+
headers[header] = value
|
55
|
+
end
|
56
|
+
|
57
|
+
body = content.join("\n")
|
58
|
+
|
59
|
+
ENV["DEBUG"] && puts("Got request: \"#{request.inspect}\n Headers: #{headers.inspect}\n Body: #{body}\"")
|
60
|
+
|
61
|
+
get_request = request[/GET (\/cb.+?) HTTP/, 1]
|
62
|
+
if get_request
|
63
|
+
socket.print [
|
64
|
+
'HTTP/1.1 200 OK',
|
65
|
+
'Content-Type: text/html',
|
66
|
+
"Content-Length: #{success_response.bytesize}",
|
67
|
+
"Connection: close",
|
68
|
+
'',
|
69
|
+
success_response
|
70
|
+
].join("\r\n")
|
71
|
+
socket.close
|
72
|
+
server.close
|
73
|
+
uri = URI.parse("http://localhost#{get_request}")
|
74
|
+
ENV["DEBUG"] && puts(" * Parsing params: \"#{uri.query}\"")
|
75
|
+
params = URI.decode_www_form(uri.query).to_h.reject{|_,v| v.to_s == ''}
|
76
|
+
params.map{|k,v| v = (v =~ /\A\d+\z$/ ? v.to_i : v); [k,v]}.to_h
|
77
|
+
else
|
78
|
+
# Unless it's a query to /cb, send an error message and keep listening,
|
79
|
+
# it might have been something funny like fetching favicon.ico
|
80
|
+
socket.print [
|
81
|
+
'HTTP/1.1 400 Bad request',
|
82
|
+
'Content-Type: text/plain',
|
83
|
+
"Content-Length: #{error_response.bytesize}",
|
84
|
+
'Connection: close',
|
85
|
+
'',
|
86
|
+
error_response
|
87
|
+
].join("\r\n")
|
88
|
+
socket.close
|
89
|
+
serve_one # serve more, this one was not proper.
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -1,125 +1,12 @@
|
|
1
|
-
class Kontena::Cli::LoginCommand <
|
1
|
+
class Kontena::Cli::LoginCommand < Kontena::Command
|
2
2
|
include Kontena::Cli::Common
|
3
3
|
|
4
|
-
parameter "URL", "
|
4
|
+
parameter "URL", "url"
|
5
5
|
|
6
|
-
|
6
|
+
banner "Command removed, use 'kontena cloud login' to authenticate to a Kontena Cloud account"
|
7
|
+
banner "or 'kontena master login' to authenticate to a Kontena Master", false
|
7
8
|
|
8
9
|
def execute
|
9
|
-
|
10
|
-
|
11
|
-
until !url.nil? && !url.empty?
|
12
|
-
api_url = ask('Kontena Master Node URL: ')
|
13
|
-
end
|
14
|
-
|
15
|
-
@api_url = url
|
16
|
-
|
17
|
-
unless request_server_info
|
18
|
-
puts 'Could not connect to server'.colorize(:red)
|
19
|
-
return false
|
20
|
-
end
|
21
|
-
|
22
|
-
email = ask("Email: ")
|
23
|
-
password = ask("Password: ") { |q| q.echo = "*" }
|
24
|
-
response = do_login(email, password)
|
25
|
-
|
26
|
-
if response
|
27
|
-
update_master_info(name, url, response['access_token'], email)
|
28
|
-
display_logo
|
29
|
-
puts ''
|
30
|
-
puts "Logged in as #{response['user']['name'].green}"
|
31
|
-
reset_client
|
32
|
-
grids = client(require_token).get('grids')['grids']
|
33
|
-
grid = grids[0]
|
34
|
-
if grid
|
35
|
-
self.current_grid = grid
|
36
|
-
puts "Using grid #{grid['name'].cyan}"
|
37
|
-
puts ""
|
38
|
-
if grids.size > 1
|
39
|
-
puts "You have access to following grids and can switch between them using 'kontena grid use <name>'"
|
40
|
-
puts ""
|
41
|
-
grids.each do |grid|
|
42
|
-
puts " * #{grid['name']}"
|
43
|
-
end
|
44
|
-
puts ""
|
45
|
-
end
|
46
|
-
else
|
47
|
-
clear_current_grid
|
48
|
-
end
|
49
|
-
|
50
|
-
puts "Welcome! See 'kontena --help' to get started."
|
51
|
-
true
|
52
|
-
else
|
53
|
-
puts 'Login Failed'.colorize(:red)
|
54
|
-
false
|
55
|
-
end
|
10
|
+
abort("Command removed. Use #{"kontena master login #{self.url}".colorize(:yellow)} to login to a Kontena Master")
|
56
11
|
end
|
57
|
-
|
58
|
-
|
59
|
-
def login_client
|
60
|
-
if @login_client.nil?
|
61
|
-
@login_client = Kontena::Client.new(@api_url)
|
62
|
-
end
|
63
|
-
@login_client
|
64
|
-
end
|
65
|
-
|
66
|
-
def do_login(email, password)
|
67
|
-
params = {
|
68
|
-
username: email,
|
69
|
-
password: password,
|
70
|
-
grant_type: 'password',
|
71
|
-
scope: 'user'
|
72
|
-
}
|
73
|
-
login_client.post('auth', params)
|
74
|
-
end
|
75
|
-
|
76
|
-
def request_server_info
|
77
|
-
valid = true
|
78
|
-
begin
|
79
|
-
login_client.get('ping') # test server connection
|
80
|
-
rescue Excon::Errors::SocketError => exc
|
81
|
-
if exc.message.include?('Unable to verify certificate')
|
82
|
-
puts "The server uses a certificate signed by an unknown authority.".colorize(:red)
|
83
|
-
puts "Protip: you can bypass the certificate check by setting #{'SSL_IGNORE_ERRORS=true'.colorize(:yellow)} env variable, but any data you send to the server could be intercepted by others."
|
84
|
-
exit(1)
|
85
|
-
else
|
86
|
-
valid = false
|
87
|
-
end
|
88
|
-
rescue => exc
|
89
|
-
valid = false
|
90
|
-
end
|
91
|
-
valid
|
92
|
-
end
|
93
|
-
|
94
|
-
##
|
95
|
-
#
|
96
|
-
# @param [String] name
|
97
|
-
# @param [String] url
|
98
|
-
# @param [String] token
|
99
|
-
#
|
100
|
-
def update_master_info(name, url, token, email)
|
101
|
-
name = name || 'default'
|
102
|
-
master = {
|
103
|
-
'name' => name,
|
104
|
-
'url' => url,
|
105
|
-
'token' => token,
|
106
|
-
'email' => email
|
107
|
-
}
|
108
|
-
|
109
|
-
self.add_master(name, master)
|
110
|
-
end
|
111
|
-
|
112
|
-
def display_logo
|
113
|
-
logo = <<LOGO
|
114
|
-
_ _
|
115
|
-
| | _____ _ __ | |_ ___ _ __ __ _
|
116
|
-
| |/ / _ \\| '_ \\| __/ _ \\ '_ \\ / _` |
|
117
|
-
| < (_) | | | | || __/ | | | (_| |
|
118
|
-
|_|\\_\\___/|_| |_|\\__\\___|_| |_|\\__,_|
|
119
|
-
-------------------------------------
|
120
|
-
Copyright (c)2016 Kontena, Inc.
|
121
|
-
LOGO
|
122
|
-
puts logo
|
123
|
-
end
|
124
|
-
|
125
12
|
end
|
@@ -1,7 +1,38 @@
|
|
1
|
-
class Kontena::Cli::LogoutCommand <
|
1
|
+
class Kontena::Cli::LogoutCommand < Kontena::Command
|
2
2
|
include Kontena::Cli::Common
|
3
3
|
|
4
|
+
option ['-A', '--all'], :flag, 'Log out from all masters. By default only log out from current master.'
|
5
|
+
option ['--accounts'], :flag, 'Log out from cloud platform accounts', hidden: true
|
6
|
+
|
7
|
+
def use_refresh_token(server)
|
8
|
+
return unless server.token
|
9
|
+
return unless server.token.refresh_token
|
10
|
+
return if server.token.expired?
|
11
|
+
client = Kontena::Client.new(server.url, server.token)
|
12
|
+
ENV["DEBUG"] && puts("Trying to invalidate refresh token on #{server.name}")
|
13
|
+
client.refresh_token
|
14
|
+
rescue
|
15
|
+
ENV["DEBUG"] && puts("Refreshing failed: #{$!} : #{$!.message}")
|
16
|
+
end
|
17
|
+
|
4
18
|
def execute
|
5
|
-
self.
|
19
|
+
if self.all?
|
20
|
+
config.servers.each do |server|
|
21
|
+
use_refresh_token(server)
|
22
|
+
server.token = nil
|
23
|
+
end
|
24
|
+
elsif self.accounts?
|
25
|
+
config.accounts.each do |account|
|
26
|
+
use_refresh_token(account)
|
27
|
+
account.token = nil
|
28
|
+
end
|
29
|
+
elsif config.current_master
|
30
|
+
use_refresh_token(config.current_master)
|
31
|
+
config.current_master.token = nil
|
32
|
+
else
|
33
|
+
puts "Current master has not been selected"
|
34
|
+
exit 0 # exiting with 0 not 1, it's not really an error situation (kontena logout && kontena master login...)
|
35
|
+
end
|
36
|
+
config.write
|
6
37
|
end
|
7
38
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Kontena::Cli::Master
|
2
|
+
class AuditLogCommand < Kontena::Command
|
3
|
+
include Kontena::Cli::Common
|
4
|
+
|
5
|
+
option ["-l", "--lines"], "LINES", "Number of lines"
|
6
|
+
|
7
|
+
requires_current_master
|
8
|
+
requires_current_master_token
|
9
|
+
|
10
|
+
def execute
|
11
|
+
audit_logs = client.get("audit_logs", {limit: lines})
|
12
|
+
puts '%-30.30s %-10s %-15s %-25s %-15s %-25s %-15s %-15s' % ['Time', 'Grid', 'Resource Type', 'Resource Name', 'Event Name', 'User', 'Source IP', 'User-Agent']
|
13
|
+
audit_logs['logs'].each do |log|
|
14
|
+
puts '%-30.30s %-10s %-15s %-25s %-15s %-25s %-15s %-15s' % [ log['time'], log['grid'], log['resource_type'], log['resource_name'], log['event_name'], log['user_identity']['email'], log['source_ip'], log['user_agent']]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Kontena::Cli::Master::Config
|
2
|
+
class ExportCommand < Kontena::Command
|
3
|
+
|
4
|
+
include Kontena::Cli::Common
|
5
|
+
|
6
|
+
requires_current_master
|
7
|
+
requires_current_master_token
|
8
|
+
|
9
|
+
banner "Reads configuration from master"
|
10
|
+
|
11
|
+
parameter '[PATH]', "Output to file in PATH, default: STDOUT", required: false
|
12
|
+
option ['-f', '--format'], '[FORMAT]', "Specify output format (json, yaml) (default: guess from PATH or json)"
|
13
|
+
|
14
|
+
option ['--filter'], "[FILTER]", "Filter keys, example: oauth2.*"
|
15
|
+
|
16
|
+
def decorate(data)
|
17
|
+
case self.format.downcase
|
18
|
+
when 'json'
|
19
|
+
require 'json'
|
20
|
+
JSON.pretty_generate(data)
|
21
|
+
when 'yaml', 'yml'
|
22
|
+
require 'yaml'
|
23
|
+
YAML.dump(data)
|
24
|
+
else
|
25
|
+
abort "Unknown output format '#{self.format}'"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def output(content)
|
30
|
+
self.path ? File.write(self.path, content) : puts(content)
|
31
|
+
end
|
32
|
+
|
33
|
+
def data
|
34
|
+
client.get("config", self.filter ? { filter: self.filter } : nil)
|
35
|
+
end
|
36
|
+
|
37
|
+
def set_default_format
|
38
|
+
self.format ||= self.path.to_s.end_with?('.yml') ? 'yaml' : 'json'
|
39
|
+
end
|
40
|
+
|
41
|
+
def execute
|
42
|
+
set_default_format
|
43
|
+
output(decorate(data))
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Kontena::Cli::Master::Config
|
2
|
+
class GetCommand < Kontena::Command
|
3
|
+
|
4
|
+
include Kontena::Cli::Common
|
5
|
+
|
6
|
+
requires_current_master
|
7
|
+
requires_current_master_token
|
8
|
+
|
9
|
+
banner "Reads a configuration value from master"
|
10
|
+
|
11
|
+
parameter "KEY", "Configuration key to read from master", required: true
|
12
|
+
|
13
|
+
option ['-p', '--pair'], :flag, "Print key=value instead of only value"
|
14
|
+
|
15
|
+
def execute
|
16
|
+
if self.pair?
|
17
|
+
puts client.get("config/#{self.key}").inspect
|
18
|
+
else
|
19
|
+
puts client.get("config/#{self.key}")[self.key]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module Kontena::Cli::Master::Config
|
2
|
+
class ImportCommand < Kontena::Command
|
3
|
+
|
4
|
+
include Kontena::Cli::Common
|
5
|
+
|
6
|
+
requires_current_master
|
7
|
+
requires_current_master_token
|
8
|
+
|
9
|
+
banner "Updates configuration from a file into Master"
|
10
|
+
|
11
|
+
parameter '[PATH]', "Input from file in PATH, default: STDIN", required: false
|
12
|
+
|
13
|
+
option ['--preset'], '[NAME]', 'Load preset'
|
14
|
+
|
15
|
+
option ['--format'], '[FORMAT]', "Specify output format (json, yaml) (default: guess from PATH or json)"
|
16
|
+
option ['--full'], :flag, "Perform full update, keys that are not present in the input are cleared"
|
17
|
+
option ['-f', '--force'], :flag, "Don't ask for confirmation"
|
18
|
+
|
19
|
+
|
20
|
+
def input_as_hash
|
21
|
+
if self.path && self.preset
|
22
|
+
abort "Options --preset and PATH can not be used together"
|
23
|
+
elsif self.path
|
24
|
+
unless File.exist?(self.path) && File.readable?(self.path)
|
25
|
+
abort "Can not read '#{self.path}'"
|
26
|
+
end
|
27
|
+
File.read(self.path)
|
28
|
+
elsif self.preset
|
29
|
+
self.format = 'yaml'
|
30
|
+
path = File.join(Kontena.root, 'lib/kontena/presets', "#{self.preset}.yml")
|
31
|
+
File.read(path)
|
32
|
+
else
|
33
|
+
STDIN.read
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def convert(data)
|
38
|
+
case self.format.downcase
|
39
|
+
when 'json'
|
40
|
+
require 'json'
|
41
|
+
JSON.parse(data)
|
42
|
+
when 'yaml', 'yml'
|
43
|
+
require 'yaml'
|
44
|
+
YAML.load(data)
|
45
|
+
else
|
46
|
+
abort "Unknown input format '#{self.format}'"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def http_method
|
51
|
+
self.full? ? :patch : :put
|
52
|
+
end
|
53
|
+
|
54
|
+
def upload(data)
|
55
|
+
confirm unless self.force?
|
56
|
+
client.send(http_method, "config", data)
|
57
|
+
end
|
58
|
+
|
59
|
+
def set_default_format
|
60
|
+
self.format ||= self.path.to_s.end_with?('.yml') ? 'yaml' : 'json'
|
61
|
+
end
|
62
|
+
|
63
|
+
def execute
|
64
|
+
set_default_format
|
65
|
+
upload(convert(input_as_hash))
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Kontena::Cli::Master::Config
|
2
|
+
class SetCommand < Kontena::Command
|
3
|
+
|
4
|
+
include Kontena::Cli::Common
|
5
|
+
|
6
|
+
requires_current_master
|
7
|
+
requires_current_master_token
|
8
|
+
|
9
|
+
banner "Sets a configuration value to Master"
|
10
|
+
|
11
|
+
parameter "KEY_VALUE_PAIR ...", "Key/value pair, for example server.root_url=http://example.com", required: true
|
12
|
+
|
13
|
+
def execute
|
14
|
+
data = Hash[*self.key_value_pair_list.flat_map{ |p| p.split('=') }]
|
15
|
+
client.patch('config', data)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|