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,207 @@
1
+ class KuberKit::Container
2
+ extend Dry::Container::Mixin
3
+
4
+ register "actions.image_compiler" do
5
+ KuberKit::Actions::ImageCompiler.new
6
+ end
7
+
8
+ register "actions.env_file_reader" do
9
+ KuberKit::Actions::EnvFileReader.new
10
+ end
11
+
12
+ register "actions.template_reader" do
13
+ KuberKit::Actions::TemplateReader.new
14
+ end
15
+
16
+ register "actions.service_reader" do
17
+ KuberKit::Actions::ServiceReader.new
18
+ end
19
+
20
+ register "actions.service_applier" do
21
+ KuberKit::Actions::ServiceApplier.new
22
+ end
23
+
24
+ register "actions.configuration_loader" do
25
+ KuberKit::Actions::ConfigurationLoader.new
26
+ end
27
+
28
+ register "actions.kubectl_applier" do
29
+ KuberKit::Actions::KubectlApplier.new
30
+ end
31
+
32
+ register "configs" do
33
+ KuberKit::Configs.new
34
+ end
35
+
36
+ register "core.image_factory" do
37
+ KuberKit::Core::ImageFactory.new
38
+ end
39
+
40
+ register "core.image_definition_factory" do
41
+ KuberKit::Core::ImageDefinitionFactory.new
42
+ end
43
+
44
+ register "core.image_store" do
45
+ KuberKit::Core::ImageStore.new
46
+ end
47
+
48
+ register "core.service_factory" do
49
+ KuberKit::Core::ServiceFactory.new
50
+ end
51
+
52
+ register "core.service_definition_factory" do
53
+ KuberKit::Core::ServiceDefinitionFactory.new
54
+ end
55
+
56
+ register "core.service_store" do
57
+ KuberKit::Core::ServiceStore.new
58
+ end
59
+
60
+ register "core.configuration_definition_factory" do
61
+ KuberKit::Core::ConfigurationDefinitionFactory.new
62
+ end
63
+
64
+ register "core.configuration_factory" do
65
+ KuberKit::Core::ConfigurationFactory.new
66
+ end
67
+
68
+ register "core.configuration_store" do
69
+ KuberKit::Core::ConfigurationStore.new
70
+ end
71
+
72
+ register "core.registry_store" do
73
+ KuberKit::Core::Registries::RegistryStore.new
74
+ end
75
+
76
+ register "core.artifact_store" do
77
+ KuberKit::Core::Artifacts::ArtifactStore.new
78
+ end
79
+
80
+ register "core.env_file_store" do
81
+ KuberKit::Core::EnvFiles::EnvFileStore.new
82
+ end
83
+
84
+ register "core.template_store" do
85
+ KuberKit::Core::Templates::TemplateStore.new
86
+ end
87
+
88
+ register "core.context_helper_factory" do
89
+ KuberKit::Core::ContextHelper::ContextHelperFactory.new
90
+ end
91
+
92
+ register "tools.file_presence_checker" do
93
+ KuberKit::Tools::FilePresenceChecker.new
94
+ end
95
+
96
+ register "tools.logger_factory" do
97
+ KuberKit::Tools::LoggerFactory.new
98
+ end
99
+
100
+ register "tools.logger" do
101
+ KuberKit::Container["tools.logger_factory"].create("/tmp/kuber_kit.log")
102
+ end
103
+
104
+ register "shell.bash_commands" do
105
+ KuberKit::Shell::BashCommands.new
106
+ end
107
+
108
+ register "shell.docker_commands" do
109
+ KuberKit::Shell::DockerCommands.new
110
+ end
111
+
112
+ register "shell.git_commands" do
113
+ KuberKit::Shell::GitCommands.new
114
+ end
115
+
116
+ register "shell.rsync_commands" do
117
+ KuberKit::Shell::RsyncCommands.new
118
+ end
119
+
120
+ register "shell.kubectl_commands" do
121
+ KuberKit::Shell::KubectlCommands.new
122
+ end
123
+
124
+ register "shell.local_shell" do
125
+ KuberKit::Shell::LocalShell.new
126
+ end
127
+
128
+ register "shell.command_counter" do
129
+ KuberKit::Shell::CommandCounter.new
130
+ end
131
+
132
+ register "preprocessing.text_preprocessor" do
133
+ KuberKit::Preprocessing::TextPreprocessor.new
134
+ end
135
+
136
+ register "preprocessing.file_preprocessor" do
137
+ KuberKit::Preprocessing::FilePreprocessor.new
138
+ end
139
+
140
+ register "preprocessing.dir_preprocessor" do
141
+ KuberKit::Preprocessing::DirPreprocessor.new
142
+ end
143
+
144
+ register "image_compiler.compiler" do
145
+ KuberKit::ImageCompiler::Compiler.new
146
+ end
147
+
148
+ register "image_compiler.image_builder" do
149
+ KuberKit::ImageCompiler::ImageBuilder.new
150
+ end
151
+
152
+ register "image_compiler.image_build_dir_creator" do
153
+ KuberKit::ImageCompiler::ImageBuildDirCreator.new
154
+ end
155
+
156
+ register "image_compiler.image_dependency_resolver" do
157
+ KuberKit::ImageCompiler::ImageDependencyResolver.new
158
+ end
159
+
160
+ register "image_compiler.version_tag_builder" do
161
+ KuberKit::ImageCompiler::VersionTagBuilder.new
162
+ end
163
+
164
+ register "artifacts_sync.artifacts_updater" do
165
+ KuberKit::ArtifactsSync::ArtifactsUpdater.new
166
+ end
167
+
168
+ register "artifacts_sync.git_artifact_resolver" do
169
+ KuberKit::ArtifactsSync::GitArtifactResolver.new
170
+ end
171
+
172
+ register "artifacts_sync.null_artifact_resolver" do
173
+ KuberKit::ArtifactsSync::NullArtifactResolver.new
174
+ end
175
+
176
+ register "env_file_reader.reader" do
177
+ KuberKit::EnvFileReader::Reader.new
178
+ end
179
+
180
+ register "env_file_reader.artifact_file_reader" do
181
+ KuberKit::EnvFileReader::ArtifactFileReader.new
182
+ end
183
+
184
+ register "template_reader.reader" do
185
+ KuberKit::TemplateReader::Reader.new
186
+ end
187
+
188
+ register "template_reader.artifact_file_reader" do
189
+ KuberKit::TemplateReader::ArtifactFileReader.new
190
+ end
191
+
192
+ register "service_deployer.service_reader" do
193
+ KuberKit::ServiceDeployer::ServiceReader.new
194
+ end
195
+
196
+ register "service_deployer.service_list_resolver" do
197
+ KuberKit::ServiceDeployer::ServiceListResolver.new
198
+ end
199
+
200
+ register "ui" do
201
+ if KuberKit.debug_mode?
202
+ KuberKit::UI::Simple.new
203
+ else
204
+ KuberKit::UI::Interactive.new
205
+ end
206
+ end
207
+ end
@@ -0,0 +1,13 @@
1
+ class KuberKit::Core::Artifacts::AbstractArtifact
2
+ include KuberKit::Extensions::Inspectable
3
+
4
+ attr_reader :name
5
+
6
+ def initialize(artifact_name)
7
+ @name = artifact_name
8
+ end
9
+
10
+ def namespace
11
+ raise KuberKit::NotImplementedError, "must be implemented"
12
+ end
13
+ end
@@ -0,0 +1,45 @@
1
+ class KuberKit::Core::Artifacts::ArtifactStore
2
+ NotFoundError = Class.new(KuberKit::NotFoundError)
3
+ AlreadyAddedError = Class.new(KuberKit::Error)
4
+
5
+ def add(artifact)
6
+ @@artifacts ||= {}
7
+
8
+ if !artifact.is_a?(KuberKit::Core::Artifacts::AbstractArtifact)
9
+ raise ArgumentError.new("should be an instance of KuberKit::Core::Artifacts::AbstractArtifact, got: #{artifact.inspect}")
10
+ end
11
+
12
+ unless @@artifacts[artifact.name].nil?
13
+ raise AlreadyAddedError, "artifact #{artifact.name} was already added"
14
+ end
15
+
16
+ @@artifacts[artifact.name] = artifact
17
+ end
18
+
19
+ def get(artifact_name)
20
+ artifact = get_from_configuration(artifact_name) ||
21
+ get_global(artifact_name)
22
+
23
+ artifact
24
+ end
25
+
26
+ def get_global(artifact_name)
27
+ @@artifacts ||= {}
28
+ artifact = @@artifacts[artifact_name]
29
+
30
+ if artifact.nil?
31
+ raise NotFoundError, "artifact '#{artifact_name}' not found"
32
+ end
33
+
34
+ artifact
35
+ end
36
+
37
+ def get_from_configuration(artifact_name)
38
+ artifacts = KuberKit.current_configuration.artifacts
39
+ artifacts[artifact_name]
40
+ end
41
+
42
+ def reset!
43
+ @@artifacts = {}
44
+ end
45
+ end
@@ -0,0 +1,22 @@
1
+ class KuberKit::Core::Artifacts::Git < KuberKit::Core::Artifacts::AbstractArtifact
2
+ attr_reader :remote_url, :remote_name, :email, :password, :branch, :ssh_key
3
+
4
+ DEFAULT_SSH_KEY = "id_rsa"
5
+ DEFAULT_REMOTE_NAME = "origin"
6
+ DEFAULT_BRANCH = "master"
7
+
8
+ def setup(remote_url:, remote_name: DEFAULT_REMOTE_NAME, branch: DEFAULT_BRANCH, clone_path: nil, ssh_key: DEFAULT_SSH_KEY)
9
+ @remote_name = remote_name
10
+ @remote_url = remote_url
11
+ @branch = branch
12
+ @clone_path = clone_path
13
+ @ssh_key = ssh_key
14
+ self
15
+ end
16
+
17
+ def cloned_path
18
+ # TODO: We should refactor to not call container here
19
+ configs = KuberKit::Container['configs']
20
+ "#{configs.artifact_clone_dir}/#{name}"
21
+ end
22
+ end
@@ -0,0 +1,14 @@
1
+ class KuberKit::Core::Artifacts::Local < KuberKit::Core::Artifacts::AbstractArtifact
2
+ def setup(root_path)
3
+ @root_path = File.expand_path(root_path)
4
+ self
5
+ end
6
+
7
+ def root_path
8
+ @root_path || (raise ArgumentError.new("root path was not set. Set it using setup method"))
9
+ end
10
+
11
+ def cloned_path
12
+ root_path
13
+ end
14
+ end
@@ -0,0 +1,20 @@
1
+ class KuberKit::Core::Configuration
2
+ attr_reader :name, :artifacts, :registries, :env_files, :templates, :kubecfg_path
3
+
4
+ Contract KeywordArgs[
5
+ name: Symbol,
6
+ artifacts: Hash,
7
+ registries: Hash,
8
+ env_files: Hash,
9
+ templates: Hash,
10
+ kubecfg_path: Maybe[String]
11
+ ] => Any
12
+ def initialize(name:, artifacts:, registries:, env_files:, templates:, kubecfg_path:)
13
+ @name = name
14
+ @artifacts = artifacts
15
+ @registries = registries
16
+ @env_files = env_files
17
+ @templates = templates
18
+ @kubecfg_path = kubecfg_path
19
+ end
20
+ end
@@ -0,0 +1,67 @@
1
+ class KuberKit::Core::ConfigurationDefinition
2
+ ResourceAlreadyAdded = Class.new(KuberKit::Error)
3
+
4
+ attr_reader :configuration_name
5
+
6
+ Contract Or[Symbol, String] => Any
7
+ def initialize(configuration_name)
8
+ @configuration_name = configuration_name.to_sym
9
+ @artifacts = {}
10
+ @registries = {}
11
+ @env_files = {}
12
+ @templates = {}
13
+ end
14
+
15
+ def to_attrs
16
+ OpenStruct.new(
17
+ name: @configuration_name,
18
+ artifacts: @artifacts,
19
+ registries: @registries,
20
+ env_files: @env_files,
21
+ templates: @templates,
22
+ kubecfg_path: @kubecfg_path
23
+ )
24
+ end
25
+
26
+ def use_artifact(artifact_name, as:)
27
+ if @artifacts.has_key?(as)
28
+ raise ResourceAlreadyAdded.new("alias name :#{as} is already used by artifact: #{@artifacts[as].inspect}")
29
+ end
30
+ @artifacts[as] = artifact_name
31
+
32
+ self
33
+ end
34
+
35
+ def use_registry(registry_name, as:)
36
+ if @registries.has_key?(as)
37
+ raise ResourceAlreadyAdded.new("alias name :#{as} is already used by registry: #{@registries[as].inspect}")
38
+ end
39
+ @registries[as] = registry_name
40
+
41
+ self
42
+ end
43
+
44
+ def use_env_file(env_file_name, as:)
45
+ if @env_files.has_key?(as)
46
+ raise ResourceAlreadyAdded.new("alias name :#{as} is already used by env_file: #{@env_files[as].inspect}")
47
+ end
48
+ @env_files[as] = env_file_name
49
+
50
+ self
51
+ end
52
+
53
+ def use_template(template_name, as:)
54
+ if @templates.has_key?(as)
55
+ raise ResourceAlreadyAdded.new("alias name :#{as} is already used by template: #{@templates[as].inspect}")
56
+ end
57
+ @templates[as] = template_name
58
+
59
+ self
60
+ end
61
+
62
+ def kubecfg_path(path)
63
+ @kubecfg_path = path
64
+
65
+ self
66
+ end
67
+ end
@@ -0,0 +1,5 @@
1
+ class KuberKit::Core::ConfigurationDefinitionFactory
2
+ def create(configuration_name)
3
+ KuberKit::Core::ConfigurationDefinition.new(configuration_name)
4
+ end
5
+ end
@@ -0,0 +1,61 @@
1
+ class KuberKit::Core::ConfigurationFactory
2
+ NotFoundError = Class.new(KuberKit::NotFoundError)
3
+
4
+ include KuberKit::Import[
5
+ "core.registry_store",
6
+ "core.artifact_store",
7
+ "core.env_file_store",
8
+ "core.template_store"
9
+ ]
10
+
11
+ def create(definition)
12
+ configuration_attrs = definition.to_attrs
13
+
14
+ artifacts = fetch_artifacts(configuration_attrs.artifacts)
15
+ registries = fetch_registries(configuration_attrs.registries)
16
+ env_files = fetch_env_files(configuration_attrs.env_files)
17
+ templates = fetch_templates(configuration_attrs.templates)
18
+
19
+ KuberKit::Core::Configuration.new(
20
+ name: configuration_attrs.name,
21
+ artifacts: artifacts,
22
+ registries: registries,
23
+ env_files: env_files,
24
+ templates: templates,
25
+ kubecfg_path: configuration_attrs.kubecfg_path
26
+ )
27
+ end
28
+
29
+ private
30
+ def fetch_registries(registries)
31
+ result = {}
32
+ registries.each do |registry_alias, registry_name|
33
+ result[registry_alias] = registry_store.get_global(registry_name)
34
+ end
35
+ result
36
+ end
37
+
38
+ def fetch_artifacts(artifacts)
39
+ result = {}
40
+ artifacts.each do |artifact_alias, artifact_name|
41
+ result[artifact_alias] = artifact_store.get_global(artifact_name)
42
+ end
43
+ result
44
+ end
45
+
46
+ def fetch_env_files(env_files)
47
+ result = {}
48
+ env_files.each do |env_file_alias, env_file_name|
49
+ result[env_file_alias] = env_file_store.get_global(env_file_name)
50
+ end
51
+ result
52
+ end
53
+
54
+ def fetch_templates(templates)
55
+ result = {}
56
+ templates.each do |template_alias, template_name|
57
+ result[template_alias] = template_store.get_global(template_name)
58
+ end
59
+ result
60
+ end
61
+ end