kontena-cli 1.1.6 → 1.2.0.dev1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -0
- data/VERSION +1 -1
- data/bin/kontena +8 -28
- data/kontena-cli.gemspec +2 -2
- data/lib/kontena/cli/app_command.rb +14 -27
- data/lib/kontena/cli/certificate_command.rb +4 -7
- data/lib/kontena/cli/cloud/master_command.rb +5 -12
- data/lib/kontena/cli/cloud_command.rb +4 -7
- data/lib/kontena/cli/container_command.rb +4 -9
- data/lib/kontena/cli/etcd/health_command.rb +1 -0
- data/lib/kontena/cli/etcd_command.rb +6 -13
- data/lib/kontena/cli/external_registry_command.rb +3 -7
- data/lib/kontena/cli/grid_command.rb +14 -29
- data/lib/kontena/cli/grids/cloud_config_command.rb +1 -0
- data/lib/kontena/cli/grids/common.rb +2 -0
- data/lib/kontena/cli/grids/create_command.rb +5 -0
- data/lib/kontena/cli/grids/trusted_subnet_command.rb +6 -8
- data/lib/kontena/cli/grids/user_command.rb +3 -6
- data/lib/kontena/cli/master/config_command.rb +6 -12
- data/lib/kontena/cli/master/token_command.rb +6 -11
- data/lib/kontena/cli/master/users/role_command.rb +2 -4
- data/lib/kontena/cli/master/users_command.rb +4 -8
- data/lib/kontena/cli/master_command.rb +14 -33
- data/lib/kontena/cli/node_command.rb +7 -15
- data/lib/kontena/cli/nodes/health_command.rb +1 -1
- data/lib/kontena/cli/nodes/label_command.rb +3 -9
- data/lib/kontena/cli/nodes/show_command.rb +1 -1
- data/lib/kontena/cli/nodes/ssh_command.rb +13 -4
- data/lib/kontena/cli/plugin_command.rb +5 -9
- data/lib/kontena/cli/registry_command.rb +2 -5
- data/lib/kontena/cli/service_command.rb +22 -45
- data/lib/kontena/cli/services/container_command.rb +2 -3
- data/lib/kontena/cli/services/env_command.rb +3 -6
- data/lib/kontena/cli/services/secret_command.rb +2 -4
- data/lib/kontena/cli/stack_command.rb +11 -24
- data/lib/kontena/cli/stacks/registry_command.rb +5 -12
- data/lib/kontena/cli/stacks/upgrade_command.rb +6 -0
- data/lib/kontena/cli/stacks/yaml/reader.rb +17 -2
- data/lib/kontena/cli/subcommand_loader.rb +82 -0
- data/lib/kontena/cli/vault_command.rb +7 -15
- data/lib/kontena/cli/version.rb +6 -1
- data/lib/kontena/cli/vpn_command.rb +3 -7
- data/lib/kontena/command.rb +28 -1
- data/lib/kontena/machine/cloud_config/cloudinit.yml +1 -1
- data/lib/kontena/main_command.rb +22 -38
- data/lib/kontena/scripts/completer +2 -233
- data/lib/kontena/scripts/completer.rb +230 -0
- data/lib/kontena/scripts/init +5 -8
- data/lib/kontena/scripts/kontena.bash +8 -0
- data/lib/kontena/scripts/kontena.zsh +11 -0
- data/lib/kontena_cli.rb +9 -1
- data/omnibus/wrappers/sh/kontena +1 -1
- data/spec/fixtures/stack-with-liquid-optional.yml +14 -0
- data/spec/fixtures/stack-with-liquid-undefined.yml +12 -0
- data/spec/kontena/cli/app/build_command_spec.rb +1 -2
- data/spec/kontena/cli/app/common_spec.rb +1 -2
- data/spec/kontena/cli/app/config_command_spec.rb +0 -1
- data/spec/kontena/cli/app/deploy_command_spec.rb +2 -3
- data/spec/kontena/cli/app/docker_helper_spec.rb +0 -1
- data/spec/kontena/cli/app/init_command_spec.rb +0 -1
- data/spec/kontena/cli/app/logs_command_spec.rb +0 -1
- data/spec/kontena/cli/app/scale_spec.rb +2 -3
- data/spec/kontena/cli/app/service_generator_spec.rb +1 -2
- data/spec/kontena/cli/app/service_generator_v2_spec.rb +0 -1
- data/spec/kontena/cli/app/yaml/reader_spec.rb +0 -1
- data/spec/kontena/cli/app/yaml/service_extender_spec.rb +0 -1
- data/spec/kontena/cli/app/yaml/validator_spec.rb +0 -1
- data/spec/kontena/cli/app/yaml/validator_v2_spec.rb +0 -1
- data/spec/kontena/cli/cloud/login_command_spec.rb +0 -1
- data/spec/kontena/cli/cloud/logout_command_spec.rb +0 -1
- data/spec/kontena/cli/cloud/master/add_command_spec.rb +0 -1
- data/spec/kontena/cli/common_spec.rb +0 -1
- data/spec/kontena/cli/containers/list_command_spec.rb +0 -1
- data/spec/kontena/cli/containers/logs_command_spec.rb +0 -1
- data/spec/kontena/cli/etcd/health_command_spec.rb +2 -0
- data/spec/kontena/cli/grids/trusted_subnets/add_command_spec.rb +0 -11
- data/spec/kontena/cli/grids/trusted_subnets/list_command_spec.rb +4 -13
- data/spec/kontena/cli/grids/trusted_subnets/remove_command_spec.rb +0 -11
- data/spec/kontena/cli/grids/use_command_spec.rb +0 -1
- data/spec/kontena/cli/helpers/log_helper_spec.rb +0 -1
- data/spec/kontena/cli/main_command_spec.rb +2 -3
- data/spec/kontena/cli/master/current_command_spec.rb +5 -15
- data/spec/kontena/cli/master/init_cloud_command_spec.rb +0 -1
- data/spec/kontena/cli/master/login_command_spec.rb +0 -1
- data/spec/kontena/cli/master/logout_command_spec.rb +0 -1
- data/spec/kontena/cli/master/use_command_spec.rb +0 -1
- data/spec/kontena/cli/master/users/invite_command_spec.rb +1 -5
- data/spec/kontena/cli/master/users/remove_command_spec.rb +2 -14
- data/spec/kontena/cli/master/users/roles/add_command_spec.rb +0 -1
- data/spec/kontena/cli/master/users/roles/remove_command_spec.rb +0 -1
- data/spec/kontena/cli/nodes/list_command_spec.rb +2 -0
- data/spec/kontena/cli/services/containers_command_spec.rb +0 -18
- data/spec/kontena/cli/services/exec_command_spec.rb +6 -4
- data/spec/kontena/cli/services/link_command_spec.rb +5 -19
- data/spec/kontena/cli/services/restart_command_spec.rb +0 -16
- data/spec/kontena/cli/services/secrets/link_command_spec.rb +0 -11
- data/spec/kontena/cli/services/secrets/unlink_command_spec.rb +1 -12
- data/spec/kontena/cli/services/services_helper_spec.rb +0 -1
- data/spec/kontena/cli/services/unlink_command_spec.rb +7 -21
- data/spec/kontena/cli/services/update_command_spec.rb +0 -15
- data/spec/kontena/cli/stacks/build_command_spec.rb +0 -1
- data/spec/kontena/cli/stacks/deploy_command_spec.rb +10 -13
- data/spec/kontena/cli/stacks/install_command_spec.rb +0 -15
- data/spec/kontena/cli/stacks/list_command_spec.rb +4 -10
- data/spec/kontena/cli/stacks/remove_command_spec.rb +1 -16
- data/spec/kontena/cli/stacks/service_generator_spec.rb +0 -1
- data/spec/kontena/cli/stacks/service_generator_v2_spec.rb +0 -1
- data/spec/kontena/cli/stacks/show_command_spec.rb +1 -14
- data/spec/kontena/cli/stacks/upgrade_command_spec.rb +19 -1
- data/spec/kontena/cli/stacks/yaml/reader_spec.rb +163 -130
- data/spec/kontena/cli/stacks/yaml/service_extender_spec.rb +0 -1
- data/spec/kontena/cli/stacks/yaml/validator_v3_spec.rb +0 -1
- data/spec/kontena/cli/vault/export_spec.rb +0 -1
- data/spec/kontena/cli/vault/import_spec.rb +4 -5
- data/spec/kontena/cli/version_command_spec.rb +8 -6
- data/spec/kontena/cli/vpn/create_command_spec.rb +3 -4
- data/spec/kontena/client_spec.rb +0 -1
- data/spec/kontena/config_spec.rb +0 -1
- data/spec/kontena/kontena_cli_spec.rb +1 -1
- data/spec/kontena/main_command_spec.rb +0 -1
- data/spec/kontena/plugin_manager_spec.rb +0 -1
- data/spec/spec_helper.rb +1 -0
- data/spec/support/client_helpers.rb +1 -1
- data/spec/support/exit_with_error_helper.rb +36 -0
- data/spec/support/fixtures_helpers.rb +5 -2
- metadata +18 -7
@@ -0,0 +1,82 @@
|
|
1
|
+
module Kontena::Cli
|
2
|
+
class SubcommandLoader
|
3
|
+
attr_reader :path
|
4
|
+
|
5
|
+
# Create a subcommand loader instance
|
6
|
+
#
|
7
|
+
# @param [String] path path to command definition
|
8
|
+
def initialize(path)
|
9
|
+
@path = path
|
10
|
+
end
|
11
|
+
|
12
|
+
# Takes something like /foo/bar/cli/master/foo_coimmand and returns [:Master, :FooCommand]
|
13
|
+
#
|
14
|
+
# @param path [String]
|
15
|
+
# @return [Array<Symbol>]
|
16
|
+
def symbolize_path(path)
|
17
|
+
path.gsub(/.*\/cli\//, '').split('/').map do |path_part|
|
18
|
+
path_part.split('_').map{ |e| e.capitalize }.join
|
19
|
+
end.map(&:to_sym)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Takes an array such as [:Foo] or [:Cli, :Foo] and returns [:Kontena, :Cli, :Foo]
|
23
|
+
def prepend_kontena_cli(tree)
|
24
|
+
[:Kontena, :Cli] + (tree - [:Cli])
|
25
|
+
end
|
26
|
+
|
27
|
+
# Takes an array such as [:Master, :FooCommand] and returns Master::FooCommand
|
28
|
+
#
|
29
|
+
# @param tree [Array<Symbol]
|
30
|
+
# @return [Class]
|
31
|
+
def const_get_tree(tree)
|
32
|
+
if tree.size == 1
|
33
|
+
Object.const_get(tree.first)
|
34
|
+
else
|
35
|
+
tree[1..-1].inject(Object.const_get(tree.first)) { |new_base, part| new_base.const_get(part) }
|
36
|
+
end
|
37
|
+
rescue
|
38
|
+
raise ArgumentError, "Can't figure out command class name from path #{path} - tried #{tree}"
|
39
|
+
end
|
40
|
+
|
41
|
+
# Tries to require a file, returns false instead of raising LoadError unless succesful
|
42
|
+
#
|
43
|
+
# @param path [String]
|
44
|
+
# @return [TrueClass,FalseClass]
|
45
|
+
def safe_require(path)
|
46
|
+
require path
|
47
|
+
true
|
48
|
+
rescue LoadError
|
49
|
+
false
|
50
|
+
end
|
51
|
+
|
52
|
+
def klass
|
53
|
+
return @subcommand_class if @subcommand_class
|
54
|
+
unless safe_require(path) || safe_require(Kontena.cli_root(path))
|
55
|
+
raise ArgumentError, "Can't load #{path} or #{Kontena.cli_root(path)}"
|
56
|
+
end
|
57
|
+
@subcommand_class = const_get_tree(prepend_kontena_cli(symbolize_path(path)))
|
58
|
+
end
|
59
|
+
|
60
|
+
def new(*args)
|
61
|
+
klass.new(*args)
|
62
|
+
end
|
63
|
+
|
64
|
+
def method_missing(meth, *args)
|
65
|
+
klass.send(meth, *args)
|
66
|
+
end
|
67
|
+
|
68
|
+
def respond_to_missing?(meth)
|
69
|
+
klass.respond_to?(meth)
|
70
|
+
end
|
71
|
+
|
72
|
+
def const_get(const)
|
73
|
+
klass.const_get(const)
|
74
|
+
end
|
75
|
+
|
76
|
+
def const_defined?(const)
|
77
|
+
klass.const_defined?(const)
|
78
|
+
end
|
79
|
+
|
80
|
+
alias_method :class, :klass
|
81
|
+
end
|
82
|
+
end
|
@@ -1,20 +1,12 @@
|
|
1
|
-
require_relative 'vault/export_command'
|
2
|
-
require_relative 'vault/import_command'
|
3
|
-
require_relative 'vault/list_command'
|
4
|
-
require_relative 'vault/read_command'
|
5
|
-
require_relative 'vault/remove_command'
|
6
|
-
require_relative 'vault/update_command'
|
7
|
-
require_relative 'vault/write_command'
|
8
|
-
|
9
1
|
class Kontena::Cli::VaultCommand < Kontena::Command
|
10
2
|
|
11
|
-
subcommand ["list", "ls"], "List secrets",
|
12
|
-
subcommand "write", "Write a secret",
|
13
|
-
subcommand "read", "Read secret",
|
14
|
-
subcommand "update", "Update secret",
|
15
|
-
subcommand ["remove", "rm"], "Remove secret",
|
16
|
-
subcommand "export", "Export secrets to STDOUT",
|
17
|
-
subcommand "import", "Import secrets from a file or STDIN",
|
3
|
+
subcommand ["list", "ls"], "List secrets", load_subcommand('vault/list_command')
|
4
|
+
subcommand "write", "Write a secret", load_subcommand('vault/write_command')
|
5
|
+
subcommand "read", "Read secret", load_subcommand('vault/read_command')
|
6
|
+
subcommand "update", "Update secret", load_subcommand('vault/update_command')
|
7
|
+
subcommand ["remove", "rm"], "Remove secret", load_subcommand('vault/remove_command')
|
8
|
+
subcommand "export", "Export secrets to STDOUT", load_subcommand('vault/export_command')
|
9
|
+
subcommand "import", "Import secrets from a file or STDIN", load_subcommand('vault/import_command')
|
18
10
|
|
19
11
|
def execute
|
20
12
|
end
|
data/lib/kontena/cli/version.rb
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
module Kontena
|
2
2
|
module Cli
|
3
|
-
|
3
|
+
unless const_defined?(:VERSION)
|
4
|
+
require 'pathname'
|
5
|
+
version_file = Pathname.new(__FILE__).dirname.join('../../../VERSION').realpath
|
6
|
+
is_head = ENV["KONTENA_EXTRA_BUILDTAGS"].to_s.include?('head')
|
7
|
+
VERSION = "#{version_file.read.strip}#{"-head" if is_head}"
|
8
|
+
end
|
4
9
|
end
|
5
10
|
end
|
@@ -1,12 +1,8 @@
|
|
1
|
-
require_relative 'vpn/create_command'
|
2
|
-
require_relative 'vpn/config_command'
|
3
|
-
require_relative 'vpn/remove_command'
|
4
|
-
|
5
1
|
class Kontena::Cli::VpnCommand < Kontena::Command
|
6
2
|
|
7
|
-
subcommand "create", "Create VPN service",
|
8
|
-
subcommand "config", "Show/Export VPN config",
|
9
|
-
subcommand ["remove", "rm"], "Remove VPN service",
|
3
|
+
subcommand "create", "Create VPN service", load_subcommand('vpn/create_command')
|
4
|
+
subcommand "config", "Show/Export VPN config", load_subcommand('vpn/config_command')
|
5
|
+
subcommand ["remove", "rm"], "Remove VPN service", load_subcommand('vpn/remove_command')
|
10
6
|
|
11
7
|
def execute
|
12
8
|
end
|
data/lib/kontena/command.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'clamp'
|
2
|
+
require_relative 'cli/subcommand_loader'
|
2
3
|
|
3
4
|
class Kontena::Command < Clamp::Command
|
4
5
|
|
@@ -62,6 +63,10 @@ class Kontena::Command < Clamp::Command
|
|
62
63
|
end
|
63
64
|
end
|
64
65
|
|
66
|
+
def self.load_subcommand(path)
|
67
|
+
Kontena::Cli::SubcommandLoader.new(path)
|
68
|
+
end
|
69
|
+
|
65
70
|
def self.inherited(where)
|
66
71
|
where.extend Finalizer
|
67
72
|
end
|
@@ -197,8 +202,30 @@ class Kontena::Command < Clamp::Command
|
|
197
202
|
run_callbacks :after unless help_requested?
|
198
203
|
exit(@exit_code) if @exit_code.to_i > 0
|
199
204
|
@result
|
205
|
+
rescue Excon::Errors::SocketError => exc
|
206
|
+
if exc.message.include?('Unable to verify certificate')
|
207
|
+
$stderr.puts " [#{Kontena.pastel.red('error')}] The server uses a certificate signed by an unknown authority."
|
208
|
+
$stderr.puts " You can trust this server by copying server CA pem file to: #{Kontena.pastel.yellow("~/.kontena/certs/<hostname>.pem")}"
|
209
|
+
$stderr.puts " Protip: you can bypass the certificate check by setting #{Kontena.pastel.yellow('SSL_IGNORE_ERRORS=true')} env variable, but any data you send to the server could be intercepted by others."
|
210
|
+
abort
|
211
|
+
else
|
212
|
+
abort(exc.message)
|
213
|
+
end
|
214
|
+
rescue Kontena::Errors::StandardError => exc
|
215
|
+
raise exc if ENV['DEBUG']
|
216
|
+
puts " [#{Kontena.pastel.red('error')}] #{exc.message}"
|
217
|
+
abort
|
218
|
+
rescue Errno::EPIPE
|
219
|
+
# If user is piping the command outputs to some other command that might exit before CLI has outputted everything
|
220
|
+
abort
|
221
|
+
rescue Clamp::HelpWanted, Clamp::UsageError
|
222
|
+
raise
|
223
|
+
rescue => exc
|
224
|
+
raise exc if ENV['DEBUG']
|
225
|
+
$stderr.puts " [#{Kontena.pastel.red('error')}] #{exc.message}"
|
226
|
+
$stderr.puts " Rerun the command with environment DEBUG=true set to get the full exception"
|
227
|
+
abort
|
200
228
|
end
|
201
|
-
|
202
229
|
end
|
203
230
|
|
204
231
|
require_relative 'callback'
|
@@ -11,7 +11,7 @@ write_files:
|
|
11
11
|
- path: /etc/systemd/system/docker.service.d/50-kontena.conf
|
12
12
|
content: |
|
13
13
|
[Service]
|
14
|
-
Environment='DOCKER_OPTS=--insecure-registry="
|
14
|
+
Environment='DOCKER_OPTS=--insecure-registry="<%= grid_subnet %>" --bip="<%= docker_bip %>"'
|
15
15
|
- path: /etc/sysctl.d/99-inotify.conf
|
16
16
|
owner: root
|
17
17
|
permissions: 0644
|
data/lib/kontena/main_command.rb
CHANGED
@@ -5,53 +5,37 @@ require_relative 'command'
|
|
5
5
|
require_relative 'callback'
|
6
6
|
require_relative 'cli/bytes_helper'
|
7
7
|
require_relative 'cli/grid_options'
|
8
|
-
require_relative 'cli/app_command'
|
9
|
-
require_relative 'cli/logout_command'
|
10
|
-
require_relative 'cli/whoami_command'
|
11
|
-
require_relative 'cli/container_command'
|
12
|
-
require_relative 'cli/grid_command'
|
13
|
-
require_relative 'cli/master_command'
|
14
|
-
require_relative 'cli/node_command'
|
15
|
-
require_relative 'cli/service_command'
|
16
|
-
require_relative 'cli/vpn_command'
|
17
|
-
require_relative 'cli/registry_command'
|
18
|
-
require_relative 'cli/external_registry_command'
|
19
|
-
require_relative 'cli/app_command'
|
20
|
-
require_relative 'cli/etcd_command'
|
21
|
-
require_relative 'cli/vault_command'
|
22
|
-
require_relative 'cli/plugin_command'
|
23
|
-
require_relative 'cli/version_command'
|
24
|
-
require_relative 'cli/stack_command'
|
25
|
-
require_relative 'cli/certificate_command'
|
26
|
-
require_relative 'cli/cloud_command'
|
27
8
|
|
28
9
|
class Kontena::MainCommand < Kontena::Command
|
29
10
|
include Kontena::Util
|
30
11
|
include Kontena::Cli::Common
|
31
12
|
|
32
13
|
option ['-v', '--version'], :flag, "Output Kontena CLI version #{Kontena::Cli::VERSION}" do
|
33
|
-
|
14
|
+
build_tags = [ 'ruby' + RUBY_VERSION ]
|
15
|
+
build_tags << RUBY_PLATFORM
|
16
|
+
build_tags += ENV["KONTENA_EXTRA_BUILDTAGS"].to_s.split(',')
|
17
|
+
puts ['kontena-cli', Kontena::Cli::VERSION, "[#{build_tags.join('+')}]"].join(' ')
|
34
18
|
exit 0
|
35
19
|
end
|
36
20
|
|
37
|
-
subcommand "cloud", "Kontena Cloud specific commands",
|
38
|
-
subcommand "logout", "Logout from Kontena Masters or Kontena Cloud accounts",
|
39
|
-
subcommand "grid", "Grid specific commands",
|
40
|
-
subcommand "app", "App specific commands",
|
41
|
-
subcommand "stack", "Stack specific commands",
|
42
|
-
subcommand "service", "Service specific commands",
|
43
|
-
subcommand "vault", "Vault specific commands",
|
44
|
-
subcommand "certificate", "LE Certificate specific commands",
|
45
|
-
subcommand "node", "Node specific commands",
|
46
|
-
subcommand "master", "Master specific commands",
|
47
|
-
subcommand "vpn", "VPN specific commands",
|
48
|
-
subcommand "registry", "Registry specific commands",
|
49
|
-
subcommand "container", "Container specific commands",
|
50
|
-
subcommand "etcd", "Etcd specific commands",
|
51
|
-
subcommand "external-registry", "External registry specific commands",
|
52
|
-
subcommand "whoami", "Shows current logged in user",
|
53
|
-
subcommand "plugin", "Plugin related commands",
|
54
|
-
subcommand "version", "Show version",
|
21
|
+
subcommand "cloud", "Kontena Cloud specific commands", load_subcommand('cloud_command')
|
22
|
+
subcommand "logout", "Logout from Kontena Masters or Kontena Cloud accounts", load_subcommand('logout_command')
|
23
|
+
subcommand "grid", "Grid specific commands", load_subcommand('grid_command')
|
24
|
+
subcommand "app", "App specific commands", load_subcommand('app_command')
|
25
|
+
subcommand "stack", "Stack specific commands", load_subcommand('stack_command')
|
26
|
+
subcommand "service", "Service specific commands", load_subcommand('service_command')
|
27
|
+
subcommand "vault", "Vault specific commands", load_subcommand('vault_command')
|
28
|
+
subcommand "certificate", "LE Certificate specific commands", load_subcommand('certificate_command')
|
29
|
+
subcommand "node", "Node specific commands", load_subcommand('node_command')
|
30
|
+
subcommand "master", "Master specific commands", load_subcommand('master_command')
|
31
|
+
subcommand "vpn", "VPN specific commands", load_subcommand('vpn_command')
|
32
|
+
subcommand "registry", "Registry specific commands", load_subcommand('registry_command')
|
33
|
+
subcommand "container", "Container specific commands", load_subcommand('container_command')
|
34
|
+
subcommand "etcd", "Etcd specific commands", load_subcommand('etcd_command')
|
35
|
+
subcommand "external-registry", "External registry specific commands", load_subcommand('external_registry_command')
|
36
|
+
subcommand "whoami", "Shows current logged in user", load_subcommand('whoami_command')
|
37
|
+
subcommand "plugin", "Plugin related commands", load_subcommand('plugin_command')
|
38
|
+
subcommand "version", "Show CLI and current master version", load_subcommand('version_command')
|
55
39
|
|
56
40
|
def execute
|
57
41
|
end
|
@@ -3,238 +3,7 @@
|
|
3
3
|
|
4
4
|
# resolve bin path, ignoring symlinks
|
5
5
|
require 'pathname'
|
6
|
-
require 'yaml'
|
7
6
|
bin_file = Pathname.new(__FILE__).realpath
|
7
|
+
$:.unshift File.expand_path('../../..', bin_file)
|
8
8
|
|
9
|
-
|
10
|
-
$:.unshift File.expand_path('../../lib', bin_file)
|
11
|
-
|
12
|
-
require_relative '../client'
|
13
|
-
require_relative '../cli/common'
|
14
|
-
|
15
|
-
class Helper
|
16
|
-
include Kontena::Cli::Common
|
17
|
-
|
18
|
-
def client
|
19
|
-
token = require_token
|
20
|
-
super(token)
|
21
|
-
end
|
22
|
-
|
23
|
-
def grids
|
24
|
-
client.get("grids")['grids'].map{|grid| grid['id']}
|
25
|
-
rescue
|
26
|
-
[]
|
27
|
-
end
|
28
|
-
|
29
|
-
def nodes
|
30
|
-
client.get("grids/#{current_grid}/nodes")['nodes'].map{|node| node['name']}
|
31
|
-
rescue
|
32
|
-
[]
|
33
|
-
end
|
34
|
-
|
35
|
-
def stacks
|
36
|
-
stacks = client.get("grids/#{current_grid}/stacks")['stacks']
|
37
|
-
results = []
|
38
|
-
results.push stacks.map{|s| s['name']}
|
39
|
-
results.delete('null')
|
40
|
-
results
|
41
|
-
rescue
|
42
|
-
[]
|
43
|
-
end
|
44
|
-
|
45
|
-
def services
|
46
|
-
services = client.get("grids/#{current_grid}/services")['services']
|
47
|
-
results = []
|
48
|
-
results.push services.map{ |s|
|
49
|
-
stack = s['stack']['id'].split('/').last
|
50
|
-
if stack != 'null'
|
51
|
-
"#{stack}/#{s['name']}"
|
52
|
-
else
|
53
|
-
s['name']
|
54
|
-
end
|
55
|
-
}
|
56
|
-
results
|
57
|
-
rescue
|
58
|
-
[]
|
59
|
-
end
|
60
|
-
|
61
|
-
def containers
|
62
|
-
results = []
|
63
|
-
client.get("grids/#{current_grid}/services")['services'].each do |service|
|
64
|
-
containers = client.get("services/#{service['id']}/containers")['containers']
|
65
|
-
results.push(containers.map{|c| c['name'] })
|
66
|
-
results.push(containers.map{|c| c['id'] })
|
67
|
-
end
|
68
|
-
results
|
69
|
-
rescue
|
70
|
-
[]
|
71
|
-
end
|
72
|
-
|
73
|
-
def yml_services
|
74
|
-
if File.exist?('kontena.yml')
|
75
|
-
yaml = YAML.safe_load(File.read('kontena.yml'))
|
76
|
-
services = yaml['services']
|
77
|
-
services.keys
|
78
|
-
end
|
79
|
-
rescue
|
80
|
-
[]
|
81
|
-
end
|
82
|
-
|
83
|
-
def yml_files
|
84
|
-
Dir["./*.yml"].map{|file| file.sub('./', '')}
|
85
|
-
rescue
|
86
|
-
[]
|
87
|
-
end
|
88
|
-
|
89
|
-
def master_names
|
90
|
-
config_file = File.expand_path('~/.kontena_client.json')
|
91
|
-
if(File.exist?(config_file))
|
92
|
-
config = JSON.parse(File.read(config_file))
|
93
|
-
return config['servers'].map{|s| s['name']}
|
94
|
-
end
|
95
|
-
rescue
|
96
|
-
[]
|
97
|
-
end
|
98
|
-
|
99
|
-
end
|
100
|
-
|
101
|
-
helper = Helper.new
|
102
|
-
|
103
|
-
words = ARGV
|
104
|
-
words.delete_at(0)
|
105
|
-
|
106
|
-
completion = []
|
107
|
-
completion.push %w(cloud logout grid app service stack vault certificate node master vpn registry container etcd external-registry whoami plugin version) if words.size < 2
|
108
|
-
if words.size > 0
|
109
|
-
case words[0]
|
110
|
-
when 'plugin'
|
111
|
-
completion.clear
|
112
|
-
sub_commands = %w(list ls search install uninstall)
|
113
|
-
if words[1]
|
114
|
-
completion.push(sub_commands) unless sub_commands.include?(words[1])
|
115
|
-
else
|
116
|
-
completion.push sub_commands
|
117
|
-
end
|
118
|
-
when 'etcd'
|
119
|
-
completion.clear
|
120
|
-
sub_commands = %w(get set mkdir mk list ls rm)
|
121
|
-
if words[1]
|
122
|
-
completion.push(sub_commands) unless sub_commands.include?(words[1])
|
123
|
-
else
|
124
|
-
completion.push sub_commands
|
125
|
-
end
|
126
|
-
when 'registry'
|
127
|
-
completion.clear
|
128
|
-
sub_commands = %w(create remove rm)
|
129
|
-
if words[1]
|
130
|
-
completion.push(sub_commands) unless sub_commands.include?(words[1])
|
131
|
-
else
|
132
|
-
completion.push sub_commands
|
133
|
-
end
|
134
|
-
when 'grid'
|
135
|
-
completion.clear
|
136
|
-
sub_commands = %w(add-user audit-log create current list user remove show use)
|
137
|
-
if words[1]
|
138
|
-
completion.push(sub_commands) unless sub_commands.include?(words[1])
|
139
|
-
completion.push helper.grids
|
140
|
-
else
|
141
|
-
completion.push sub_commands
|
142
|
-
end
|
143
|
-
when 'node'
|
144
|
-
completion.clear
|
145
|
-
sub_commands = %w(list show remove)
|
146
|
-
if words[1]
|
147
|
-
completion.push(sub_commands) unless sub_commands.include?(words[1])
|
148
|
-
completion.push helper.nodes
|
149
|
-
else
|
150
|
-
completion.push sub_commands
|
151
|
-
end
|
152
|
-
when 'master'
|
153
|
-
completion.clear
|
154
|
-
sub_commands = %w(list use users current remove rm config cfg login logout token join audit-log init-cloud)
|
155
|
-
if words[1] && words[1] == 'use'
|
156
|
-
completion.push helper.master_names
|
157
|
-
elsif words[1] && words[1] == 'users'
|
158
|
-
users_sub_commands = %(invite list role)
|
159
|
-
completion.push users_sub_commands
|
160
|
-
elsif words[1] && ['config', 'cfg'].include?(words[1])
|
161
|
-
config_sub_commands = %(set get dump load import export unset)
|
162
|
-
completion.push config_sub_commands
|
163
|
-
elsif words[1] && words[1] == 'token'
|
164
|
-
token_sub_commands = %(list ls rm remove show current create)
|
165
|
-
completion.push token_sub_commands
|
166
|
-
elsif words[1]
|
167
|
-
completion.push(sub_commands) unless sub_commands.include?(words[1])
|
168
|
-
else
|
169
|
-
completion.push sub_commands
|
170
|
-
end
|
171
|
-
when 'cloud'
|
172
|
-
completion.clear
|
173
|
-
sub_commands = %w(login logout master)
|
174
|
-
if words[1] && words[1] == 'master'
|
175
|
-
cloud_master_sub_commands = %(list ls remove rm add show update)
|
176
|
-
completion.push cloud_master_sub_commands
|
177
|
-
elsif words[1]
|
178
|
-
completion.push(sub_commands) unless sub_commands.include?(words[1])
|
179
|
-
else
|
180
|
-
completion.push sub_commands
|
181
|
-
end
|
182
|
-
when 'service'
|
183
|
-
completion.clear
|
184
|
-
sub_commands = %w(containers create delete deploy list logs restart
|
185
|
-
scale show start stats stop update monitor env
|
186
|
-
secret link unlink)
|
187
|
-
if words[1]
|
188
|
-
completion.push(sub_commands) unless sub_commands.include?(words[1])
|
189
|
-
completion.push helper.services
|
190
|
-
else
|
191
|
-
completion.push sub_commands
|
192
|
-
end
|
193
|
-
when 'container'
|
194
|
-
completion.clear
|
195
|
-
sub_commands = %w(exec inspect logs)
|
196
|
-
if words[1]
|
197
|
-
completion.push(sub_commands) unless sub_commands.include?(words[1])
|
198
|
-
completion.push helper.containers
|
199
|
-
else
|
200
|
-
completion.push sub_commands
|
201
|
-
end
|
202
|
-
when 'vpn'
|
203
|
-
completion.clear
|
204
|
-
completion.push %w(config create delete)
|
205
|
-
when 'external-registry'
|
206
|
-
completion.clear
|
207
|
-
completion.push %w(add list delete)
|
208
|
-
when 'app'
|
209
|
-
completion.clear
|
210
|
-
sub_commands = %w(init build config deploy start stop remove rm ps list
|
211
|
-
logs monitor show)
|
212
|
-
if words[1]
|
213
|
-
completion.push(sub_commands) unless sub_commands.include?(words[1])
|
214
|
-
completion.push helper.yml_services
|
215
|
-
else
|
216
|
-
completion.push sub_commands
|
217
|
-
end
|
218
|
-
when 'stack'
|
219
|
-
completion.clear
|
220
|
-
sub_commands = %w(build install upgrade deploy start stop remove rm ls list
|
221
|
-
logs monitor show registry)
|
222
|
-
if words[1]
|
223
|
-
if words[1] == 'registry'
|
224
|
-
registry_sub_commands = %(push pull search show rm)
|
225
|
-
completion.push registry_sub_commands
|
226
|
-
elsif %w(install).include?(words[1])
|
227
|
-
completion.push helper.yml_files
|
228
|
-
elsif words[1] == 'upgrade' && words[3]
|
229
|
-
completion.push helper.yml_files
|
230
|
-
else
|
231
|
-
completion.push(sub_commands) unless sub_commands.include?(words[1])
|
232
|
-
completion.push helper.stacks
|
233
|
-
end
|
234
|
-
else
|
235
|
-
completion.push sub_commands
|
236
|
-
end
|
237
|
-
end
|
238
|
-
end
|
239
|
-
|
240
|
-
puts completion
|
9
|
+
require 'kontena/scripts/completer'
|