3131import com .github .dockerjava .api .model .LogConfig ;
3232import com .github .dockerjava .api .model .RestartPolicy ;
3333import com .github .dockerjava .api .model .Volume ;
34+ import eu .cloudnetservice .driver .document .property .DocProperty ;
3435import eu .cloudnetservice .driver .event .EventManager ;
3536import eu .cloudnetservice .driver .language .I18n ;
37+ import eu .cloudnetservice .driver .provider .ServiceTaskProvider ;
3638import eu .cloudnetservice .driver .registry .Service ;
3739import eu .cloudnetservice .driver .service .ServiceConfiguration ;
40+ import eu .cloudnetservice .driver .service .ServiceTask ;
3841import eu .cloudnetservice .modules .docker .config .DockerConfiguration ;
3942import eu .cloudnetservice .modules .docker .config .DockerImage ;
4043import eu .cloudnetservice .modules .docker .config .DockerPortMapping ;
@@ -86,7 +89,11 @@ public class DockerizedService extends JVMService {
8689 Capability .DAC_OVERRIDE ,
8790 Capability .NET_BIND_SERVICE
8891 ).toArray (Capability []::new );
92+ protected static final DocProperty <TaskDockerConfig > TASK_DOCKER_CONFIG = DocProperty .property (
93+ "dockerConfig" ,
94+ TaskDockerConfig .class );
8995
96+ protected final ServiceTask selfTask ;
9097 protected final DockerClient dockerClient ;
9198 protected final DockerConfiguration configuration ;
9299
@@ -105,6 +112,7 @@ protected DockerizedService(
105112 @ NonNull EventManager eventManager ,
106113 @ NonNull ServiceVersionProvider versionProvider ,
107114 @ NonNull ServiceConfigurationPreparer serviceConfigurationPreparer ,
115+ @ NonNull ServiceTaskProvider serviceTaskProvider ,
108116 @ NonNull DockerClient dockerClient ,
109117 @ NonNull DockerConfiguration dockerConfiguration
110118 ) {
@@ -120,6 +128,7 @@ protected DockerizedService(
120128 versionProvider ,
121129 serviceConfigurationPreparer );
122130
131+ this .selfTask = serviceTaskProvider .serviceTask (configuration .serviceId ().taskName ());
123132 this .dockerClient = dockerClient ;
124133 this .configuration = dockerConfiguration ;
125134 }
@@ -170,16 +179,17 @@ protected void doStartProcess(
170179 var user = Objects .requireNonNullElse (this .configuration .user (), "" );
171180
172181 // get the task specific options
182+ var taskConfig = this .resolveTaskDockerConfig ();
173183 var image = Objects .requireNonNullElse (
174- this .readFromTaskConfig (TaskDockerConfig ::javaImage ),
184+ this .readFromTaskConfig (taskConfig , TaskDockerConfig ::javaImage ),
175185 this .configuration .javaImage ());
176186 var taskExposedPorts = Objects .requireNonNullElse (
177- this .readFromTaskConfig (TaskDockerConfig ::exposedPorts ),
187+ this .readFromTaskConfig (taskConfig , TaskDockerConfig ::exposedPorts ),
178188 Set .<DockerPortMapping >of ());
179189
180190 // 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 );
183193 var exposedPorts = Stream .of (taskExposedPorts , this .configuration .exposedPorts ())
184194 .flatMap (Collection ::stream )
185195 .map (portMapping -> {
@@ -309,7 +319,7 @@ public void doDelete() {
309319 }
310320 }
311321
312- protected @ NonNull Bind [] collectBinds (@ NonNull Path wrapperFilePath ) {
322+ protected @ NonNull Bind [] collectBinds (@ NonNull Path wrapperFilePath , @ Nullable TaskDockerConfig config ) {
313323 Set <Bind > binds = new HashSet <>();
314324
315325 // allow the container full access to the work directory and the wrapper file
@@ -319,7 +329,9 @@ public void doDelete() {
319329 binds .add (this .bindFromPath (this .serviceDirectory .toAbsolutePath ().toString (), AccessMode .rw ));
320330
321331 // 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 ());
323335 binds .addAll (Stream .concat (taskBinds .stream (), this .configuration .binds ().stream ())
324336 .map (path -> this .serviceDirectory .resolve (path ).toAbsolutePath ().toString ())
325337 .map (path -> this .bindFromPath (path , AccessMode .rw ))
@@ -329,16 +341,28 @@ public void doDelete() {
329341 return binds .toArray (Bind []::new );
330342 }
331343
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 ());
334348 return Stream .concat (this .configuration .volumes ().stream (), taskVolumes .stream ())
335349 .map (Volume ::new )
336350 .distinct ()
337351 .toArray (Volume []::new );
338352 }
339353
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+ ) {
342366 return config == null ? null : reader .apply (config );
343367 }
344368
0 commit comments