indocker 0.0.0 → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/.rspec +1 -0
- data/Gemfile.lock +12 -0
- data/bin/indocker +9 -0
- data/indocker.gemspec +7 -2
- data/lib/indocker/application_initializer.rb +30 -0
- data/lib/indocker/cli.rb +17 -0
- data/lib/indocker/configs/config.rb +76 -0
- data/lib/indocker/configs/config_factory.rb +36 -0
- data/lib/indocker/configs/locator.rb +23 -0
- data/lib/indocker/container/container_dsl.rb +60 -0
- data/lib/indocker/container/container_evaluator.rb +13 -0
- data/lib/indocker/container/container_manager.rb +111 -0
- data/lib/indocker/container/container_metadata.rb +107 -0
- data/lib/indocker/container/container_metadata_factory.rb +27 -0
- data/lib/indocker/container/container_metadata_repository.rb +27 -0
- data/lib/indocker/directives/base.rb +21 -0
- data/lib/indocker/directives/container_directives/base.rb +19 -0
- data/lib/indocker/directives/container_directives/depends_on.rb +7 -0
- data/lib/indocker/directives/container_directives/env_file.rb +7 -0
- data/lib/indocker/directives/container_directives/expose.rb +7 -0
- data/lib/indocker/directives/container_directives/from.rb +12 -0
- data/lib/indocker/directives/container_directives/network.rb +12 -0
- data/lib/indocker/directives/container_directives/ports.rb +8 -0
- data/lib/indocker/directives/container_directives/ready.rb +16 -0
- data/lib/indocker/directives/container_directives_runner.rb +46 -0
- data/lib/indocker/directives/docker_directives/base.rb +15 -0
- data/lib/indocker/directives/docker_directives/cmd.rb +15 -0
- data/lib/indocker/directives/docker_directives/copy.rb +27 -0
- data/lib/indocker/directives/docker_directives/entrypoint.rb +15 -0
- data/lib/indocker/directives/docker_directives/env.rb +5 -0
- data/lib/indocker/directives/docker_directives/from.rb +30 -0
- data/lib/indocker/directives/docker_directives/run.rb +5 -0
- data/lib/indocker/directives/docker_directives/workdir.rb +5 -0
- data/lib/indocker/directives/image_directives_runner.rb +120 -0
- data/lib/indocker/directives/partial.rb +13 -0
- data/lib/indocker/directives/prepare_directives/base.rb +6 -0
- data/lib/indocker/directives/prepare_directives/docker_cp.rb +16 -0
- data/lib/indocker/docker_api.rb +180 -0
- data/lib/indocker/dsl_context.rb +17 -0
- data/lib/indocker/envs/env_metadata.rb +35 -0
- data/lib/indocker/envs/loader.rb +19 -0
- data/lib/indocker/envs/manager.rb +30 -0
- data/lib/indocker/errors.rb +17 -0
- data/lib/indocker/handlers/base.rb +13 -0
- data/lib/indocker/handlers/run_container.rb +29 -0
- data/lib/indocker/image/image_builder.rb +39 -0
- data/lib/indocker/image/image_dependencies_manager.rb +43 -0
- data/lib/indocker/image/image_dsl.rb +69 -0
- data/lib/indocker/image/image_evaluator.rb +21 -0
- data/lib/indocker/image/image_metadata.rb +50 -0
- data/lib/indocker/image/image_metadata_factory.rb +30 -0
- data/lib/indocker/image/image_metadata_repository.rb +30 -0
- data/lib/indocker/networks/network_metadata.rb +9 -0
- data/lib/indocker/networks/network_metadata_factory.rb +9 -0
- data/lib/indocker/networks/network_metadata_repository.rb +34 -0
- data/lib/indocker/partial/partial_metadata.rb +8 -0
- data/lib/indocker/partial/partial_metadata_repository.rb +26 -0
- data/lib/indocker/utils/ioc_container.rb +17 -0
- data/lib/indocker/utils/logger.rb +62 -0
- data/lib/indocker/utils/logger_factory.rb +12 -0
- data/lib/indocker/utils/registry_authenticator.rb +19 -0
- data/lib/indocker/utils/render_namespace.rb +11 -0
- data/lib/indocker/utils/render_util.rb +15 -0
- data/lib/indocker/utils/string_utils.rb +11 -0
- data/lib/indocker/utils/tar_helper.rb +40 -0
- data/lib/indocker/utils/test_logger_factory.rb +9 -0
- data/lib/indocker/version.rb +2 -2
- data/lib/indocker.rb +123 -1
- data/spec/example/.indocker/config.rb +23 -0
- data/spec/example/.indocker/images_and_containers.rb +25 -0
- data/spec/example/assets/index.css +1 -0
- data/spec/example/assets/index.js +1 -0
- data/spec/indocker/configs/config_factory_spec.rb +18 -0
- data/spec/indocker/configs/config_spec.rb +37 -0
- data/spec/indocker/container/container_manager_spec.rb +230 -0
- data/spec/indocker/directives/image_directives_runner_spec.rb +121 -0
- data/spec/indocker/handlers/run_container_spec.rb +107 -0
- data/spec/indocker/image/image_builder_spec.rb +115 -0
- data/spec/indocker/image/image_evaluator_spec.rb +65 -0
- data/spec/indocker/utils/docker_api_spec.rb +87 -0
- data/spec/spec_helper.rb +46 -0
- data/spec/tmp/indocker_list_container_files/deeper/example3.txt +1 -0
- data/spec/tmp/indocker_list_container_files/deeper/example4.txt +1 -0
- data/spec/tmp/indocker_list_container_files/example1.txt +1 -0
- data/spec/tmp/indocker_list_container_files/example2.txt +1 -0
- metadata +159 -6
- data/spec/.gitkeep +0 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
class Indocker::ContainerDirectives::From < Indocker::ContainerDirectives::Base
|
|
2
|
+
attr_accessor :repo, :tag
|
|
3
|
+
|
|
4
|
+
def initialize(repo, tag: Indocker::ImageMetadata::DEFAULT_TAG)
|
|
5
|
+
@repo = repo
|
|
6
|
+
@tag = tag
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def image
|
|
10
|
+
"#{@repo}:#{@tag}"
|
|
11
|
+
end
|
|
12
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
class Indocker::ContainerDirectives::Network < Indocker::ContainerDirectives::Base
|
|
2
|
+
attr_accessor :network_name, :container_name
|
|
3
|
+
|
|
4
|
+
def initialize(network_name:, container_name:)
|
|
5
|
+
@network_name = network_name
|
|
6
|
+
@container_name = container_name
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def before_start?
|
|
10
|
+
true
|
|
11
|
+
end
|
|
12
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
class Indocker::ContainerDirectives::Ready < Indocker::ContainerDirectives::Base
|
|
2
|
+
attr_accessor :ready_block, :sleep, :timeout
|
|
3
|
+
|
|
4
|
+
DEFAULT_SLEEP = 0.1
|
|
5
|
+
DEFAULT_TIMEPUT = 10
|
|
6
|
+
|
|
7
|
+
def initialize(sleep: DEFAULT_SLEEP, timeout: DEFAULT_TIMEPUT, ready_block:)
|
|
8
|
+
@sleep = sleep
|
|
9
|
+
@timeout = timeout
|
|
10
|
+
@ready_block = ready_block
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def after_start?
|
|
14
|
+
true
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
require 'timeout'
|
|
2
|
+
|
|
3
|
+
class Indocker::ContainerDirectivesRunner
|
|
4
|
+
include SmartIoC::Iocify
|
|
5
|
+
|
|
6
|
+
bean :container_directives_runner
|
|
7
|
+
|
|
8
|
+
inject :docker_api
|
|
9
|
+
inject :config
|
|
10
|
+
|
|
11
|
+
def run_all(directives)
|
|
12
|
+
directives.each {|c| run(c)}
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def run(directive)
|
|
16
|
+
case directive
|
|
17
|
+
when Indocker::ContainerDirectives::Network
|
|
18
|
+
run_network(directive)
|
|
19
|
+
when Indocker::ContainerDirectives::Ready
|
|
20
|
+
run_ready(directive)
|
|
21
|
+
else
|
|
22
|
+
# do nothing
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def run_network(directive)
|
|
27
|
+
if !docker_api.network_exists?(directive.network_name)
|
|
28
|
+
docker_api.create_network(directive.network_name)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
docker_api.add_container_to_network(
|
|
32
|
+
container_name: directive.container_name,
|
|
33
|
+
network_name: directive.network_name
|
|
34
|
+
)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def run_ready(directive)
|
|
38
|
+
Timeout::timeout(directive.timeout) do
|
|
39
|
+
while (!directive.ready_block.call)
|
|
40
|
+
sleep directive.sleep
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
rescue Timeout::Error
|
|
44
|
+
raise Indocker::Errors::ContainerTimeoutError
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
class Indocker::DockerDirectives::Copy < Indocker::DockerDirectives::Base
|
|
2
|
+
attr_reader :copy_actions, :context, :compile
|
|
3
|
+
|
|
4
|
+
def initialize(context:, copy_actions:, compile: false)
|
|
5
|
+
@copy_actions = copy_actions
|
|
6
|
+
@context = context
|
|
7
|
+
@compile = compile
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def type
|
|
11
|
+
'COPY'
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def to_s
|
|
15
|
+
result = []
|
|
16
|
+
|
|
17
|
+
copy_actions.each do |from, to|
|
|
18
|
+
result.push "#{type} #{from} #{to}"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
result.join("\n")
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def prepare_directive?
|
|
25
|
+
true
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
class Indocker::DockerDirectives::Entrypoint < Indocker::DockerDirectives::Base
|
|
2
|
+
attr_reader :command
|
|
3
|
+
|
|
4
|
+
def initialize(command)
|
|
5
|
+
@command = command
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def to_s
|
|
9
|
+
"#{type} #{command.inspect}"
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def type
|
|
13
|
+
'ENTRYPOINT'
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
class Indocker::DockerDirectives::From < Indocker::DockerDirectives::Base
|
|
2
|
+
attr_reader :repo, :tag
|
|
3
|
+
|
|
4
|
+
def initialize(repo_tag, tag: nil)
|
|
5
|
+
case repo_tag
|
|
6
|
+
when String
|
|
7
|
+
@repo = repo_tag.split(':')[0]
|
|
8
|
+
@tag = tag || repo_tag.split(':')[1] || Indocker::ImageMetadata::DEFAULT_TAG
|
|
9
|
+
else
|
|
10
|
+
@repo = repo_tag
|
|
11
|
+
@tag = tag || Indocker::ImageMetadata::DEFAULT_TAG
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def full_name
|
|
16
|
+
"#{repo}:#{tag}"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def dockerhub_image?
|
|
20
|
+
repo.is_a?(String)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def to_s
|
|
24
|
+
"#{type} #{repo}:#{tag}"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def type
|
|
28
|
+
'FROM'
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
class Indocker::ImageDirectivesRunner
|
|
2
|
+
include SmartIoC::Iocify
|
|
3
|
+
|
|
4
|
+
bean :image_directives_runner
|
|
5
|
+
|
|
6
|
+
inject :container_manager
|
|
7
|
+
inject :config
|
|
8
|
+
inject :render_util
|
|
9
|
+
|
|
10
|
+
def run_all(directives)
|
|
11
|
+
directives.each {|c| run(c)}
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def run(directive)
|
|
15
|
+
case directive
|
|
16
|
+
when Indocker::PrepareDirectives::DockerCp
|
|
17
|
+
run_docker_cp(directive)
|
|
18
|
+
when Indocker::DockerDirectives::Copy
|
|
19
|
+
run_copy(directive)
|
|
20
|
+
else
|
|
21
|
+
# do nothing
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def run_docker_cp(directive)
|
|
26
|
+
directive.copy_actions.each do |from, to|
|
|
27
|
+
container_manager.copy(
|
|
28
|
+
name: directive.container_name,
|
|
29
|
+
copy_from: from,
|
|
30
|
+
copy_to: to
|
|
31
|
+
)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def run_copy(directive)
|
|
36
|
+
directive.copy_actions.each do |from, to|
|
|
37
|
+
build_dir_file = File.join(directive.context.build_dir, from)
|
|
38
|
+
|
|
39
|
+
source_dir = File.join(config.root, from)
|
|
40
|
+
|
|
41
|
+
if File.directory?(from)
|
|
42
|
+
dest_dir = File.join(directive.context.build_dir, from)
|
|
43
|
+
else
|
|
44
|
+
dest_dir = File.join(directive.context.build_dir, File.dirname(from))
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
if File.exists?(build_dir_file) && File.file?(build_dir_file)
|
|
48
|
+
copy_compile_file(
|
|
49
|
+
from: build_dir_file,
|
|
50
|
+
to: build_dir_file,
|
|
51
|
+
locals: directive.context.storage,
|
|
52
|
+
compile: directive.compile
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
return
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
if File.exist?(build_dir_file) && File.directory?(build_dir_file)
|
|
59
|
+
files_list = Dir.glob(File.join(build_dir_file, '**', '*'), File::FNM_DOTMATCH)
|
|
60
|
+
|
|
61
|
+
files_list.each do |from|
|
|
62
|
+
copy_compile_file(
|
|
63
|
+
from: from,
|
|
64
|
+
to: from,
|
|
65
|
+
locals: directive.context.storage,
|
|
66
|
+
compile: directive.compile
|
|
67
|
+
)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
return
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
if File.exists?(source_dir) && File.file?(source_dir)
|
|
74
|
+
copy_compile_file(
|
|
75
|
+
from: source_dir,
|
|
76
|
+
to: File.join(dest_dir, File.basename(source_dir)),
|
|
77
|
+
locals: directive.context.storage,
|
|
78
|
+
compile: directive.compile
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
return
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
if Dir.exist?(source_dir) && File.directory?(source_dir)
|
|
85
|
+
files_list = Dir.glob(File.join(source_dir, '**', '*'), File::FNM_DOTMATCH)
|
|
86
|
+
|
|
87
|
+
files_list.each do |from|
|
|
88
|
+
relative_to = Pathname.new(from).relative_path_from( Pathname.new(source_dir) ).to_s
|
|
89
|
+
absolute_to = File.join(dest_dir, relative_to)
|
|
90
|
+
|
|
91
|
+
copy_compile_file(
|
|
92
|
+
from: from,
|
|
93
|
+
to: absolute_to,
|
|
94
|
+
locals: directive.context.storage,
|
|
95
|
+
compile: directive.compile
|
|
96
|
+
)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
return
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
raise Indocker::Errors::FileDoesNotExists
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
private
|
|
107
|
+
|
|
108
|
+
def copy_compile_file(from:, to:, locals: {}, compile: false)
|
|
109
|
+
return if !File.exists?(from) || File.directory?(from)
|
|
110
|
+
|
|
111
|
+
FileUtils.mkdir_p(File.dirname(to)) unless Dir.exist?(File.dirname(to))
|
|
112
|
+
|
|
113
|
+
write_content = compile ? render_util.render( File.read(from), locals ) :
|
|
114
|
+
File.read(from)
|
|
115
|
+
|
|
116
|
+
File.write(to, write_content)
|
|
117
|
+
|
|
118
|
+
File.chmod(File.stat(from).mode, to)
|
|
119
|
+
end
|
|
120
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
class Indocker::PrepareDirectives::DockerCp < Indocker::PrepareDirectives::Base
|
|
2
|
+
attr_reader :container_name, :copy_actions, :build_dir
|
|
3
|
+
|
|
4
|
+
def initialize(container_name, build_dir, &block)
|
|
5
|
+
@container_name = container_name
|
|
6
|
+
@build_dir = build_dir
|
|
7
|
+
|
|
8
|
+
instance_exec &block if block_given?
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
def copy(copy_actions)
|
|
14
|
+
@copy_actions = copy_actions
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
class Indocker::DockerApi
|
|
2
|
+
include SmartIoC::Iocify
|
|
3
|
+
|
|
4
|
+
bean :docker_api
|
|
5
|
+
|
|
6
|
+
inject :logger
|
|
7
|
+
|
|
8
|
+
def check_docker_installed!
|
|
9
|
+
Docker.info
|
|
10
|
+
nil
|
|
11
|
+
rescue Excon::Error::Socket
|
|
12
|
+
raise Indocker::Errors::DockerDoesNotInstalled
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def authenticate!(serveraddress:, email:, password:, username:)
|
|
16
|
+
params = {
|
|
17
|
+
'serveraddress' => serveraddress,
|
|
18
|
+
'email' => email,
|
|
19
|
+
'password' => password,
|
|
20
|
+
'username' => username
|
|
21
|
+
}.delete_if {|_, value| value.to_s.empty?}
|
|
22
|
+
|
|
23
|
+
Docker.authenticate!(params)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Networks
|
|
27
|
+
|
|
28
|
+
def create_network(name)
|
|
29
|
+
Docker::Network.create(name.to_s).id
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def add_container_to_network(network_name:, container_name:)
|
|
33
|
+
container_id = get_container_id(container_name.to_s)
|
|
34
|
+
|
|
35
|
+
Docker::Network.get(network_name.to_s).connect(container_id.to_s)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def remove_network(name)
|
|
39
|
+
Docker::Network.get(name.to_s).remove
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def network_exists?(name)
|
|
43
|
+
!Docker::Network.get(name.to_s).nil? rescue false
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def get_network_id(name)
|
|
47
|
+
Docker::Network.get(name.to_s).id
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def inspect_network(name)
|
|
51
|
+
Docker::Network.get(name.to_s).info
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def delete_networks_where(&condition)
|
|
55
|
+
Docker::Network.all.select(&condition).map do |network|
|
|
56
|
+
network.remove
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Images
|
|
61
|
+
|
|
62
|
+
def get_image_id(repo, tag: Indocker::ImageMetadata::DEFAULT_TAG)
|
|
63
|
+
Docker::Image.get(full_name(repo, tag)).id
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def image_exists?(repo, tag: Indocker::ImageMetadata::DEFAULT_TAG)
|
|
67
|
+
Docker::Image.exist?(full_name(repo, tag))
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def delete_image(repo, tag: Indocker::ImageMetadata::DEFAULT_TAG)
|
|
71
|
+
Docker::Image.get(full_name(repo, tag)).remove(force: true)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def build_from_dir(repo:, tag:, build_dir:)
|
|
75
|
+
image = Docker::Image.build_from_dir(build_dir) do |x|
|
|
76
|
+
x.split("\r\n").each do |y|
|
|
77
|
+
if (log = JSON.parse(y)) && log.has_key?("stream")
|
|
78
|
+
yield log["stream"].strip
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
image.tag(
|
|
84
|
+
repo: repo,
|
|
85
|
+
tag: tag,
|
|
86
|
+
force: true
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
image.id
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def pull(opts)
|
|
93
|
+
image = Docker::Image.create(opts)
|
|
94
|
+
|
|
95
|
+
image.id
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def delete_images_where(&condition)
|
|
99
|
+
Docker::Image.all.select(&condition).map do |image|
|
|
100
|
+
image.remove(force: true)
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
# Containers
|
|
106
|
+
|
|
107
|
+
def inspect_container(name)
|
|
108
|
+
Docker::Container.get(name.to_s).info
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def get_container_id(name)
|
|
112
|
+
Docker::Container.get(name.to_s).id
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def get_container_state(name)
|
|
116
|
+
Docker::Container.get(name.to_s).info["State"]["Status"]
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def container_exists?(name)
|
|
120
|
+
!Docker::Container.get(name.to_s).nil? rescue false
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def start_container(name)
|
|
124
|
+
Docker::Container.get(name.to_s).start!.id
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def stop_container(name)
|
|
128
|
+
Docker::Container.get(name.to_s).stop
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def copy_from_container(name:, path:)
|
|
132
|
+
Docker::Container.get(name.to_s).copy(path) { |chunk| yield chunk }
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def create_container(repo:, tag:, name: nil, command: nil, env: nil,
|
|
136
|
+
exposed_ports: nil, port_bindings: nil)
|
|
137
|
+
params = {
|
|
138
|
+
'Image' => full_name(repo, tag),
|
|
139
|
+
'name' => name.to_s,
|
|
140
|
+
'Cmd' => command,
|
|
141
|
+
'Env' => env,
|
|
142
|
+
'ExposedPorts' => exposed_ports,
|
|
143
|
+
'HostConfig' => {
|
|
144
|
+
'PortBindings' => port_bindings
|
|
145
|
+
}
|
|
146
|
+
}.delete_if { |_, value| value.to_s.empty? }
|
|
147
|
+
|
|
148
|
+
Docker::Container.create(params).id
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def exec_container(name:, command:)
|
|
152
|
+
Docker::Container.get(name.to_s).exec(command) do |stream, chunk|
|
|
153
|
+
yield stream, chunk
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def delete_container(name)
|
|
158
|
+
Docker::Container.get(name.to_s).delete(:force => true)
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def delete_containers_where(&condition)
|
|
162
|
+
Docker::Container.all(all: true).select(&condition).map do |container|
|
|
163
|
+
container.stop
|
|
164
|
+
container.delete(force: true)
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def container_run(repo:, tag:, command:)
|
|
169
|
+
container = Docker::Container.create(
|
|
170
|
+
'Image' => full_name(repo, tag),
|
|
171
|
+
'Cmd' => command
|
|
172
|
+
).id
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
private
|
|
176
|
+
|
|
177
|
+
def full_name(repo, tag)
|
|
178
|
+
"#{repo.to_s}:#{tag.to_s}"
|
|
179
|
+
end
|
|
180
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
class Indocker::DSLContext
|
|
2
|
+
attr_reader :storage
|
|
3
|
+
|
|
4
|
+
def initialize(storage = {})
|
|
5
|
+
@storage = storage
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def method_missing(method, *args)
|
|
9
|
+
super unless @storage.key?(method)
|
|
10
|
+
|
|
11
|
+
@storage.fetch(method)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def set_value(key, value)
|
|
15
|
+
@storage[key] = value
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module Indocker::Envs
|
|
2
|
+
class EnvMetadata
|
|
3
|
+
def initialize(variables = {})
|
|
4
|
+
@variables = variables
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def to_hash
|
|
8
|
+
@variables
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def to_json
|
|
12
|
+
to_hash.to_json
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def to_array
|
|
16
|
+
to_hash.inject([]) do |all, (k, v)|
|
|
17
|
+
all.push "#{k}=#{v}"
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def set(key:, value:)
|
|
22
|
+
overwritten = @variables.has_key?(key) and @variables[key] != value
|
|
23
|
+
|
|
24
|
+
@variables[key] = value
|
|
25
|
+
|
|
26
|
+
overwritten
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def +(other)
|
|
30
|
+
combined_hash = to_hash.merge(other.to_hash)
|
|
31
|
+
|
|
32
|
+
Indocker::Envs::EnvMetadata.new(combined_hash)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
class Indocker::Envs::Loader
|
|
2
|
+
include SmartIoC::Iocify
|
|
3
|
+
|
|
4
|
+
bean :envs_loader
|
|
5
|
+
|
|
6
|
+
def parse(path)
|
|
7
|
+
raise Indocker::Errors::EnvFileDoesNotExist if !File.exists?(path)
|
|
8
|
+
|
|
9
|
+
env_metadata = Indocker::Envs::EnvMetadata.new
|
|
10
|
+
|
|
11
|
+
File.foreach(path) do |line|
|
|
12
|
+
key, value = line.split('=').map(&:strip)
|
|
13
|
+
|
|
14
|
+
env_metadata.set(key: key, value: value)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
env_metadata
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
class Indocker::Envs::Manager
|
|
2
|
+
include SmartIoC::Iocify
|
|
3
|
+
|
|
4
|
+
bean :envs_manager
|
|
5
|
+
|
|
6
|
+
inject :config
|
|
7
|
+
inject :logger
|
|
8
|
+
inject :envs_loader
|
|
9
|
+
|
|
10
|
+
def load_init_application_env_variables
|
|
11
|
+
config.load_env_file.each do |path|
|
|
12
|
+
env_file = File.join(config.root, path)
|
|
13
|
+
|
|
14
|
+
begin
|
|
15
|
+
metadata = envs_loader.parse(env_file)
|
|
16
|
+
rescue Indocker::Errors::EnvFileDoesNotExist
|
|
17
|
+
logger.warn "File #{env_file} doesn't exist. Check path to environment_path"
|
|
18
|
+
return
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
metadata.to_hash.each do |key, value|
|
|
22
|
+
if ENV.has_key?(key) && ENV[key] != value
|
|
23
|
+
logger.warn "Environment file '#{path}' overwrites ENV['#{key}'] from '#{ENV[key]}' to '#{value}'!"
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
ENV[key] = value
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module Indocker::Errors
|
|
2
|
+
class ImageIsNotDefined < StandardError; end
|
|
3
|
+
class PartialIsNotDefined < StandardError; end
|
|
4
|
+
class ContainerIsNotDefined < StandardError; end
|
|
5
|
+
class CircularImageDependency < StandardError; end
|
|
6
|
+
class ImageForContainerDoesNotExist < StandardError; end
|
|
7
|
+
class DockerDoesNotInstalled < StandardError; end
|
|
8
|
+
class ConfigFilesDoesNotFound < StandardError; end
|
|
9
|
+
class ConfigOptionTypeMismatch < StandardError; end
|
|
10
|
+
class DockerRegistryAuthenticationError < StandardError; end
|
|
11
|
+
class FileDoesNotExists < StandardError; end
|
|
12
|
+
class EnvFileDoesNotExist < StandardError; end
|
|
13
|
+
class ContainerTimeoutError < StandardError; end
|
|
14
|
+
|
|
15
|
+
class ContainerImageAlreadyDefined < StandardError; end
|
|
16
|
+
class NetworkAlreadyDefined < StandardError; end
|
|
17
|
+
end
|