kuber_kit 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.rspec +3 -0
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +7 -0
  7. data/Gemfile +8 -0
  8. data/Gemfile.lock +67 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +19 -0
  11. data/Rakefile +6 -0
  12. data/TODO.md +2 -0
  13. data/bin/console +14 -0
  14. data/bin/kit +8 -0
  15. data/example/app_data/service.yml +7 -0
  16. data/example/app_data/test.env +2 -0
  17. data/example/app_data/test.txt +1 -0
  18. data/example/configurations/review.rb +4 -0
  19. data/example/images/app_sources/Dockerfile +6 -0
  20. data/example/images/app_sources/build_context/source.rb +2 -0
  21. data/example/images/app_sources/image.rb +4 -0
  22. data/example/images/ruby/Dockerfile +1 -0
  23. data/example/images/ruby/image.rb +3 -0
  24. data/example/images/ruby_app/Dockerfile +11 -0
  25. data/example/images/ruby_app/build_context/example_file.txt +2 -0
  26. data/example/images/ruby_app/image.rb +15 -0
  27. data/example/images/ruby_app2/Dockerfile +8 -0
  28. data/example/images/ruby_app2/build_context/example_file.txt +2 -0
  29. data/example/images/ruby_app2/image.rb +4 -0
  30. data/example/infrastructure/artifacts.rb +13 -0
  31. data/example/infrastructure/env_files.rb +4 -0
  32. data/example/infrastructure/registries.rb +4 -0
  33. data/example/infrastructure/services.rb +3 -0
  34. data/example/infrastructure/templates.rb +4 -0
  35. data/kuber_kit.gemspec +35 -0
  36. data/lib/kuber_kit.rb +205 -0
  37. data/lib/kuber_kit/actions/configuration_loader.rb +74 -0
  38. data/lib/kuber_kit/actions/env_file_reader.rb +17 -0
  39. data/lib/kuber_kit/actions/image_compiler.rb +56 -0
  40. data/lib/kuber_kit/actions/kubectl_applier.rb +17 -0
  41. data/lib/kuber_kit/actions/service_applier.rb +39 -0
  42. data/lib/kuber_kit/actions/service_reader.rb +17 -0
  43. data/lib/kuber_kit/actions/template_reader.rb +17 -0
  44. data/lib/kuber_kit/artifacts_sync/abstract_artifact_resolver.rb +5 -0
  45. data/lib/kuber_kit/artifacts_sync/artifacts_updater.rb +42 -0
  46. data/lib/kuber_kit/artifacts_sync/git_artifact_resolver.rb +31 -0
  47. data/lib/kuber_kit/artifacts_sync/null_artifact_resolver.rb +7 -0
  48. data/lib/kuber_kit/cli.rb +72 -0
  49. data/lib/kuber_kit/configs.rb +42 -0
  50. data/lib/kuber_kit/container.rb +207 -0
  51. data/lib/kuber_kit/core/artifacts/abstract_artifact.rb +13 -0
  52. data/lib/kuber_kit/core/artifacts/artifact_store.rb +45 -0
  53. data/lib/kuber_kit/core/artifacts/git.rb +22 -0
  54. data/lib/kuber_kit/core/artifacts/local.rb +14 -0
  55. data/lib/kuber_kit/core/configuration.rb +20 -0
  56. data/lib/kuber_kit/core/configuration_definition.rb +67 -0
  57. data/lib/kuber_kit/core/configuration_definition_factory.rb +5 -0
  58. data/lib/kuber_kit/core/configuration_factory.rb +61 -0
  59. data/lib/kuber_kit/core/configuration_store.rb +72 -0
  60. data/lib/kuber_kit/core/context_helper/base_helper.rb +28 -0
  61. data/lib/kuber_kit/core/context_helper/context_helper_factory.rb +23 -0
  62. data/lib/kuber_kit/core/context_helper/image_helper.rb +2 -0
  63. data/lib/kuber_kit/core/context_helper/service_helper.rb +19 -0
  64. data/lib/kuber_kit/core/env_files/abstract_env_file.rb +9 -0
  65. data/lib/kuber_kit/core/env_files/artifact_file.rb +9 -0
  66. data/lib/kuber_kit/core/env_files/env_file_store.rb +45 -0
  67. data/lib/kuber_kit/core/image.rb +39 -0
  68. data/lib/kuber_kit/core/image_definition.rb +85 -0
  69. data/lib/kuber_kit/core/image_definition_factory.rb +5 -0
  70. data/lib/kuber_kit/core/image_factory.rb +40 -0
  71. data/lib/kuber_kit/core/image_store.rb +60 -0
  72. data/lib/kuber_kit/core/registries/abstract_registry.rb +25 -0
  73. data/lib/kuber_kit/core/registries/registry.rb +24 -0
  74. data/lib/kuber_kit/core/registries/registry_store.rb +49 -0
  75. data/lib/kuber_kit/core/service.rb +14 -0
  76. data/lib/kuber_kit/core/service_definition.rb +33 -0
  77. data/lib/kuber_kit/core/service_definition_factory.rb +5 -0
  78. data/lib/kuber_kit/core/service_factory.rb +17 -0
  79. data/lib/kuber_kit/core/service_store.rb +68 -0
  80. data/lib/kuber_kit/core/templates/abstract_template.rb +9 -0
  81. data/lib/kuber_kit/core/templates/artifact_file.rb +9 -0
  82. data/lib/kuber_kit/core/templates/template_store.rb +45 -0
  83. data/lib/kuber_kit/env_file_reader/abstract_env_file_reader.rb +5 -0
  84. data/lib/kuber_kit/env_file_reader/artifact_file_reader.rb +86 -0
  85. data/lib/kuber_kit/env_file_reader/reader.rb +35 -0
  86. data/lib/kuber_kit/extensions/colored_string.rb +43 -0
  87. data/lib/kuber_kit/extensions/contracts.rb +4 -0
  88. data/lib/kuber_kit/extensions/indocker_compat.rb +19 -0
  89. data/lib/kuber_kit/extensions/inspectable.rb +10 -0
  90. data/lib/kuber_kit/image_compiler/compiler.rb +21 -0
  91. data/lib/kuber_kit/image_compiler/image_build_dir_creator.rb +37 -0
  92. data/lib/kuber_kit/image_compiler/image_builder.rb +26 -0
  93. data/lib/kuber_kit/image_compiler/image_dependency_resolver.rb +40 -0
  94. data/lib/kuber_kit/image_compiler/version_tag_builder.rb +5 -0
  95. data/lib/kuber_kit/preprocessing/dir_preprocessor.rb +19 -0
  96. data/lib/kuber_kit/preprocessing/file_preprocessor.rb +33 -0
  97. data/lib/kuber_kit/preprocessing/text_preprocessor.rb +7 -0
  98. data/lib/kuber_kit/service_deployer/service_list_resolver.rb +56 -0
  99. data/lib/kuber_kit/service_deployer/service_reader.rb +20 -0
  100. data/lib/kuber_kit/shell/abstract_shell.rb +20 -0
  101. data/lib/kuber_kit/shell/bash_commands.rb +25 -0
  102. data/lib/kuber_kit/shell/command_counter.rb +19 -0
  103. data/lib/kuber_kit/shell/docker_commands.rb +16 -0
  104. data/lib/kuber_kit/shell/git_commands.rb +28 -0
  105. data/lib/kuber_kit/shell/kubectl_commands.rb +12 -0
  106. data/lib/kuber_kit/shell/local_shell.rb +64 -0
  107. data/lib/kuber_kit/shell/rsync_commands.rb +20 -0
  108. data/lib/kuber_kit/template_reader/abstract_template_reader.rb +5 -0
  109. data/lib/kuber_kit/template_reader/artifact_file_reader.rb +14 -0
  110. data/lib/kuber_kit/template_reader/reader.rb +35 -0
  111. data/lib/kuber_kit/tools/file_presence_checker.rb +25 -0
  112. data/lib/kuber_kit/tools/files_sync.rb +10 -0
  113. data/lib/kuber_kit/tools/logger_factory.rb +34 -0
  114. data/lib/kuber_kit/ui.rb +18 -0
  115. data/lib/kuber_kit/ui/interactive.rb +44 -0
  116. data/lib/kuber_kit/ui/simple.rb +75 -0
  117. data/lib/kuber_kit/version.rb +3 -0
  118. metadata +273 -0
@@ -0,0 +1,72 @@
1
+ class KuberKit::Core::ConfigurationStore
2
+ NotFoundError = Class.new(KuberKit::NotFoundError)
3
+ AlreadyAddedError = Class.new(KuberKit::Error)
4
+
5
+ include KuberKit::Import[
6
+ "core.configuration_factory",
7
+ "core.configuration_definition_factory",
8
+ "shell.local_shell",
9
+ "tools.logger"
10
+ ]
11
+
12
+ def define(configuration_name)
13
+ definition = configuration_definition_factory.create(configuration_name)
14
+ add_definition(definition)
15
+ definition
16
+ end
17
+
18
+ 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
26
+ end
27
+
28
+ def get_definition(configuration_name)
29
+ @@configuration_definitions ||= {}
30
+
31
+ if @@configuration_definitions[configuration_name].nil?
32
+ raise NotFoundError, "configuration #{configuration_name} not found"
33
+ end
34
+
35
+ @@configuration_definitions[configuration_name]
36
+ end
37
+
38
+ def get_configuration(configuration_name)
39
+ definition = get_definition(configuration_name)
40
+
41
+ configuration_factory.create(definition)
42
+ end
43
+
44
+ def load_definitions(dir_path)
45
+ files = local_shell.recursive_list_files(dir_path).each do |path|
46
+ load_definition(path)
47
+ end
48
+ rescue KuberKit::Shell::AbstractShell::DirNotFoundError
49
+ logger.warn("Directory with configurations not found: #{dir_path}")
50
+ []
51
+ end
52
+
53
+ def load_definition(file_path)
54
+ require(file_path)
55
+ end
56
+
57
+ def reset!
58
+ @@configuration_definitions = {}
59
+ end
60
+
61
+ def all_definitions
62
+ @@configuration_definitions ||= {}
63
+ end
64
+
65
+ def count
66
+ all_definitions.count
67
+ end
68
+
69
+ def exists?(configuration_name)
70
+ !all_definitions[configuration_name].nil?
71
+ end
72
+ end
@@ -0,0 +1,28 @@
1
+ class KuberKit::Core::ContextHelper::BaseHelper
2
+ attr_reader :shell, :artifact_store, :image_store
3
+
4
+ def initialize(image_store:, artifact_store:, shell:)
5
+ @image_store = image_store
6
+ @artifact_store = artifact_store
7
+ @shell = shell
8
+ end
9
+
10
+ def image_url(image_name)
11
+ image = @image_store.get_image(image_name)
12
+
13
+ image.remote_registry_url
14
+ end
15
+
16
+ def artifact_path(name, file_name = nil)
17
+ artifact = @artifact_store.get(name)
18
+ [artifact.cloned_path, file_name].compact.join("/")
19
+ end
20
+
21
+ def configuration_name
22
+ KuberKit.current_configuration.name
23
+ end
24
+
25
+ def get_binding
26
+ binding
27
+ end
28
+ end
@@ -0,0 +1,23 @@
1
+ class KuberKit::Core::ContextHelper::ContextHelperFactory
2
+ include KuberKit::Import[
3
+ "core.image_store",
4
+ "core.artifact_store"
5
+ ]
6
+
7
+ def build_image_context(shell)
8
+ KuberKit::Core::ContextHelper::ImageHelper.new(
9
+ image_store: image_store,
10
+ artifact_store: artifact_store,
11
+ shell: shell
12
+ )
13
+ end
14
+
15
+ def build_service_context(shell, service)
16
+ KuberKit::Core::ContextHelper::ServiceHelper.new(
17
+ image_store: image_store,
18
+ artifact_store: artifact_store,
19
+ shell: shell,
20
+ service: service
21
+ )
22
+ end
23
+ end
@@ -0,0 +1,2 @@
1
+ class KuberKit::Core::ContextHelper::ImageHelper < KuberKit::Core::ContextHelper::BaseHelper
2
+ end
@@ -0,0 +1,19 @@
1
+ class KuberKit::Core::ContextHelper::ServiceHelper < KuberKit::Core::ContextHelper::BaseHelper
2
+ def initialize(image_store:, artifact_store:, shell:, service:)
3
+ super(
4
+ image_store: image_store,
5
+ artifact_store: artifact_store,
6
+ shell: shell
7
+ )
8
+ @service = service
9
+ end
10
+
11
+ def service_name
12
+ @service.name.to_s
13
+ end
14
+
15
+ def service_uri
16
+ service_name
17
+ .sub("_", "-")
18
+ end
19
+ end
@@ -0,0 +1,9 @@
1
+ class KuberKit::Core::EnvFiles::AbstractEnvFile
2
+ include KuberKit::Extensions::Inspectable
3
+
4
+ attr_reader :name
5
+
6
+ def initialize(env_file_name)
7
+ @name = env_file_name
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ class KuberKit::Core::EnvFiles::ArtifactFile < KuberKit::Core::EnvFiles::AbstractEnvFile
2
+ attr_reader :artifact_name, :file_path
3
+
4
+ def initialize(env_file_name, artifact_name:, file_path:)
5
+ super(env_file_name)
6
+ @artifact_name = artifact_name
7
+ @file_path = file_path
8
+ end
9
+ end
@@ -0,0 +1,45 @@
1
+ class KuberKit::Core::EnvFiles::EnvFileStore
2
+ NotFoundError = Class.new(KuberKit::NotFoundError)
3
+ AlreadyAddedError = Class.new(KuberKit::Error)
4
+
5
+ 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
17
+ end
18
+
19
+ def get(env_file_name)
20
+ env_file = get_from_configuration(env_file_name) ||
21
+ get_global(env_file_name)
22
+
23
+ env_file
24
+ end
25
+
26
+ 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
35
+ end
36
+
37
+ def get_from_configuration(env_file_name)
38
+ env_files = KuberKit.current_configuration.env_files
39
+ env_files[env_file_name]
40
+ end
41
+
42
+ def reset!
43
+ @@env_files = {}
44
+ end
45
+ end
@@ -0,0 +1,39 @@
1
+ class KuberKit::Core::Image
2
+ attr_reader :name, :dependencies, :registry, :dockerfile_path, :build_vars, :build_context_dir, :tag, :before_build_callback, :after_build_callback
3
+
4
+ Contract KeywordArgs[
5
+ name: Symbol,
6
+ dependencies: ArrayOf[Symbol],
7
+ registry: Maybe[KuberKit::Core::Registries::Registry],
8
+ dockerfile_path: String,
9
+ build_vars: Hash,
10
+ build_context_dir: Maybe[String],
11
+ tag: String,
12
+ before_build_callback: Maybe[Proc],
13
+ after_build_callback: Maybe[Proc]
14
+ ] => Any
15
+ def initialize(name:, dependencies:, registry:, dockerfile_path:, build_vars:, build_context_dir:, tag:, before_build_callback:, after_build_callback:)
16
+ @name = name
17
+ @dependencies = dependencies
18
+ @registry = registry
19
+ @dockerfile_path = dockerfile_path
20
+ @build_vars = build_vars
21
+ @build_context_dir = build_context_dir
22
+ @tag = tag
23
+ @before_build_callback = before_build_callback
24
+ @after_build_callback = after_build_callback
25
+ end
26
+
27
+ def registry_url
28
+ "#{registry.path}/#{name}:#{tag}"
29
+ end
30
+
31
+ def remote_registry_url
32
+ "#{registry.remote_path}/#{name}:#{tag}"
33
+ end
34
+
35
+ def build_args
36
+ puts "WARNING: build_args is deprecated, please use build_vars instead"
37
+ build_vars
38
+ end
39
+ end
@@ -0,0 +1,85 @@
1
+ class KuberKit::Core::ImageDefinition
2
+ attr_reader :image_name, :path,
3
+ :dependencies, :registry_name, :dockerfile_path,
4
+ :build_vars, :build_context_dir, :tag,
5
+ :before_build_callback, :after_build_callback
6
+
7
+ Contract Or[Symbol, String], Maybe[String] => Any
8
+ def initialize(image_name, image_dir)
9
+ @image_name = image_name.to_sym
10
+ @image_dir = image_dir
11
+ @dependencies = []
12
+ end
13
+
14
+ def to_image_attrs
15
+ OpenStruct.new(
16
+ name: @image_name,
17
+ dir: @image_dir,
18
+ dependencies: @dependencies,
19
+ registry_name: get_value(@registry_name),
20
+ dockerfile_path: get_value(@dockerfile_path),
21
+ build_vars: get_value(@build_vars),
22
+ build_context_dir: get_value(@build_context_dir),
23
+ tag: get_value(@tag),
24
+ before_build_callback: @before_build_callback,
25
+ after_build_callback: @after_build_callback
26
+ )
27
+ end
28
+
29
+ def depends_on(*value, &block)
30
+ @dependencies = Array(value).flatten
31
+ self
32
+ end
33
+
34
+ def registry(value = nil, &block)
35
+ @registry_name = block_given? ? block : value
36
+
37
+ self
38
+ end
39
+
40
+ def dockerfile(value = nil, &block)
41
+ @dockerfile_path = block_given? ? block : value
42
+
43
+ self
44
+ end
45
+
46
+ def build_args(value = nil, &block)
47
+ puts "WARNING: build_args is deprecated, please use build_vars instead"
48
+ build_vars(value, *block)
49
+ end
50
+
51
+ def build_vars(value = nil, &block)
52
+ @build_vars = block_given? ? block : value
53
+
54
+ self
55
+ end
56
+
57
+ def build_context(value = nil, &block)
58
+ @build_context_dir = block_given? ? block : value
59
+
60
+ self
61
+ end
62
+
63
+ def tag(value = nil, &block)
64
+ @tag = block_given? ? block : value
65
+
66
+ self
67
+ end
68
+
69
+ def before_build(&block)
70
+ @before_build_callback = block
71
+
72
+ self
73
+ end
74
+
75
+ def after_build(&block)
76
+ @after_build_callback = block
77
+
78
+ self
79
+ end
80
+
81
+ private
82
+ def get_value(variable)
83
+ variable.is_a?(Proc) ? variable.call : variable
84
+ end
85
+ end
@@ -0,0 +1,5 @@
1
+ class KuberKit::Core::ImageDefinitionFactory
2
+ def create(image_name, image_dir)
3
+ KuberKit::Core::ImageDefinition.new(image_name, image_dir)
4
+ end
5
+ end
@@ -0,0 +1,40 @@
1
+ class KuberKit::Core::ImageFactory
2
+ include KuberKit::Import[
3
+ "configs",
4
+ "tools.file_presence_checker",
5
+ "core.registry_store"
6
+ ]
7
+
8
+ def create(definition)
9
+ image_attrs = definition.to_image_attrs
10
+
11
+ dockerfile_path = image_attrs.dockerfile_path || File.join(image_attrs.dir, configs.image_dockerfile_name)
12
+ file_presence_checker.check_file!(dockerfile_path)
13
+
14
+ if image_attrs.build_context_dir
15
+ file_presence_checker.check_dir!(image_attrs.build_context_dir)
16
+ build_context_dir = image_attrs.build_context_dir
17
+ else
18
+ default_dir = File.join(image_attrs.dir, configs.image_build_context_dir)
19
+ build_context_dir = file_presence_checker.dir_exists?(default_dir) ? default_dir : nil
20
+ end
21
+
22
+ if image_attrs.registry_name
23
+ registry = registry_store.get(image_attrs.registry_name)
24
+ else
25
+ registry = registry_store.default_registry
26
+ end
27
+
28
+ KuberKit::Core::Image.new(
29
+ name: image_attrs.name,
30
+ dependencies: image_attrs.dependencies,
31
+ registry: registry,
32
+ dockerfile_path: dockerfile_path,
33
+ build_vars: image_attrs.build_vars || {},
34
+ build_context_dir: build_context_dir,
35
+ tag: image_attrs.tag || configs.image_tag,
36
+ before_build_callback: image_attrs.before_build_callback,
37
+ after_build_callback: image_attrs.after_build_callback
38
+ )
39
+ end
40
+ end
@@ -0,0 +1,60 @@
1
+ class KuberKit::Core::ImageStore
2
+ NotFoundError = Class.new(KuberKit::Error)
3
+ AlreadyAddedError = Class.new(KuberKit::Error)
4
+
5
+ include KuberKit::Import[
6
+ "core.image_factory",
7
+ "core.image_definition_factory",
8
+ "shell.local_shell",
9
+ "tools.logger"
10
+ ]
11
+
12
+ def define(image_name, image_dir = nil)
13
+ definition = image_definition_factory.create(image_name, image_dir)
14
+ add_definition(definition)
15
+ definition
16
+ end
17
+
18
+ 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
26
+ end
27
+
28
+ def get_definition(image_name)
29
+ @@image_definitions ||= {}
30
+
31
+ if @@image_definitions[image_name].nil?
32
+ raise NotFoundError, "image #{image_name} not found"
33
+ end
34
+
35
+ @@image_definitions[image_name]
36
+ end
37
+
38
+ def get_image(image_name)
39
+ definition = get_definition(image_name)
40
+
41
+ image_factory.create(definition)
42
+ end
43
+
44
+ def load_definitions(dir_path)
45
+ files = local_shell.recursive_list_files(dir_path, name: "image.rb").each do |path|
46
+ load_definition(path)
47
+ end
48
+ rescue KuberKit::Shell::AbstractShell::DirNotFoundError
49
+ logger.warn("Directory with images not found: #{dir_path}")
50
+ []
51
+ end
52
+
53
+ def load_definition(file_path)
54
+ require(file_path)
55
+ end
56
+
57
+ def reset!
58
+ @@image_definitions = {}
59
+ end
60
+ end