31
31
import com .github .dockerjava .api .model .LogConfig ;
32
32
import com .github .dockerjava .api .model .RestartPolicy ;
33
33
import com .github .dockerjava .api .model .Volume ;
34
+ import eu .cloudnetservice .driver .document .property .DocProperty ;
34
35
import eu .cloudnetservice .driver .event .EventManager ;
35
36
import eu .cloudnetservice .driver .language .I18n ;
37
+ import eu .cloudnetservice .driver .provider .ServiceTaskProvider ;
36
38
import eu .cloudnetservice .driver .registry .Service ;
37
39
import eu .cloudnetservice .driver .service .ServiceConfiguration ;
40
+ import eu .cloudnetservice .driver .service .ServiceTask ;
38
41
import eu .cloudnetservice .modules .docker .config .DockerConfiguration ;
39
42
import eu .cloudnetservice .modules .docker .config .DockerImage ;
40
43
import eu .cloudnetservice .modules .docker .config .DockerPortMapping ;
@@ -86,7 +89,11 @@ public class DockerizedService extends JVMService {
86
89
Capability .DAC_OVERRIDE ,
87
90
Capability .NET_BIND_SERVICE
88
91
).toArray (Capability []::new );
92
+ protected static final DocProperty <TaskDockerConfig > TASK_DOCKER_CONFIG = DocProperty .property (
93
+ "dockerConfig" ,
94
+ TaskDockerConfig .class );
89
95
96
+ protected final ServiceTask selfTask ;
90
97
protected final DockerClient dockerClient ;
91
98
protected final DockerConfiguration configuration ;
92
99
@@ -105,6 +112,7 @@ protected DockerizedService(
105
112
@ NonNull EventManager eventManager ,
106
113
@ NonNull ServiceVersionProvider versionProvider ,
107
114
@ NonNull ServiceConfigurationPreparer serviceConfigurationPreparer ,
115
+ @ NonNull ServiceTaskProvider serviceTaskProvider ,
108
116
@ NonNull DockerClient dockerClient ,
109
117
@ NonNull DockerConfiguration dockerConfiguration
110
118
) {
@@ -120,6 +128,7 @@ protected DockerizedService(
120
128
versionProvider ,
121
129
serviceConfigurationPreparer );
122
130
131
+ this .selfTask = serviceTaskProvider .serviceTask (configuration .serviceId ().taskName ());
123
132
this .dockerClient = dockerClient ;
124
133
this .configuration = dockerConfiguration ;
125
134
}
@@ -170,16 +179,17 @@ protected void doStartProcess(
170
179
var user = Objects .requireNonNullElse (this .configuration .user (), "" );
171
180
172
181
// get the task specific options
182
+ var taskConfig = this .resolveTaskDockerConfig ();
173
183
var image = Objects .requireNonNullElse (
174
- this .readFromTaskConfig (TaskDockerConfig ::javaImage ),
184
+ this .readFromTaskConfig (taskConfig , TaskDockerConfig ::javaImage ),
175
185
this .configuration .javaImage ());
176
186
var taskExposedPorts = Objects .requireNonNullElse (
177
- this .readFromTaskConfig (TaskDockerConfig ::exposedPorts ),
187
+ this .readFromTaskConfig (taskConfig , TaskDockerConfig ::exposedPorts ),
178
188
Set .<DockerPortMapping >of ());
179
189
180
190
// combine the task options with the global options
181
- var volumes = this .collectVolumes ();
182
- var binds = this .collectBinds (wrapperPath );
191
+ var volumes = this .collectVolumes (taskConfig );
192
+ var binds = this .collectBinds (wrapperPath , taskConfig );
183
193
var exposedPorts = Stream .of (taskExposedPorts , this .configuration .exposedPorts ())
184
194
.flatMap (Collection ::stream )
185
195
.map (portMapping -> {
@@ -309,7 +319,7 @@ public void doDelete() {
309
319
}
310
320
}
311
321
312
- protected @ NonNull Bind [] collectBinds (@ NonNull Path wrapperFilePath ) {
322
+ protected @ NonNull Bind [] collectBinds (@ NonNull Path wrapperFilePath , @ Nullable TaskDockerConfig config ) {
313
323
Set <Bind > binds = new HashSet <>();
314
324
315
325
// allow the container full access to the work directory and the wrapper file
@@ -319,7 +329,9 @@ public void doDelete() {
319
329
binds .add (this .bindFromPath (this .serviceDirectory .toAbsolutePath ().toString (), AccessMode .rw ));
320
330
321
331
// get the task specific volumes and concat them with the default volumes
322
- var taskBinds = Objects .requireNonNullElse (this .readFromTaskConfig (TaskDockerConfig ::binds ), Set .<String >of ());
332
+ var taskBinds = Objects .requireNonNullElse (
333
+ this .readFromTaskConfig (config , TaskDockerConfig ::binds ),
334
+ Set .<String >of ());
323
335
binds .addAll (Stream .concat (taskBinds .stream (), this .configuration .binds ().stream ())
324
336
.map (path -> this .serviceDirectory .resolve (path ).toAbsolutePath ().toString ())
325
337
.map (path -> this .bindFromPath (path , AccessMode .rw ))
@@ -329,16 +341,28 @@ public void doDelete() {
329
341
return binds .toArray (Bind []::new );
330
342
}
331
343
332
- protected @ NonNull Volume [] collectVolumes () {
333
- var taskVolumes = Objects .requireNonNullElse (this .readFromTaskConfig (TaskDockerConfig ::volumes ), Set .<String >of ());
344
+ protected @ NonNull Volume [] collectVolumes (@ Nullable TaskDockerConfig config ) {
345
+ var taskVolumes = Objects .requireNonNullElse (
346
+ this .readFromTaskConfig (config , TaskDockerConfig ::volumes ),
347
+ Set .<String >of ());
334
348
return Stream .concat (this .configuration .volumes ().stream (), taskVolumes .stream ())
335
349
.map (Volume ::new )
336
350
.distinct ()
337
351
.toArray (Volume []::new );
338
352
}
339
353
340
- protected @ Nullable <T > T readFromTaskConfig (@ NonNull Function <TaskDockerConfig , T > reader ) {
341
- var config = this .serviceConfiguration .propertyHolder ().readObject ("dockerConfig" , TaskDockerConfig .class );
354
+ protected @ Nullable TaskDockerConfig resolveTaskDockerConfig () {
355
+ return this .serviceConfiguration .readPropertyOrGet (
356
+ TASK_DOCKER_CONFIG ,
357
+ // it is possible to start a service with a task name that is not linked to a real task therefore we need to
358
+ // make sure that the task exists before proceeding
359
+ () -> this .selfTask == null ? null : this .selfTask .readProperty (TASK_DOCKER_CONFIG ));
360
+ }
361
+
362
+ protected @ Nullable <T > T readFromTaskConfig (
363
+ @ Nullable TaskDockerConfig config ,
364
+ @ NonNull Function <TaskDockerConfig , T > reader
365
+ ) {
342
366
return config == null ? null : reader .apply (config );
343
367
}
344
368
0 commit comments