Skip to content

Commit e81422c

Browse files
committed
Refactor DockerCompose::File to create file with new config format
1 parent abc3046 commit e81422c

File tree

2 files changed

+62
-50
lines changed

2 files changed

+62
-50
lines changed

lib/matrixeval/ruby/docker_compose/file.rb

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,11 @@ def docker_compose_file_path
4242
def build_content
4343
{
4444
"version" => "3",
45-
"networks" => { network => nil },
4645
"services" => services_json,
4746
"volumes" => volumes_json
4847
}.to_yaml.sub(/---\n/, "")
4948
end
5049

51-
def network
52-
"matrixeval-#{context.id}"
53-
end
54-
5550
def services_json
5651
services = {}
5752

@@ -64,12 +59,11 @@ def services_json
6459
"BUNDLE_APP_CONFIG" => "/bundle",
6560
"BUNDLE_BIN" => "/bundle/bin",
6661
"PATH" => "/app/bin:/bundle/bin:$PATH"
67-
}.merge(context.env).merge(main_variant.container.env),
68-
"working_dir" => "/app",
69-
"networks" => [network],
62+
}.merge(extra_env),
63+
"working_dir" => "/app"
7064
}.merge(depends_on)
7165

72-
services.merge(extra_services)
66+
services.merge(docker_compose_extend.services)
7367
end
7468

7569
def volumes_json
@@ -88,10 +82,9 @@ def depends_on
8882
end
8983
end
9084

91-
def extra_services
92-
docker_compose_extend.services.map do |name, config|
93-
[name, config.merge({'networks' => [network]})]
94-
end.to_h
85+
def extra_env
86+
Config.env.merge(context.env)
87+
.merge(main_variant.container.env)
9588
end
9689

9790
def main_variant
@@ -107,7 +100,21 @@ def mounts(variant)
107100
"../..:/app:cached",
108101
"#{variant.bundle_volume_name}:/bundle",
109102
"../Gemfile.lock.#{context.id}:/app/Gemfile.lock"
110-
] + Config.main_vector.mounts
103+
] + extra_mounts
104+
end
105+
106+
def extra_mounts
107+
mounts = Config.mounts + context.variants.map(&:mounts).flatten
108+
mounts.map do |mount|
109+
local_path, in_docker_path = mount.split(':')
110+
next mount if Pathname.new(local_path).absolute?
111+
112+
local_path = Matrixeval.working_dir.join(local_path)
113+
docker_compose_folder_path = Matrixeval.working_dir.join(".matrixeval/docker-compose")
114+
local_path = local_path.relative_path_from docker_compose_folder_path
115+
116+
"#{local_path}:#{in_docker_path}"
117+
end
111118
end
112119

113120
def docker_compose_extend

test/matrixeval/ruby/docker_compose/file_test.rb

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ def test_create_all
1616
"version" => "0.2",
1717
"target" => "ruby",
1818
"project_name" => "Dummy_gem",
19+
"mounts" => ["/a/b:/app/c/d"],
20+
"env" => {
21+
"DATABASE_HOST" => "postgres"
22+
},
1923
"matrix" => {
2024
"ruby" => {
2125
"variants" => [
@@ -25,8 +29,21 @@ def test_create_all
2529
},
2630
"active_model" => {
2731
"variants" => [
28-
{ "key" => "6.1", "env" => { "ACTIVE_MODEL_VERSION" => "6.1.4" }, "default" => true },
29-
{ "key" => "7.0", "env" => { "ACTIVE_MODEL_VERSION" => "7.0.0" } }
32+
{
33+
"key" => "6.1",
34+
"env" => { "ACTIVE_MODEL_VERSION" => "6.1.4" },
35+
"default" => true,
36+
"mounts" => [
37+
".matrixeval/schema/rails_6_1.rb:/app/test/dummy/db/schema.rb"
38+
]
39+
},
40+
{
41+
"key" => "7.0",
42+
"env" => { "ACTIVE_MODEL_VERSION" => "7.0.0" },
43+
"mounts" => [
44+
".matrixeval/schema/rails_7_0.rb:/app/test/dummy/db/schema.rb"
45+
]
46+
}
3047
]
3148
}
3249
},
@@ -35,15 +52,15 @@ def test_create_all
3552
"postgres" => {
3653
"image" => "postgres:12.8",
3754
"volumes" => [
38-
"postgres12-<%= matrix_combination_id %>:/var/lib/postgresql/data"
55+
"postgres12:/var/lib/postgresql/data"
3956
],
4057
"environment" => {
4158
"POSTGRES_HOST_AUTH_METHOD" => "trust"
4259
}
4360
}
4461
},
4562
"volumes" => {
46-
"postgres12-<%= matrix_combination_id %>" => nil
63+
"postgres12" => nil
4764
}
4865
}
4966
})
@@ -53,156 +70,144 @@ def test_create_all
5370

5471
expect_file_content = <<~DOCKER_COMPOSE
5572
version: '3'
56-
networks:
57-
matrixeval-ruby_3_0_active_model_6_1:
5873
services:
5974
ruby_3_0:
6075
image: ruby:3.0.0
6176
volumes:
6277
- "../..:/app:cached"
6378
- bundle_ruby_3_0_0:/bundle
6479
- "../Gemfile.lock.ruby_3_0_active_model_6_1:/app/Gemfile.lock"
80+
- "/a/b:/app/c/d"
81+
- "../schema/rails_6_1.rb:/app/test/dummy/db/schema.rb"
6582
environment:
6683
BUNDLE_PATH: "/bundle"
6784
GEM_HOME: "/bundle"
6885
BUNDLE_APP_CONFIG: "/bundle"
6986
BUNDLE_BIN: "/bundle/bin"
7087
PATH: "/app/bin:/bundle/bin:$PATH"
88+
DATABASE_HOST: postgres
7189
ACTIVE_MODEL_VERSION: 6.1.4
7290
working_dir: "/app"
73-
networks:
74-
- matrixeval-ruby_3_0_active_model_6_1
7591
depends_on:
7692
- postgres
7793
postgres:
7894
image: postgres:12.8
7995
volumes:
80-
- postgres12-ruby_3_0_active_model_6_1:/var/lib/postgresql/data
96+
- postgres12:/var/lib/postgresql/data
8197
environment:
8298
POSTGRES_HOST_AUTH_METHOD: trust
83-
networks:
84-
- matrixeval-ruby_3_0_active_model_6_1
8599
volumes:
86100
bundle_ruby_3_0_0:
87101
name: bundle_ruby_3_0_0
88-
postgres12-ruby_3_0_active_model_6_1:
102+
postgres12:
89103
DOCKER_COMPOSE
90104
assert_equal expect_file_content, file_content
91105

92106
file_content = File.read(dummy_gem_docker_compose_folder_path.join("ruby_3_0_active_model_7_0.yml"))
93107
expect_file_content = <<~DOCKER_COMPOSE
94108
version: '3'
95-
networks:
96-
matrixeval-ruby_3_0_active_model_7_0:
97109
services:
98110
ruby_3_0:
99111
image: ruby:3.0.0
100112
volumes:
101113
- "../..:/app:cached"
102114
- bundle_ruby_3_0_0:/bundle
103115
- "../Gemfile.lock.ruby_3_0_active_model_7_0:/app/Gemfile.lock"
116+
- "/a/b:/app/c/d"
117+
- "../schema/rails_7_0.rb:/app/test/dummy/db/schema.rb"
104118
environment:
105119
BUNDLE_PATH: "/bundle"
106120
GEM_HOME: "/bundle"
107121
BUNDLE_APP_CONFIG: "/bundle"
108122
BUNDLE_BIN: "/bundle/bin"
109123
PATH: "/app/bin:/bundle/bin:$PATH"
124+
DATABASE_HOST: postgres
110125
ACTIVE_MODEL_VERSION: 7.0.0
111126
working_dir: "/app"
112-
networks:
113-
- matrixeval-ruby_3_0_active_model_7_0
114127
depends_on:
115128
- postgres
116129
postgres:
117130
image: postgres:12.8
118131
volumes:
119-
- postgres12-ruby_3_0_active_model_7_0:/var/lib/postgresql/data
132+
- postgres12:/var/lib/postgresql/data
120133
environment:
121134
POSTGRES_HOST_AUTH_METHOD: trust
122-
networks:
123-
- matrixeval-ruby_3_0_active_model_7_0
124135
volumes:
125136
bundle_ruby_3_0_0:
126137
name: bundle_ruby_3_0_0
127-
postgres12-ruby_3_0_active_model_7_0:
138+
postgres12:
128139
DOCKER_COMPOSE
129140
assert_equal expect_file_content, file_content
130141

131142
file_content = File.read(dummy_gem_docker_compose_folder_path.join("ruby_3_1_active_model_6_1.yml"))
132143
expect_file_content = <<~DOCKER_COMPOSE
133144
version: '3'
134-
networks:
135-
matrixeval-ruby_3_1_active_model_6_1:
136145
services:
137146
ruby_3_1:
138147
image: ruby:3.1.0
139148
volumes:
140149
- "../..:/app:cached"
141150
- bundle_ruby_3_1_0:/bundle
142151
- "../Gemfile.lock.ruby_3_1_active_model_6_1:/app/Gemfile.lock"
152+
- "/a/b:/app/c/d"
153+
- "../schema/rails_6_1.rb:/app/test/dummy/db/schema.rb"
143154
environment:
144155
BUNDLE_PATH: "/bundle"
145156
GEM_HOME: "/bundle"
146157
BUNDLE_APP_CONFIG: "/bundle"
147158
BUNDLE_BIN: "/bundle/bin"
148159
PATH: "/app/bin:/bundle/bin:$PATH"
160+
DATABASE_HOST: postgres
149161
ACTIVE_MODEL_VERSION: 6.1.4
150162
working_dir: "/app"
151-
networks:
152-
- matrixeval-ruby_3_1_active_model_6_1
153163
depends_on:
154164
- postgres
155165
postgres:
156166
image: postgres:12.8
157167
volumes:
158-
- postgres12-ruby_3_1_active_model_6_1:/var/lib/postgresql/data
168+
- postgres12:/var/lib/postgresql/data
159169
environment:
160170
POSTGRES_HOST_AUTH_METHOD: trust
161-
networks:
162-
- matrixeval-ruby_3_1_active_model_6_1
163171
volumes:
164172
bundle_ruby_3_1_0:
165173
name: bundle_ruby_3_1_0
166-
postgres12-ruby_3_1_active_model_6_1:
174+
postgres12:
167175
DOCKER_COMPOSE
168176
assert_equal expect_file_content, file_content
169177

170178
file_content = File.read(dummy_gem_docker_compose_folder_path.join("ruby_3_1_active_model_7_0.yml"))
171179
expect_file_content = <<~DOCKER_COMPOSE
172180
version: '3'
173-
networks:
174-
matrixeval-ruby_3_1_active_model_7_0:
175181
services:
176182
ruby_3_1:
177183
image: ruby:3.1.0
178184
volumes:
179185
- "../..:/app:cached"
180186
- bundle_ruby_3_1_0:/bundle
181187
- "../Gemfile.lock.ruby_3_1_active_model_7_0:/app/Gemfile.lock"
188+
- "/a/b:/app/c/d"
189+
- "../schema/rails_7_0.rb:/app/test/dummy/db/schema.rb"
182190
environment:
183191
BUNDLE_PATH: "/bundle"
184192
GEM_HOME: "/bundle"
185193
BUNDLE_APP_CONFIG: "/bundle"
186194
BUNDLE_BIN: "/bundle/bin"
187195
PATH: "/app/bin:/bundle/bin:$PATH"
196+
DATABASE_HOST: postgres
188197
ACTIVE_MODEL_VERSION: 7.0.0
189198
working_dir: "/app"
190-
networks:
191-
- matrixeval-ruby_3_1_active_model_7_0
192199
depends_on:
193200
- postgres
194201
postgres:
195202
image: postgres:12.8
196203
volumes:
197-
- postgres12-ruby_3_1_active_model_7_0:/var/lib/postgresql/data
204+
- postgres12:/var/lib/postgresql/data
198205
environment:
199206
POSTGRES_HOST_AUTH_METHOD: trust
200-
networks:
201-
- matrixeval-ruby_3_1_active_model_7_0
202207
volumes:
203208
bundle_ruby_3_1_0:
204209
name: bundle_ruby_3_1_0
205-
postgres12-ruby_3_1_active_model_7_0:
210+
postgres12:
206211
DOCKER_COMPOSE
207212
assert_equal expect_file_content, file_content
208213
end

0 commit comments

Comments
 (0)