1- package de .unibi .cebitec .bibiserv .jobproxy .javadocker ;
1+ package de .unibi .cebitec .bibiserv .jobproxy .local ;
22
33import com .spotify .docker .client .DefaultDockerClient ;
4+ import com .spotify .docker .client .DockerClient ;
45import com .spotify .docker .client .exceptions .DockerException ;
56import com .spotify .docker .client .messages .*;
67import de .unibi .cebitec .bibiserv .jobproxy .model .JobProxyInterface ;
1213import de .unibi .cebitec .bibiserv .jobproxy .model .task .Task ;
1314
1415
15- import java .util . Arrays ;
16+ import java .io . IOException ;
1617import java .util .List ;
1718import java .util .Properties ;
1819import java .util .stream .Collectors ;
1920
2021
21- public class JavaDocker extends JobProxyInterface {
22+ public class Local extends JobProxyInterface {
2223
2324 private final DefaultDockerClient dockerClient ;
2425
25- public JavaDocker (Properties properties ) {
26+ public Local (Properties properties ) {
2627 super (properties );
2728 dockerClient = new DefaultDockerClient ("unix:///var/run/docker.sock" );
2829 }
2930
30- @ Override
31- public String addTask (Task task ) throws FrameworkException {
32-
31+ private String handleDockerTask (Task task ){
3332 HostConfig .Builder hostConfigBuilder = HostConfig .builder ();
3433
3534 task .getContainer ().getMounts ().forEach (mounts -> {
@@ -82,6 +81,27 @@ public String addTask(Task task) throws FrameworkException {
8281 return id ;
8382 }
8483
84+ private String handleLocalCommand (Task task ) throws FrameworkException {
85+ try {
86+ Runtime .getRuntime ().exec (task .getCmd ());
87+ } catch (IOException e ) {
88+ e .printStackTrace ();
89+ throw new FrameworkException (e .getMessage ());
90+ }
91+ return "" ;
92+ }
93+
94+ @ Override
95+ public String addTask (Task task ) throws FrameworkException {
96+
97+ if (task .getContainer () == null ) {
98+ return handleLocalCommand (task );
99+ } else {
100+ return handleDockerTask (task );
101+ }
102+ }
103+
104+
85105 @ Override
86106 public Task getTask (String id ) throws FrameworkException {
87107
@@ -128,7 +148,6 @@ public Task getTask(String id) throws FrameworkException {
128148 tmounts .setMount (mount );
129149 container .getMounts ().add (tmounts );
130150 });
131-
132151 task .setContainer (container );
133152
134153 return task ;
@@ -150,9 +169,9 @@ public void delTask(String id) throws FrameworkException {
150169
151170 @ Override
152171 public State getState (String id ) throws FrameworkException {
153- ExecState inspect ;
172+ ContainerInfo container ;
154173 try {
155- inspect = dockerClient .execInspect (id );
174+ container = dockerClient .inspectContainer (id );
156175 } catch (DockerException e ) {
157176 e .printStackTrace ();
158177 throw new FrameworkException (e .getMessage ());
@@ -161,24 +180,27 @@ public State getState(String id) throws FrameworkException {
161180 throw new FrameworkException (e .getMessage ());
162181 }
163182 State state = new State ();
164- state .setId (inspect .container ().id ());
165- if (!inspect .running ()) {
166- state .setCode (String .valueOf (inspect .exitCode ()));
183+ state .setId (container .id ());
184+ state .setDescription (container .name ());
185+
186+ if (!container .state ().running ()) {
187+ state .setCode (String .valueOf (container .state ().exitCode ()));
167188 }
168- state .setDescription (inspect . container () .name ());
189+ state .setDescription (container .name ());
169190 return state ;
170191 }
171192
172193 @ Override
173194 public States getState () throws FrameworkException {
174195 States states = new States ();
175196 try {
176- List <Container > containers = dockerClient .listContainers ();
197+ List <Container > containers = dockerClient .listContainers (DockerClient . ListContainersParam . allContainers () );
177198 for (Container container : containers ) {
178199 states .getState ().add (getState (container .id ()));
179200 }
180201 } catch (FrameworkException e ) {
181-
202+ e .printStackTrace ();
203+ throw new FrameworkException (e .getMessage ());
182204 } catch (DockerException e ) {
183205 e .printStackTrace ();
184206 throw new FrameworkException (e .getMessage ());
@@ -191,12 +213,15 @@ public States getState() throws FrameworkException {
191213
192214 @ Override
193215 public String getName () {
194- return "JavaDocker " ;
216+ return "JobProxyLocal " ;
195217 }
196218
197219 @ Override
198220 public String help () {
199- throw new UnsupportedOperationException ("Not supported yet." ); //To change body of generated methods, choose Tools | Templates.
221+ return "Simple Jobproxy implementation for executing commands and docker container on the local system.\n " +
222+ "Note!: \n " +
223+ "Ressources for system native commands can not be restricted! This means that fields like mem,cpu and cputime are ignored\n " +
224+ "Executed native commands are not reported by using the 'state' or 'states' endpoint." ;
200225 }
201226}
202227
0 commit comments