kontena-cli 0.11.7 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (137) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/bin/kontena +2 -4
  4. data/kontena-cli.gemspec +1 -0
  5. data/lib/kontena/cli/app_command.rb +1 -1
  6. data/lib/kontena/cli/apps/common.rb +30 -3
  7. data/lib/kontena/cli/apps/deploy_command.rb +1 -0
  8. data/lib/kontena/cli/apps/list_command.rb +1 -0
  9. data/lib/kontena/cli/apps/logs_command.rb +9 -3
  10. data/lib/kontena/cli/apps/monitor_command.rb +1 -0
  11. data/lib/kontena/cli/apps/remove_command.rb +2 -0
  12. data/lib/kontena/cli/apps/scale_command.rb +2 -1
  13. data/lib/kontena/cli/apps/show_command.rb +1 -0
  14. data/lib/kontena/cli/apps/start_command.rb +1 -0
  15. data/lib/kontena/cli/apps/stop_command.rb +1 -0
  16. data/lib/kontena/cli/common.rb +5 -1
  17. data/lib/kontena/cli/containers/exec_command.rb +1 -0
  18. data/lib/kontena/cli/containers/inspect_command.rb +1 -0
  19. data/lib/kontena/cli/etcd/get_command.rb +2 -1
  20. data/lib/kontena/cli/etcd/list_command.rb +1 -0
  21. data/lib/kontena/cli/etcd/mkdir_command.rb +2 -1
  22. data/lib/kontena/cli/etcd/remove_command.rb +2 -1
  23. data/lib/kontena/cli/etcd/set_command.rb +2 -1
  24. data/lib/kontena/cli/etcd_command.rb +2 -2
  25. data/lib/kontena/cli/external_registries/add_command.rb +1 -0
  26. data/lib/kontena/cli/external_registries/delete_command.rb +2 -0
  27. data/lib/kontena/cli/external_registries/list_command.rb +1 -0
  28. data/lib/kontena/cli/external_registries/remove_command.rb +13 -0
  29. data/lib/kontena/cli/external_registry_command.rb +4 -3
  30. data/lib/kontena/cli/grid_command.rb +5 -1
  31. data/lib/kontena/cli/grid_options.rb +12 -0
  32. data/lib/kontena/cli/grids/add_user_command.rb +1 -0
  33. data/lib/kontena/cli/grids/cloud_config_command.rb +41 -0
  34. data/lib/kontena/cli/grids/common.rb +65 -4
  35. data/lib/kontena/cli/grids/logs_command.rb +9 -4
  36. data/lib/kontena/cli/grids/remove_user_command.rb +1 -0
  37. data/lib/kontena/cli/grids/update_command.rb +27 -0
  38. data/lib/kontena/cli/master/use_command.rb +12 -2
  39. data/lib/kontena/cli/master/users/add_role_command.rb +26 -0
  40. data/lib/kontena/cli/master/users/invite_command.rb +24 -0
  41. data/lib/kontena/cli/master/users/list_command.rb +18 -0
  42. data/lib/kontena/cli/master/users/remove_role_command.rb +26 -0
  43. data/lib/kontena/cli/master/users_command.rb +14 -0
  44. data/lib/kontena/cli/master_command.rb +2 -1
  45. data/lib/kontena/cli/node_command.rb +7 -1
  46. data/lib/kontena/cli/nodes/add_label_command.rb +19 -0
  47. data/lib/kontena/cli/nodes/aws/create_command.rb +2 -1
  48. data/lib/kontena/cli/nodes/aws/restart_command.rb +1 -0
  49. data/lib/kontena/cli/nodes/aws/terminate_command.rb +1 -0
  50. data/lib/kontena/cli/nodes/azure/create_command.rb +1 -0
  51. data/lib/kontena/cli/nodes/azure/restart_command.rb +1 -0
  52. data/lib/kontena/cli/nodes/azure/terminate_command.rb +1 -0
  53. data/lib/kontena/cli/nodes/digital_ocean/create_command.rb +1 -0
  54. data/lib/kontena/cli/nodes/digital_ocean/restart_command.rb +1 -0
  55. data/lib/kontena/cli/nodes/digital_ocean/terminate_command.rb +1 -0
  56. data/lib/kontena/cli/nodes/list_command.rb +10 -14
  57. data/lib/kontena/cli/nodes/remove_command.rb +1 -0
  58. data/lib/kontena/cli/nodes/remove_label_command.rb +23 -0
  59. data/lib/kontena/cli/nodes/show_command.rb +1 -0
  60. data/lib/kontena/cli/nodes/ssh_command.rb +1 -0
  61. data/lib/kontena/cli/nodes/update_command.rb +1 -0
  62. data/lib/kontena/cli/nodes/vagrant/create_command.rb +1 -0
  63. data/lib/kontena/cli/nodes/vagrant/restart_command.rb +1 -0
  64. data/lib/kontena/cli/nodes/vagrant/ssh_command.rb +1 -0
  65. data/lib/kontena/cli/nodes/vagrant/start_command.rb +1 -0
  66. data/lib/kontena/cli/nodes/vagrant/stop_command.rb +1 -0
  67. data/lib/kontena/cli/nodes/vagrant/terminate_command.rb +1 -0
  68. data/lib/kontena/cli/registry/create_command.rb +80 -30
  69. data/lib/kontena/cli/registry/delete_command.rb +3 -0
  70. data/lib/kontena/cli/registry/remove_command.rb +15 -0
  71. data/lib/kontena/cli/registry_command.rb +3 -1
  72. data/lib/kontena/cli/service_command.rb +20 -4
  73. data/lib/kontena/cli/services/add_env_command.rb +1 -0
  74. data/lib/kontena/cli/services/add_secret_command.rb +24 -0
  75. data/lib/kontena/cli/services/containers_command.rb +1 -0
  76. data/lib/kontena/cli/services/create_command.rb +3 -2
  77. data/lib/kontena/cli/services/delete_command.rb +2 -0
  78. data/lib/kontena/cli/services/deploy_command.rb +1 -0
  79. data/lib/kontena/cli/services/envs_command.rb +1 -0
  80. data/lib/kontena/cli/services/link_command.rb +26 -0
  81. data/lib/kontena/cli/services/list_command.rb +13 -10
  82. data/lib/kontena/cli/services/logs_command.rb +13 -5
  83. data/lib/kontena/cli/services/monitor_command.rb +1 -0
  84. data/lib/kontena/cli/services/remove_command.rb +17 -0
  85. data/lib/kontena/cli/services/remove_env_command.rb +1 -0
  86. data/lib/kontena/cli/services/remove_secret_command.rb +28 -0
  87. data/lib/kontena/cli/services/restart_command.rb +1 -0
  88. data/lib/kontena/cli/services/scale_command.rb +2 -1
  89. data/lib/kontena/cli/services/show_command.rb +1 -0
  90. data/lib/kontena/cli/services/start_command.rb +1 -0
  91. data/lib/kontena/cli/services/stats_command.rb +5 -4
  92. data/lib/kontena/cli/services/stop_command.rb +1 -0
  93. data/lib/kontena/cli/services/unlink_command.rb +25 -0
  94. data/lib/kontena/cli/services/update_command.rb +4 -3
  95. data/lib/kontena/cli/users_command.rb +15 -0
  96. data/lib/kontena/cli/vault/list_command.rb +1 -0
  97. data/lib/kontena/cli/vault/read_command.rb +1 -0
  98. data/lib/kontena/cli/vault/remove_command.rb +1 -0
  99. data/lib/kontena/cli/vault/write_command.rb +1 -0
  100. data/lib/kontena/cli/version_command.rb +9 -1
  101. data/lib/kontena/cli/vpn/config_command.rb +1 -0
  102. data/lib/kontena/cli/vpn/create_command.rb +1 -0
  103. data/lib/kontena/cli/vpn/delete_command.rb +2 -0
  104. data/lib/kontena/cli/vpn/remove_command.rb +15 -0
  105. data/lib/kontena/cli/vpn_command.rb +3 -1
  106. data/lib/kontena/machine/aws/node_destroyer.rb +1 -1
  107. data/lib/kontena/machine/cloud_config/cloudinit.yml +70 -0
  108. data/lib/kontena/machine/cloud_config/node_generator.rb +28 -0
  109. data/lib/kontena/machine/digital_ocean/cloudinit.yml +22 -0
  110. data/lib/kontena/machine/digital_ocean/node_destroyer.rb +4 -2
  111. data/lib/kontena/machine/random_name.rb +3 -3
  112. data/lib/kontena/scripts/completer +7 -3
  113. data/spec/fixtures/docker-compose.yml +8 -0
  114. data/spec/fixtures/kontena.yml +17 -0
  115. data/spec/fixtures/mysql.yml +3 -0
  116. data/spec/fixtures/wordpress-scaled.yml +3 -0
  117. data/spec/fixtures/wordpress.yml +2 -0
  118. data/spec/kontena/cli/app/common_spec.rb +42 -0
  119. data/spec/kontena/cli/app/deploy_command_spec.rb +4 -31
  120. data/spec/kontena/cli/app/scale_spec.rb +4 -23
  121. data/spec/kontena/cli/master/use_command_spec.rb +12 -3
  122. data/spec/kontena/cli/master/users/add_role_command_spec.rb +35 -0
  123. data/spec/kontena/cli/master/users/invite_command_spec.rb +35 -0
  124. data/spec/kontena/cli/master/users/remove_role_command_spec.rb +35 -0
  125. data/spec/kontena/cli/services/add_secret_command_spec.rb +61 -0
  126. data/spec/kontena/cli/services/link_command_spec.rb +43 -0
  127. data/spec/kontena/cli/services/remove_secret_command_spec.rb +48 -0
  128. data/spec/kontena/cli/services/restart_command_spec.rb +1 -23
  129. data/spec/kontena/cli/services/unlink_command_spec.rb +43 -0
  130. data/spec/kontena/cli/version_command_spec.rb +17 -0
  131. data/spec/spec_helper.rb +6 -1
  132. data/spec/support/client_helpers.rb +31 -0
  133. data/spec/support/fixtures_helpers.rb +7 -0
  134. metadata +67 -6
  135. data/lib/kontena/cli/deploy_command.rb +0 -164
  136. data/lib/kontena/cli/invite_command.rb +0 -13
  137. data/spec/kontena/cli/deploy_command_spec.rb +0 -239
@@ -1,6 +1,7 @@
1
1
  module Kontena::Cli::ExternalRegistries
2
2
  class ListCommand < Clamp::Command
3
3
  include Kontena::Cli::Common
4
+ include Kontena::Cli::GridOptions
4
5
 
5
6
  def execute
6
7
  require_api_url
@@ -0,0 +1,13 @@
1
+ module Kontena::Cli::ExternalRegistries
2
+ class RemoveCommand < Clamp::Command
3
+ include Kontena::Cli::Common
4
+
5
+ parameter "NAME", "External registry name to remove"
6
+
7
+ def execute
8
+ require_api_url
9
+ token = require_token
10
+ client(token).delete("external_registries/#{current_grid}/#{name}")
11
+ end
12
+ end
13
+ end
@@ -1,13 +1,14 @@
1
1
  require_relative 'external_registries/add_command'
2
2
  require_relative 'external_registries/list_command'
3
3
  require_relative 'external_registries/delete_command'
4
-
4
+ require_relative 'external_registries/remove_command'
5
5
 
6
6
  class Kontena::Cli::ExternalRegistryCommand < Clamp::Command
7
7
 
8
8
  subcommand "add", "Add external Docker image registry", Kontena::Cli::ExternalRegistries::AddCommand
9
- subcommand "list", "List external Docker image registries", Kontena::Cli::ExternalRegistries::ListCommand
10
- subcommand "delete", "Delete external Docker image registry", Kontena::Cli::ExternalRegistries::DeleteCommand
9
+ subcommand ["list", "ls"], "List external Docker image registries", Kontena::Cli::ExternalRegistries::ListCommand
10
+ subcommand ["remove", "rm"], "Remove external Docker image registry", Kontena::Cli::ExternalRegistries::RemoveCommand
11
+ subcommand "delete", "[DEPRECATED] Delete external Docker image registry", Kontena::Cli::ExternalRegistries::DeleteCommand
11
12
 
12
13
  def execute
13
14
  end
@@ -1,5 +1,6 @@
1
1
  require_relative 'grids/list_command'
2
2
  require_relative 'grids/create_command'
3
+ require_relative 'grids/update_command'
3
4
  require_relative 'grids/use_command'
4
5
  require_relative 'grids/show_command'
5
6
  require_relative 'grids/logs_command'
@@ -10,11 +11,13 @@ require_relative 'grids/audit_log_command'
10
11
  require_relative 'grids/list_users_command'
11
12
  require_relative 'grids/add_user_command'
12
13
  require_relative 'grids/remove_user_command'
14
+ require_relative 'grids/cloud_config_command'
13
15
 
14
16
  class Kontena::Cli::GridCommand < Clamp::Command
15
17
 
16
- subcommand "list", "List all grids", Kontena::Cli::Grids::ListCommand
18
+ subcommand ["list","ls"], "List all grids", Kontena::Cli::Grids::ListCommand
17
19
  subcommand "create", "Create a new grid", Kontena::Cli::Grids::CreateCommand
20
+ subcommand "update", "Update grid", Kontena::Cli::Grids::UpdateCommand
18
21
  subcommand "use", "Switch to use specific grid", Kontena::Cli::Grids::UseCommand
19
22
  subcommand "show", "Show grid details", Kontena::Cli::Grids::ShowCommand
20
23
  subcommand "logs", "Show logs from grid containers", Kontena::Cli::Grids::LogsCommand
@@ -25,6 +28,7 @@ class Kontena::Cli::GridCommand < Clamp::Command
25
28
  subcommand "list-users", "List current grid users", Kontena::Cli::Grids::ListUsersCommand
26
29
  subcommand "add-user", "Add user to the current grid", Kontena::Cli::Grids::AddUserCommand
27
30
  subcommand "remove-user", "Remove user from the current grid", Kontena::Cli::Grids::RemoveUserCommand
31
+ subcommand "cloud-config", "Generate cloud-config", Kontena::Cli::Grids::CloudConfigCommand
28
32
 
29
33
  def execute
30
34
  end
@@ -0,0 +1,12 @@
1
+ module Kontena
2
+ module Cli
3
+ module GridOptions
4
+
5
+ def self.included(base)
6
+ if base.respond_to?(:option)
7
+ base.option '--grid', 'GRID', 'Specify grid to use'
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -3,6 +3,7 @@ require_relative 'common'
3
3
  module Kontena::Cli::Grids
4
4
  class AddUserCommand < Clamp::Command
5
5
  include Kontena::Cli::Common
6
+ include Kontena::Cli::GridOptions
6
7
  include Common
7
8
 
8
9
  parameter "EMAIL", "Email address"
@@ -0,0 +1,41 @@
1
+ require_relative 'common'
2
+
3
+ module Kontena::Cli::Grids
4
+ class CloudConfigCommand < Clamp::Command
5
+ include Kontena::Cli::Common
6
+ include Common
7
+
8
+ parameter "NAME", "Grid name"
9
+ option "--dns", "DNS", "DNS server", multivalued: true
10
+ option "--peer-interface", "IFACE", "Peer (private) network interface", default: "eth1"
11
+ option "--docker-bip", "BIP", "Docker bridge ip", default: "172.17.43.1/16"
12
+ option "--version", "VERSION", "Agent version", default: "latest"
13
+
14
+ def execute
15
+ require_api_url
16
+ token = require_token
17
+
18
+ grid = find_grid_by_name(name)
19
+ abort("Grid not found".colorize(:red)) unless grid
20
+
21
+ default_dns = docker_bip.split('/')[0]
22
+ if dns_list.size > 0
23
+ dns_servers = [default_dns] + dns_list
24
+ else
25
+ dns_servers = [default_dns, '8.8.8.8', '8.8.4.4']
26
+ end
27
+
28
+ require 'kontena/machine/cloud_config/node_generator'
29
+ generator = Kontena::Machine::CloudConfig::NodeGenerator.new
30
+ config = generator.generate(
31
+ master_uri: api_url,
32
+ grid_token: grid['token'],
33
+ peer_interface: peer_interface,
34
+ dns_servers: dns_servers,
35
+ docker_bip: docker_bip,
36
+ version: version
37
+ )
38
+ puts config
39
+ end
40
+ end
41
+ end
@@ -7,18 +7,79 @@ module Kontena::Cli::Grids
7
7
  puts "#{grid['name']}:"
8
8
  puts " uri: #{self.current_master['url'].sub('http', 'ws')}"
9
9
  puts " token: #{grid['token']}"
10
- puts " users: #{grid['user_count']}"
11
- puts " nodes: #{grid['node_count']}"
12
- puts " services: #{grid['service_count']}"
13
- puts " containers: #{grid['container_count']}"
10
+ root_dir = grid['engine_root_dir']
11
+ nodes = client(require_token).get("grids/#{grid['name']}/nodes")
12
+ nodes = nodes['nodes'].select{|n| n['connected'] == true }
13
+ node_count = nodes.size
14
+ puts " stats:"
15
+ puts " nodes: #{nodes.size} of #{grid['node_count']}"
16
+
17
+ cpu_total = nodes.map{|n| n['cpus'].to_i}.inject(:+)
18
+ puts " cpus: #{cpu_total || 0}"
19
+
20
+ loads = calculate_loads(nodes, node_count)
21
+ puts " load: #{(loads[:'1m'] || 0.0).round(2)} #{(loads[:'5m'] || 0.0).round(2)} #{(loads[:'15m'] || 0.0).round(2)}"
22
+
23
+ mem_total = nodes.map{|n| n['mem_total'].to_i}.inject(:+)
24
+ mem_wired = nodes.map{|n|
25
+ n.dig('resource_usage', 'memory', 'active').to_f
26
+ }.inject(:+)
27
+ puts " memory: #{to_gigabytes(mem_wired)} of #{to_gigabytes(mem_total)} GB"
28
+
29
+ total_fs = calculate_filesystem_stats(nodes)
30
+ puts " filesystem: #{to_gigabytes(total_fs['used'])} of #{to_gigabytes(total_fs['total'])} GB"
31
+
32
+ puts " users: #{grid['user_count']}"
33
+ puts " services: #{grid['service_count']}"
34
+ puts " containers: #{grid['container_count']}"
35
+ if statsd = grid.dig('stats', 'statsd')
36
+ puts " exports:"
37
+ puts " statsd: #{statsd['server']}:#{statsd['port']}"
38
+ end
14
39
  end
15
40
 
16
41
  def grids
17
42
  @grids ||= client(require_token).get('grids')
18
43
  end
19
44
 
45
+ # @param [Array<Hash>] nodes
46
+ # @param [Fixnum] node_count
47
+ # @return [Hash]
48
+ def calculate_loads(nodes, node_count)
49
+ loads = {:'1m' => 0.0, :'5m' => 0.0, :'15m' => 0.0}
50
+ return loads if node_count == 0
51
+
52
+ loads[:'1m'] = nodes.map{|n| n.dig('resource_usage', 'load', '1m').to_f }.inject(:+) / node_count
53
+ loads[:'5m'] = nodes.map{|n| n.dig('resource_usage', 'load', '5m').to_f }.inject(:+) / node_count
54
+ loads[:'15m'] = nodes.map{|n| n.dig('resource_usage', 'load', '15m').to_f }.inject(:+) / node_count
55
+ loads
56
+ end
57
+
58
+ # @param [Array<Hash>] nodes
59
+ # @return [Hash]
60
+ def calculate_filesystem_stats(nodes)
61
+ total_fs = {
62
+ 'used' => 0.0,
63
+ 'total' => 0.0
64
+ }
65
+ nodes.each do |node|
66
+ root_dir = node['engine_root_dir']
67
+ filesystems = node.dig('resource_usage', 'filesystem') || []
68
+ root_fs = filesystems.find{|fs| fs['name'] == root_dir}
69
+ total_fs['used'] += root_fs['used']
70
+ total_fs['total'] += root_fs['total']
71
+ end
72
+
73
+ total_fs
74
+ end
75
+
20
76
  def find_grid_by_name(name)
21
77
  grids['grids'].find {|grid| grid['name'] == name }
22
78
  end
79
+
80
+ def to_gigabytes(amount)
81
+ return 0.0 if amount.nil?
82
+ (amount.to_f / 1024 / 1024 / 1024).to_f.round(2)
83
+ end
23
84
  end
24
85
  end
@@ -2,9 +2,10 @@ module Kontena::Cli::Grids
2
2
  class LogsCommand < Clamp::Command
3
3
  include Kontena::Cli::Common
4
4
 
5
- option ["-f", "--follow"], :flag, "Follow (tail) logs", default: false
5
+ option ["-t", "--tail"], :flag, "Tail (follow) logs", default: false
6
6
  option ["-s", "--search"], "SEARCH", "Search from logs"
7
7
  option "--lines", "LINES", "Number of lines to show from the end of the logs"
8
+ option "--since", "SINCE", "Show logs since given timestamp"
8
9
  option "--node", "NODE", "Filter by node name", multivalued: true
9
10
  option "--service", "SERVICE", "Filter by service name", multivalued: true
10
11
  option ["-c", "--container"], "CONTAINER", "Filter by container", multivalued: true
@@ -19,9 +20,9 @@ module Kontena::Cli::Grids
19
20
  query_params[:containers] = container_list.join(",") unless container_list.empty?
20
21
  query_params[:search] = search if search
21
22
  query_params[:limit] = lines if lines
23
+ query_params[:since] = since if since
22
24
 
23
-
24
- if follow?
25
+ if tail?
25
26
  @buffer = ''
26
27
  query_params[:follow] = 1
27
28
  stream_logs(token, query_params)
@@ -34,7 +35,11 @@ module Kontena::Cli::Grids
34
35
  result = client(token).get("grids/#{current_grid}/container_logs", query_params)
35
36
  result['logs'].each do |log|
36
37
  color = color_for_container(log['name'])
37
- puts "#{log['name'].colorize(color)} | #{log['data']}"
38
+ prefix = ""
39
+ prefix << "#{log['created_at']} "
40
+ prefix << "#{log['name']}:"
41
+ prefix = prefix.colorize(color)
42
+ puts "#{prefix} #{log['data']}"
38
43
  end
39
44
  end
40
45
 
@@ -3,6 +3,7 @@ require_relative 'common'
3
3
  module Kontena::Cli::Grids
4
4
  class RemoveUserCommand < Clamp::Command
5
5
  include Kontena::Cli::Common
6
+ include Kontena::Cli::GridOptions
6
7
  include Common
7
8
 
8
9
  parameter "EMAIL", "Email address"
@@ -0,0 +1,27 @@
1
+ require_relative 'common'
2
+
3
+ module Kontena::Cli::Grids
4
+ class UpdateCommand < Clamp::Command
5
+ include Kontena::Cli::Common
6
+ include Common
7
+
8
+ parameter "NAME", "Grid name"
9
+ option "--statsd-server", "STATSD_SERVER", "Statsd server address (host:port)"
10
+
11
+ def execute
12
+ require_api_url
13
+ token = require_token
14
+ payload = {}
15
+ if statsd_server
16
+ server, port = statsd_server.split(':')
17
+ payload[:stats] = {
18
+ statsd: {
19
+ server: server,
20
+ port: port || 8125
21
+ }
22
+ }
23
+ end
24
+ client(token).put("grids/#{name}", payload)
25
+ end
26
+ end
27
+ end
@@ -8,8 +8,18 @@ module Kontena::Cli::Master
8
8
  master = find_master_by_name(name)
9
9
  if !master.nil?
10
10
  self.current_master = master['name']
11
- puts "Using master: #{master['name'].cyan}"
12
- puts "URL: #{master['url'].cyan}"
11
+ puts "Using master: #{master['name'].cyan} (#{master['url']})"
12
+ puts "Using grid: #{current_grid.cyan}" if current_grid
13
+ grids = client(require_token).get('grids')['grids']
14
+ if grids.size > 1
15
+ puts ""
16
+ puts "You have access to following grids and can switch between them using 'kontena grid use <name>'"
17
+ puts ""
18
+ grids.each do |grid|
19
+ puts " * #{grid['name']}"
20
+ end
21
+ puts ""
22
+ end
13
23
  else
14
24
  abort "Could not resolve master by name [#{name}]. For a list of known masters please run: kontena master list".colorize(:red)
15
25
  end
@@ -0,0 +1,26 @@
1
+ require_relative '../../common'
2
+
3
+ module Kontena::Cli::Master::Users
4
+ class AddRoleCommand < Clamp::Command
5
+ include Kontena::Cli::Common
6
+
7
+ parameter "ROLE", "Role name"
8
+ parameter "USER ...", "List of users"
9
+
10
+ def execute
11
+ require_api_url
12
+ token = require_token
13
+ data = { role: role }
14
+
15
+ user_list.each do |email|
16
+ begin
17
+ response = client(token).post("users/#{email}/roles", data)
18
+ puts "Added role #{role} to #{email}"
19
+ rescue => exc
20
+ puts "Failed to add role #{role} to #{email}".colorize(:red)
21
+ puts exc.message
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,24 @@
1
+ require_relative '../../common'
2
+
3
+ module Kontena::Cli::Master::Users
4
+ class InviteCommand < Clamp::Command
5
+ include Kontena::Cli::Common
6
+
7
+ parameter "EMAIL ...", "List of emails"
8
+
9
+ def execute
10
+ require_api_url
11
+ token = require_token
12
+ email_list.each do |email|
13
+ begin
14
+ data = { email: email }
15
+ response = client(token).post('users', data)
16
+ puts "Invitation send to #{email}"
17
+ rescue
18
+ puts "Failed to invite #{email}".colorize(:red)
19
+ puts exc.message
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,18 @@
1
+ require_relative '../../common'
2
+
3
+ module Kontena::Cli::Master::Users
4
+ class ListCommand < Clamp::Command
5
+ include Kontena::Cli::Common
6
+
7
+ def execute
8
+ require_api_url
9
+ token = require_token
10
+ response = client(token).get('users')
11
+
12
+ response['users'].each do |user|
13
+ roles = user['roles'].map{|r| r['name']}
14
+ puts "#{user['email']} - #{roles.join(', ')}"
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,26 @@
1
+ require_relative '../../common'
2
+
3
+ module Kontena::Cli::Master::Users
4
+ class RemoveRoleCommand < Clamp::Command
5
+ include Kontena::Cli::Common
6
+
7
+ parameter "ROLE", "Role name"
8
+ parameter "USER ...", "List of users"
9
+
10
+
11
+ def execute
12
+ require_api_url
13
+ token = require_token
14
+
15
+ user_list.each do |email|
16
+ begin
17
+ response = client(token).delete("users/#{email}/roles/#{role}")
18
+ puts "Removed role #{role} from #{email}" if response
19
+ rescue => exc
20
+ puts "Failed to remove role #{role} from #{email}".colorize(:red)
21
+ puts exc.message
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,14 @@
1
+ module Kontena::Cli::Master
2
+
3
+ require_relative 'users/add_role_command'
4
+ require_relative 'users/invite_command'
5
+ require_relative 'users/list_command'
6
+ require_relative 'users/remove_role_command'
7
+
8
+ class UsersCommand < Clamp::Command
9
+ subcommand "invite", "Invite user to Kontena Master", Users::InviteCommand
10
+ subcommand ["list", "ls"], "List users", Users::ListCommand
11
+ subcommand "add-role", "Add role to user", Users::AddRoleCommand
12
+ subcommand "remove-role", "Remove role from user", Users::RemoveRoleCommand
13
+ end
14
+ end
@@ -4,6 +4,7 @@ require_relative 'master/digital_ocean_command'
4
4
  require_relative 'master/azure_command'
5
5
  require_relative 'master/use_command'
6
6
  require_relative 'master/list_command'
7
+ require_relative 'master/users_command'
7
8
 
8
9
  class Kontena::Cli::MasterCommand < Clamp::Command
9
10
 
@@ -13,7 +14,7 @@ class Kontena::Cli::MasterCommand < Clamp::Command
13
14
  subcommand "azure", "Azure specific commands", Kontena::Cli::Master::AzureCommand
14
15
  subcommand ["list", "ls"], "List masters where client has logged in", Kontena::Cli::Master::ListCommand
15
16
  subcommand "use", "Switch to use selected master", Kontena::Cli::Master::UseCommand
16
-
17
+ subcommand "users", "Users specific commands", Kontena::Cli::Master::UsersCommand
17
18
  def execute
18
19
  end
19
20
  end