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.
Files changed (207) hide show
  1. checksums.yaml +4 -4
  2. data/Dockerfile +0 -3
  3. data/Gemfile +3 -0
  4. data/LOGO +8 -0
  5. data/VERSION +1 -1
  6. data/kontena-cli.gemspec +2 -3
  7. data/lib/kontena/callback.rb +57 -0
  8. data/lib/kontena/callbacks/.gitkeep +0 -0
  9. data/lib/kontena/callbacks/auth/01_list_and_select_grid_after_master_auth.rb +27 -0
  10. data/lib/kontena/callbacks/master/01_clear_current_master_after_terminate.rb +20 -0
  11. data/lib/kontena/callbacks/master/deploy/01_show_logo_before_deploy.rb +15 -0
  12. data/lib/kontena/callbacks/master/deploy/05_before_deploy_configuration_wizard.rb +124 -0
  13. data/lib/kontena/callbacks/master/deploy/50_authenticate_after_deploy.rb +53 -0
  14. data/lib/kontena/callbacks/master/deploy/55_create_initial_grid_after_deploy.rb +32 -0
  15. data/lib/kontena/callbacks/master/deploy/60_configure_auth_provider_after_deploy.rb +49 -0
  16. data/lib/kontena/callbacks/master/deploy/90_suggest_inviting_yourself_after_deploy.rb +24 -0
  17. data/lib/kontena/cli/app_command.rb +2 -1
  18. data/lib/kontena/cli/apps/build_command.rb +1 -1
  19. data/lib/kontena/cli/apps/common.rb +6 -1
  20. data/lib/kontena/cli/apps/config_command.rb +1 -1
  21. data/lib/kontena/cli/apps/deploy_command.rb +1 -1
  22. data/lib/kontena/cli/apps/init_command.rb +3 -5
  23. data/lib/kontena/cli/apps/list_command.rb +1 -1
  24. data/lib/kontena/cli/apps/logs_command.rb +1 -1
  25. data/lib/kontena/cli/apps/monitor_command.rb +1 -1
  26. data/lib/kontena/cli/apps/remove_command.rb +2 -3
  27. data/lib/kontena/cli/apps/restart_command.rb +1 -1
  28. data/lib/kontena/cli/apps/scale_command.rb +1 -1
  29. data/lib/kontena/cli/apps/show_command.rb +1 -1
  30. data/lib/kontena/cli/apps/start_command.rb +1 -1
  31. data/lib/kontena/cli/apps/stop_command.rb +1 -1
  32. data/lib/kontena/cli/apps/yaml/reader.rb +3 -13
  33. data/lib/kontena/cli/apps/yaml/validator.rb +0 -4
  34. data/lib/kontena/cli/apps/yaml/validator_v2.rb +1 -5
  35. data/lib/kontena/cli/certificate/authorize_command.rb +1 -1
  36. data/lib/kontena/cli/certificate/get_command.rb +1 -1
  37. data/lib/kontena/cli/certificate/register_command.rb +1 -1
  38. data/lib/kontena/cli/certificate_command.rb +1 -1
  39. data/lib/kontena/cli/cloud/login_command.rb +128 -0
  40. data/lib/kontena/cli/cloud/master/add_command.rb +54 -0
  41. data/lib/kontena/cli/cloud/master/delete_command.rb +20 -0
  42. data/lib/kontena/cli/cloud/master/list_command.rb +29 -0
  43. data/lib/kontena/cli/cloud/master/show_command.rb +23 -0
  44. data/lib/kontena/cli/cloud/master/update_command.rb +58 -0
  45. data/lib/kontena/cli/cloud/master_command.rb +21 -0
  46. data/lib/kontena/cli/cloud_command.rb +10 -0
  47. data/lib/kontena/cli/common.rb +230 -88
  48. data/lib/kontena/cli/config.rb +537 -0
  49. data/lib/kontena/cli/container_command.rb +1 -1
  50. data/lib/kontena/cli/containers/exec_command.rb +1 -1
  51. data/lib/kontena/cli/containers/inspect_command.rb +1 -1
  52. data/lib/kontena/cli/etcd/get_command.rb +1 -1
  53. data/lib/kontena/cli/etcd/list_command.rb +1 -1
  54. data/lib/kontena/cli/etcd/mkdir_command.rb +1 -1
  55. data/lib/kontena/cli/etcd/remove_command.rb +1 -1
  56. data/lib/kontena/cli/etcd/set_command.rb +1 -1
  57. data/lib/kontena/cli/etcd_command.rb +1 -1
  58. data/lib/kontena/cli/external_registries/add_command.rb +1 -1
  59. data/lib/kontena/cli/external_registries/delete_command.rb +1 -1
  60. data/lib/kontena/cli/external_registries/list_command.rb +1 -1
  61. data/lib/kontena/cli/external_registries/remove_command.rb +1 -1
  62. data/lib/kontena/cli/external_registry_command.rb +1 -1
  63. data/lib/kontena/cli/grid_command.rb +1 -1
  64. data/lib/kontena/cli/grids/audit_log_command.rb +6 -5
  65. data/lib/kontena/cli/grids/cloud_config_command.rb +1 -1
  66. data/lib/kontena/cli/grids/common.rb +1 -1
  67. data/lib/kontena/cli/grids/create_command.rb +8 -4
  68. data/lib/kontena/cli/grids/current_command.rb +1 -1
  69. data/lib/kontena/cli/grids/env_command.rb +1 -1
  70. data/lib/kontena/cli/grids/list_command.rb +35 -10
  71. data/lib/kontena/cli/grids/logs_command.rb +1 -1
  72. data/lib/kontena/cli/grids/remove_command.rb +2 -2
  73. data/lib/kontena/cli/grids/show_command.rb +1 -1
  74. data/lib/kontena/cli/grids/trusted_subnet_command.rb +1 -1
  75. data/lib/kontena/cli/grids/trusted_subnets/add_command.rb +1 -1
  76. data/lib/kontena/cli/grids/trusted_subnets/list_command.rb +1 -1
  77. data/lib/kontena/cli/grids/trusted_subnets/remove_command.rb +1 -1
  78. data/lib/kontena/cli/grids/update_command.rb +1 -1
  79. data/lib/kontena/cli/grids/use_command.rb +11 -6
  80. data/lib/kontena/cli/grids/user_command.rb +1 -1
  81. data/lib/kontena/cli/grids/users/add_command.rb +1 -1
  82. data/lib/kontena/cli/grids/users/list_command.rb +1 -1
  83. data/lib/kontena/cli/grids/users/remove_command.rb +1 -1
  84. data/lib/kontena/cli/localhost_web_server.rb +93 -0
  85. data/lib/kontena/cli/login_command.rb +5 -118
  86. data/lib/kontena/cli/logout_command.rb +33 -2
  87. data/lib/kontena/cli/master/audit_log_command.rb +19 -0
  88. data/lib/kontena/cli/master/config/export_command.rb +47 -0
  89. data/lib/kontena/cli/master/config/get_command.rb +24 -0
  90. data/lib/kontena/cli/master/config/import_command.rb +69 -0
  91. data/lib/kontena/cli/master/config/set_command.rb +19 -0
  92. data/lib/kontena/cli/master/config/unset_command.rb +20 -0
  93. data/lib/kontena/cli/master/config_command.rb +24 -0
  94. data/lib/kontena/cli/master/create_command.rb +76 -0
  95. data/lib/kontena/cli/master/current_command.rb +10 -2
  96. data/lib/kontena/cli/master/join_command.rb +20 -0
  97. data/lib/kontena/cli/master/list_command.rb +4 -4
  98. data/lib/kontena/cli/master/login_command.rb +274 -0
  99. data/lib/kontena/cli/master/use_command.rb +8 -19
  100. data/lib/kontena/cli/master/users/invite_command.rb +33 -6
  101. data/lib/kontena/cli/master/users/list_command.rb +2 -2
  102. data/lib/kontena/cli/master/users/remove_command.rb +1 -1
  103. data/lib/kontena/cli/master/users/role_command.rb +1 -1
  104. data/lib/kontena/cli/master/users/roles/add_command.rb +18 -16
  105. data/lib/kontena/cli/master/users/roles/remove_command.rb +1 -1
  106. data/lib/kontena/cli/master/users_command.rb +1 -1
  107. data/lib/kontena/cli/master_command.rb +21 -1
  108. data/lib/kontena/cli/node_command.rb +1 -1
  109. data/lib/kontena/cli/nodes/label_command.rb +1 -1
  110. data/lib/kontena/cli/nodes/labels/add_command.rb +1 -1
  111. data/lib/kontena/cli/nodes/labels/remove_command.rb +1 -1
  112. data/lib/kontena/cli/nodes/list_command.rb +1 -1
  113. data/lib/kontena/cli/nodes/remove_command.rb +1 -1
  114. data/lib/kontena/cli/nodes/show_command.rb +1 -1
  115. data/lib/kontena/cli/nodes/ssh_command.rb +1 -1
  116. data/lib/kontena/cli/nodes/update_command.rb +1 -1
  117. data/lib/kontena/cli/plugin_command.rb +1 -1
  118. data/lib/kontena/cli/plugins/install_command.rb +2 -2
  119. data/lib/kontena/cli/plugins/list_command.rb +2 -2
  120. data/lib/kontena/cli/plugins/search_command.rb +1 -1
  121. data/lib/kontena/cli/plugins/uninstall_command.rb +2 -2
  122. data/lib/kontena/cli/registry/create_command.rb +2 -4
  123. data/lib/kontena/cli/registry/delete_command.rb +1 -1
  124. data/lib/kontena/cli/registry/remove_command.rb +1 -1
  125. data/lib/kontena/cli/registry_command.rb +1 -1
  126. data/lib/kontena/cli/service_command.rb +1 -1
  127. data/lib/kontena/cli/services/container_command.rb +1 -1
  128. data/lib/kontena/cli/services/containers_command.rb +1 -1
  129. data/lib/kontena/cli/services/create_command.rb +1 -1
  130. data/lib/kontena/cli/services/delete_command.rb +1 -1
  131. data/lib/kontena/cli/services/deploy_command.rb +1 -1
  132. data/lib/kontena/cli/services/env_command.rb +1 -1
  133. data/lib/kontena/cli/services/envs/add_command.rb +1 -1
  134. data/lib/kontena/cli/services/envs/list_command.rb +1 -1
  135. data/lib/kontena/cli/services/envs/remove_command.rb +1 -1
  136. data/lib/kontena/cli/services/link_command.rb +1 -1
  137. data/lib/kontena/cli/services/list_command.rb +1 -1
  138. data/lib/kontena/cli/services/logs_command.rb +1 -1
  139. data/lib/kontena/cli/services/monitor_command.rb +1 -1
  140. data/lib/kontena/cli/services/remove_command.rb +1 -1
  141. data/lib/kontena/cli/services/restart_command.rb +1 -1
  142. data/lib/kontena/cli/services/scale_command.rb +1 -1
  143. data/lib/kontena/cli/services/secret_command.rb +1 -1
  144. data/lib/kontena/cli/services/secrets/link_command.rb +1 -1
  145. data/lib/kontena/cli/services/secrets/unlink_command.rb +1 -1
  146. data/lib/kontena/cli/services/services_helper.rb +6 -3
  147. data/lib/kontena/cli/services/show_command.rb +1 -1
  148. data/lib/kontena/cli/services/start_command.rb +1 -1
  149. data/lib/kontena/cli/services/stats_command.rb +1 -1
  150. data/lib/kontena/cli/services/stop_command.rb +1 -1
  151. data/lib/kontena/cli/services/unlink_command.rb +1 -1
  152. data/lib/kontena/cli/services/update_command.rb +1 -1
  153. data/lib/kontena/cli/spinner.rb +122 -0
  154. data/lib/kontena/cli/stack_command.rb +1 -1
  155. data/lib/kontena/cli/stacks/create_command.rb +1 -1
  156. data/lib/kontena/cli/stacks/deploy_command.rb +1 -1
  157. data/lib/kontena/cli/stacks/list_command.rb +1 -1
  158. data/lib/kontena/cli/stacks/remove_command.rb +1 -1
  159. data/lib/kontena/cli/stacks/show_command.rb +1 -1
  160. data/lib/kontena/cli/stacks/update_command.rb +1 -1
  161. data/lib/kontena/cli/vault/list_command.rb +1 -1
  162. data/lib/kontena/cli/vault/read_command.rb +1 -1
  163. data/lib/kontena/cli/vault/remove_command.rb +1 -1
  164. data/lib/kontena/cli/vault/update_command.rb +1 -1
  165. data/lib/kontena/cli/vault/write_command.rb +1 -1
  166. data/lib/kontena/cli/vault_command.rb +1 -1
  167. data/lib/kontena/cli/version.rb +1 -1
  168. data/lib/kontena/cli/version_command.rb +1 -1
  169. data/lib/kontena/cli/vpn/config_command.rb +1 -1
  170. data/lib/kontena/cli/vpn/create_command.rb +2 -4
  171. data/lib/kontena/cli/vpn/delete_command.rb +1 -1
  172. data/lib/kontena/cli/vpn/remove_command.rb +1 -1
  173. data/lib/kontena/cli/vpn_command.rb +1 -1
  174. data/lib/kontena/cli/whoami_command.rb +16 -13
  175. data/lib/kontena/client.rb +410 -90
  176. data/lib/kontena/command.rb +172 -0
  177. data/lib/kontena/main_command.rb +7 -8
  178. data/lib/kontena/presets/github_auth_provider.yml +11 -0
  179. data/lib/kontena/presets/kontena_auth_provider.yml +11 -0
  180. data/lib/kontena_cli.rb +51 -1
  181. data/spec/kontena/cli/app/deploy_command_spec.rb +14 -44
  182. data/spec/kontena/cli/app/scale_spec.rb +1 -1
  183. data/spec/kontena/cli/app/yaml/reader_spec.rb +0 -48
  184. data/spec/kontena/cli/common_spec.rb +63 -59
  185. data/spec/kontena/cli/grids/use_command_spec.rb +43 -0
  186. data/spec/kontena/cli/master/current_command_spec.rb +3 -24
  187. data/spec/kontena/cli/master/use_command_spec.rb +2 -27
  188. data/spec/kontena/cli/master/users/invite_command_spec.rb +4 -18
  189. data/spec/kontena/cli/master/users/roles/add_command_spec.rb +2 -16
  190. data/spec/kontena/cli/master/users/roles/remove_command_spec.rb +2 -13
  191. data/spec/kontena/cli/services/restart_command_spec.rb +1 -1
  192. data/spec/kontena/cli/services/update_command_spec.rb +5 -5
  193. data/spec/kontena/client_spec.rb +104 -35
  194. data/spec/kontena/config_spec.rb +65 -0
  195. data/spec/spec_helper.rb +25 -3
  196. data/spec/support/client_helpers.rb +10 -3
  197. data/spec/support/requirements_helper.rb +32 -0
  198. metadata +61 -48
  199. data/lib/kontena/cli/register_command.rb +0 -23
  200. data/lib/kontena/cli/user/forgot_password_command.rb +0 -16
  201. data/lib/kontena/cli/user/reset_password_command.rb +0 -23
  202. data/lib/kontena/cli/user/verify_command.rb +0 -20
  203. data/lib/kontena/cli/user_command.rb +0 -13
  204. data/spec/fixtures/kontena-malformed-yaml.yml +0 -6
  205. data/spec/fixtures/kontena-not-hash-service-config.yml +0 -3
  206. data/spec/kontena/cli/login_command_spec.rb +0 -32
  207. data/spec/kontena/cli/register_command_spec.rb +0 -57
@@ -0,0 +1,54 @@
1
+ module Kontena::Cli::Cloud::Master
2
+ class AddCommand < Kontena::Command
3
+
4
+ include Kontena::Cli::Common
5
+
6
+ callback_matcher 'cloud-master', 'create'
7
+
8
+ requires_current_account_token
9
+
10
+ parameter "NAME", "Master name"
11
+
12
+ option ['--redirect-uri'], '[URL]', 'Se master redirect URL'
13
+ option ['--url'], '[URL]', 'Se master URL'
14
+ option ['--provider'], '[NAME]', 'Se master provider'
15
+ option ['--name'], '[NAME]', 'Se master name', hidden: true
16
+ option ['--version'], '[VERSION]', 'Se master version', hidden: true
17
+ option ['--owner'], '[NAME]', 'Se master owner', hidden: true
18
+
19
+ option ['--id'], :flag, 'Just output the ID'
20
+ option ['--arg'], :flag, 'Output as command line arguments'
21
+
22
+ option ['--return'], :flag, 'Return the ID', hidden: true
23
+
24
+ def execute
25
+ attributes = { 'name' => self.name }
26
+ attributes['url'] = self.url if self.url
27
+ attributes['provider'] = self.provider if self.provider
28
+ attributes['redirect-uri'] = self.redirect_uri if self.redirect_uri
29
+ attributes['version'] = self.version if self.version
30
+ attributes['owner'] = self.owner if self.owner
31
+
32
+ response = cloud_client.post('user/masters', { data: { attributes: attributes } })
33
+ if response.kind_of?(Hash)
34
+ if response['error']
35
+ puts "Failed: #{response['error']}"
36
+ exit 1
37
+ else
38
+ if self.return?
39
+ return response['data']['id']
40
+ elsif self.id?
41
+ puts response['data']['id']
42
+ elsif self.arg?
43
+ puts "--client-id #{response['data']['attributes']['client-id']} --client-secret #{response['data']['attributes']['client-secret']}"
44
+ else
45
+ puts "Created master.".colorize(:green)
46
+ puts "ID: #{response['data']['id']}"
47
+ puts "Client ID: #{response['data']['attributes']['client-id']}"
48
+ puts "Client Secret: #{response['data']['attributes']['client-secret']}"
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,20 @@
1
+ module Kontena::Cli::Cloud::Master
2
+ class DeleteCommand < Kontena::Command
3
+
4
+ include Kontena::Cli::Common
5
+
6
+ callback_matcher 'cloud-master', 'delete'
7
+
8
+ requires_current_account_token
9
+
10
+ parameter "MASTER_ID", "Master ID"
11
+
12
+ option ['-f', '--force'], :flag, "Don't ask for confirmation"
13
+
14
+ def execute
15
+ confirm unless self.force?
16
+ cloud_client.delete("user/masters/#{self.master_id}")
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,29 @@
1
+ module Kontena::Cli::Cloud::Master
2
+ class ListCommand < Kontena::Command
3
+
4
+ include Kontena::Cli::Common
5
+
6
+ callback_matcher 'cloud-master', 'list'
7
+
8
+ requires_current_account_token
9
+
10
+ def execute
11
+ response = cloud_client.get('user/masters')
12
+ unless response && response.kind_of?(Hash) && response['data'].kind_of?(Array)
13
+ puts "Listing masters failed".colorize(:red)
14
+ exit 1
15
+ end
16
+
17
+ if response['data'].empty?
18
+ puts "No masters registered"
19
+ else
20
+ puts '%-26.26s %-24s %-12s %s' % ['ID', 'NAME', 'OWNER', 'URL']
21
+ response['data'].each do |data|
22
+ attr = data['attributes']
23
+ puts '%-26.26s %-24s %-12s %s' % [data['id'], attr['name'], attr['owner'], attr['url']]
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+
@@ -0,0 +1,23 @@
1
+ require 'pp'
2
+
3
+ module Kontena::Cli::Cloud::Master
4
+ class ShowCommand < Kontena::Command
5
+
6
+ include Kontena::Cli::Common
7
+
8
+ callback_matcher 'cloud-master', 'show'
9
+
10
+ requires_current_account_token
11
+
12
+ parameter "MASTER_ID", "Master ID"
13
+
14
+ def execute
15
+ response = cloud_client.get("user/masters/#{master_id}")
16
+ response['data']['attributes']['id'] = response['data']['id']
17
+ response['data']['attributes'].each do |key, value|
18
+ puts "%20.20s : %s" % [key, value]
19
+ end
20
+ end
21
+ end
22
+ end
23
+
@@ -0,0 +1,58 @@
1
+ module Kontena::Cli::Cloud::Master
2
+ class UpdateCommand < Kontena::Command
3
+
4
+ include Kontena::Cli::Common
5
+
6
+ callback_matcher 'cloud-master', 'update'
7
+
8
+ requires_current_account_token
9
+
10
+ parameter "MASTER_ID", "Master ID"
11
+
12
+ option ['--redirect-uri'], '[URL]', 'Set master redirect URL'
13
+ option ['--url'], '[URL]', 'Set master URL'
14
+ option ['--provider'], '[NAME]', 'Set master provider'
15
+ option ['--name'], '[NAME]', 'Set master name', hidden: true
16
+ option ['--version'], '[VERSION]', 'Set master version', hidden: true
17
+ option ['--owner'], '[NAME]', 'Set master owner', hidden: true
18
+
19
+ def get_attributes
20
+ cloud_client.get("user/masters/#{self.master_id}")["data"]["attributes"]
21
+ rescue
22
+ nil
23
+ end
24
+
25
+ def execute
26
+ attrs = get_attributes
27
+ unless attrs
28
+ puts "Failed to obtain master credentials".colorize(:red)
29
+ exit 1
30
+ end
31
+
32
+ attrs["name"] = self.name if self.name
33
+ attrs["redirect-uri"] = self.redirect_uri if self.redirect_uri
34
+ attrs["url"] = self.url if self.url
35
+ attrs["provider"] = self.provider if self.provider
36
+ attrs["version"] = self.version if self.version
37
+ attrs["owner"] = self.owner if self.owner
38
+
39
+ response = cloud_client.put(
40
+ "master",
41
+ { data: { attributes: attrs.reject{ |k, _| ['client-id', 'client-secret'].include?(k) } } },
42
+ {},
43
+ cloud_client.basic_auth_header(
44
+ attrs["client-id"],
45
+ attrs["client-secret"]
46
+ ),
47
+ false
48
+ )
49
+
50
+ if response
51
+ puts "Master settings updated"
52
+ else
53
+ puts "Request failed"
54
+ exit 1
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,21 @@
1
+ require_relative 'master/add_command'
2
+ require_relative 'master/list_command'
3
+ require_relative 'master/delete_command'
4
+ require_relative 'master/update_command'
5
+ require_relative 'master/show_command'
6
+
7
+ module Kontena::Cli::Cloud
8
+ class MasterCommand < Kontena::Command
9
+ include Kontena::Cli::Common
10
+
11
+ subcommand ['list', 'ls'], "List masters in Kontena Cloud", Kontena::Cli::Cloud::Master::ListCommand
12
+ subcommand "add", "Register a master in Kontena Cloud", Kontena::Cli::Cloud::Master::AddCommand
13
+ subcommand "delete", "Delete a master in Kontena Cloud", Kontena::Cli::Cloud::Master::DeleteCommand
14
+ subcommand "show", "Show master settings in Kontena Cloud", Kontena::Cli::Cloud::Master::ShowCommand
15
+ subcommand "update", "Update master settings in Kontena Cloud", Kontena::Cli::Cloud::Master::UpdateCommand
16
+
17
+ def execute
18
+ end
19
+ end
20
+ end
21
+
@@ -0,0 +1,10 @@
1
+ require_relative 'cloud/login_command'
2
+ require_relative 'cloud/master_command'
3
+
4
+ class Kontena::Cli::CloudCommand < Kontena::Command
5
+ subcommand "login", "Authenticate to Kontena Cloud", Kontena::Cli::Cloud::LoginCommand
6
+ subcommand "master", "Master specific commands", Kontena::Cli::Cloud::MasterCommand
7
+
8
+ def execute
9
+ end
10
+ end
@@ -1,31 +1,161 @@
1
- require 'colorize'
1
+ require 'tty-prompt'
2
+ require 'pastel'
2
3
  require 'uri'
4
+ require 'io/console'
5
+
6
+ require_relative 'config'
7
+ require_relative 'spinner'
3
8
 
4
9
  module Kontena
5
10
  module Cli
6
11
  module Common
12
+
13
+ def pastel
14
+ ::Kontena.pastel
15
+ end
16
+
17
+ def logger
18
+ return @logger if @logger
19
+ @logger = Logger.new(STDOUT)
20
+ @logger.level = ENV["DEBUG"].nil? ? Logger::INFO : Logger::DEBUG
21
+ @logger.progname = 'COMMON'
22
+ @logger
23
+ end
24
+
25
+ def pastel
26
+ @pastel ||= Pastel.new(enabled: $stdout.tty?)
27
+ end
28
+
29
+ def running_silent?
30
+ self.respond_to?(:silent?) && self.silent?
31
+ end
32
+
33
+ def running_verbose?
34
+ self.respond_to?(:verbose?) && self.verbose?
35
+ end
36
+
37
+ # Puts that puts even when self.silent?
38
+ def sputs(*msgs)
39
+ ::Kernel.puts(*msgs)
40
+ end
41
+
42
+ # Print that prints even when self.silent?
43
+ def sprint(*msgs)
44
+ ::Kernel.print(*msgs)
45
+ end
46
+
47
+ # Print that doesn't print if self.silent?
48
+ def print(*msgs)
49
+ if running_silent?
50
+ logger.debug(msgs.join)
51
+ else
52
+ super(*msgs)
53
+ end
54
+ end
55
+
56
+ # Puts that doesn't puts if self.silent?
57
+ def puts(*msgs)
58
+ if running_silent?
59
+ msgs.compact.each { |msg| logger.debug(msg) }
60
+ elsif Thread.main['spinners'] && !Thread.main['spinners'].empty?
61
+ Thread.main['spinner_msgs'] ||= []
62
+ msgs.each { |msg| Thread.main['spinner_msgs'] << msg }
63
+ else
64
+ super(*msgs)
65
+ end
66
+ end
67
+
68
+ # Only output message if in verbose mode
69
+ def vputs(msg = nil)
70
+ if running_verbose?
71
+ puts msg
72
+ elsif ENV["DEBUG"] && msg
73
+ logger.debug msg
74
+ end
75
+ end
76
+
77
+ # Only show spinner when in verbose mode
78
+ def vspinner(msg, &block)
79
+ unless block_given?
80
+ vfakespinner(msg)
81
+ end
82
+ if running_verbose?
83
+ spinner(msg, &block)
84
+ else
85
+ yield
86
+ end
87
+ end
88
+
89
+ # Like vspinner but without actually running any block
90
+ def vfakespinner(msg, success: true)
91
+ if !running_verbose?
92
+ logger.debug msg
93
+ return
94
+ end
95
+ puts " [#{ success ? 'done'.colorize(:green) : 'fail'.colorize(:red)}] #{msg}"
96
+ end
97
+
98
+ def config
99
+ Kontena::Cli::Config.instance
100
+ end
101
+
7
102
  def require_api_url
8
- api_url
103
+ config.require_current_master.url
9
104
  end
10
105
 
11
106
  def require_token
12
- token = ENV['KONTENA_TOKEN'] || current_master['token']
13
- unless token
14
- raise ArgumentError.new("Please login first using: kontena login")
107
+ retried ||= false
108
+ config.require_current_master_token
109
+ rescue Kontena::Cli::Config::TokenExpiredError
110
+ if retried
111
+ raise ArgumentError, "Current master access token has expired and refresh failed."
112
+ else
113
+ logger.debug "Access token expired, trying to refresh"
114
+ retried = true
115
+ client.refresh_token && retry
15
116
  end
16
- token
17
117
  end
18
118
 
19
- def client(token = nil)
20
- if @client.nil?
21
- headers = {}
22
- unless token.nil?
23
- headers['Authorization'] = "Bearer #{token}"
24
- end
119
+ def require_current_master
120
+ config.require_current_master
121
+ end
122
+
123
+ def require_current_account
124
+ config.require_current_account
125
+ end
126
+
127
+ def current_account
128
+ config.current_account
129
+ end
130
+
131
+ def kontena_account
132
+ @kontena_account ||= config.find_account(ENV['KONTENA_ACCOUNT'] || 'kontena')
133
+ end
134
+
135
+ def kontena_auth?
136
+ return false unless kontena_account
137
+ return false unless kontena_account.token
138
+ return false unless kontena_account.token.access_token
139
+ true
140
+ end
141
+
142
+ def api_url_version
143
+ client.server_version
144
+ end
145
+
146
+ def cloud_client
147
+ @cloud_client ||= Kontena::Client.new(kontena_account.url, kontena_account.token, prefix: '/')
148
+ end
25
149
 
26
- @client = Kontena::Client.new(api_url, headers)
150
+ def client(token = nil, api_url = nil)
151
+ if token.kind_of?(String)
152
+ token = Kontena::Cli::Config::Token.new(access_token: token)
27
153
  end
28
- @client
154
+
155
+ @client ||= Kontena::Client.new(
156
+ api_url || require_current_master.url,
157
+ token || require_current_master.token
158
+ )
29
159
  end
30
160
 
31
161
  def reset_client
@@ -33,39 +163,15 @@ module Kontena
33
163
  end
34
164
 
35
165
  def settings_filename
36
- File.join(Dir.home, '/.kontena_client.json')
166
+ config.config_filename
37
167
  end
38
168
 
39
169
  def settings
40
- if @settings.nil?
41
- if File.exists?(settings_filename)
42
- @settings = JSON.parse(File.read(settings_filename))
43
- unless @settings['current_server']
44
- # Let's migrate the old settings model to new
45
- @settings['server']['name'] = 'default'
46
- @settings = {
47
- 'current_server' => 'default',
48
- 'servers' => [ @settings['server']]
49
- }
50
- save_settings
51
- end
52
- else
53
- @settings = {
54
- 'current_server' => 'default',
55
- 'servers' => [{}]
56
- }
57
- end
58
- end
59
- @settings
170
+ config
60
171
  end
61
172
 
62
173
  def api_url
63
- url = ENV['KONTENA_URL'] || current_master['url']
64
- unless url
65
- raise ArgumentError.new("It seem's that you are not logged into Kontena master, please login with: kontena login")
66
- end
67
- ensure_custom_ssl_ca(url)
68
- url
174
+ config.require_current_master.url
69
175
  end
70
176
 
71
177
  def ensure_custom_ssl_ca(url)
@@ -79,96 +185,132 @@ module Kontena
79
185
  end
80
186
 
81
187
  def current_grid=(grid)
82
- settings['servers'][current_master_index]['grid'] = grid['id']
83
- save_settings
188
+ config.current_grid=(grid)
84
189
  end
85
190
 
86
191
  def require_current_grid
87
- if current_grid.nil?
88
- raise ArgumentError.new("Please select grid first using: kontena grid use <grid name>")
89
- end
192
+ config.require_current_grid
90
193
  end
91
194
 
92
195
  def clear_current_grid
93
- settings['servers'][current_master_index].delete('grid')
94
- save_settings
196
+ current_master.delete_field(:grid) if require_current_master.respond_to?(:grid)
197
+ config.write
95
198
  end
96
199
 
97
200
  def current_grid
98
- if self.respond_to?(:grid)
99
- ENV['KONTENA_GRID'] || grid || current_master['grid']
100
- else
101
- ENV['KONTENA_GRID'] || current_master['grid']
102
- end
103
- rescue ArgumentError => e
104
- nil
201
+ config.current_grid || (self.respond_to?(:grid) ? self.grid : nil)
105
202
  end
106
203
 
107
204
  def current_master_index
108
- current_server = settings['current_server'] || 'default'
109
- settings['servers'].find_index{|m| m['name'] == current_server}
205
+ config.find_server_index(require_current_master.name)
110
206
  end
111
207
 
112
208
  def current_master
113
- index = current_master_index
114
- unless index
115
- raise ArgumentError.new("It seem's that you are not logged into ANY Kontena master, please login with: kontena login")
116
- end
117
- settings['servers'][index]
209
+ config.current_master
118
210
  end
119
211
 
120
212
  def current_master=(master_alias)
121
- settings['current_server'] = master_alias
122
- save_settings
213
+ config.current_master = master_alias
123
214
  end
124
215
 
125
- def error(message = nil)
126
- $stderr.puts(message) if message
216
+ def error(message = "Error")
217
+ prompt.error(message)
127
218
  exit(1)
128
219
  end
129
220
 
130
- def prompt(prefix = '> ')
131
- require 'highline/import'
132
- ask(prefix)
221
+ def ask(question = "")
222
+ prompt.ask(question)
223
+ end
224
+
225
+ def yes?(question = "")
226
+ prompt.yes?(question)
227
+ end
228
+
229
+ def prompt
230
+ ::Kontena.prompt
133
231
  end
134
232
 
135
233
  def confirm_command(name, message = nil)
234
+ if self.respond_to?(:force?) && self.force?
235
+ return
236
+ end
136
237
  puts message if message
137
238
  puts "Destructive command. To proceed, type \"#{name}\" or re-run this command with --force option."
138
239
 
139
- prompt == name || error("Confirmation did not match #{name}. Aborted command.")
240
+ ask("Enter '#{name}' to confirm: ") == name || error("Confirmation did not match #{name}. Aborted command.")
140
241
  end
141
242
 
142
- def confirm(message = 'Destructive command. Are you sure? (y/n) or re-run this command with --force option.')
143
- puts message
144
-
145
- ['y', 'yes'].include?(prompt) || error("Aborted command.")
243
+ def confirm(message = 'Destructive command. You can skip this prompt by running this command with --force option. Are you sure?')
244
+ if self.respond_to?(:force?) && self.force?
245
+ return
246
+ end
247
+ prompt.yes?(message) || error('Aborted command.')
146
248
  end
147
249
 
148
250
  def api_url=(api_url)
149
- settings['servers'][current_master_index]['url'] = api_url
150
- save_settings
251
+ config.current_master.url = api_url
252
+ config.write
151
253
  end
152
254
 
153
255
  def access_token=(token)
154
- settings['servers'][current_master_index]['token'] = token
155
- save_settings
256
+ require_current_master.token.access_token = token
257
+ config.write
156
258
  end
157
259
 
158
260
  def add_master(server_name, master_info)
159
- server_name = server_name || 'default'
160
- index = settings['servers'].find_index{|m| m['name'] == server_name}
161
- if index
162
- settings['servers'][index] = master_info
163
- else
164
- settings['servers'] << master_info
261
+ config.add_server(master_info.merge('name' => server_name))
262
+ end
263
+
264
+ def spinner(msg, &block)
265
+ Kontena::Cli::Spinner.spin(msg, &block)
266
+ end
267
+
268
+ def any_key_to_continue
269
+ return nil if running_silent?
270
+ msg = "Press any key to continue or ctrl-c to cancel.. "
271
+ print pastel.bright_white("#{msg}")
272
+ char = STDIN.getch
273
+ print "\r#{' ' * msg.length}\r"
274
+ if char == "\u0003"
275
+ error "Canceled"
276
+ end
277
+ end
278
+
279
+ def display_login_info(only: nil)
280
+ unless only == :account
281
+ server = config.current_master
282
+ if server
283
+ puts [
284
+ pastel.green('Authenticated to Kontena Master'),
285
+ pastel.yellow(server.name),
286
+ pastel.green('at'),
287
+ pastel.yellow(server.url),
288
+ pastel.green('as'),
289
+ pastel.yellow(server.username)
290
+ ].join(' ')
291
+ else
292
+ puts pastel.red("master not selected")
293
+ end
294
+ end
295
+ unless only == :master
296
+ if kontena_account
297
+ if kontena_account.token && kontena_account.token.access_token
298
+ begin
299
+ puts [
300
+ pastel.green("Authenticated to Kontena Cloud at"),
301
+ pastel.yellow(kontena_account.url),
302
+ pastel.green("as"),
303
+ pastel.yellow(kontena_account.username)
304
+ ].join(' ')
305
+ rescue
306
+ end
307
+ end
308
+ end
165
309
  end
166
- settings['current_server'] = server_name
167
- save_settings
168
310
  end
169
311
 
170
- def save_settings
171
- File.write(settings_filename, JSON.pretty_generate(settings))
312
+ def display_logo
313
+ puts File.read(File.expand_path('../../../../LOGO', __FILE__))
172
314
  end
173
315
  end
174
316
  end