11package com .flowci .core .job .service ;
22
3+ import com .flowci .core .api .adviser .ApiAuth ;
4+ import com .flowci .core .common .domain .Variables ;
35import com .flowci .core .common .manager .SpringEventManager ;
46import com .flowci .core .flow .domain .Flow ;
57import com .flowci .core .job .dao .ExecutedLocalTaskDao ;
68import com .flowci .core .job .domain .Executed ;
79import com .flowci .core .job .domain .ExecutedLocalTask ;
810import com .flowci .core .job .domain .Job ;
911import com .flowci .core .job .event .TaskUpdateEvent ;
10- import com .flowci .core .job .manager .DockerManager ;
1112import com .flowci .core .job .manager .YmlManager ;
1213import com .flowci .core .plugin .domain .Plugin ;
1314import com .flowci .core .plugin .event .GetPluginAndVerifySetContext ;
1415import com .flowci .core .plugin .event .GetPluginEvent ;
16+ import com .flowci .docker .ContainerManager ;
17+ import com .flowci .docker .DockerManager ;
18+ import com .flowci .docker .ImageManager ;
19+ import com .flowci .docker .domain .DockerStartOption ;
1520import com .flowci .domain .LocalTask ;
16- import com .flowci .domain .StringVars ;
17- import com .flowci .exception .NotAvailableException ;
1821import com .flowci .exception .StatusException ;
1922import com .flowci .tree .NodeTree ;
2023import com .flowci .util .ObjectsHelper ;
@@ -34,7 +37,10 @@ public class LocalTaskServiceImpl implements LocalTaskService {
3437
3538 private static final String DefaultImage = "flowci/plugin-runtime" ;
3639
37- private static final int DefaultTimeout = 60 ; // seconds
40+ private static final int DefaultTimeout = 120 ; // seconds
41+
42+ @ Autowired
43+ private String serverUrl ;
3844
3945 @ Autowired
4046 private ExecutedLocalTaskDao executedLocalTaskDao ;
@@ -109,13 +115,21 @@ public ExecutedLocalTask execute(Job job, LocalTask task) {
109115 ExecutedLocalTask exec = optional .get ();
110116 updateStatusTimeAndSave (exec , Executed .Status .RUNNING , null );
111117
112- DockerManager . Option option = new DockerManager . Option ();
118+ DockerStartOption option = new DockerStartOption ();
113119 option .setImage (DefaultImage );
114- option .setInputs (new StringVars (job .getContext ()).merge (task .getEnvs ()));
120+ option .addEntryPoint ("/bin/bash" );
121+ option .addEntryPoint ("-c" );
122+ option .getEnv ()
123+ .putAndReturn (Variables .App .Url , serverUrl )
124+ .putAndReturn (Variables .Agent .Token , ApiAuth .LocalTaskToken )
125+ .putAndReturn (Variables .Agent .Workspace , "/ws/" )
126+ .putAndReturn (Variables .Agent .PluginDir , "/ws/.plugins" )
127+ .merge (job .getContext ())
128+ .merge (task .getEnvs ());
115129
116130 if (task .hasPlugin ()) {
117131 String name = task .getPlugin ();
118- GetPluginEvent event = eventManager .publish (new GetPluginAndVerifySetContext (this , name , option .getInputs ()));
132+ GetPluginEvent event = eventManager .publish (new GetPluginAndVerifySetContext (this , name , option .getEnv ()));
119133
120134 if (event .hasError ()) {
121135 String message = event .getError ().getMessage ();
@@ -125,15 +139,9 @@ public ExecutedLocalTask execute(Job job, LocalTask task) {
125139 }
126140
127141 Plugin plugin = event .getFetched ();
128-
129- option .setScript (plugin .getScript ());
130- option .setPlugin (plugin .getName ());
131- option .setPluginDir (event .getDir ().toString ());
132-
133- // apply docker image only from plugin if it's specified
134- ObjectsHelper .ifNotNull (plugin .getDocker (), (docker ) -> {
135- option .setImage (plugin .getDocker ().getImage ());
136- });
142+ option .addEntryPoint (plugin .getScript ());
143+ option .addBind (event .getDir ().toString (), "/ws/.plugins/" + plugin .getName ());
144+ ObjectsHelper .ifNotNull (plugin .getDocker (), (docker ) -> option .setImage (docker .getImage ()));
137145 }
138146
139147 try {
@@ -170,26 +178,28 @@ private void updateStatusTimeAndSave(ExecutedLocalTask t, Executed.Status status
170178 eventManager .publish (new TaskUpdateEvent (this , t .getJobId (), list , false ));
171179 }
172180
173- private void runDockerTask (DockerManager .Option option , ExecutedLocalTask r ) throws Exception {
181+ private void runDockerTask (DockerStartOption option , ExecutedLocalTask r ) throws Exception {
182+ ContainerManager cm = dockerManager .getContainerManager ();
183+ ImageManager im = dockerManager .getImageManager ();
184+ String image = option .getImage ();
185+
174186 try {
175- String image = option .getImage ();
176- boolean isSuccess = dockerManager .pullImage (image );
177- if (!isSuccess ) {
178- throw new NotAvailableException ("Docker image {0} not available" , image );
179- }
187+ im .pull (image , DefaultTimeout , (item ) -> {
188+ log .info (item .getStatus ());
189+ });
180190
181- String cid = dockerManager . createAndStartContainer (option );
191+ String cid = cm . start (option );
182192 r .setContainerId (cid );
183- dockerManager .printContainerLog (cid );
184193
185- if (! dockerManager . waitContainer (cid , DefaultTimeout )) {
186- dockerManager . killContainer ( cid );
187- }
194+ cm . wait (cid , DefaultTimeout , ( frame -> {
195+ log . info ( new String ( frame . getPayload ()) );
196+ }));
188197
189- r .setCode (dockerManager .getContainerExitCode (cid ));
198+ Long exitCode = cm .inspect (cid ).getState ().getExitCodeLong ();
199+ r .setCode (exitCode .intValue ());
190200 } finally {
191201 if (r .hasContainerId ()) {
192- dockerManager . removeContainer (r .getContainerId ());
202+ cm . delete (r .getContainerId ());
193203 }
194204 }
195205 }
0 commit comments