kuber_kit 0.1.5 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +4 -2
  3. data/README.md +16 -3
  4. data/TODO.md +5 -4
  5. data/example/app_data/env_file.yml +10 -0
  6. data/example/configurations/review.rb +2 -1
  7. data/example/images/app_sources/Dockerfile +1 -1
  8. data/example/images/ruby_app/image.rb +4 -4
  9. data/example/infrastructure/build_servers.rb +8 -0
  10. data/example/infrastructure/templates.rb +5 -0
  11. data/example/services/env_file.rb +6 -0
  12. data/kuber_kit.gemspec +1 -0
  13. data/lib/kuber_kit.rb +51 -23
  14. data/lib/kuber_kit/actions/configuration_loader.rb +11 -2
  15. data/lib/kuber_kit/actions/env_file_reader.rb +8 -5
  16. data/lib/kuber_kit/actions/image_compiler.rb +18 -22
  17. data/lib/kuber_kit/actions/kubectl_applier.rb +6 -1
  18. data/lib/kuber_kit/actions/kubectl_attacher.rb +19 -0
  19. data/lib/kuber_kit/actions/service_deployer.rb +13 -3
  20. data/lib/kuber_kit/actions/service_reader.rb +9 -6
  21. data/lib/kuber_kit/actions/template_reader.rb +5 -0
  22. data/lib/kuber_kit/cli.rb +51 -20
  23. data/lib/kuber_kit/configs.rb +24 -22
  24. data/lib/kuber_kit/container.rb +43 -19
  25. data/lib/kuber_kit/core/artifacts/artifact_store.rb +12 -23
  26. data/lib/kuber_kit/core/build_servers/abstract_build_server.rb +21 -0
  27. data/lib/kuber_kit/core/build_servers/build_server.rb +24 -0
  28. data/lib/kuber_kit/core/build_servers/build_server_store.rb +18 -0
  29. data/lib/kuber_kit/core/configuration.rb +14 -4
  30. data/lib/kuber_kit/core/configuration_definition.rb +22 -1
  31. data/lib/kuber_kit/core/configuration_factory.rb +11 -1
  32. data/lib/kuber_kit/core/configuration_store.rb +14 -24
  33. data/lib/kuber_kit/core/context_helper/base_helper.rb +12 -7
  34. data/lib/kuber_kit/core/context_helper/context_helper_factory.rb +11 -8
  35. data/lib/kuber_kit/core/context_helper/service_helper.rb +7 -6
  36. data/lib/kuber_kit/core/env_files/env_file_store.rb +8 -23
  37. data/lib/kuber_kit/core/image_store.rb +8 -18
  38. data/lib/kuber_kit/core/registries/registry_store.rb +8 -23
  39. data/lib/kuber_kit/core/service.rb +6 -2
  40. data/lib/kuber_kit/core/service_factory.rb +4 -1
  41. data/lib/kuber_kit/core/service_store.rb +13 -23
  42. data/lib/kuber_kit/core/store.rb +48 -0
  43. data/lib/kuber_kit/core/templates/template_store.rb +12 -23
  44. data/lib/kuber_kit/env_file_reader/action_handler.rb +12 -0
  45. data/lib/kuber_kit/env_file_reader/reader.rb +4 -4
  46. data/lib/kuber_kit/env_file_reader/{abstract_env_file_reader.rb → strategies/abstract.rb} +1 -1
  47. data/lib/kuber_kit/env_file_reader/{artifact_file_reader.rb → strategies/artifact_file.rb} +1 -1
  48. data/lib/kuber_kit/image_compiler/action_handler.rb +21 -0
  49. data/lib/kuber_kit/image_compiler/build_server_pool.rb +30 -0
  50. data/lib/kuber_kit/image_compiler/build_server_pool_factory.rb +13 -0
  51. data/lib/kuber_kit/image_compiler/compiler.rb +2 -5
  52. data/lib/kuber_kit/image_compiler/image_build_dir_creator.rb +13 -7
  53. data/lib/kuber_kit/image_compiler/image_dependency_resolver.rb +25 -5
  54. data/lib/kuber_kit/preprocessing/file_preprocessor.rb +5 -4
  55. data/lib/kuber_kit/preprocessing/text_preprocessor.rb +1 -1
  56. data/lib/kuber_kit/service_deployer/action_handler.rb +16 -0
  57. data/lib/kuber_kit/service_deployer/deployer.rb +28 -6
  58. data/lib/kuber_kit/service_deployer/strategies/abstract.rb +1 -1
  59. data/lib/kuber_kit/service_deployer/strategies/kubernetes.rb +9 -4
  60. data/lib/kuber_kit/service_deployer/strategy_detector.rb +6 -0
  61. data/lib/kuber_kit/service_reader/action_handler.rb +13 -0
  62. data/lib/kuber_kit/{service_deployer/service_reader.rb → service_reader/reader.rb} +1 -1
  63. data/lib/kuber_kit/shell/abstract_shell.rb +4 -0
  64. data/lib/kuber_kit/shell/{bash_commands.rb → commands/bash_commands.rb} +1 -1
  65. data/lib/kuber_kit/shell/{docker_commands.rb → commands/docker_commands.rb} +1 -1
  66. data/lib/kuber_kit/shell/{git_commands.rb → commands/git_commands.rb} +1 -1
  67. data/lib/kuber_kit/shell/{kubectl_commands.rb → commands/kubectl_commands.rb} +25 -1
  68. data/lib/kuber_kit/shell/commands/rsync_commands.rb +32 -0
  69. data/lib/kuber_kit/shell/local_shell.rb +24 -5
  70. data/lib/kuber_kit/shell/ssh_session.rb +60 -0
  71. data/lib/kuber_kit/shell/ssh_shell.rb +77 -0
  72. data/lib/kuber_kit/tools/file_presence_checker.rb +6 -2
  73. data/lib/kuber_kit/ui/interactive.rb +8 -0
  74. data/lib/kuber_kit/ui/simple.rb +6 -0
  75. data/lib/kuber_kit/version.rb +2 -2
  76. metadata +41 -13
  77. data/lib/kuber_kit/preprocessing/dir_preprocessor.rb +0 -19
  78. data/lib/kuber_kit/service_deployer/service_restarter.rb +0 -37
  79. data/lib/kuber_kit/shell/rsync_commands.rb +0 -20
  80. data/lib/kuber_kit/tools/files_sync.rb +0 -10
@@ -0,0 +1,21 @@
1
+ class KuberKit::Core::BuildServers::AbstractBuildServer
2
+ include KuberKit::Extensions::Inspectable
3
+
4
+ attr_reader :name
5
+
6
+ def initialize(build_server_name)
7
+ @name = build_server_name
8
+ end
9
+
10
+ def host
11
+ raise KuberKit::NotImplementedError, "must be implemented"
12
+ end
13
+
14
+ def user
15
+ raise KuberKit::NotImplementedError, "must be implemented"
16
+ end
17
+
18
+ def port
19
+ raise KuberKit::NotImplementedError, "must be implemented"
20
+ end
21
+ end
@@ -0,0 +1,24 @@
1
+ class KuberKit::Core::BuildServers::BuildServer < KuberKit::Core::BuildServers::AbstractBuildServer
2
+ def setup(host:, user:, port:)
3
+ @host = host
4
+ @user = user
5
+ @port = port
6
+
7
+ self
8
+ end
9
+
10
+ def host
11
+ raise ArgumentError, "host is not set, please use #setup method" if @host.nil?
12
+ @host
13
+ end
14
+
15
+ def user
16
+ raise ArgumentError, "user is not set, please use #setup method" if @user.nil?
17
+ @user
18
+ end
19
+
20
+ def port
21
+ raise ArgumentError, "port is not set, please use #setup method" if @port.nil?
22
+ @port
23
+ end
24
+ end
@@ -0,0 +1,18 @@
1
+ class KuberKit::Core::BuildServers::BuildServerStore
2
+ def add(build_server)
3
+ store.add(build_server.name, build_server)
4
+ end
5
+
6
+ def get(build_server_name)
7
+ store.get(build_server_name)
8
+ end
9
+
10
+ def reset!
11
+ store.reset!
12
+ end
13
+
14
+ private
15
+ def store
16
+ @@store ||= KuberKit::Core::Store.new(KuberKit::Core::BuildServers::AbstractBuildServer)
17
+ end
18
+ end
@@ -1,5 +1,6 @@
1
1
  class KuberKit::Core::Configuration
2
- attr_reader :name, :artifacts, :registries, :env_files, :templates, :kubeconfig_path, :deploy_strategy
2
+ attr_reader :name, :artifacts, :registries, :env_files, :templates, :kubeconfig_path,
3
+ :deploy_strategy, :services_attributes, :build_servers
3
4
 
4
5
  Contract KeywordArgs[
5
6
  name: Symbol,
@@ -7,10 +8,13 @@ class KuberKit::Core::Configuration
7
8
  registries: Hash,
8
9
  env_files: Hash,
9
10
  templates: Hash,
10
- kubeconfig_path: Maybe[String],
11
- deploy_strategy: Symbol
11
+ kubeconfig_path: Maybe[String],
12
+ deploy_strategy: Symbol,
13
+ services_attributes: HashOf[Symbol => Hash],
14
+ build_servers: ArrayOf[KuberKit::Core::BuildServers::AbstractBuildServer]
12
15
  ] => Any
13
- def initialize(name:, artifacts:, registries:, env_files:, templates:, kubeconfig_path:, deploy_strategy:)
16
+ def initialize(name:, artifacts:, registries:, env_files:, templates:, kubeconfig_path:,
17
+ deploy_strategy:, services_attributes:, build_servers:)
14
18
  @name = name
15
19
  @artifacts = artifacts
16
20
  @registries = registries
@@ -18,5 +22,11 @@ class KuberKit::Core::Configuration
18
22
  @templates = templates
19
23
  @kubeconfig_path = kubeconfig_path
20
24
  @deploy_strategy = deploy_strategy
25
+ @services_attributes = services_attributes
26
+ @build_servers = build_servers
27
+ end
28
+
29
+ def service_attributes(service_name)
30
+ services_attributes[service_name.to_sym] || {}
21
31
  end
22
32
  end
@@ -10,6 +10,9 @@ class KuberKit::Core::ConfigurationDefinition
10
10
  @registries = {}
11
11
  @env_files = {}
12
12
  @templates = {}
13
+ @build_servers = []
14
+ @enabled_services = []
15
+ @services_attributes = {}
13
16
  end
14
17
 
15
18
  def to_attrs
@@ -20,7 +23,10 @@ class KuberKit::Core::ConfigurationDefinition
20
23
  env_files: @env_files,
21
24
  templates: @templates,
22
25
  kubeconfig_path: @kubeconfig_path,
23
- deploy_strategy: @deploy_strategy
26
+ deploy_strategy: @deploy_strategy,
27
+ enabled_services: @enabled_services,
28
+ build_servers: @build_servers,
29
+ services_attributes: @services_attributes
24
30
  )
25
31
  end
26
32
 
@@ -60,6 +66,14 @@ class KuberKit::Core::ConfigurationDefinition
60
66
  self
61
67
  end
62
68
 
69
+ def use_build_server(build_server_name)
70
+ unless @build_servers.include?(build_server_name)
71
+ @build_servers.push(build_server_name)
72
+ end
73
+
74
+ self
75
+ end
76
+
63
77
  def kubeconfig_path(path)
64
78
  @kubeconfig_path = path
65
79
 
@@ -71,4 +85,11 @@ class KuberKit::Core::ConfigurationDefinition
71
85
 
72
86
  self
73
87
  end
88
+
89
+ def enabled_services(services_hash)
90
+ @enabled_services += services_hash.keys.map(&:to_sym)
91
+ @services_attributes = @services_attributes.merge(services_hash)
92
+
93
+ self
94
+ end
74
95
  end
@@ -6,6 +6,7 @@ class KuberKit::Core::ConfigurationFactory
6
6
  "core.artifact_store",
7
7
  "core.env_file_store",
8
8
  "core.template_store",
9
+ "core.build_server_store",
9
10
  "configs"
10
11
  ]
11
12
 
@@ -16,6 +17,7 @@ class KuberKit::Core::ConfigurationFactory
16
17
  registries = fetch_registries(configuration_attrs.registries)
17
18
  env_files = fetch_env_files(configuration_attrs.env_files)
18
19
  templates = fetch_templates(configuration_attrs.templates)
20
+ build_servers = fetch_build_servers(configuration_attrs.build_servers)
19
21
 
20
22
  KuberKit::Core::Configuration.new(
21
23
  name: configuration_attrs.name,
@@ -24,7 +26,9 @@ class KuberKit::Core::ConfigurationFactory
24
26
  env_files: env_files,
25
27
  templates: templates,
26
28
  kubeconfig_path: configuration_attrs.kubeconfig_path,
27
- deploy_strategy: configuration_attrs.deploy_strategy || configs.deploy_strategy
29
+ deploy_strategy: configuration_attrs.deploy_strategy || configs.deploy_strategy,
30
+ services_attributes: configuration_attrs.services_attributes,
31
+ build_servers: build_servers
28
32
  )
29
33
  end
30
34
 
@@ -60,4 +64,10 @@ class KuberKit::Core::ConfigurationFactory
60
64
  end
61
65
  result
62
66
  end
67
+
68
+ def fetch_build_servers(build_servers)
69
+ build_servers.map do |build_server_name|
70
+ build_server_store.get(build_server_name)
71
+ end
72
+ end
63
73
  end
@@ -1,7 +1,4 @@
1
1
  class KuberKit::Core::ConfigurationStore
2
- NotFoundError = Class.new(KuberKit::NotFoundError)
3
- AlreadyAddedError = Class.new(KuberKit::Error)
4
-
5
2
  include KuberKit::Import[
6
3
  "core.configuration_factory",
7
4
  "core.configuration_definition_factory",
@@ -16,24 +13,12 @@ class KuberKit::Core::ConfigurationStore
16
13
  end
17
14
 
18
15
  def add_definition(configuration_definition)
19
- @@configuration_definitions ||= {}
20
-
21
- unless @@configuration_definitions[configuration_definition.configuration_name].nil?
22
- raise AlreadyAddedError, "image #{configuration_definition.configuration_name} was already added"
23
- end
24
-
25
- @@configuration_definitions[configuration_definition.configuration_name] = configuration_definition
16
+ definitions_store.add(configuration_definition.configuration_name, configuration_definition)
26
17
  end
27
18
 
28
19
  Contract Symbol => Any
29
20
  def get_definition(configuration_name)
30
- @@configuration_definitions ||= {}
31
-
32
- if @@configuration_definitions[configuration_name].nil?
33
- raise NotFoundError, "configuration #{configuration_name} not found"
34
- end
35
-
36
- @@configuration_definitions[configuration_name]
21
+ definitions_store.get(configuration_name)
37
22
  end
38
23
 
39
24
  Contract Symbol => Any
@@ -57,18 +42,23 @@ class KuberKit::Core::ConfigurationStore
57
42
  end
58
43
 
59
44
  def reset!
60
- @@configuration_definitions = {}
61
- end
62
-
63
- def all_definitions
64
- @@configuration_definitions ||= {}
45
+ definitions_store.reset!
65
46
  end
66
47
 
67
48
  def count
68
- all_definitions.count
49
+ definitions_store.size
69
50
  end
70
51
 
71
52
  def exists?(configuration_name)
72
- !all_definitions[configuration_name].nil?
53
+ definitions_store.exists?(configuration_name)
73
54
  end
55
+
56
+ def all_definitions
57
+ definitions_store.items
58
+ end
59
+
60
+ private
61
+ def definitions_store
62
+ @@definitions_store ||= KuberKit::Core::Store.new(KuberKit::Core::ConfigurationDefinition)
63
+ end
74
64
  end
@@ -1,25 +1,30 @@
1
1
  class KuberKit::Core::ContextHelper::BaseHelper
2
2
  CONTRACT = RespondTo[:get_binding]
3
3
 
4
- attr_reader :shell, :artifact_store, :image_store
4
+ attr_reader :shell, :artifact_store, :image_store, :env_file_reader
5
5
 
6
- def initialize(image_store:, artifact_store:, shell:)
7
- @image_store = image_store
8
- @artifact_store = artifact_store
9
- @shell = shell
6
+ def initialize(image_store:, artifact_store:, shell:, env_file_reader:)
7
+ @image_store = image_store
8
+ @artifact_store = artifact_store
9
+ @shell = shell
10
+ @env_file_reader = env_file_reader
10
11
  end
11
12
 
12
13
  def image_url(image_name)
13
- image = @image_store.get_image(image_name)
14
+ image = @image_store.get_image(image_name.to_sym)
14
15
 
15
16
  image.remote_registry_url
16
17
  end
17
18
 
18
19
  def artifact_path(name, file_name = nil)
19
- artifact = @artifact_store.get(name)
20
+ artifact = @artifact_store.get(name.to_sym)
20
21
  [artifact.cloned_path, file_name].compact.join("/")
21
22
  end
22
23
 
24
+ def env_file(env_file_name)
25
+ @env_file_reader.call(@shell, env_file_name)
26
+ end
27
+
23
28
  def configuration_name
24
29
  KuberKit.current_configuration.name
25
30
  end
@@ -1,23 +1,26 @@
1
1
  class KuberKit::Core::ContextHelper::ContextHelperFactory
2
2
  include KuberKit::Import[
3
3
  "core.image_store",
4
- "core.artifact_store"
4
+ "core.artifact_store",
5
+ env_file_reader: "env_file_reader.action_handler"
5
6
  ]
6
7
 
7
8
  def build_image_context(shell)
8
9
  KuberKit::Core::ContextHelper::ImageHelper.new(
9
- image_store: image_store,
10
- artifact_store: artifact_store,
11
- shell: shell
10
+ image_store: image_store,
11
+ artifact_store: artifact_store,
12
+ shell: shell,
13
+ env_file_reader: env_file_reader
12
14
  )
13
15
  end
14
16
 
15
17
  def build_service_context(shell, service)
16
18
  KuberKit::Core::ContextHelper::ServiceHelper.new(
17
- image_store: image_store,
18
- artifact_store: artifact_store,
19
- shell: shell,
20
- service: service
19
+ image_store: image_store,
20
+ artifact_store: artifact_store,
21
+ shell: shell,
22
+ env_file_reader: env_file_reader,
23
+ service: service,
21
24
  )
22
25
  end
23
26
  end
@@ -1,9 +1,10 @@
1
1
  class KuberKit::Core::ContextHelper::ServiceHelper < KuberKit::Core::ContextHelper::BaseHelper
2
- def initialize(image_store:, artifact_store:, shell:, service:)
2
+ def initialize(image_store:, artifact_store:, shell:, env_file_reader:, service:)
3
3
  super(
4
- image_store: image_store,
5
- artifact_store: artifact_store,
6
- shell: shell
4
+ image_store: image_store,
5
+ artifact_store: artifact_store,
6
+ shell: shell,
7
+ env_file_reader: env_file_reader
7
8
  )
8
9
  @service = service
9
10
  end
@@ -16,7 +17,7 @@ class KuberKit::Core::ContextHelper::ServiceHelper < KuberKit::Core::ContextHelp
16
17
  @service.uri
17
18
  end
18
19
 
19
- def attribute(attribute_name)
20
- @service.attribute(attribute_name)
20
+ def attribute(attribute_name, default: nil)
21
+ @service.attribute(attribute_name, default: default)
21
22
  end
22
23
  end
@@ -1,19 +1,6 @@
1
1
  class KuberKit::Core::EnvFiles::EnvFileStore
2
- NotFoundError = Class.new(KuberKit::NotFoundError)
3
- AlreadyAddedError = Class.new(KuberKit::Error)
4
-
5
2
  def add(env_file)
6
- @@env_files ||= {}
7
-
8
- if !env_file.is_a?(KuberKit::Core::EnvFiles::AbstractEnvFile)
9
- raise ArgumentError.new("should be an instance of KuberKit::Core::EnvFiles::AbstractEnvFile, got: #{env_file.inspect}")
10
- end
11
-
12
- unless @@env_files[env_file.name].nil?
13
- raise AlreadyAddedError, "env_file #{env_file.name} was already added"
14
- end
15
-
16
- @@env_files[env_file.name] = env_file
3
+ store.add(env_file.name, env_file)
17
4
  end
18
5
 
19
6
  def get(env_file_name)
@@ -24,14 +11,7 @@ class KuberKit::Core::EnvFiles::EnvFileStore
24
11
  end
25
12
 
26
13
  def get_global(env_file_name)
27
- @@env_files ||= {}
28
- env_file = @@env_files[env_file_name]
29
-
30
- if env_file.nil?
31
- raise NotFoundError, "env_file '#{env_file_name}' not found"
32
- end
33
-
34
- env_file
14
+ store.get(env_file_name)
35
15
  end
36
16
 
37
17
  def get_from_configuration(env_file_name)
@@ -40,6 +20,11 @@ class KuberKit::Core::EnvFiles::EnvFileStore
40
20
  end
41
21
 
42
22
  def reset!
43
- @@env_files = {}
23
+ store.reset!
44
24
  end
25
+
26
+ private
27
+ def store
28
+ @@store ||= KuberKit::Core::Store.new(KuberKit::Core::EnvFiles::AbstractEnvFile)
29
+ end
45
30
  end
@@ -1,7 +1,4 @@
1
1
  class KuberKit::Core::ImageStore
2
- NotFoundError = Class.new(KuberKit::Error)
3
- AlreadyAddedError = Class.new(KuberKit::Error)
4
-
5
2
  include KuberKit::Import[
6
3
  "core.image_factory",
7
4
  "core.image_definition_factory",
@@ -16,24 +13,12 @@ class KuberKit::Core::ImageStore
16
13
  end
17
14
 
18
15
  def add_definition(image_definition)
19
- @@image_definitions ||= {}
20
-
21
- unless @@image_definitions[image_definition.image_name].nil?
22
- raise AlreadyAddedError, "image #{image_definition.image_name} was already added"
23
- end
24
-
25
- @@image_definitions[image_definition.image_name] = image_definition
16
+ definitions_store.add(image_definition.image_name, image_definition)
26
17
  end
27
18
 
28
19
  Contract Symbol => Any
29
20
  def get_definition(image_name)
30
- @@image_definitions ||= {}
31
-
32
- if @@image_definitions[image_name].nil?
33
- raise NotFoundError, "image #{image_name} not found"
34
- end
35
-
36
- @@image_definitions[image_name]
21
+ definitions_store.get(image_name)
37
22
  end
38
23
 
39
24
  Contract Symbol => Any
@@ -57,6 +42,11 @@ class KuberKit::Core::ImageStore
57
42
  end
58
43
 
59
44
  def reset!
60
- @@image_definitions = {}
45
+ definitions_store.reset!
61
46
  end
47
+
48
+ private
49
+ def definitions_store
50
+ @@definitions_store ||= KuberKit::Core::Store.new(KuberKit::Core::ImageDefinition)
51
+ end
62
52
  end