indocker 0.0.2 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +42 -6
- data/bin/indocker +1 -1
- data/indocker.gemspec +4 -0
- data/lib/indocker/application_initializer.rb +1 -12
- data/lib/indocker/cli.rb +10 -0
- data/lib/indocker/configs/config.rb +93 -22
- data/lib/indocker/configs/config_factory.rb +17 -16
- data/lib/indocker/configs/config_initializer.rb +9 -0
- data/lib/indocker/container/container_builder.rb +29 -0
- data/lib/indocker/{directives → container}/container_directives_runner.rb +8 -0
- data/lib/indocker/container/container_dsl.rb +26 -3
- data/lib/indocker/container/container_inspector.rb +26 -0
- data/lib/indocker/container/container_manager.rb +43 -32
- data/lib/indocker/container/container_metadata.rb +31 -26
- data/lib/indocker/container/container_metadata_factory.rb +5 -2
- data/lib/indocker/directives/base.rb +6 -10
- data/lib/indocker/directives/container_directives/base.rb +4 -0
- data/lib/indocker/directives/container_directives/cmd.rb +7 -0
- data/lib/indocker/directives/container_directives/env.rb +7 -0
- data/lib/indocker/directives/container_directives/env_file.rb +3 -3
- data/lib/indocker/directives/container_directives/from.rb +4 -2
- data/lib/indocker/directives/container_directives/ports.rb +11 -4
- data/lib/indocker/directives/container_directives/volume.rb +23 -0
- data/lib/indocker/directives/{docker_directives → image_directives}/base.rb +13 -1
- data/lib/indocker/directives/{docker_directives → image_directives}/cmd.rb +5 -1
- data/lib/indocker/directives/image_directives/copy.rb +32 -0
- data/lib/indocker/directives/{prepare_directives → image_directives}/docker_cp.rb +5 -1
- data/lib/indocker/directives/{docker_directives → image_directives}/entrypoint.rb +5 -1
- data/lib/indocker/directives/image_directives/env.rb +9 -0
- data/lib/indocker/directives/image_directives/env_file.rb +19 -0
- data/lib/indocker/directives/image_directives/expose.rb +9 -0
- data/lib/indocker/directives/image_directives/from.rb +36 -0
- data/lib/indocker/directives/image_directives/registry.rb +30 -0
- data/lib/indocker/directives/image_directives/run.rb +17 -0
- data/lib/indocker/directives/image_directives/workdir.rb +9 -0
- data/lib/indocker/directives/partial.rb +9 -1
- data/lib/indocker/docker_api/container_config.rb +121 -0
- data/lib/indocker/{docker_api.rb → docker_api/docker_api.rb} +79 -52
- data/lib/indocker/dsl_context.rb +4 -0
- data/lib/indocker/envs/env_metadata.rb +4 -0
- data/lib/indocker/errors.rb +5 -1
- data/lib/indocker/git/git_api.rb +32 -0
- data/lib/indocker/git/git_helper.rb +34 -0
- data/lib/indocker/git/git_service.rb +21 -0
- data/lib/indocker/handlers/{run_container.rb → container_run.rb} +4 -13
- data/lib/indocker/handlers/container_stop.rb +17 -0
- data/lib/indocker/handlers/performable.rb +22 -0
- data/lib/indocker/image/image_builder.rb +22 -7
- data/lib/indocker/image/image_dependencies_manager.rb +13 -9
- data/lib/indocker/image/image_directives_runner.rb +99 -0
- data/lib/indocker/image/image_dockerfile_builder.rb +24 -0
- data/lib/indocker/image/image_dsl.rb +38 -18
- data/lib/indocker/image/image_evaluator.rb +1 -1
- data/lib/indocker/image/image_helper.rb +9 -0
- data/lib/indocker/image/image_metadata.rb +9 -9
- data/lib/indocker/image/image_metadata_factory.rb +11 -10
- data/lib/indocker/image/image_metadata_repository.rb +8 -9
- data/lib/indocker/registry/registry_api.rb +46 -0
- data/lib/indocker/registry/registry_helper.rb +20 -0
- data/lib/indocker/registry/registry_service.rb +28 -0
- data/lib/indocker/utils/logger_factory.rb +1 -0
- data/lib/indocker/utils/registry_authenticator.rb +5 -5
- data/lib/indocker/utils/tar_helper.rb +5 -5
- data/lib/indocker/version.rb +1 -1
- data/lib/indocker/volumes/volume_metadata.rb +9 -0
- data/lib/indocker/volumes/volume_metadata_factory.rb +9 -0
- data/lib/indocker/volumes/volume_metadata_repository.rb +34 -0
- data/lib/indocker.rb +56 -27
- data/spec/example/.indocker/config.rb +14 -7
- data/spec/fixtures/spec.env +2 -0
- data/spec/indocker/configs/config_spec.rb +52 -1
- data/spec/indocker/container/container_builder_spec.rb +67 -0
- data/spec/indocker/container/container_manager_spec.rb +177 -129
- data/spec/indocker/docker_api/container_config_spec.rb +64 -0
- data/spec/indocker/{utils → docker_api}/docker_api_spec.rb +38 -13
- data/spec/indocker/handlers/container_run_spec.rb +60 -0
- data/spec/indocker/image/image_builder_spec.rb +64 -26
- data/spec/indocker/image/image_directives_runner_spec.rb +141 -0
- data/spec/indocker/image/image_dockerfile_builder_spec.rb +25 -0
- data/spec/indocker/image/image_evaluator_spec.rb +29 -9
- data/spec/spec_helper.rb +38 -16
- metadata +108 -32
- data/lib/indocker/directives/docker_directives/copy.rb +0 -27
- data/lib/indocker/directives/docker_directives/env.rb +0 -5
- data/lib/indocker/directives/docker_directives/from.rb +0 -30
- data/lib/indocker/directives/docker_directives/run.rb +0 -5
- data/lib/indocker/directives/docker_directives/workdir.rb +0 -5
- data/lib/indocker/directives/image_directives_runner.rb +0 -120
- data/lib/indocker/directives/prepare_directives/base.rb +0 -6
- data/lib/indocker/envs/manager.rb +0 -30
- data/lib/indocker/handlers/base.rb +0 -13
- data/spec/indocker/directives/image_directives_runner_spec.rb +0 -121
- data/spec/indocker/handlers/run_container_spec.rb +0 -107
- data/spec/tmp/indocker_list_container_files/deeper/example3.txt +0 -1
- data/spec/tmp/indocker_list_container_files/deeper/example4.txt +0 -1
- data/spec/tmp/indocker_list_container_files/example1.txt +0 -1
- data/spec/tmp/indocker_list_container_files/example2.txt +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bbe4627a9f936ec5d771857d794648c14cb5ebf8
|
4
|
+
data.tar.gz: 58d394820260d8b77bddbd87c72d56df2682c317
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d57be13657ee31659a52d4adcec2deec04e12ee2f6b8e9f2addfbc3483c4e59410b551fd13416f35607c38e4c576639fa119f30b16b1369d7b717949e529888f
|
7
|
+
data.tar.gz: d4e51c196fb6ea6532f957bef347f0f6c01e92cf383ded6b902c6fb467525c271fa5b8cd8e2d51ecca7a0c5fcbc87624c6bcbb4e40c25b1dedce1fe0ddffb51b
|
data/Gemfile.lock
CHANGED
@@ -1,9 +1,12 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
indocker (0.0.
|
4
|
+
indocker (0.0.5)
|
5
|
+
byebug
|
5
6
|
colorize
|
6
7
|
docker-api
|
8
|
+
docker_registry2
|
9
|
+
git
|
7
10
|
smart_ioc
|
8
11
|
thor
|
9
12
|
|
@@ -12,13 +15,45 @@ GEM
|
|
12
15
|
specs:
|
13
16
|
byebug (9.1.0)
|
14
17
|
colorize (0.8.1)
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
+
diff-lcs (1.3)
|
19
|
+
docker-api (1.34.0)
|
20
|
+
excon (>= 0.47.0)
|
21
|
+
multi_json
|
22
|
+
docker_registry2 (1.3.0)
|
23
|
+
rest-client (>= 1.8.0)
|
24
|
+
domain_name (0.5.20170404)
|
25
|
+
unf (>= 0.0.5, < 1.0.0)
|
18
26
|
excon (0.59.0)
|
19
|
-
|
27
|
+
git (1.3.0)
|
28
|
+
http-cookie (1.0.3)
|
29
|
+
domain_name (~> 0.5)
|
30
|
+
mime-types (3.1)
|
31
|
+
mime-types-data (~> 3.2015)
|
32
|
+
mime-types-data (3.2016.0521)
|
33
|
+
multi_json (1.12.2)
|
34
|
+
netrc (0.11.0)
|
35
|
+
rest-client (2.0.2)
|
36
|
+
http-cookie (>= 1.0.2, < 2.0)
|
37
|
+
mime-types (>= 1.16, < 4.0)
|
38
|
+
netrc (~> 0.8)
|
39
|
+
rspec (3.7.0)
|
40
|
+
rspec-core (~> 3.7.0)
|
41
|
+
rspec-expectations (~> 3.7.0)
|
42
|
+
rspec-mocks (~> 3.7.0)
|
43
|
+
rspec-core (3.7.0)
|
44
|
+
rspec-support (~> 3.7.0)
|
45
|
+
rspec-expectations (3.7.0)
|
46
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
47
|
+
rspec-support (~> 3.7.0)
|
48
|
+
rspec-mocks (3.7.0)
|
49
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
50
|
+
rspec-support (~> 3.7.0)
|
51
|
+
rspec-support (3.7.0)
|
20
52
|
smart_ioc (0.2.1)
|
21
53
|
thor (0.20.0)
|
54
|
+
unf (0.1.4)
|
55
|
+
unf_ext
|
56
|
+
unf_ext (0.0.7.4)
|
22
57
|
|
23
58
|
PLATFORMS
|
24
59
|
ruby
|
@@ -26,6 +61,7 @@ PLATFORMS
|
|
26
61
|
DEPENDENCIES
|
27
62
|
byebug
|
28
63
|
indocker!
|
64
|
+
rspec
|
29
65
|
|
30
66
|
BUNDLED WITH
|
31
|
-
1.
|
67
|
+
1.16.0.pre.3
|
data/bin/indocker
CHANGED
data/indocker.gemspec
CHANGED
@@ -13,11 +13,15 @@ Gem::Specification.new do |s|
|
|
13
13
|
s.test_files = s.files.grep(%r{^(spec)/})
|
14
14
|
|
15
15
|
s.add_development_dependency "byebug"
|
16
|
+
s.add_development_dependency "rspec"
|
16
17
|
|
17
18
|
s.add_dependency "smart_ioc"
|
18
19
|
s.add_dependency "docker-api"
|
20
|
+
s.add_dependency "docker_registry2"
|
19
21
|
s.add_dependency "thor"
|
20
22
|
s.add_dependency "colorize"
|
23
|
+
s.add_dependency "git"
|
24
|
+
s.add_dependency "byebug" # TODO: remove after release
|
21
25
|
|
22
26
|
s.homepage = 'https://github.com/droidlabs/indocker'
|
23
27
|
s.license = 'MIT'
|
@@ -6,25 +6,14 @@ class Indocker::ApplicationInitializer
|
|
6
6
|
inject :config_locator
|
7
7
|
inject :docker_api
|
8
8
|
inject :registry_authenticator
|
9
|
-
inject :envs_manager
|
10
9
|
inject :config
|
11
10
|
|
12
11
|
|
13
12
|
def init_app(current_path, env: :development)
|
14
13
|
docker_api.check_docker_installed!
|
15
14
|
|
16
|
-
config.root(
|
17
|
-
Pathname.new(
|
18
|
-
File.expand_path(
|
19
|
-
File.join(config_locator.locate(current_path), '../..')
|
20
|
-
)
|
21
|
-
)
|
22
|
-
)
|
23
|
-
|
24
15
|
require(config_locator.locate(current_path))
|
25
16
|
|
26
|
-
registry_authenticator.authenticate!
|
27
|
-
|
28
|
-
envs_manager.load_init_application_env_variables
|
17
|
+
# registry_authenticator.authenticate!()
|
29
18
|
end
|
30
19
|
end
|
data/lib/indocker/cli.rb
CHANGED
@@ -12,6 +12,16 @@ module Indocker
|
|
12
12
|
env: env
|
13
13
|
)
|
14
14
|
end
|
15
|
+
|
16
|
+
desc "container:stop CONTAINER_NAME", "stops specified container"
|
17
|
+
option :env
|
18
|
+
define_method('container:stop') do |name, env = :development|
|
19
|
+
ioc.stop_container_handler.perform(
|
20
|
+
name: name,
|
21
|
+
current_path: Dir.pwd,
|
22
|
+
env: env
|
23
|
+
)
|
24
|
+
end
|
15
25
|
end
|
16
26
|
end
|
17
27
|
end
|
@@ -1,21 +1,41 @@
|
|
1
1
|
module Indocker::Configs
|
2
2
|
class Config
|
3
|
+
attr_reader :scope
|
4
|
+
|
3
5
|
def scope
|
4
|
-
@scope ||= Hash.new(
|
6
|
+
@scope ||= Hash.new()
|
7
|
+
end
|
8
|
+
|
9
|
+
def options
|
10
|
+
scope.reject {|_, opt| opt.is_a?(Indocker::Configs::Config)}.keys
|
11
|
+
end
|
12
|
+
|
13
|
+
def configs
|
14
|
+
scope.select {|_, opt| opt.is_a?(Indocker::Configs::Config)}.keys
|
5
15
|
end
|
6
16
|
|
7
17
|
def set(&block)
|
8
18
|
instance_exec(&block)
|
19
|
+
|
20
|
+
self
|
9
21
|
end
|
10
22
|
|
11
|
-
def option(name, group:
|
12
|
-
|
13
|
-
write_value = block || value
|
23
|
+
def option(name, group: nil, type: nil, required: nil)
|
24
|
+
raise Indocker::Errors::ReservedKeywordUsed, name if respond_to?(name.to_sym)
|
14
25
|
|
15
|
-
|
16
|
-
|
26
|
+
define_singleton_method(name) do |value = nil, &block|
|
27
|
+
if type == :config and options.include?(name) and block
|
28
|
+
return read_setting(name).set(&block)
|
29
|
+
end
|
17
30
|
|
18
|
-
|
31
|
+
if block || value
|
32
|
+
write_setting(
|
33
|
+
name: name,
|
34
|
+
value: block || value,
|
35
|
+
group: group,
|
36
|
+
type: type,
|
37
|
+
required: required
|
38
|
+
)
|
19
39
|
else
|
20
40
|
read_setting(name)
|
21
41
|
end
|
@@ -23,32 +43,87 @@ module Indocker::Configs
|
|
23
43
|
end
|
24
44
|
|
25
45
|
def config(name, group: :default, &block)
|
46
|
+
raise Indocker::Errors::ReservedKeywordUsed, name if respond_to?(name.to_sym)
|
47
|
+
|
26
48
|
subconfiguration = Indocker::Configs::Config.new
|
27
49
|
subconfiguration.instance_exec(&block)
|
28
50
|
|
29
51
|
option(name, group: group, type: :config)
|
30
52
|
send(name, subconfiguration)
|
53
|
+
|
54
|
+
subconfiguration
|
55
|
+
end
|
56
|
+
|
57
|
+
def hash_config(hash_config_name, group: :default, &hash_config_block)
|
58
|
+
raise Indocker::Errors::ReservedKeywordUsed, hash_config_name if respond_to?(hash_config_name.to_sym)
|
59
|
+
|
60
|
+
define_singleton_method(hash_config_name) do |name, &self_block|
|
61
|
+
config = config(name, &hash_config_block)
|
62
|
+
config.set(&self_block)
|
63
|
+
|
64
|
+
config
|
65
|
+
end
|
31
66
|
end
|
32
67
|
|
33
|
-
|
34
|
-
|
68
|
+
private
|
69
|
+
|
70
|
+
def read_setting(name)
|
71
|
+
read_value = scope.has_key?(name.intern) ? scope[name.intern].value : nil
|
35
72
|
|
36
73
|
read_value.is_a?(Proc) ? read_value.call : read_value
|
37
74
|
end
|
38
75
|
|
39
|
-
def write_setting(
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
76
|
+
def write_setting(name:, value:, group:, type:, required:)
|
77
|
+
new_option = Option.new(
|
78
|
+
name: name,
|
79
|
+
value: value,
|
80
|
+
group: group,
|
81
|
+
required: required,
|
82
|
+
type: type
|
83
|
+
)
|
84
|
+
|
85
|
+
new_option.validate!
|
86
|
+
|
87
|
+
scope[name.intern] = new_option
|
44
88
|
end
|
45
89
|
|
46
|
-
def validate!(name, value, type)
|
47
|
-
value_type = cast_class_to_type(value)
|
48
90
|
|
49
|
-
|
91
|
+
def method_missing(method, *args, &block)
|
92
|
+
raise "Undefined keyword #{method.inspect} for Indocker configuration file"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
class Option
|
97
|
+
attr_reader :value, :required, :type, :group
|
98
|
+
|
99
|
+
def initialize(name:, value:, required: nil, type: nil, group: nil)
|
100
|
+
@name = name
|
101
|
+
@value = value
|
102
|
+
@required = required || false
|
103
|
+
@type = type || :string
|
104
|
+
@group = group || :default
|
105
|
+
end
|
106
|
+
|
107
|
+
def validate!
|
108
|
+
check_required
|
109
|
+
check_type
|
110
|
+
end
|
111
|
+
|
112
|
+
private
|
113
|
+
|
114
|
+
def check_required
|
115
|
+
if @required && @value.nil?
|
116
|
+
raise Indocker::Errors::ConfigInitializationError,
|
117
|
+
"Configuration option :#{option} is required"
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def check_type
|
122
|
+
value_type = cast_class_to_type(@value)
|
123
|
+
|
124
|
+
if @type != value_type
|
50
125
|
raise Indocker::Errors::ConfigOptionTypeMismatch,
|
51
|
-
"Expected option #{name.inspect} => #{value.inspect} to be a #{type.inspect}, not a #{value_type.inspect}"
|
126
|
+
"Expected option #{@name.inspect} => #{@value.inspect} to be a #{@type.inspect}, not a #{value_type.inspect}"
|
52
127
|
end
|
53
128
|
|
54
129
|
nil
|
@@ -68,9 +143,5 @@ module Indocker::Configs
|
|
68
143
|
Indocker::StringUtils.underscore(value.class.name).to_sym
|
69
144
|
end
|
70
145
|
end
|
71
|
-
|
72
|
-
def method_missing(method, *args, &block)
|
73
|
-
raise "Undefined keyword #{method.inspect} for Indocker configuration file"
|
74
|
-
end
|
75
146
|
end
|
76
147
|
end
|
@@ -4,33 +4,34 @@ class Indocker::Configs::ConfigFactory
|
|
4
4
|
bean :config, factory_method: :build
|
5
5
|
|
6
6
|
CONFIG_STRUCTURE = Proc.new do
|
7
|
-
option :namespace, group: :common
|
8
|
-
option :
|
7
|
+
option :namespace, group: :common, type: :symbol
|
8
|
+
option :build_dir, group: :common, type: :pathname
|
9
9
|
|
10
10
|
option :load_env_file, group: :load, type: :array
|
11
11
|
option :load_docker_items, group: :load, type: :array
|
12
12
|
|
13
13
|
config :git, group: :git do
|
14
|
-
option :
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
option :cache_dir, group: :common, type: :pathname
|
15
|
+
|
16
|
+
hash_config :repo do
|
17
|
+
option :repository
|
18
|
+
option :tag
|
19
|
+
option :branch
|
20
|
+
end
|
18
21
|
end
|
19
22
|
|
20
23
|
config :docker, group: :docker do
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
24
|
+
hash_config :registry do
|
25
|
+
option :serveraddress, required: true
|
26
|
+
option :email
|
27
|
+
option :password
|
28
|
+
option :username
|
29
|
+
option :skip_push, type: :boolean
|
30
|
+
end
|
26
31
|
end
|
27
32
|
end
|
28
33
|
|
29
34
|
def build(&block)
|
30
|
-
|
31
|
-
|
32
|
-
@configuration = Indocker::Configs::Config.new
|
33
|
-
@configuration.instance_exec(&CONFIG_STRUCTURE)
|
34
|
-
@configuration
|
35
|
+
@configuration ||= Indocker::Configs::Config.new.set(&CONFIG_STRUCTURE)
|
35
36
|
end
|
36
37
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class Indocker::ContainerBuilder
|
2
|
+
include SmartIoC::Iocify
|
3
|
+
|
4
|
+
bean :container_builder
|
5
|
+
|
6
|
+
inject :container_metadata_repository
|
7
|
+
inject :envs_loader
|
8
|
+
inject :config
|
9
|
+
|
10
|
+
def build(name)
|
11
|
+
container_metadata = container_metadata_repository.get_by_name(name)
|
12
|
+
|
13
|
+
env_metadata = container_metadata.env_files.inject(Indocker::Envs::EnvMetadata.new) do |all, path|
|
14
|
+
all += envs_loader.parse(path)
|
15
|
+
end
|
16
|
+
|
17
|
+
Indocker::DockerAPI::ContainerConfig.new(
|
18
|
+
name: name,
|
19
|
+
repo: container_metadata.repo,
|
20
|
+
tag: container_metadata.tag,
|
21
|
+
exposed_ports: container_metadata.exposed_ports,
|
22
|
+
port_bindings: container_metadata.port_bindings,
|
23
|
+
cmd: container_metadata.command,
|
24
|
+
volumes: container_metadata.volumes,
|
25
|
+
binds: container_metadata.binds,
|
26
|
+
env: env_metadata.to_array + container_metadata.envs
|
27
|
+
)
|
28
|
+
end
|
29
|
+
end
|
@@ -18,6 +18,8 @@ class Indocker::ContainerDirectivesRunner
|
|
18
18
|
run_network(directive)
|
19
19
|
when Indocker::ContainerDirectives::Ready
|
20
20
|
run_ready(directive)
|
21
|
+
when Indocker::ContainerDirectives::Volume
|
22
|
+
run_volume(directive)
|
21
23
|
else
|
22
24
|
# do nothing
|
23
25
|
end
|
@@ -43,4 +45,10 @@ class Indocker::ContainerDirectivesRunner
|
|
43
45
|
rescue Timeout::Error
|
44
46
|
raise Indocker::Errors::ContainerTimeoutError
|
45
47
|
end
|
48
|
+
|
49
|
+
def run_volume(directive)
|
50
|
+
if !docker_api.volume_exists?(directive.name)
|
51
|
+
docker_api.create_volume(directive.name)
|
52
|
+
end
|
53
|
+
end
|
46
54
|
end
|
@@ -26,23 +26,46 @@ class Indocker::ContainerDSL
|
|
26
26
|
container_name: @context.container_name,
|
27
27
|
network_name: item.name
|
28
28
|
)
|
29
|
+
when Indocker::Volumes::VolumeMetadata
|
30
|
+
@directives << Indocker::ContainerDirectives::Volume.new(
|
31
|
+
volume_name: item.name
|
32
|
+
)
|
29
33
|
end
|
30
34
|
end
|
31
35
|
|
36
|
+
# TODO: Add contract
|
37
|
+
def mount(volume, to:)
|
38
|
+
@directives << Indocker::ContainerDirectives::Volume.new(
|
39
|
+
name: volume.name,
|
40
|
+
to: to
|
41
|
+
)
|
42
|
+
end
|
43
|
+
|
44
|
+
def cmd(command)
|
45
|
+
first_cmd_directive = @directives.detect {|c| c.instance_of?(Indocker::ContainerDirectives::Cmd)}
|
46
|
+
raise Indocker::Errors::DirectiveAlreadyInUse, first_cmd_directive if first_cmd_directive
|
47
|
+
|
48
|
+
@directives << Indocker::ContainerDirectives::Cmd.new(command)
|
49
|
+
end
|
50
|
+
|
32
51
|
def env_file(path)
|
33
52
|
@directives << Indocker::ContainerDirectives::EnvFile.new(path)
|
34
53
|
end
|
35
54
|
|
55
|
+
def env(env_string)
|
56
|
+
@directives << Indocker::ContainerDirectives::Env.new(env_string)
|
57
|
+
end
|
58
|
+
|
36
59
|
def expose(port)
|
37
60
|
@directives << Indocker::ContainerDirectives::Expose.new(path)
|
38
61
|
end
|
39
62
|
|
40
63
|
def ports(ports)
|
41
|
-
|
64
|
+
container_port, host_port = ports.split(':').map(&:strip)
|
42
65
|
|
43
66
|
@directives << Indocker::ContainerDirectives::Ports.new(
|
44
|
-
|
45
|
-
host_port:
|
67
|
+
container_port: container_port,
|
68
|
+
host_port: host_port
|
46
69
|
)
|
47
70
|
end
|
48
71
|
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class Indocker::ContainerInspector
|
2
|
+
include SmartIoC::Iocify
|
3
|
+
|
4
|
+
bean :container_inspector
|
5
|
+
|
6
|
+
inject :container_builder
|
7
|
+
|
8
|
+
def changed?(name)
|
9
|
+
container_config = docker_api.inspect_container(name)[Config]
|
10
|
+
host_config = docker_api.inspect_container(name)[HostConfig]
|
11
|
+
|
12
|
+
inspected_config = Indocker::ContainerConfig.new(
|
13
|
+
image: container_config['Image'],
|
14
|
+
cmd: container_config['Cmd'],
|
15
|
+
env: container_config['Env'],
|
16
|
+
volumes: container_config['Volumes'],
|
17
|
+
binds: host_config['Binds'],
|
18
|
+
exposed_ports: container_config['ExposedPorts'],
|
19
|
+
port_bindings: host_config['PortBindings']
|
20
|
+
)
|
21
|
+
|
22
|
+
metadata_config = container_builder.build(name)
|
23
|
+
|
24
|
+
inspected_config == metadata_config
|
25
|
+
end
|
26
|
+
end
|
@@ -3,7 +3,7 @@ class Indocker::ContainerManager
|
|
3
3
|
|
4
4
|
include SmartIoC::Iocify
|
5
5
|
|
6
|
-
bean
|
6
|
+
bean :container_manager
|
7
7
|
|
8
8
|
inject :container_metadata_repository
|
9
9
|
inject :image_metadata_repository
|
@@ -12,30 +12,41 @@ class Indocker::ContainerManager
|
|
12
12
|
inject :logger
|
13
13
|
inject :tar_helper
|
14
14
|
inject :config
|
15
|
-
inject :
|
15
|
+
inject :container_builder
|
16
16
|
|
17
17
|
def create(name)
|
18
|
-
|
19
|
-
|
20
|
-
env_metadata = container_metadata.env_files
|
21
|
-
.map {|path| envs_loader.parse(config.root.join(path))}
|
22
|
-
.inject(Indocker::Envs::EnvMetadata.new) {|sum, env| sum += env}
|
18
|
+
container_config = container_builder.build(name)
|
23
19
|
|
24
|
-
container_id = docker_api.create_container(
|
25
|
-
name: name,
|
26
|
-
repo: container_metadata.repo,
|
27
|
-
tag: container_metadata.tag,
|
28
|
-
exposed_ports: container_metadata.exposed_ports,
|
29
|
-
port_bindings: container_metadata.port_bindings,
|
30
|
-
env: env_metadata.to_array
|
31
|
-
)
|
20
|
+
container_id = docker_api.create_container(container_config)
|
32
21
|
|
33
22
|
logger.info "Successfully created container :#{name}"
|
34
23
|
|
35
24
|
container_id
|
36
25
|
end
|
37
26
|
|
38
|
-
def
|
27
|
+
def run(name)
|
28
|
+
container_metadata = container_metadata_repository.get_by_name(name)
|
29
|
+
|
30
|
+
container_image_id = docker_api.get_container_image_id(name)
|
31
|
+
image_id = docker_api.get_image_id(container_metadata.repo, tag: container_metadata.tag)
|
32
|
+
|
33
|
+
raise Indocker::Errors::ImageIsNotBuilded, container_metadata.image if image_id.nil?
|
34
|
+
|
35
|
+
if docker_api.container_exists?(name)
|
36
|
+
if image_id != container_image_id
|
37
|
+
stop(name)
|
38
|
+
delete(name)
|
39
|
+
create(name)
|
40
|
+
end
|
41
|
+
else
|
42
|
+
create(name)
|
43
|
+
end
|
44
|
+
|
45
|
+
stop(name)
|
46
|
+
start(name, attach: container_metadata.attach)
|
47
|
+
end
|
48
|
+
|
49
|
+
def start(name, attach: false)
|
39
50
|
container_metadata = container_metadata_repository.get_by_name(name)
|
40
51
|
|
41
52
|
container_directives_runner.run_all(
|
@@ -43,14 +54,17 @@ class Indocker::ContainerManager
|
|
43
54
|
)
|
44
55
|
|
45
56
|
container_metadata.container_dependencies.each do |dependency|
|
57
|
+
dependency_metadata = container_metadata_repository.get_by_name(dependency)
|
46
58
|
create(dependency) unless docker_api.container_exists?(dependency)
|
47
59
|
|
48
|
-
|
49
|
-
|
60
|
+
if docker_api.get_container_state(dependency) == Indocker::ContainerMetadata::States::RUNNING
|
61
|
+
logger.info "Dependency container :#{dependency} already running"
|
62
|
+
else
|
63
|
+
start(dependency, attach: dependency_metadata.attach)
|
50
64
|
end
|
51
65
|
end
|
52
66
|
|
53
|
-
container_id = docker_api.start_container(name)
|
67
|
+
container_id = docker_api.start_container(name, attach: attach)
|
54
68
|
|
55
69
|
logger.info "Successfully started container :#{name}"
|
56
70
|
|
@@ -59,9 +73,6 @@ class Indocker::ContainerManager
|
|
59
73
|
)
|
60
74
|
|
61
75
|
container_id
|
62
|
-
rescue Docker::Error::ClientError => e
|
63
|
-
logger.error "The following error occured when starting :#{name} container"
|
64
|
-
logger.error e.message
|
65
76
|
end
|
66
77
|
|
67
78
|
def stop(name)
|
@@ -73,6 +84,8 @@ class Indocker::ContainerManager
|
|
73
84
|
end
|
74
85
|
|
75
86
|
def delete(name)
|
87
|
+
stop(name)
|
88
|
+
|
76
89
|
container_id = docker_api.delete_container(name)
|
77
90
|
|
78
91
|
logger.info "Successfully deleted container :#{name}"
|
@@ -82,19 +95,17 @@ class Indocker::ContainerManager
|
|
82
95
|
|
83
96
|
def copy(name:, copy_from:, copy_to:)
|
84
97
|
container_metadata = container_metadata_repository.get_by_name(name)
|
85
|
-
|
86
|
-
container_id = docker_api.create_container(
|
87
|
-
repo: container_metadata.repo,
|
88
|
-
tag: container_metadata.tag,
|
89
|
-
command: KEEP_CONTAINER_RUNNING_COMMAND
|
90
|
-
) if !docker_api.container_exists?(name)
|
91
|
-
|
92
|
-
tar_snapshot = File.join(config.root, 'tmp', "#{name.to_s}.tar")
|
93
98
|
|
99
|
+
container_id = docker_api.get_container_id(name) ||
|
100
|
+
docker_api.create_container(container_builder.build(name))
|
101
|
+
|
102
|
+
tar_snapshot = config.build_dir.join('snapshots', "#{name.to_s}.tar")
|
103
|
+
|
104
|
+
FileUtils.mkdir_p(File.dirname(tar_snapshot))
|
94
105
|
docker_api.copy_from_container(name: container_id, path: copy_from) do |tar_archive|
|
95
|
-
File.open(tar_snapshot, '
|
106
|
+
File.open(tar_snapshot, 'a+') {|f| f.write(tar_archive)}
|
96
107
|
end
|
97
|
-
|
108
|
+
|
98
109
|
files_list = tar_helper.untar(
|
99
110
|
io: File.open(tar_snapshot, 'r'),
|
100
111
|
destination: copy_to,
|