kuber_kit 0.2.7 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +15 -13
  3. data/TODO.md +5 -7
  4. data/example/app_data/docker_compose.yml +6 -0
  5. data/example/config.rb +3 -0
  6. data/example/configurations/review.rb +1 -1
  7. data/example/images/ruby_app/Dockerfile +1 -1
  8. data/example/images/ruby_app/image.rb +3 -0
  9. data/example/infrastructure/build_servers.rb +5 -5
  10. data/example/infrastructure/templates.rb +5 -0
  11. data/example/services/compose_app.rb +10 -0
  12. data/example/services/env_file.rb +1 -1
  13. data/lib/kuber_kit.rb +22 -3
  14. data/lib/kuber_kit/actions/configuration_loader.rb +19 -1
  15. data/lib/kuber_kit/actions/kubectl_applier.rb +2 -2
  16. data/lib/kuber_kit/actions/kubectl_attacher.rb +2 -2
  17. data/lib/kuber_kit/actions/template_reader.rb +3 -6
  18. data/lib/kuber_kit/configs.rb +63 -32
  19. data/lib/kuber_kit/container.rb +14 -2
  20. data/lib/kuber_kit/core/configuration.rb +19 -8
  21. data/lib/kuber_kit/core/configuration_definition.rb +30 -10
  22. data/lib/kuber_kit/core/configuration_factory.rb +11 -10
  23. data/lib/kuber_kit/core/context_helper/base_helper.rb +4 -0
  24. data/lib/kuber_kit/core/context_helper/context_helper_factory.rb +3 -2
  25. data/lib/kuber_kit/core/context_helper/context_vars.rb +39 -0
  26. data/lib/kuber_kit/core/context_helper/image_helper.rb +17 -0
  27. data/lib/kuber_kit/core/image.rb +3 -1
  28. data/lib/kuber_kit/core/image_definition.rb +7 -5
  29. data/lib/kuber_kit/core/service.rb +4 -4
  30. data/lib/kuber_kit/core/service_definition.rb +3 -3
  31. data/lib/kuber_kit/core/service_factory.rb +6 -6
  32. data/lib/kuber_kit/env_file_reader/reader.rb +10 -6
  33. data/lib/kuber_kit/extensions/indocker_compat.rb +4 -0
  34. data/lib/kuber_kit/image_compiler/compiler.rb +1 -1
  35. data/lib/kuber_kit/service_deployer/deployer.rb +14 -8
  36. data/lib/kuber_kit/service_deployer/strategies/docker_compose.rb +24 -0
  37. data/lib/kuber_kit/service_deployer/strategies/kubernetes.rb +6 -6
  38. data/lib/kuber_kit/service_deployer/strategies/kubernetes_runner.rb +7 -7
  39. data/lib/kuber_kit/service_deployer/strategy_detector.rb +1 -1
  40. data/lib/kuber_kit/shell/abstract_shell.rb +4 -0
  41. data/lib/kuber_kit/shell/commands/docker_commands.rb +14 -0
  42. data/lib/kuber_kit/shell/commands/docker_compose_commands.rb +17 -0
  43. data/lib/kuber_kit/shell/commands/kubectl_commands.rb +5 -6
  44. data/lib/kuber_kit/shell/local_shell.rb +14 -0
  45. data/lib/kuber_kit/shell/ssh_shell.rb +4 -0
  46. data/lib/kuber_kit/template_reader/action_handler.rb +13 -0
  47. data/lib/kuber_kit/template_reader/reader.rb +13 -9
  48. data/lib/kuber_kit/template_reader/{abstract_template_reader.rb → strategies/abstract.rb} +1 -1
  49. data/lib/kuber_kit/template_reader/{artifact_file_reader.rb → strategies/artifact_file.rb} +1 -1
  50. data/lib/kuber_kit/version.rb +1 -1
  51. metadata +11 -4
@@ -0,0 +1,24 @@
1
+ class KuberKit::ServiceDeployer::Strategies::DockerCompose < KuberKit::ServiceDeployer::Strategies::Abstract
2
+ include KuberKit::Import[
3
+ "service_reader.reader",
4
+ "shell.docker_compose_commands",
5
+ "configs",
6
+ ]
7
+
8
+ Contract KuberKit::Shell::AbstractShell, KuberKit::Core::Service => Any
9
+ def deploy(shell, service)
10
+ service_config = reader.read(shell, service)
11
+ config_path = "#{configs.service_config_dir}/#{service.name}.yml"
12
+ shell.write(config_path, service_config)
13
+
14
+ deployer_service_name = service.attribute(:deployer_service_name, default: service.name.to_s)
15
+ deployer_command_name = service.attribute(:deployer_command_name, default: "bash")
16
+ deployer_interactive = service.attribute(:deployer_interactive, default: false)
17
+
18
+ docker_compose_commands.run(shell, config_path,
19
+ service: deployer_service_name,
20
+ command: deployer_command_name,
21
+ interactive: deployer_interactive
22
+ )
23
+ end
24
+ end
@@ -12,14 +12,14 @@ class KuberKit::ServiceDeployer::Strategies::Kubernetes < KuberKit::ServiceDeplo
12
12
  shell.write(config_path, service_config)
13
13
 
14
14
  kubeconfig_path = KuberKit.current_configuration.kubeconfig_path
15
- deploy_namespace = KuberKit.current_configuration.deploy_namespace
15
+ deployer_namespace = KuberKit.current_configuration.deployer_namespace
16
16
 
17
- kubectl_commands.apply_file(shell, config_path, kubeconfig_path: kubeconfig_path, namespace: deploy_namespace)
17
+ kubectl_commands.apply_file(shell, config_path, kubeconfig_path: kubeconfig_path, namespace: deployer_namespace)
18
18
 
19
- deployment_restart_enabled = service.attribute(:deployment_restart_enabled, default: true)
20
- deployment_restart_name = service.attribute(:deployment_restart_name, default: service.uri)
21
- if deployment_restart_enabled
22
- kubectl_commands.rolling_restart(shell, deployment_restart_name, kubeconfig_path: kubeconfig_path, namespace: deploy_namespace)
19
+ deployer_restart_enabled = service.attribute(:deployer_restart_enabled, default: true)
20
+ deployer_restart_name = service.attribute(:deployer_restart_name, default: service.uri)
21
+ if deployer_restart_enabled
22
+ kubectl_commands.rolling_restart(shell, deployer_restart_name, kubeconfig_path: kubeconfig_path, namespace: deployer_namespace)
23
23
  end
24
24
  end
25
25
  end
@@ -12,17 +12,17 @@ class KuberKit::ServiceDeployer::Strategies::KubernetesRunner < KuberKit::Servic
12
12
  shell.write(config_path, service_config)
13
13
 
14
14
  kubeconfig_path = KuberKit.current_configuration.kubeconfig_path
15
- deploy_namespace = KuberKit.current_configuration.deploy_namespace
15
+ deployer_namespace = KuberKit.current_configuration.deployer_namespace
16
16
 
17
- deployment_resource_name = service.attribute(:deployment_resource_name, default: service.uri)
18
- deployment_resource_type = service.attribute(:deployment_resource_type, default: "job")
17
+ deployer_resource_name = service.attribute(:deployer_resource_name, default: service.uri)
18
+ deployer_resource_type = service.attribute(:deployer_resource_type, default: "job")
19
19
 
20
- deployment_delete_enabled = service.attribute(:deployment_delete_enabled, default: true)
21
- if deployment_delete_enabled
22
- delete_resource_if_exists(shell, deployment_resource_type, deployment_resource_name, kubeconfig_path: kubeconfig_path, namespace: deploy_namespace)
20
+ deployer_delete_enabled = service.attribute(:deployer_delete_enabled, default: true)
21
+ if deployer_delete_enabled
22
+ delete_resource_if_exists(shell, deployer_resource_type, deployer_resource_name, kubeconfig_path: kubeconfig_path, namespace: deployer_namespace)
23
23
  end
24
24
 
25
- kubectl_commands.apply_file(shell, config_path, kubeconfig_path: kubeconfig_path, namespace: deploy_namespace)
25
+ kubectl_commands.apply_file(shell, config_path, kubeconfig_path: kubeconfig_path, namespace: deployer_namespace)
26
26
  end
27
27
 
28
28
  private
@@ -1,6 +1,6 @@
1
1
  class KuberKit::ServiceDeployer::StrategyDetector
2
2
  Contract KuberKit::Core::Service => Symbol
3
3
  def call(service)
4
- service.deploy_strategy || KuberKit.current_configuration.deploy_strategy
4
+ service.deployer_strategy || KuberKit.current_configuration.deployer_strategy
5
5
  end
6
6
  end
@@ -6,6 +6,10 @@ class KuberKit::Shell::AbstractShell
6
6
  raise KuberKit::NotImplementedError, "must be implemented"
7
7
  end
8
8
 
9
+ def interactive!(command)
10
+ raise KuberKit::NotImplementedError, "must be implemented"
11
+ end
12
+
9
13
  def read(file_path)
10
14
  raise KuberKit::NotImplementedError, "must be implemented"
11
15
  end
@@ -13,4 +13,18 @@ class KuberKit::Shell::Commands::DockerCommands
13
13
  def push(shell, tag_name)
14
14
  shell.exec!(%Q{docker push #{tag_name}})
15
15
  end
16
+
17
+ def get_container_id(shell, container_name, only_healthy: false, status: "running")
18
+ command_parts = []
19
+ command_parts << "docker ps -a -q"
20
+
21
+ if only_healthy
22
+ command_parts << "--filter=\"health=healthy\""
23
+ end
24
+
25
+ command_parts << "--filter=\"status=#{status}\""
26
+ command_parts << "--filter=\"name=#{container_name}\""
27
+
28
+ shell.exec!(command_parts.join(" "))
29
+ end
16
30
  end
@@ -0,0 +1,17 @@
1
+ class KuberKit::Shell::Commands::DockerComposeCommands
2
+ def run(shell, path, service:, command:, interactive: false)
3
+ command_parts = [
4
+ "docker-compose",
5
+ "-f #{path}",
6
+ "run",
7
+ service,
8
+ command
9
+ ]
10
+
11
+ if interactive
12
+ shell.interactive!(command_parts.join(" "))
13
+ else
14
+ shell.exec!(command_parts.join(" "))
15
+ end
16
+ end
17
+ end
@@ -16,9 +16,8 @@ class KuberKit::Shell::Commands::KubectlCommands
16
16
 
17
17
  command_parts += Array(command_list)
18
18
 
19
- # TODO: investigate how to do it with shell.
20
19
  if interactive
21
- system(command_parts.join(" "))
20
+ shell.interactive!(command_parts.join(" "))
22
21
  else
23
22
  shell.exec!(command_parts.join(" "))
24
23
  end
@@ -58,8 +57,8 @@ class KuberKit::Shell::Commands::KubectlCommands
58
57
  kubectl_run(shell, command, kubeconfig_path: kubeconfig_path, namespace: namespace)
59
58
  end
60
59
 
61
- def rolling_restart(shell, deployment_name, kubeconfig_path: nil, namespace: nil)
62
- patch_deployment(shell, deployment_name, {
60
+ def rolling_restart(shell, deployer_name, kubeconfig_path: nil, namespace: nil)
61
+ patch_deployment(shell, deployer_name, {
63
62
  spec: {
64
63
  template: {
65
64
  metadata: {
@@ -72,10 +71,10 @@ class KuberKit::Shell::Commands::KubectlCommands
72
71
  }, kubeconfig_path: kubeconfig_path, namespace: namespace)
73
72
  end
74
73
 
75
- def patch_deployment(shell, deployment_name, specs, kubeconfig_path: nil, namespace: nil)
74
+ def patch_deployment(shell, deployer_name, specs, kubeconfig_path: nil, namespace: nil)
76
75
  specs_json = JSON.dump(specs).gsub('"', '\"')
77
76
 
78
- command = %Q{patch deployment #{deployment_name} -p "#{specs_json}"}
77
+ command = %Q{patch deployment #{deployer_name} -p "#{specs_json}"}
79
78
 
80
79
  kubectl_run(shell, command, kubeconfig_path: kubeconfig_path, namespace: namespace)
81
80
  end
@@ -30,6 +30,20 @@ class KuberKit::Shell::LocalShell < KuberKit::Shell::AbstractShell
30
30
  result
31
31
  end
32
32
 
33
+ def interactive!(command, log_command: true)
34
+ command_number = command_counter.get_number.to_s.rjust(2, "0")
35
+
36
+ if log_command
37
+ logger.info("Interactive: [#{command_number}]: #{command.to_s.cyan}")
38
+ end
39
+
40
+ result = system(command)
41
+
42
+ if !$?.success?
43
+ raise ShellError, "Shell command failed: #{command}\r\n#{result}"
44
+ end
45
+ end
46
+
33
47
  def sync(local_path, remote_path, exclude: nil, delete: true)
34
48
  rsync_commands.rsync(self, local_path, remote_path, exclude: exclude, delete: delete)
35
49
  end
@@ -38,6 +38,10 @@ class KuberKit::Shell::SshShell < KuberKit::Shell::LocalShell
38
38
  raise ShellError.new(e.message)
39
39
  end
40
40
 
41
+ def interactive!(command, log_command: true)
42
+ raise "Currently interactive run is not supported for ssh shell."
43
+ end
44
+
41
45
  def sync(local_path, remote_path, exclude: nil, delete: true)
42
46
  rsync_commands.rsync(
43
47
  local_shell, local_path, remote_path,
@@ -0,0 +1,13 @@
1
+ class KuberKit::TemplateReader::ActionHandler
2
+ include KuberKit::Import[
3
+ "template_reader.reader",
4
+ "core.template_store",
5
+ ]
6
+
7
+ Contract KuberKit::Shell::AbstractShell, Symbol => Any
8
+ def call(shell, template_name)
9
+ template = template_store.get(template_name)
10
+
11
+ reader.read(shell, template)
12
+ end
13
+ end
@@ -2,22 +2,25 @@ class KuberKit::TemplateReader::Reader
2
2
  ReaderNotFoundError = Class.new(KuberKit::NotFoundError)
3
3
 
4
4
  include KuberKit::Import[
5
- "template_reader.artifact_file_reader",
5
+ "template_reader.strategies.artifact_file",
6
6
  ]
7
7
 
8
+ def initialize(**injected_deps)
9
+ super(injected_deps)
10
+ add_default_strategies
11
+ end
12
+
8
13
  def use_reader(template_reader, template_class:)
9
14
  @@readers ||= {}
10
15
 
11
- if !template_reader.is_a?(KuberKit::TemplateReader::AbstractTemplateReader)
12
- raise ArgumentError.new("should be an instance of KuberKit::TemplateReader::AbstractTemplateReader, got: #{template_reader.inspect}")
16
+ if !template_reader.is_a?(KuberKit::TemplateReader::Strategies::Abstract)
17
+ raise ArgumentError.new("should be an instance of KuberKit::TemplateReader::Strategies::Abstract, got: #{template_reader.inspect}")
13
18
  end
14
19
 
15
20
  @@readers[template_class] = template_reader
16
21
  end
17
22
 
18
23
  def read(shell, template)
19
- add_default_readers
20
-
21
24
  reader = @@readers[template.class]
22
25
 
23
26
  raise ReaderNotFoundError, "Can't find reader for template #{template}" if reader.nil?
@@ -25,11 +28,12 @@ class KuberKit::TemplateReader::Reader
25
28
  reader.read(shell, template)
26
29
  end
27
30
 
28
- def add_default_readers
29
- use_reader(artifact_file_reader, template_class: KuberKit::Core::Templates::ArtifactFile)
30
- end
31
-
32
31
  def reset!
33
32
  @@readers = {}
34
33
  end
34
+
35
+ private
36
+ def add_default_strategies
37
+ use_reader(artifact_file, template_class: KuberKit::Core::Templates::ArtifactFile)
38
+ end
35
39
  end
@@ -1,4 +1,4 @@
1
- class KuberKit::TemplateReader::AbstractTemplateReader
1
+ class KuberKit::TemplateReader::Strategies::Abstract
2
2
  def read(shell, template)
3
3
  raise KuberKit::NotImplementedError, "must be implemented"
4
4
  end
@@ -1,4 +1,4 @@
1
- class KuberKit::TemplateReader::ArtifactFileReader < KuberKit::TemplateReader::AbstractTemplateReader
1
+ class KuberKit::TemplateReader::Strategies::ArtifactFile < KuberKit::TemplateReader::Strategies::Abstract
2
2
  include KuberKit::Import[
3
3
  "core.artifact_store"
4
4
  ]
@@ -1,3 +1,3 @@
1
1
  module KuberKit
2
- VERSION = "0.2.7"
2
+ VERSION = "0.3.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kuber_kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Iskander Khaziev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-04 00:00:00.000000000 Z
11
+ date: 2020-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: contracts-lite
@@ -158,10 +158,12 @@ files:
158
158
  - TODO.md
159
159
  - bin/console
160
160
  - bin/kit
161
+ - example/app_data/docker_compose.yml
161
162
  - example/app_data/env_file.yml
162
163
  - example/app_data/service.yml
163
164
  - example/app_data/test.env
164
165
  - example/app_data/test.txt
166
+ - example/config.rb
165
167
  - example/configurations/review.rb
166
168
  - example/images/app_sources/Dockerfile
167
169
  - example/images/app_sources/build_context/source.rb
@@ -179,6 +181,7 @@ files:
179
181
  - example/infrastructure/env_files.rb
180
182
  - example/infrastructure/registries.rb
181
183
  - example/infrastructure/templates.rb
184
+ - example/services/compose_app.rb
182
185
  - example/services/env_file.rb
183
186
  - example/services/ruby_app.rb
184
187
  - kuber_kit.gemspec
@@ -212,6 +215,7 @@ files:
212
215
  - lib/kuber_kit/core/configuration_store.rb
213
216
  - lib/kuber_kit/core/context_helper/base_helper.rb
214
217
  - lib/kuber_kit/core/context_helper/context_helper_factory.rb
218
+ - lib/kuber_kit/core/context_helper/context_vars.rb
215
219
  - lib/kuber_kit/core/context_helper/image_helper.rb
216
220
  - lib/kuber_kit/core/context_helper/service_helper.rb
217
221
  - lib/kuber_kit/core/env_files/abstract_env_file.rb
@@ -256,6 +260,7 @@ files:
256
260
  - lib/kuber_kit/service_deployer/deployer.rb
257
261
  - lib/kuber_kit/service_deployer/service_list_resolver.rb
258
262
  - lib/kuber_kit/service_deployer/strategies/abstract.rb
263
+ - lib/kuber_kit/service_deployer/strategies/docker_compose.rb
259
264
  - lib/kuber_kit/service_deployer/strategies/kubernetes.rb
260
265
  - lib/kuber_kit/service_deployer/strategies/kubernetes_runner.rb
261
266
  - lib/kuber_kit/service_deployer/strategy_detector.rb
@@ -265,15 +270,17 @@ files:
265
270
  - lib/kuber_kit/shell/command_counter.rb
266
271
  - lib/kuber_kit/shell/commands/bash_commands.rb
267
272
  - lib/kuber_kit/shell/commands/docker_commands.rb
273
+ - lib/kuber_kit/shell/commands/docker_compose_commands.rb
268
274
  - lib/kuber_kit/shell/commands/git_commands.rb
269
275
  - lib/kuber_kit/shell/commands/kubectl_commands.rb
270
276
  - lib/kuber_kit/shell/commands/rsync_commands.rb
271
277
  - lib/kuber_kit/shell/local_shell.rb
272
278
  - lib/kuber_kit/shell/ssh_session.rb
273
279
  - lib/kuber_kit/shell/ssh_shell.rb
274
- - lib/kuber_kit/template_reader/abstract_template_reader.rb
275
- - lib/kuber_kit/template_reader/artifact_file_reader.rb
280
+ - lib/kuber_kit/template_reader/action_handler.rb
276
281
  - lib/kuber_kit/template_reader/reader.rb
282
+ - lib/kuber_kit/template_reader/strategies/abstract.rb
283
+ - lib/kuber_kit/template_reader/strategies/artifact_file.rb
277
284
  - lib/kuber_kit/tools/file_presence_checker.rb
278
285
  - lib/kuber_kit/tools/logger_factory.rb
279
286
  - lib/kuber_kit/ui.rb