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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 079f30719baffc353268aacf9888a55cdc8bcc47
4
- data.tar.gz: 4811e07b537b72b0cc7a38d487e156bab702f005
3
+ metadata.gz: 6dd79efbf65d99f1a53951c4064ae5361a6f45e7
4
+ data.tar.gz: 826215936d53f0c1e45896d648c21c3fbd2862b4
5
5
  SHA512:
6
- metadata.gz: 11cfafd1ca12b7d2035a26279a33ea59f7bf516953abb7ced8d2ae21c3d058bb2324f5701add4b6a5a71c44d5eef951d6250850c724bacdec50c0e02a7ee5104
7
- data.tar.gz: d0e72504efba04edc5b35c5a56005f6c408d05887b64d1fb8bdeeeb6238284dbfc2b3597d67b6d0b1576487e6171d5ad0e91433c517fc5936115f547a5f9d15e
6
+ metadata.gz: 226857ae59df5a8d5e4d9ae3eb22900360ee35eb3dc84754ad242d06c48e04dcc5130f47888abc555c850d438c9757eb716323474125cbb38c84e3e829d2046a
7
+ data.tar.gz: da4bc3d5892b56af1bb0adc7ab1d1fd2ab581fdcf0284675fa703d5d8ad8c27b1ca81db89ae73e7b78483f023448f1a3876cf82c22c037375ed4104a1eaccce6
data/Dockerfile CHANGED
@@ -9,12 +9,9 @@ RUN apk update && \
9
9
  ca-certificates libssl1.0 openssl libstdc++ && \
10
10
  gem install kontena-cli --no-rdoc --no-ri -v ${CLI_VERSION} && \
11
11
  adduser kontena -D -h /home/kontena -s /bin/sh && \
12
- echo "gem: --user-install" > /home/kontena/.gemrc && \
13
12
  chown -R kontena.kontena /home/kontena && \
14
13
  chmod +sx /usr/local/bin/docker
15
14
 
16
- ENV PATH=$PATH:/home/kontena/.gem/ruby/2.3.0/bin
17
-
18
15
  VOLUME ["/home/kontena"]
19
16
  WORKDIR /home/kontena
20
17
  USER kontena
data/Gemfile CHANGED
@@ -4,5 +4,8 @@ source 'https://rubygems.org'
4
4
  gemspec
5
5
  group :development, :test do
6
6
  gem "rspec"
7
+ gem 'simplecov', :require => false, :group => :test
7
8
  gem "kontena-plugin-hello", path: "./examples/kontena-plugin-hello"
9
+ gem 'pry', require: false
10
+ gem 'pry-byebug', require: false
8
11
  end
data/LOGO ADDED
@@ -0,0 +1,8 @@
1
+ _ _
2
+ | | _____ _ __ | |_ ___ _ __ __ _
3
+ | |/ / _ \| '_ \| __/ _ \ '_ \ / _` |
4
+ | < (_) | | | | || __/ | | | (_| |
5
+ |_|\_\___/|_| |_|\__\___|_| |_|\__,_|
6
+ -------------------------------------
7
+ Copyright (c)2016 Kontena, Inc.
8
+
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.15.5
1
+ 0.16.0.pre1
@@ -23,14 +23,13 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "bundler", "~> 1.7"
24
24
  spec.add_development_dependency "rake", "~> 10.0"
25
25
  spec.add_runtime_dependency "excon", "~> 0.49.0"
26
- spec.add_runtime_dependency "colorize", "~> 0.8.1"
26
+ spec.add_runtime_dependency "tty-prompt", "~> 0.7.1"
27
27
  spec.add_runtime_dependency "clamp", "~> 1.0.0"
28
- spec.add_runtime_dependency "highline", "~> 1.7.8"
29
- spec.add_runtime_dependency "shell-spinner", "~> 1.0.4"
30
28
  spec.add_runtime_dependency "ruby_dig", "~> 0.0.2"
31
29
  spec.add_runtime_dependency "dry-validation", "0.8.0"
32
30
  spec.add_runtime_dependency "dry-types", "0.8.0"
33
31
  spec.add_runtime_dependency "dry-configurable", "0.1.6"
34
32
  spec.add_runtime_dependency "dry-monads", "0.1.1"
35
33
  spec.add_runtime_dependency "dry-logic", "0.3.0"
34
+ spec.add_runtime_dependency "launchy", "~> 2.4.3"
36
35
  end
@@ -0,0 +1,57 @@
1
+ class Kontena::Callback
2
+
3
+ attr_reader :command
4
+
5
+ def initialize(command)
6
+ @command = command
7
+ end
8
+
9
+ # Register callback for command types it is supposed to run with.
10
+ def self.matches_commands(*commands)
11
+ cmd_types = {}
12
+
13
+ commands.each do |cmd|
14
+ cmd_class, cmd_type = cmd.split(' ', 2)
15
+
16
+ if cmd_class == '*'
17
+ cmd_class = :all
18
+ end
19
+
20
+ if cmd_type.nil? || cmd_type == '*'
21
+ cmd_type = :all
22
+ else
23
+ cmd_type = cmd_type.to_sym
24
+ end
25
+ cmd_types[cmd_class.to_sym] = cmd_type
26
+ end
27
+
28
+ # Finally it should be normalized into a hash that looks like :cmd_class => :cmd_type, :app => :init, :grid => :all
29
+ cmd_types.each do |cmd_class, cmd_type|
30
+ Kontena::Callback.callbacks[cmd_class] ||= {}
31
+ Kontena::Callback.callbacks[cmd_class][cmd_type] ||= []
32
+ Kontena::Callback.callbacks[cmd_class][cmd_type] << self
33
+ end
34
+ end
35
+
36
+ def self.callbacks
37
+ @@callbacks ||= {}
38
+ end
39
+
40
+ def self.run_callbacks(cmd_type, state, obj)
41
+ [cmd_type.last, :all].compact.uniq.each do |cmdtype|
42
+ [cmd_type.first, :all].compact.uniq.each do |cmdclass|
43
+ callbacks.fetch(cmdclass, {}).fetch(cmdtype, []).each do |klass|
44
+ if klass.instance_methods.include?(state)
45
+ cb = klass.new(obj)
46
+ if cb.send(state).kind_of?(FalseClass)
47
+ ENV["DEBUG"] && puts("Execution aborted by #{klass}")
48
+ exit 1
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ Dir[File.expand_path('../callbacks/**/*.rb', __FILE__)].sort_by{ |file| File.basename(file) }.each { |file| require file }
File without changes
@@ -0,0 +1,27 @@
1
+ module Kontena
2
+ module Callbacks
3
+ class ListAndSelectGrid < Kontena::Callback
4
+
5
+ include Kontena::Cli::Common
6
+
7
+ matches_commands 'master auth'
8
+
9
+ def after_load
10
+ command.class_eval do
11
+ option ['--skip-grid-auto-select'], :flag, 'Do not auto select grid'
12
+ end
13
+ end
14
+
15
+ # Runs kontena grids list --use which will auto join the first available
16
+ # grid
17
+ def after
18
+ return if command.skip_grid_auto_select?
19
+ return unless current_master
20
+ return unless command.exit_code == 0
21
+ return unless current_master.grid.nil?
22
+
23
+ Kontena.run('grid list --use --verbose')
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,20 @@
1
+ module Kontena
2
+ module Callbacks
3
+ class ClearCurrentMasterAfterTerminate < Kontena::Callback
4
+
5
+ include Kontena::Cli::Common
6
+
7
+ matches_commands 'master terminate'
8
+
9
+ def after
10
+ return unless command.exit_code == 0
11
+ return unless config.current_master
12
+
13
+ ENV["DEBUG"] && puts("Removing current master from config")
14
+ config.servers.delete_at(config.find_server_index(config.current_master.name))
15
+ config.current_server = nil
16
+ config.write
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+ module Kontena
2
+ module Callbacks
3
+ class DisplayLogoBeforeMasterDeploy < Kontena::Callback
4
+
5
+ include Kontena::Cli::Common
6
+
7
+ matches_commands 'master create'
8
+
9
+ def before
10
+ display_logo
11
+ end
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,124 @@
1
+ module Kontena
2
+ module Callbacks
3
+ class BeforeDeployConfigurationWizard < Kontena::Callback
4
+
5
+ include Kontena::Cli::Common
6
+
7
+ matches_commands 'master create'
8
+
9
+ def after_load
10
+ command.class_eval do
11
+ option ['--no-prompt'], :flag, "Don't ask questions"
12
+ option ['--skip-auth-provider'], :flag, "Skip auth provider configuration (single user mode)"
13
+ option ['--cloud-master-id'], '[ID]', "Use Kontena Cloud Master ID for auth provider configuration"
14
+ end
15
+ end
16
+
17
+ def unless_param(param, &block)
18
+ return if command.respond_to?(param) && !command.send(param).nil?
19
+ return if command.respond_to?("#{param}?".to_sym) && command.send("#{param}?".to_sym)
20
+ yield
21
+ end
22
+
23
+ # Scans config server names and returns default-2 if default exists,
24
+ # default-3 if default-2 exists, etc.
25
+ def next_default_name
26
+ last_default = config.servers.map(&:name).select{ |n| n =~ /default(?:\-\d+)?$/ }.sort.last
27
+ return "default" unless last_default
28
+ unless last_default =~ /\d$/
29
+ last_default << "-1"
30
+ end
31
+ last_default.succ
32
+ end
33
+
34
+ def login_to_kontena
35
+ if kontena_auth?
36
+ return true if cloud_client.authentication_ok?(kontena_account.userinfo_endpoint)
37
+ end
38
+ puts
39
+ puts "You don't seem to be logged in to Kontena Cloud"
40
+ puts
41
+ Kontena.run("cloud login --verbose")
42
+ cloud_client.authentication_ok?(kontena_account.userinfo_endpoint)
43
+ end
44
+
45
+ def select_cloud_master
46
+ masters = []
47
+ spinner "Retrieving a list of registered masters on your Kontena Cloud account" do
48
+ masters = cloud_client.get("user/masters")["data"]
49
+ end
50
+ if masters.empty?
51
+ puts
52
+ puts "You have no registered masters. Adding a new master."
53
+ puts
54
+ return create_cloud_master
55
+ else
56
+ puts
57
+ return prompt.select("Select master") do |menu|
58
+ masters.each do |master|
59
+ name = master['attributes']['name']
60
+ url = master['attributes']['url']
61
+ master_id = master['id']
62
+ menu.choice "#{master['attributes']['name']} (#{url.nil? ? "setup not done" : url})", master_id
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ def create_cloud_master
69
+ master_id = nil
70
+ spinner "Registering a new cloud master '#{command.name}'" do
71
+ master_id = Kontena.run("cloud master add --return #{command.name}", returning: :result)
72
+ end
73
+ if master_id.to_s =~ /^[0-9a-f]{16,32}$/
74
+ master_id
75
+ else
76
+ abort 'Cloud Master registration failed'
77
+ end
78
+ end
79
+
80
+ def before
81
+ unless_param(:name) do
82
+ if command.no_prompt?
83
+ command.name = next_default_name
84
+ else
85
+ command.name = prompt.ask("Enter a name for this Kontena Master: ", default: next_default_name, required: true) do |q|
86
+ q.validate /^[a-z0-9\_\-\.]+$/, 'Name should only include lower case letters, numbers and -._, example: "master-4"'
87
+ end
88
+ end
89
+ end
90
+
91
+ unless_param(:skip_auth_provider) do
92
+ if command.no_prompt?
93
+ command.cloud_master_id ||= create_cloud_master
94
+ elsif command.cloud_master_id.nil?
95
+ answer = prompt.select("Select user authentication provider") do |menu|
96
+ menu.choice 'Kontena Cloud (add a new master)', :kontena_new
97
+ menu.choice 'Kontena Cloud (use an existing master)', :kontena_existing
98
+ menu.choice 'Custom', :custom
99
+ menu.choice 'None (single user mode)', :none
100
+ end
101
+ case answer
102
+ when :kontena_new, :kontena_existing
103
+ login_to_kontena || abort('You must login to Kontena Cloud')
104
+ if answer == :kontena_new
105
+ command.cloud_master_id = create_cloud_master
106
+ else
107
+ command.cloud_master_id = select_cloud_master
108
+ end
109
+ when :custom
110
+ puts 'Learn how to configure custom user authentication provider after installation at: www.kontena.io/docs/configuring-custom-auth-provider'
111
+ when :none
112
+ 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/configuring-custom-auth-provider"
113
+ else
114
+ abort 'Should never be here'
115
+ end
116
+ end
117
+ command.skip_auth_provider = !command.cloud_master_id.nil?
118
+ end
119
+ end
120
+ end
121
+ end
122
+ end
123
+
124
+
@@ -0,0 +1,53 @@
1
+
2
+ module Kontena
3
+ module Callbacks
4
+ class AuthenticateAfterDeploy < Kontena::Callback
5
+
6
+ include Kontena::Cli::Common
7
+
8
+ matches_commands 'master create'
9
+
10
+ def after
11
+ ENV["DEBUG"] && puts("Command result: #{command.result.inspect}")
12
+ ENV["DEBUG"] && puts("Command exit code: #{command.exit_code.inspect}")
13
+ return unless command.exit_code == 0
14
+ return unless command.result.kind_of?(Hash)
15
+ return unless command.result.has_key?(:public_ip)
16
+ return unless command.result.has_key?(:code)
17
+ return unless command.result.has_key?(:name)
18
+
19
+ # In case there already is a server with the same name add random characters to name
20
+ if config.find_server(command.result[:name])
21
+ command.result[:name] = "#{command.result[:name]}-#{SecureRandom.hex(2)}"
22
+ end
23
+
24
+ new_master = Kontena::Cli::Config::Server.new(
25
+ url: "https://#{command.result[:public_ip]}",
26
+ name: command.result[:name]
27
+ )
28
+
29
+ retried = false
30
+
31
+ # Figure out if HTTPS works, if not, try HTTP
32
+ begin
33
+ ENV["DEBUG"] && puts("Trying to request / from #{new_master.url}")
34
+ client = Kontena::Client.new(new_master.url, nil, ignore_ssl_errors: true)
35
+ client.get('/')
36
+ rescue
37
+ ENV["DEBUG"] && puts("HTTPS test failed: #{$!} #{$!.message}")
38
+ unless retried
39
+ new_master.url = "http://#{command.result[:public_ip]}"
40
+ retried = true
41
+ retry
42
+ end
43
+ return
44
+ end
45
+
46
+ require 'shellwords'
47
+ cmd = "master login --verbose --name #{command.result[:name].shellescape} --code #{command.result[:code].shellescape} #{new_master.url.shellescape}"
48
+ ENV["DEBUG"] && puts("Running: #{cmd}")
49
+ Kontena.run(cmd)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,32 @@
1
+ module Kontena
2
+ module Callbacks
3
+ # Runs kontena master auth-provider config after deployment if
4
+ # auth provider configuration options have been supplied.
5
+ #
6
+ # Runs the config command with --preset kontena if the user has
7
+ # authenticated to Kontena unless --no-auth-config is used.
8
+ class CreateInitialGridAfterDeploy < Kontena::Callback
9
+
10
+ include Kontena::Cli::Common
11
+
12
+ matches_commands 'master create'
13
+
14
+ def after
15
+ return unless command.exit_code == 0
16
+ return unless config.current_master
17
+ return unless config.current_master.name == command.result[:name]
18
+
19
+ cmd = "grid create --silent test"
20
+ ENV["DEBUG"] && puts("Running: #{cmd}")
21
+
22
+ spinner "Creating initial grid 'test'" do
23
+ Kontena.run(cmd)
24
+ end
25
+
26
+ spinner "Selecting 'test' as current grid" do
27
+ Kontena.run("grid use test")
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,49 @@
1
+ module Kontena
2
+ module Callbacks
3
+ class ConfigureAuthProviderAfterDeploy < Kontena::Callback
4
+
5
+ include Kontena::Cli::Common
6
+
7
+ matches_commands 'master create'
8
+
9
+ def get_oauth_app_config(master_id)
10
+ attrs = nil
11
+ spinner "Retrieving master OAuth2 application settings from Kontena Cloud" do
12
+ attrs = cloud_client.get("user/masters/#{master_id}")["data"]["attributes"]
13
+ end
14
+ attrs
15
+ rescue
16
+ nil
17
+ end
18
+
19
+ def configure_auth_provider(oauth_config)
20
+ require 'shellwords'
21
+ spinner "Setting Kontena Cloud authentication provider base settings to Master config" do
22
+ Kontena.run("master config import --force --preset kontena_auth_provider")
23
+ end
24
+ spinner "Setting Kontena Cloud authentication provider consumer credentials to Master config" do
25
+ Kontena.run("master config set oauth2.client_id=#{oauth_config['client-id'].shellescape} oauth2.client_secret=#{oauth_config['client-secret'].shellescape} server.root_url=#{config.current_master.url.shellescape}")
26
+ end
27
+ end
28
+
29
+ def after
30
+ return unless command.exit_code == 0
31
+ return unless command.result.kind_of?(Hash)
32
+ return unless command.result.has_key?(:name)
33
+ return unless config.current_master
34
+ return unless config.current_master.name == command.result[:name]
35
+ if command.respond_to?(:skip_auth_provider) && command.skip_auth_provider?
36
+ return
37
+ end
38
+
39
+ if command.respond_to?(:cloud_master_id) && command.cloud_master_id
40
+ oauth_config = get_oauth_app_config(command.cloud_master_id)
41
+ if oauth_config
42
+ configure_auth_provider(oauth_config)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+