kontena-cli 0.7.3 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/bin/kontena +64 -2
  4. data/kontena-cli.gemspec +4 -2
  5. data/lib/kontena/cli/app_command.rb +20 -0
  6. data/lib/kontena/cli/apps/common.rb +53 -0
  7. data/lib/kontena/cli/{stacks/stacks.rb → apps/deploy_command.rb} +17 -31
  8. data/lib/kontena/cli/apps/init_command.rb +101 -0
  9. data/lib/kontena/cli/apps/list_command.rb +41 -0
  10. data/lib/kontena/cli/apps/logs_command.rb +58 -0
  11. data/lib/kontena/cli/apps/remove_command.rb +64 -0
  12. data/lib/kontena/cli/apps/start_command.rb +38 -0
  13. data/lib/kontena/cli/apps/stop_command.rb +38 -0
  14. data/lib/kontena/cli/container_command.rb +9 -0
  15. data/lib/kontena/cli/containers/{containers.rb → exec_command.rb} +5 -5
  16. data/lib/kontena/cli/deploy_command.rb +159 -0
  17. data/lib/kontena/cli/external_registries/add_command.rb +22 -0
  18. data/lib/kontena/cli/external_registries/delete_command.rb +13 -0
  19. data/lib/kontena/cli/external_registries/list_command.rb +16 -0
  20. data/lib/kontena/cli/external_registry_command.rb +14 -0
  21. data/lib/kontena/cli/forgot_password_command.rb +13 -0
  22. data/lib/kontena/cli/grid_command.rb +27 -0
  23. data/lib/kontena/cli/grids/add_user_command.rb +17 -0
  24. data/lib/kontena/cli/grids/{audit_log.rb → audit_log_command.rb} +7 -6
  25. data/lib/kontena/cli/grids/common.rb +24 -0
  26. data/lib/kontena/cli/grids/create_command.rb +26 -0
  27. data/lib/kontena/cli/grids/current_command.rb +18 -0
  28. data/lib/kontena/cli/grids/list_command.rb +26 -0
  29. data/lib/kontena/cli/grids/list_users_command.rb +18 -0
  30. data/lib/kontena/cli/grids/remove_command.rb +26 -0
  31. data/lib/kontena/cli/grids/remove_user_command.rb +16 -0
  32. data/lib/kontena/cli/grids/show_command.rb +19 -0
  33. data/lib/kontena/cli/grids/use_command.rb +21 -0
  34. data/lib/kontena/cli/invite_command.rb +13 -0
  35. data/lib/kontena/cli/login_command.rb +114 -0
  36. data/lib/kontena/cli/logout_command.rb +8 -0
  37. data/lib/kontena/cli/node_command.rb +21 -0
  38. data/lib/kontena/cli/nodes/digital_ocean/create_command.rb +31 -0
  39. data/lib/kontena/cli/nodes/digital_ocean/restart_command.rb +26 -0
  40. data/lib/kontena/cli/nodes/digital_ocean/terminate_command.rb +18 -0
  41. data/lib/kontena/cli/nodes/digital_ocean_command.rb +15 -0
  42. data/lib/kontena/cli/nodes/list_command.rb +28 -0
  43. data/lib/kontena/cli/nodes/remove_command.rb +15 -0
  44. data/lib/kontena/cli/nodes/show_command.rb +31 -0
  45. data/lib/kontena/cli/nodes/update_command.rb +18 -0
  46. data/lib/kontena/cli/nodes/vagrant/create_command.rb +26 -0
  47. data/lib/kontena/cli/nodes/vagrant/restart_command.rb +25 -0
  48. data/lib/kontena/cli/nodes/vagrant/ssh_command.rb +20 -0
  49. data/lib/kontena/cli/nodes/vagrant/start_command.rb +25 -0
  50. data/lib/kontena/cli/nodes/vagrant/stop_command.rb +25 -0
  51. data/lib/kontena/cli/nodes/vagrant/terminate_command.rb +16 -0
  52. data/lib/kontena/cli/nodes/vagrant_command.rb +21 -0
  53. data/lib/kontena/cli/register_command.rb +21 -0
  54. data/lib/kontena/cli/{grids/registry.rb → registry/create_command.rb} +32 -35
  55. data/lib/kontena/cli/registry/delete_command.rb +15 -0
  56. data/lib/kontena/cli/registry_command.rb +11 -0
  57. data/lib/kontena/cli/reset_password_command.rb +17 -0
  58. data/lib/kontena/cli/service_command.rb +33 -0
  59. data/lib/kontena/cli/services/container_command.rb +9 -0
  60. data/lib/kontena/cli/services/containers_command.rb +31 -0
  61. data/lib/kontena/cli/services/create_command.rb +62 -0
  62. data/lib/kontena/cli/services/delete_command.rb +17 -0
  63. data/lib/kontena/cli/services/deploy_command.rb +23 -0
  64. data/lib/kontena/cli/services/list_command.rb +20 -0
  65. data/lib/kontena/cli/services/logs_command.rb +51 -0
  66. data/lib/kontena/cli/services/restart_command.rb +16 -0
  67. data/lib/kontena/cli/services/scale_command.rb +20 -0
  68. data/lib/kontena/cli/services/services_helper.rb +94 -0
  69. data/lib/kontena/cli/services/show_command.rb +17 -0
  70. data/lib/kontena/cli/services/start_command.rb +16 -0
  71. data/lib/kontena/cli/services/{stats.rb → stats_command.rb} +11 -10
  72. data/lib/kontena/cli/services/stop_command.rb +16 -0
  73. data/lib/kontena/cli/services/update_command.rb +51 -0
  74. data/lib/kontena/cli/verify_account_command.rb +13 -0
  75. data/lib/kontena/cli/version_command.rb +8 -0
  76. data/lib/kontena/cli/vpn/config_command.rb +12 -0
  77. data/lib/kontena/cli/{grids/vpn.rb → vpn/create_command.rb} +12 -29
  78. data/lib/kontena/cli/vpn/delete_command.rb +15 -0
  79. data/lib/kontena/cli/vpn_command.rb +13 -0
  80. data/lib/kontena/cli/whoami_command.rb +19 -0
  81. data/lib/kontena/client.rb +14 -11
  82. data/lib/kontena/machine/common.rb +17 -0
  83. data/lib/kontena/machine/digital_ocean.rb +11 -0
  84. data/lib/kontena/machine/digital_ocean/cloudinit.yml +66 -0
  85. data/lib/kontena/machine/digital_ocean/node_destroyer.rb +38 -0
  86. data/lib/kontena/machine/digital_ocean/node_provisioner.rb +74 -0
  87. data/lib/kontena/machine/random_name.rb +42 -0
  88. data/lib/kontena/machine/vagrant.rb +10 -0
  89. data/lib/kontena/machine/vagrant/Vagrantfile.coreos.rb.erb +32 -0
  90. data/lib/kontena/machine/vagrant/cloudinit.yml +65 -0
  91. data/lib/kontena/machine/vagrant/node_destroyer.rb +36 -0
  92. data/lib/kontena/machine/vagrant/node_provisioner.rb +68 -0
  93. data/lib/kontena/scripts/completer +5 -5
  94. data/spec/kontena/cli/app/deploy_command_spec.rb +227 -0
  95. data/spec/kontena/cli/deploy_command_spec.rb +213 -0
  96. data/spec/kontena/cli/login_command_spec.rb +22 -0
  97. data/spec/kontena/cli/register_command_spec.rb +57 -0
  98. data/spec/spec_helper.rb +5 -1
  99. metadata +132 -36
  100. data/lib/kontena/cli/commands.rb +0 -20
  101. data/lib/kontena/cli/containers/commands.rb +0 -12
  102. data/lib/kontena/cli/grids/commands.rb +0 -169
  103. data/lib/kontena/cli/grids/external_registries.rb +0 -40
  104. data/lib/kontena/cli/grids/grids.rb +0 -108
  105. data/lib/kontena/cli/grids/users.rb +0 -32
  106. data/lib/kontena/cli/nodes/commands.rb +0 -27
  107. data/lib/kontena/cli/nodes/nodes.rb +0 -64
  108. data/lib/kontena/cli/server/commands.rb +0 -69
  109. data/lib/kontena/cli/server/server.rb +0 -45
  110. data/lib/kontena/cli/server/user.rb +0 -174
  111. data/lib/kontena/cli/services/commands.rb +0 -138
  112. data/lib/kontena/cli/services/containers.rb +0 -24
  113. data/lib/kontena/cli/services/logs.rb +0 -44
  114. data/lib/kontena/cli/services/services.rb +0 -175
  115. data/lib/kontena/cli/stacks/commands.rb +0 -13
  116. data/spec/kontena/cli/server/user_spec.rb +0 -59
  117. data/spec/kontena/cli/stacks/stacks_spec.rb +0 -212
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3373d4421b6f61fd8e141235ecc1c00d98ff9953
4
- data.tar.gz: 577e0ebe31f0536ac54376be8154278651ee2caa
3
+ metadata.gz: 7509ba73a33bfc06a0e681f8d61a7ab3d7288284
4
+ data.tar.gz: 5f7bdc77590bebc819880d555f55d779188a634b
5
5
  SHA512:
6
- metadata.gz: 10a7dff3ac50b99808ca0754e8de9a77740f2535801dc304574d2c25082e79c32a5503765c6c39f15f7b66ed9fa70b6026c851e7ac9bd78ef0d6f9ec4b9c4e50
7
- data.tar.gz: 54af8670055cbeac319144712d3add58bd1a334b385d48cab3a35acab0f5b324f70eb98557589e0ebfbeeeefd04b95e6ec3366c6f07058595371e56e92e08ca1
6
+ metadata.gz: c1c92dabc02bdf30be79976ccfff1e2f1a1422d1e91676a882782f02f64f92892eb7b49f2d70d9dbc67d55f2cddd7eec3816db202e9d2e98e603cca4f3955cd6
7
+ data.tar.gz: ada1b9cd60a4b602b1e9ac6eda9773e99cd6ca8364a9d5f992b91a983d86e3603716b74efabbe4d13b9060ed34cf77bf8ef9ed894445b4756da89dd80b2fc7e3
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.7.3
1
+ 0.8.0
data/bin/kontena CHANGED
@@ -10,5 +10,67 @@ $:.unshift File.expand_path('../../lib', bin_file)
10
10
 
11
11
  STDOUT.sync = true
12
12
 
13
- require 'commander/import'
14
- require 'kontena/cli/commands'
13
+ require 'clamp'
14
+ require 'kontena/client'
15
+ require 'kontena/cli/common'
16
+ require 'kontena/cli/app_command'
17
+ require 'kontena/cli/register_command'
18
+ require 'kontena/cli/login_command'
19
+ require 'kontena/cli/logout_command'
20
+ require 'kontena/cli/whoami_command'
21
+ require 'kontena/cli/verify_account_command'
22
+ require 'kontena/cli/invite_command'
23
+ require 'kontena/cli/forgot_password_command'
24
+ require 'kontena/cli/container_command'
25
+ require 'kontena/cli/grid_command'
26
+ require 'kontena/cli/node_command'
27
+ require 'kontena/cli/service_command'
28
+ require 'kontena/cli/vpn_command'
29
+ require 'kontena/cli/registry_command'
30
+ require 'kontena/cli/external_registry_command'
31
+ require 'kontena/cli/deploy_command'
32
+ require 'kontena/cli/app_command'
33
+ require 'kontena/cli/version_command'
34
+
35
+ class MainCommand < Clamp::Command
36
+
37
+ subcommand "app", "Grid specific commands", Kontena::Cli::AppCommand
38
+ subcommand "grid", "Grid specific commands", Kontena::Cli::GridCommand
39
+ subcommand "app", "App specific commands", Kontena::Cli::AppCommand
40
+ subcommand "service", "Service specific commands", Kontena::Cli::ServiceCommand
41
+ subcommand "deploy", "Create and deploy multiple services from YAML file", Kontena::Cli::DeployCommand
42
+ subcommand "node", "Node specific commands", Kontena::Cli::NodeCommand
43
+ subcommand "vpn", "VPN specific commands", Kontena::Cli::VpnCommand
44
+ subcommand "registry", "Registry specific commands", Kontena::Cli::RegistryCommand
45
+ subcommand "container", "Container specific commands", Kontena::Cli::ContainerCommand
46
+ subcommand "external-registry", "External registry specific commands", Kontena::Cli::ExternalRegistryCommand
47
+ subcommand "register", "Register Kontena Account", Kontena::Cli::RegisterCommand
48
+ subcommand "login", "Login to Kontena Master", Kontena::Cli::LoginCommand
49
+ subcommand "logout", "Logout from Kontena Master", Kontena::Cli::LogoutCommand
50
+ subcommand "whoami", "Shows current logged in user", Kontena::Cli::WhoamiCommand
51
+ subcommand "verify-account", "Verify registered account", Kontena::Cli::VerifyAccountCommand
52
+ subcommand "invite", "Invite user to Kontena Master", Kontena::Cli::InviteCommand
53
+ subcommand "forgot-password", "Request password reset for Kontena account", Kontena::Cli::ForgotPasswordCommand
54
+ subcommand "version", "Show version", Kontena::Cli::VersionCommand
55
+
56
+ def execute
57
+ end
58
+ end
59
+
60
+ begin
61
+ MainCommand.run
62
+ rescue Excon::Errors::SocketError => exc
63
+ if exc.message.include?('Unable to verify certificate')
64
+ puts "The server uses a certificate signed by an unknown authority.".colorize(:red)
65
+ puts "Protip: you can bypass the certificate check by setting #{'SSL_IGNORE_ERRORS=true'.colorize(:yellow)} env variable, but any data you send to the server could be intercepted by others."
66
+ exit(1)
67
+ else
68
+ abort(exc.message)
69
+ end
70
+ rescue => exc
71
+ if ENV['DEBUG']
72
+ raise exc
73
+ else
74
+ abort(exc.message)
75
+ end
76
+ end
data/kontena-cli.gemspec CHANGED
@@ -20,7 +20,9 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.7"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
- spec.add_runtime_dependency 'httpclient', '~> 2.3'
24
- spec.add_runtime_dependency 'commander'
23
+ spec.add_runtime_dependency 'excon'
25
24
  spec.add_runtime_dependency 'colorize'
25
+ spec.add_runtime_dependency 'clamp'
26
+ spec.add_runtime_dependency 'highline'
27
+ spec.add_runtime_dependency 'shell-spinner'
26
28
  end
@@ -0,0 +1,20 @@
1
+ require_relative 'apps/init_command'
2
+ require_relative 'apps/deploy_command'
3
+ require_relative 'apps/start_command'
4
+ require_relative 'apps/stop_command'
5
+ require_relative 'apps/remove_command'
6
+ require_relative 'apps/list_command'
7
+ require_relative 'apps/logs_command'
8
+
9
+ class Kontena::Cli::AppCommand < Clamp::Command
10
+
11
+ subcommand "init", "Init Kontena application", Kontena::Cli::Apps::InitCommand
12
+ subcommand "deploy", "Deploy Kontena services", Kontena::Cli::Apps::DeployCommand
13
+ subcommand "start", "Start services", Kontena::Cli::Apps::StartCommand
14
+ subcommand "stop", "Stop services", Kontena::Cli::Apps::StopCommand
15
+ subcommand ["remove","rm"], "Remove services", Kontena::Cli::Apps::RemoveCommand
16
+ subcommand ["ps", "list"], "List services", Kontena::Cli::Apps::ListCommand
17
+ subcommand ["logs"], "Show service logs", Kontena::Cli::Apps::LogsCommand
18
+ def execute
19
+ end
20
+ end
@@ -0,0 +1,53 @@
1
+ require_relative '../services/services_helper'
2
+
3
+ module Kontena::Cli::Apps
4
+ module Common
5
+ include Kontena::Cli::Services::ServicesHelper
6
+
7
+ def load_services_from_yml
8
+ @service_prefix = project_name || current_dir
9
+
10
+ abort("File #{filename} does not exist") unless File.exists?(filename)
11
+ services = parse_yml_file(filename, nil, service_prefix)
12
+
13
+ services.delete_if { |name, service| !service_list.include?(name)} unless service_list.empty?
14
+ services
15
+ end
16
+
17
+ def token
18
+ @token ||= require_token
19
+ end
20
+
21
+ def prefixed_name(name)
22
+ "#{service_prefix}-#{name}"
23
+ end
24
+
25
+ def current_dir
26
+ File.basename(Dir.getwd)
27
+ end
28
+
29
+ def service_exists?(name)
30
+ get_service(token, prefixed_name(name)) rescue false
31
+ end
32
+
33
+ def parse_yml_file(file, name = nil, prefix='')
34
+ services = YAML.load(File.read(file) % {prefix: prefix})
35
+ services.each do |name, options|
36
+ if options.has_key?('extends')
37
+ extends = options['extends']
38
+ options.delete('extends')
39
+ services[name] = parse_yml_file(extends['file'], extends['service']).merge(options)
40
+ end
41
+ if options.has_key?('build')
42
+ options.delete('build')
43
+ end
44
+
45
+ end
46
+ if name.nil?
47
+ services
48
+ else
49
+ services[name]
50
+ end
51
+ end
52
+ end
53
+ end
@@ -1,28 +1,22 @@
1
- require 'kontena/client'
2
1
  require 'yaml'
3
- require_relative '../common'
4
- require_relative '../services/services_helper'
2
+ require_relative 'common'
5
3
 
6
- module Kontena::Cli::Stacks
7
- class Stacks
4
+ module Kontena::Cli::Apps
5
+ class DeployCommand < Clamp::Command
8
6
  include Kontena::Cli::Common
9
- include Kontena::Cli::Services::ServicesHelper
7
+ include Common
10
8
 
11
- attr_reader :services, :service_prefix, :deploy_queue
12
- def initialize
13
- @deploy_queue = []
14
- end
9
+ option ['-f', '--file'], 'FILE', 'Specify an alternate Kontena compose file', attribute_name: :filename, default: 'kontena.yml'
10
+ option ['-p', '--project-name'], 'NAME', 'Specify an alternate project name (default: directory name)'
15
11
 
16
- def deploy(options)
17
- require_api_url
18
- require_token
12
+ parameter "[SERVICE] ...", "Services to start"
19
13
 
20
- filename = options.file || './kontena.yml'
21
- @service_prefix = options.prefix || current_dir
22
-
23
- @services = YAML.load(File.read(filename) % {prefix: service_prefix})
24
- @services = @services.delete_if { |name, service| !options.service.include?(name)} if options.service
14
+ attr_reader :services, :service_prefix
25
15
 
16
+ def execute
17
+ require_api_url
18
+ require_token
19
+ @services = load_services_from_yml
26
20
  Dir.chdir(File.dirname(filename))
27
21
  init_services(services)
28
22
  deploy_services(deploy_queue)
@@ -65,7 +59,7 @@ module Kontena::Cli::Stacks
65
59
 
66
60
  merge_env_vars(options)
67
61
 
68
- if find_service_by_name(name)
62
+ if service_exists?(name)
69
63
  service = update(name, options)
70
64
  else
71
65
  service = create(name, options)
@@ -100,14 +94,6 @@ module Kontena::Cli::Stacks
100
94
  deploy_queue.find {|service| service['id'] == prefixed_name(name)} != nil
101
95
  end
102
96
 
103
- def prefixed_name(name)
104
- "#{service_prefix}-#{name}"
105
- end
106
-
107
- def current_dir
108
- File.basename(Dir.getwd)
109
- end
110
-
111
97
  def merge_env_vars(options)
112
98
  return unless options['env_file']
113
99
 
@@ -137,7 +123,7 @@ module Kontena::Cli::Stacks
137
123
  data[:memory] = parse_memory(options['mem_limit']) if options['mem_limit']
138
124
  data[:memory_swap] = parse_memory(options['memswap_limit']) if options['memswap_limit']
139
125
  data[:cpu_shares] = options['cpu_shares'] if options['cpu_shares']
140
- data[:volumes] = options['volume'] if options['volume']
126
+ data[:volumes] = options['volumes'] if options['volumes']
141
127
  data[:volumes_from] = options['volumes_from'] if options['volumes_from']
142
128
  data[:cmd] = options['command'].split(" ") if options['command']
143
129
  data[:affinity] = options['affinity'] if options['affinity']
@@ -148,8 +134,8 @@ module Kontena::Cli::Stacks
148
134
  data
149
135
  end
150
136
 
151
- def token
152
- @token ||= require_token
137
+ def deploy_queue
138
+ @deploy_queue ||= []
153
139
  end
154
140
  end
155
- end
141
+ end
@@ -0,0 +1,101 @@
1
+ require 'yaml'
2
+
3
+ module Kontena::Cli::Apps
4
+ class InitCommand < Clamp::Command
5
+ include Kontena::Cli::Common
6
+
7
+ option ["-f", "--file"], "FILE", "Specify a docker-compose file", attribute_name: :docker_compose_file, default: 'docker-compose.yml'
8
+ option ["-i", "--image-name"], "IMAGE_NAME", "Specify a docker image name"
9
+ option ["-b", "--base-image"], "BASE_IMAGE_NAME", "Specify a docker base image name", default: "kontena/buildstep"
10
+
11
+ def execute
12
+ require 'highline/import'
13
+
14
+ if File.exist?('Dockerfile')
15
+ puts 'Found Dockerfile'
16
+ else
17
+ create_dockerfile if create_dockerfile?
18
+ end
19
+
20
+ if File.exist?(docker_compose_file)
21
+ puts "Found #{docker_compose_file}."
22
+ else
23
+ create_docker_compose_yml if create_docker_compose_yml?
24
+ end
25
+
26
+ services = generate_kontena_services(docker_compose_file)
27
+ if File.exist?('kontena.yml')
28
+ merge_kontena_yml!(services)
29
+ puts "#{'kontena.yml'.colorize(:cyan)} updated."
30
+ else
31
+ puts "Creating #{'kontena.yml'.colorize(:cyan)}"
32
+ end
33
+ create_yml(services, 'kontena.yml')
34
+
35
+ puts "You are ready to go!".colorize(:green)
36
+ end
37
+
38
+
39
+ protected
40
+ def create_dockerfile?
41
+ %w(y yes).include? ask('Dockerfile not found. Do you want to create it? [Yn]: ').downcase
42
+ end
43
+
44
+ def current_user
45
+ token = require_token
46
+ client(token).get('user')
47
+ end
48
+
49
+ def create_dockerfile
50
+ puts "Creating #{'Dockerfile'.colorize(:cyan)}"
51
+ dockerfile = File.new('Dockerfile', 'w')
52
+ dockerfile.puts "FROM #{base_image}"
53
+ dockerfile.puts "MAINTAINER #{current_user['email']}"
54
+ dockerfile.close
55
+ end
56
+
57
+ def merge_kontena_yml!(services)
58
+ puts "kontena.yml already exists. Merging changes."
59
+ kontena_services = YAML.load(File.read('kontena.yml'))
60
+ services.each do |name, options|
61
+ if kontena_services[name]
62
+ services[name].merge!(kontena_services[name])
63
+ end
64
+ end
65
+ end
66
+
67
+ def create_docker_compose_yml?
68
+ %w(y yes).include? ask("#{docker_compose_file} not found. Do you want to create it? [Yn]: ").downcase
69
+ end
70
+
71
+ def create_docker_compose_yml
72
+ puts "Creating #{docker_compose_file.colorize(:cyan)}"
73
+ create_yml({'app' => { 'build' => '.'}}, docker_compose_file)
74
+ end
75
+
76
+ def generate_kontena_services(docker_compose = nil)
77
+ services = {}
78
+ if docker_compose && File.exist?(docker_compose)
79
+ compose_services = YAML.load(File.read(docker_compose))
80
+ compose_services.each do |name, options|
81
+ services[name] = {'extends' => { 'file' => 'docker-compose.yml', 'service' => name }}
82
+ if options.has_key?('build')
83
+ image = image_name || "registry.kontena.local/#{File.basename(Dir.getwd)}-#{name}:latest"
84
+ services[name]['image'] = image
85
+ options.delete('build')
86
+ end
87
+ end
88
+ else
89
+ services = {'app' => { 'image' => "registry.kontena.local/#{File.basename(Dir.getwd)}:latest" }}
90
+ end
91
+ services
92
+ end
93
+
94
+ def create_yml(services, file='kontena.yml')
95
+ yml = File.new(file, 'w')
96
+ yml.puts services.to_yaml
97
+ yml.close
98
+ end
99
+
100
+ end
101
+ end
@@ -0,0 +1,41 @@
1
+ require 'yaml'
2
+ require_relative 'common'
3
+
4
+ module Kontena::Cli::Apps
5
+ class ListCommand < Clamp::Command
6
+ include Kontena::Cli::Common
7
+ include Common
8
+
9
+ option ['-f', '--file'], 'FILE', 'Specify an alternate Kontena compose file', attribute_name: :filename, default: 'kontena.yml'
10
+ option ['-p', '--project-name'], 'NAME', 'Specify an alternate project name (default: directory name)'
11
+
12
+ parameter "[SERVICE] ...", "Services to start"
13
+
14
+ attr_reader :services, :service_prefix
15
+
16
+ def execute
17
+ @services = load_services_from_yml
18
+ if services.size > 0
19
+ Dir.chdir(File.dirname(filename))
20
+ show_services(services)
21
+ elsif !service_list.empty?
22
+ puts "No such service: #{service_list.join(', ')}".colorize(:red)
23
+ end
24
+
25
+ end
26
+
27
+ def show_services(services)
28
+ puts "%-30.30s %-50.50s %-15s %-10.10s %-15.20s %-50s" % ['NAME', 'IMAGE', 'INSTANCES', 'STATEFUL', 'STATE', 'PORTS']
29
+
30
+ services.each do |service_name, opts|
31
+ service = get_service(token, prefixed_name(service_name)) rescue false
32
+ if service
33
+ state = service['stateful'] ? 'yes' : 'no'
34
+
35
+ ports = service['ports'].map{|p| "#{p['ip']}:#{p['node_port']}->#{p['container_port']}/#{p['protocol']}"}.join(", ")
36
+ puts "%-30.30s %-50.50s %-15.10s %-10.10s %-15.20s %-50s" % [service['name'], service['image'], service['container_count'], state, service['state'], ports]
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,58 @@
1
+ require 'yaml'
2
+ require_relative 'common'
3
+
4
+ module Kontena::Cli::Apps
5
+ class LogsCommand < Clamp::Command
6
+ include Kontena::Cli::Common
7
+ include Common
8
+
9
+ option ['-f', '--file'], 'FILE', 'Specify an alternate Kontena compose file', attribute_name: :filename, default: 'kontena.yml'
10
+ option ['-p', '--project-name'], 'NAME', 'Specify an alternate project name (default: directory name)'
11
+
12
+ parameter "[SERVICE] ...", "Services to start"
13
+
14
+ attr_reader :services, :service_prefix
15
+
16
+ def execute
17
+ @services = load_services_from_yml
18
+ if services.size > 0
19
+ Dir.chdir(File.dirname(filename))
20
+ show_logs(services)
21
+ elsif !service_list.empty?
22
+ puts "No such service: #{service_list.join(', ')}".colorize(:red)
23
+ end
24
+
25
+ end
26
+
27
+ def show_logs(services)
28
+ logs = []
29
+ services.each do |service_name, opts|
30
+ service = get_service(token, prefixed_name(service_name)) rescue false
31
+ result = client(token).get("services/#{service['id']}/container_logs")
32
+ logs = logs + result['logs']
33
+ end
34
+ logs.sort!{|x,y| DateTime.parse(x['created_at']) <=> DateTime.parse(y['created_at'])}
35
+ logs.each do |log|
36
+ color = color_for_container(log['name'])
37
+ puts "#{log['name'].colorize(color)} | #{log['data']}"
38
+ end
39
+ end
40
+
41
+ def color_for_container(container_id)
42
+ color_maps[container_id] = colors.shift unless color_maps[container_id]
43
+ color_maps[container_id].to_sym
44
+ end
45
+
46
+ def color_maps
47
+ @color_maps ||= {}
48
+ end
49
+
50
+ def colors
51
+ if(@colors.nil? || @colors.size == 0)
52
+ @colors = [:green, :magenta, :yellow, :cyan, :red,
53
+ :light_green, :light_yellow, :ligh_magenta, :light_cyan, :light_red]
54
+ end
55
+ @colors
56
+ end
57
+ end
58
+ end