1 | /* |
---|
2 | * Author : Gérald FENOY |
---|
3 | * |
---|
4 | * Copyright (c) 2017 GeoLabs SARL |
---|
5 | * |
---|
6 | * This work was supported by public funds received in the framework of GEOSUD, |
---|
7 | * a project (ANR-10-EQPX-20) of the program "Investissements d'Avenir" managed |
---|
8 | * by the French National Research Agency |
---|
9 | * |
---|
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
11 | * of this software and associated documentation files (the "Software"), to deal |
---|
12 | * in the Software without restriction, including without limitation the rights |
---|
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
14 | * copies of the Software, and to permit persons to whom the Software is |
---|
15 | * furnished to do so, subject to the following conditions: |
---|
16 | * |
---|
17 | * The above copyright notice and this permission notice shall be included in |
---|
18 | * all copies or substantial portions of the Software. |
---|
19 | * |
---|
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
26 | * THE SOFTWARE. |
---|
27 | * |
---|
28 | */ |
---|
29 | |
---|
30 | #include "service_internal_hpc.h" |
---|
31 | #include "response_print.h" |
---|
32 | #include "server_internal.h" |
---|
33 | #include "service_callback.h" |
---|
34 | #include "mimetypes.h" |
---|
35 | #include <sys/un.h> |
---|
36 | |
---|
37 | typedef struct { |
---|
38 | maps* conf; |
---|
39 | char* local_file; |
---|
40 | char* target_file; |
---|
41 | } local_params; |
---|
42 | |
---|
43 | /** |
---|
44 | * Add nested outputs to every outputs that is geographic format |
---|
45 | * @see isGeographic |
---|
46 | * @param s the service current definition |
---|
47 | */ |
---|
48 | void addNestedOutputs(service** s){ |
---|
49 | if((*s)==NULL){ |
---|
50 | return; |
---|
51 | } |
---|
52 | if(*s==NULL || (*s)->outputs==NULL){ |
---|
53 | return; |
---|
54 | } |
---|
55 | elements *out=(*s)->outputs; |
---|
56 | elements* cur=out; |
---|
57 | map* serviceType=getMap((*s)->content,"ServiceType"); |
---|
58 | if(strncmp(serviceType->value,"HPC",3)!=0) |
---|
59 | return; |
---|
60 | while(cur!=NULL && cur->defaults!=NULL){ |
---|
61 | map* mimeType=getMap(cur->defaults->content,"mimeType"); |
---|
62 | if(mimeType!=NULL){ |
---|
63 | int geo=isGeographic(mimeType->value); |
---|
64 | if(geo>0){ |
---|
65 | elements *tmp[3]={ |
---|
66 | dupElements(cur), |
---|
67 | dupElements(cur), |
---|
68 | dupElements(cur) |
---|
69 | }; |
---|
70 | char *geoLink="wcs_link"; |
---|
71 | if(geo==2){ |
---|
72 | geoLink="wfs_link"; |
---|
73 | } |
---|
74 | int i=0; |
---|
75 | for(;i<3;i++){ |
---|
76 | if(tmp[i]->next!=NULL){ |
---|
77 | freeElements(&tmp[i]->next); |
---|
78 | free(tmp[i]->next); |
---|
79 | tmp[i]->next=NULL; |
---|
80 | } |
---|
81 | free(tmp[i]->name); |
---|
82 | if(tmp[i]->format!=NULL) |
---|
83 | free(tmp[i]->format); |
---|
84 | tmp[i]->format=zStrdup("ComplexData"); |
---|
85 | freeMap(&tmp[i]->content); |
---|
86 | free(tmp[i]->content); |
---|
87 | tmp[i]->content=NULL; |
---|
88 | switch(i){ |
---|
89 | case 0: |
---|
90 | tmp[i]->name=zStrdup("download_link"); |
---|
91 | tmp[i]->content=createMap("Title",_("Download link")); |
---|
92 | addToMap(tmp[i]->content,"Abstract",_("The download link")); |
---|
93 | addToMap(tmp[i]->defaults->content,"useMapserver","false"); |
---|
94 | if(tmp[i]->supported!=NULL){ |
---|
95 | freeIOType(&tmp[i]->supported); |
---|
96 | free(tmp[i]->supported); |
---|
97 | tmp[i]->supported=NULL; |
---|
98 | } |
---|
99 | break; |
---|
100 | case 1: |
---|
101 | tmp[i]->name=zStrdup("wms_link"); |
---|
102 | tmp[i]->content=createMap("Title",_("WMS link")); |
---|
103 | addToMap(tmp[i]->content,"Abstract",_("The WMS link")); |
---|
104 | if(tmp[i]->supported!=NULL && tmp[i]->supported->next!=NULL){ |
---|
105 | freeIOType(&tmp[i]->supported->next); |
---|
106 | free(tmp[i]->supported->next); |
---|
107 | tmp[i]->supported->next=NULL; |
---|
108 | }else{ |
---|
109 | if(tmp[i]->supported!=NULL) |
---|
110 | addToMap(tmp[i]->supported->content,"useMapserver","true"); |
---|
111 | addToMap(tmp[i]->defaults->content,"useMapserver","true"); |
---|
112 | } |
---|
113 | break; |
---|
114 | case 2: |
---|
115 | if(geo==2){ |
---|
116 | tmp[i]->name=zStrdup("wfs_link"); |
---|
117 | tmp[i]->content=createMap("Title",_("WFS link")); |
---|
118 | addToMap(tmp[i]->content,"Abstract",_("The WFS link")); |
---|
119 | }else{ |
---|
120 | tmp[i]->name=zStrdup("wcs_link"); |
---|
121 | tmp[i]->content=createMap("Title",_("WCS link")); |
---|
122 | addToMap(tmp[i]->content,"Abstract",_("The WCS link")); |
---|
123 | } |
---|
124 | if(tmp[i]->supported!=NULL && tmp[i]->supported->next!=NULL && |
---|
125 | tmp[i]->supported->next->content!=NULL){ |
---|
126 | freeIOType(&tmp[i]->supported); |
---|
127 | free(tmp[i]->supported); |
---|
128 | tmp[i]->supported=NULL; |
---|
129 | tmp[i]->supported=createIoType(); |
---|
130 | iotype* cnext=cur->supported->next; |
---|
131 | tmp[i]->supported->content=createMap(cnext->content->name,cnext->content->value); |
---|
132 | addMapToMap(&tmp[i]->supported->content,cnext->content->next); |
---|
133 | addToMap(tmp[i]->supported->content,"useMapserver","true"); |
---|
134 | }else |
---|
135 | addToMap(tmp[i]->defaults->content,"useMapserver","true"); |
---|
136 | break; |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | addToElements(&cur->child,tmp[0]); |
---|
141 | addToElements(&cur->child,tmp[1]); |
---|
142 | addToElements(&cur->child,tmp[2]); |
---|
143 | free(cur->format); |
---|
144 | cur->format=NULL; |
---|
145 | if(cur->defaults!=NULL){ |
---|
146 | freeIOType(&cur->defaults); |
---|
147 | cur->defaults=NULL; |
---|
148 | } |
---|
149 | if(cur->supported!=NULL){ |
---|
150 | freeIOType(&cur->supported); |
---|
151 | cur->supported=NULL; |
---|
152 | } |
---|
153 | freeElements(&tmp[2]); |
---|
154 | free(tmp[2]); |
---|
155 | freeElements(&tmp[1]); |
---|
156 | free(tmp[1]); |
---|
157 | freeElements(&tmp[0]); |
---|
158 | free(tmp[0]); |
---|
159 | //addToMap(cur->content,"internal","true"); |
---|
160 | } |
---|
161 | } |
---|
162 | cur=cur->next; |
---|
163 | } |
---|
164 | //dumpElements((*s)->outputs); |
---|
165 | } |
---|
166 | |
---|
167 | /** |
---|
168 | * Load and run a HPC Application corresponding to the service. |
---|
169 | * |
---|
170 | * @param main_conf the conf maps containing the main.cfg settings |
---|
171 | * @param request the map containing the HTTP request |
---|
172 | * @param s the service structure |
---|
173 | * @param real_inputs the maps containing the inputs |
---|
174 | * @param real_outputs the maps containing the outputs |
---|
175 | */ |
---|
176 | int zoo_hpc_support(maps** main_conf,map* request,service* s,maps **real_inputs,maps **real_outputs){ |
---|
177 | maps* m=*main_conf; |
---|
178 | maps* inputs=*real_inputs; |
---|
179 | maps* outputs=*real_outputs; |
---|
180 | map* tmp0=getMapFromMaps(*main_conf,"lenv","cwd"); |
---|
181 | char *ntmp=tmp0->value; |
---|
182 | map* tmp=NULL; |
---|
183 | int res=-1; |
---|
184 | char *serviceType; |
---|
185 | map* mServiceType=getMap(s->content,"serviceType"); |
---|
186 | if(mServiceType!=NULL) |
---|
187 | serviceType=mServiceType->value; |
---|
188 | else |
---|
189 | serviceType="HPC"; |
---|
190 | map* targetPathMap=getMapFromMaps(*main_conf,serviceType,"storagePath"); |
---|
191 | map* tmpPath=getMapFromMaps(*main_conf,"main","tmpPath"); |
---|
192 | map* uuid=getMapFromMaps(*main_conf,"lenv","usid"); |
---|
193 | pthread_t threads_pool[50]; |
---|
194 | // Force the HPC services to be called asynchronously |
---|
195 | map* isAsync=getMapFromMaps(*main_conf,"lenv","async"); |
---|
196 | if(isAsync==NULL){ |
---|
197 | errorException(*main_conf,_("The synchronous mode is not supported by this type of service"),"NoSuchMode",s->name); |
---|
198 | return -1; |
---|
199 | } |
---|
200 | |
---|
201 | maps* input=*real_inputs; |
---|
202 | char **parameters=NULL; |
---|
203 | int parameters_cnt=0; |
---|
204 | while(input!=NULL && input->content!=NULL){ |
---|
205 | if(getMaps(*real_outputs,input->name)==NULL){ |
---|
206 | parameters_cnt+=1; |
---|
207 | if(parameters_cnt==1) |
---|
208 | parameters=(char**)malloc(parameters_cnt*sizeof(char*)); |
---|
209 | else |
---|
210 | parameters=(char**)realloc(parameters,parameters_cnt*sizeof(char*)); |
---|
211 | if(getMap(input->content,"mimeType")!=NULL){ |
---|
212 | // Input is ComplexData |
---|
213 | if(getMap(input->content,"cache_file")==NULL){ |
---|
214 | // Input data has been passed by value |
---|
215 | // TODO: publish input through MapServer / use output publication |
---|
216 | dumpMapsValuesToFiles(main_conf,&input); |
---|
217 | addToMap(input->content,"toPublish","true"); |
---|
218 | } |
---|
219 | if(getMap(input->content,"cache_file")!=NULL){ |
---|
220 | map* length=getMap(input->content,"length"); |
---|
221 | if(length==NULL){ |
---|
222 | addToMap(input->content,"length","1"); |
---|
223 | length=getMap(input->content,"length"); |
---|
224 | } |
---|
225 | int len=atoi(length->value); |
---|
226 | int i=0; |
---|
227 | for(i=0;i<len;i++){ |
---|
228 | map* tmp=getMapArray(input->content,"cache_file",i); |
---|
229 | char* targetName=strrchr(tmp->value,'/'); |
---|
230 | char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char)); |
---|
231 | sprintf(targetPath,"%s/%s",targetPathMap->value,targetName); |
---|
232 | setMapArray(input->content,"targetPath",i,targetPath); |
---|
233 | setMapArray(input->content,"localPath",i,tmp->value); |
---|
234 | addToUploadQueue(main_conf,input); |
---|
235 | if(i==0){ |
---|
236 | parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+3)*sizeof(char)); |
---|
237 | sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,targetPath); |
---|
238 | }else{ |
---|
239 | char *tmpStr=zStrdup(parameters[parameters_cnt-1]); |
---|
240 | parameters[parameters_cnt-1]=(char*)realloc(parameters[parameters_cnt-1],(strlen(tmpStr)+strlen(targetPath)+2)*sizeof(char)); |
---|
241 | sprintf(parameters[parameters_cnt-1],"%s %s",tmpStr,targetPath); |
---|
242 | free(tmpStr); |
---|
243 | } |
---|
244 | free(targetPath); |
---|
245 | } |
---|
246 | }else{ |
---|
247 | // ??? |
---|
248 | fprintf(stderr,"%s %d\n",__FILE__,__LINE__); |
---|
249 | fflush(stderr); |
---|
250 | } |
---|
251 | }else{ |
---|
252 | // LitteralData and BboxData |
---|
253 | if(getMap(input->content,"dataType")!=NULL){ |
---|
254 | // For LitteralData, simply pass the value |
---|
255 | map* val=getMap(input->content,"value"); |
---|
256 | parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(val->value)+3)*sizeof(char)); |
---|
257 | sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,val->value); |
---|
258 | } |
---|
259 | } |
---|
260 | } |
---|
261 | input=input->next; |
---|
262 | } |
---|
263 | |
---|
264 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
265 | invokeCallback(m,inputs,NULL,2,0); |
---|
266 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
267 | |
---|
268 | // Upload data on HPC |
---|
269 | runUpload(main_conf); |
---|
270 | |
---|
271 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
272 | invokeCallback(m,inputs,NULL,2,1); |
---|
273 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
274 | |
---|
275 | // Add the filename to generate for every output to the parameters |
---|
276 | input=*real_outputs; |
---|
277 | // TODO: fix appendOutputParameters |
---|
278 | //appendOutputParameters(input,parameters,¶meters_cnt,s,uuid,targetPathMap); |
---|
279 | while(input!=NULL){ |
---|
280 | // TODO: parse all outputs including inner outputs if required. |
---|
281 | if(input->child==NULL){ |
---|
282 | parameters_cnt+=1; |
---|
283 | if(parameters_cnt==1) |
---|
284 | parameters=(char**)malloc(parameters_cnt*sizeof(char*)); |
---|
285 | else |
---|
286 | parameters=(char**)realloc(parameters,parameters_cnt*sizeof(char*)); |
---|
287 | // Name every files that should be produced by the service execution |
---|
288 | map* mime=getMap(input->content,"mimeType"); |
---|
289 | char* targetName; |
---|
290 | if(mime!=NULL){ |
---|
291 | bool hasExt=false; |
---|
292 | map* fileExt=getFileExtensionMap(mime->value,&hasExt); |
---|
293 | targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+strlen(fileExt->value)+11)*sizeof(char)); |
---|
294 | sprintf(targetName,"output_%s_%s_%s.%s",s->name,input->name,uuid->value,fileExt->value); |
---|
295 | freeMap(&fileExt); |
---|
296 | free(fileExt); |
---|
297 | }else{ |
---|
298 | targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+14)*sizeof(char)); |
---|
299 | sprintf(targetName,"output_%s_%s_%s.tif",s->name,input->name,uuid->value); |
---|
300 | } |
---|
301 | char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char)); |
---|
302 | sprintf(targetPath,"%s/%s",targetPathMap->value,targetName); |
---|
303 | setMapInMaps(*real_outputs,input->name,"generated_file",targetPath); |
---|
304 | // We should verify if any optional tag for output is required |
---|
305 | // (i.e. -out output.tiff *int8*), meaning that we should search |
---|
306 | // for a corresponding inputs name. |
---|
307 | map* inValue=getMapFromMaps(*real_inputs,input->name,"value"); |
---|
308 | if(inValue!=NULL){ |
---|
309 | parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+strlen(inValue->value)+4)*sizeof(char)); |
---|
310 | sprintf(parameters[parameters_cnt-1],"-%s %s %s",input->name,targetPath,inValue->value); |
---|
311 | }else{ |
---|
312 | parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+3)*sizeof(char)); |
---|
313 | sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,targetPath); |
---|
314 | } |
---|
315 | free(targetPath); |
---|
316 | }// In other case it means we need to return the cache_file as generated_file |
---|
317 | else{ |
---|
318 | parameters_cnt+=1; |
---|
319 | if(parameters_cnt==1) |
---|
320 | parameters=(char**)malloc(parameters_cnt*sizeof(char*)); |
---|
321 | else |
---|
322 | parameters=(char**)realloc(parameters,parameters_cnt*sizeof(char*)); |
---|
323 | // Name every files that should be produced by the service execution |
---|
324 | map* mime=getMap(input->child->content,"mimeType"); |
---|
325 | char* targetName; |
---|
326 | if(mime!=NULL){ |
---|
327 | bool hasExt=false; |
---|
328 | map* fileExt=getFileExtensionMap(mime->value,&hasExt); |
---|
329 | targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+strlen(fileExt->value)+11)*sizeof(char)); |
---|
330 | sprintf(targetName,"output_%s_%s_%s.%s",s->name,input->name,uuid->value,fileExt->value); |
---|
331 | freeMap(&fileExt); |
---|
332 | free(fileExt); |
---|
333 | }else{ |
---|
334 | targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+14)*sizeof(char)); |
---|
335 | sprintf(targetName,"output_%s_%s_%s.tif",s->name,input->name,uuid->value); |
---|
336 | } |
---|
337 | char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char)); |
---|
338 | sprintf(targetPath,"%s/%s",targetPathMap->value,targetName); |
---|
339 | addToMap(input->content,"generated_file",targetPath); |
---|
340 | // We should verify if any optional tag for output is required |
---|
341 | // (i.e. -out output.tiff *int8*), meaning that we should search |
---|
342 | // for a corresponding inputs name. |
---|
343 | map* inValue=getMapFromMaps(*real_inputs,input->name,"value"); |
---|
344 | if(inValue!=NULL){ |
---|
345 | parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+strlen(inValue->value)+4)*sizeof(char)); |
---|
346 | sprintf(parameters[parameters_cnt-1],"-%s %s %s",input->name,targetPath,inValue->value); |
---|
347 | }else{ |
---|
348 | parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+3)*sizeof(char)); |
---|
349 | sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,targetPath); |
---|
350 | } |
---|
351 | free(targetPath); |
---|
352 | } |
---|
353 | input=input->next; |
---|
354 | } |
---|
355 | |
---|
356 | // Produce the SBATCH File locally |
---|
357 | char *scriptPath=(char*)malloc((strlen(s->name)+strlen(tmpPath->value)+strlen(uuid->value)+10)*sizeof(char)); |
---|
358 | sprintf(scriptPath,"%s/zoo_%s_%s.sh",tmpPath->value,s->name,uuid->value); |
---|
359 | setMapInMaps(*main_conf,"lenv","local_script",scriptPath); |
---|
360 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
361 | fflush(stderr); |
---|
362 | invokeCallback(m,inputs,NULL,3,0); |
---|
363 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
364 | fflush(stderr); |
---|
365 | FILE* scriptFile=fopen(scriptPath,"w+"); |
---|
366 | map* headerMap=getMapFromMaps(*main_conf,serviceType,"header"); |
---|
367 | if(headerMap!=NULL){ |
---|
368 | // Use the header file if defined in the HPC section of the main.cfg file |
---|
369 | struct stat f_status; |
---|
370 | int s=stat(headerMap->value, &f_status); |
---|
371 | if(s==0){ |
---|
372 | char* fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1)); |
---|
373 | FILE* f=fopen(headerMap->value,"rb"); |
---|
374 | fread(fcontent,f_status.st_size,1,f); |
---|
375 | int fsize=f_status.st_size; |
---|
376 | fcontent[fsize]=0; |
---|
377 | fclose(f); |
---|
378 | fprintf(scriptFile,"%s\n### --- ZOO-Service HEADER end --- ###\n\n",fcontent); |
---|
379 | free(fcontent); |
---|
380 | }else |
---|
381 | fprintf(scriptFile,"#!/bin/bash\n\n### *** Default ZOO-Service HEADER (no header found) *** ###\n\n"); |
---|
382 | }else |
---|
383 | fprintf(scriptFile,"#!/bin/bash\n\n### *** Default ZOO-Service HEADER *** ###\n\n"); |
---|
384 | maps* hpc_opts=getMaps(*main_conf,"sbatch_options"); |
---|
385 | if(hpc_opts!=NULL){ |
---|
386 | map* hpc_opts_content=hpc_opts->content; |
---|
387 | while(hpc_opts_content!=NULL){ |
---|
388 | fprintf(scriptFile,"#SBATCH --%s=%s\n",hpc_opts_content->name,hpc_opts_content->value); |
---|
389 | hpc_opts_content=hpc_opts_content->next; |
---|
390 | } |
---|
391 | } |
---|
392 | fprintf(scriptFile,"#SBATCH --job-name=ZOO-Project_%s_%s\n\n",uuid->value,s->name); |
---|
393 | map* mods=getMap(s->content,"hpcModules"); |
---|
394 | if(mods!=NULL) |
---|
395 | fprintf(scriptFile,"#SBATCH --export=MODULES=%s\n",mods->value); |
---|
396 | |
---|
397 | map* bodyMap=getMapFromMaps(*main_conf,serviceType,"body"); |
---|
398 | if(bodyMap!=NULL){ |
---|
399 | // Use the header file if defined in the HPC section of the main.cfg file |
---|
400 | struct stat f_status; |
---|
401 | int s=stat(bodyMap->value, &f_status); |
---|
402 | if(s==0){ |
---|
403 | char* fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1)); |
---|
404 | FILE* f=fopen(bodyMap->value,"rb"); |
---|
405 | fread(fcontent,f_status.st_size,1,f); |
---|
406 | int fsize=f_status.st_size; |
---|
407 | fcontent[fsize]=0; |
---|
408 | fclose(f); |
---|
409 | fprintf(scriptFile,"%s\n### --- ZOO-Service BODY end --- ###\n\n",fcontent); |
---|
410 | free(fcontent); |
---|
411 | }else |
---|
412 | fprintf(scriptFile,"#!/bin/bash\n\n### *** Default ZOO-Service BODY (no body found) *** ###\n\n"); |
---|
413 | }else |
---|
414 | fprintf(scriptFile,"#!/bin/bash\n\n### *** Default ZOO-Service BODY *** ###\n\n"); |
---|
415 | |
---|
416 | |
---|
417 | map* sp=getMap(s->content,"serviceProvider"); |
---|
418 | |
---|
419 | // Require to produce the command line to be executed |
---|
420 | fprintf(scriptFile,"\n\necho \"Job started at: $(date)\"\n"); |
---|
421 | fprintf(scriptFile,"echo \"Running service: [%s]\"\n",sp->value); |
---|
422 | fprintf(scriptFile,"%s ",sp->value); |
---|
423 | for(int i=0;i<parameters_cnt;i++){ |
---|
424 | fprintf(scriptFile," %s",parameters[i]); |
---|
425 | } |
---|
426 | for(int i=parameters_cnt-1;i>=0;i--){ |
---|
427 | free(parameters[i]); |
---|
428 | } |
---|
429 | free(parameters); |
---|
430 | fprintf(scriptFile,"\n"); |
---|
431 | fprintf(scriptFile,"echo \"Job finished at: $(date)\"\n"); |
---|
432 | fflush(scriptFile); |
---|
433 | fclose(scriptFile); |
---|
434 | |
---|
435 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
436 | invokeCallback(m,inputs,NULL,3,1); |
---|
437 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
438 | |
---|
439 | // Upload the SBATCH File to the remote host |
---|
440 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
441 | invokeCallback(m,inputs,NULL,4,0); |
---|
442 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
443 | targetPathMap=getMapFromMaps(*main_conf,serviceType,"executePath"); |
---|
444 | if(targetPathMap==NULL){ |
---|
445 | setMapInMaps(*main_conf,"lenv","message",_("There is no executePath defined in you HPC section!")); |
---|
446 | return SERVICE_FAILED; |
---|
447 | } |
---|
448 | char* targetName=strrchr(scriptPath,'/'); |
---|
449 | char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char)); |
---|
450 | sprintf(targetPath,"%s/%s",targetPathMap->value,targetName); |
---|
451 | setMapInMaps(*main_conf,"lenv","remote_script",targetPath); |
---|
452 | SSHCON *test=ssh_connect(*main_conf); |
---|
453 | ssh_copy(*main_conf,scriptPath,targetPath,ssh_get_cnt(*main_conf)); |
---|
454 | |
---|
455 | // Execute the SBATCH script remotely |
---|
456 | map* subStr=getMapFromMaps(*main_conf,"HPC","subStr"); |
---|
457 | char *command=(char*)malloc((strlen(targetPath)+strlen(targetPathMap->value)+strlen(subStr->value)+strlen(uuid->value)+137)*sizeof(char)); |
---|
458 | sprintf(command,"sbatch %s 2> %s/error_%s.log | sed \"s:%s::g\"",targetPath,targetPathMap->value,uuid->value,subStr->value); |
---|
459 | if(ssh_exec(*main_conf,command,ssh_get_cnt(m))==0){ |
---|
460 | // The sbatch command has failed! |
---|
461 | // Download the error log file from the HPC server |
---|
462 | char tmpS[1024]; |
---|
463 | free(command); |
---|
464 | command=(char*)malloc((strlen(targetPathMap->value)+strlen(uuid->value)+11)*sizeof(char)); |
---|
465 | sprintf(command,"%s/error_%s.log",targetPathMap->value,uuid->value); |
---|
466 | targetName=strrchr(command,'/'); |
---|
467 | free(targetPath); |
---|
468 | targetPath=(char*)malloc((strlen(tmpPath->value)+strlen(targetName)+2)*sizeof(char)); |
---|
469 | sprintf(targetPath,"%s/%s",tmpPath->value,targetName); |
---|
470 | if(ssh_fetch(*main_conf,targetPath,command,ssh_get_cnt(m))==0){ |
---|
471 | struct stat f_status; |
---|
472 | int ts=stat(targetPath, &f_status); |
---|
473 | if(ts==0) { |
---|
474 | char* fcontent = NULL; |
---|
475 | fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1)); |
---|
476 | FILE* f=fopen(targetPath,"rb"); |
---|
477 | fread(fcontent,f_status.st_size,1,f); |
---|
478 | int fsize=f_status.st_size; |
---|
479 | fcontent[fsize]=0; |
---|
480 | fclose(f); |
---|
481 | setMapInMaps(*main_conf,"lenv","message",fcontent); |
---|
482 | free(fcontent); |
---|
483 | }else |
---|
484 | setMapInMaps(*main_conf,"lenv","message",_("No message provided")); |
---|
485 | }else |
---|
486 | setMapInMaps(*main_conf,"lenv","message",_("Unable to fetch the remote error log file")); |
---|
487 | tmpPath=getMapFromMaps(*main_conf,"lenv","message"); |
---|
488 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
489 | fflush(stderr); |
---|
490 | invokeCallback(m,NULL,NULL,7,1); |
---|
491 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
492 | fflush(stderr); |
---|
493 | sprintf(tmpS, "Cannot execute the HPC ZOO-Service %s: %s", s->name, tmpPath->value); |
---|
494 | errorException(m,tmpS,"NoApplicableCode",NULL); |
---|
495 | free(command); |
---|
496 | free(targetPath); |
---|
497 | ssh_close(*main_conf); |
---|
498 | sleep(120); |
---|
499 | return -1; |
---|
500 | } |
---|
501 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
502 | fflush(stderr); |
---|
503 | invokeCallback(m,NULL,NULL,4,1); |
---|
504 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
505 | fflush(stderr); |
---|
506 | free(command); |
---|
507 | |
---|
508 | struct sockaddr_un addr; |
---|
509 | memset(&addr, 0, sizeof(addr)); |
---|
510 | addr.sun_family = AF_UNIX; |
---|
511 | int rc, cl, fd = socket(AF_UNIX, SOCK_STREAM, 0); |
---|
512 | char *sname=(char*)malloc((strlen(tmpPath->value)+strlen(uuid->value)+20)); |
---|
513 | sprintf(sname,"%s/.wait_socket_%s.sock",tmpPath->value,uuid->value); |
---|
514 | strncpy(addr.sun_path, sname, sizeof(addr.sun_path)-1); |
---|
515 | |
---|
516 | if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) { |
---|
517 | perror("bind error"); |
---|
518 | sleep(120); |
---|
519 | return -1; |
---|
520 | } |
---|
521 | if (listen(fd, 5) == -1) { |
---|
522 | setMapInMaps(*main_conf,"lenv","message",_("Listen error")); |
---|
523 | return -1; |
---|
524 | } |
---|
525 | if ( (cl = accept(fd, NULL, NULL)) == -1) { |
---|
526 | setMapInMaps(*main_conf,"lenv","message",_("Accept error")); |
---|
527 | return -1; |
---|
528 | }else{ |
---|
529 | int hasPassed=-1; |
---|
530 | char buf[11]; |
---|
531 | memset(&buf,0,11); |
---|
532 | while ( (rc=read(cl,buf,10)) ) { |
---|
533 | if(rc==0){ |
---|
534 | sleep(1); |
---|
535 | setMapInMaps(*main_conf,"lenv","message",_("Read closed")); |
---|
536 | invokeCallback(m,NULL,NULL,7,1); |
---|
537 | return -1; |
---|
538 | }else{ |
---|
539 | if(rc<0){ |
---|
540 | setMapInMaps(*main_conf,"lenv","message",_("Read error")); |
---|
541 | invokeCallback(m,NULL,NULL,7,1); |
---|
542 | return -1; |
---|
543 | } |
---|
544 | } |
---|
545 | hasPassed=1; |
---|
546 | res=atoi(buf); |
---|
547 | unlink(sname); |
---|
548 | //free(sname); |
---|
549 | |
---|
550 | if(res==3){ |
---|
551 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
552 | fflush(stderr); |
---|
553 | invokeCallback(m,NULL,outputs,5,0); |
---|
554 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
555 | fflush(stderr); |
---|
556 | input=*real_outputs; |
---|
557 | while(input!=NULL){ |
---|
558 | if(input->child==NULL){ |
---|
559 | map* generatedFile=getMap(input->content,"generated_file"); |
---|
560 | if(generatedFile!=NULL){ |
---|
561 | char* filename=strrchr(generatedFile->value,'/'); |
---|
562 | char* targetPath=(char*)malloc((strlen(tmpPath->value)+strlen(filename)+2)*sizeof(char)); |
---|
563 | sprintf(targetPath,"%s/%s",tmpPath->value,filename); |
---|
564 | test=ssh_connect(*main_conf); |
---|
565 | if(ssh_fetch(*main_conf,targetPath,generatedFile->value,ssh_get_cnt(m))==0){ |
---|
566 | setMapInMaps(*real_outputs,input->name,"generated_file",targetPath); |
---|
567 | free(targetPath); |
---|
568 | }else{ |
---|
569 | char *tmpStr=(char*)malloc((strlen(filename)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char)); |
---|
570 | sprintf(tmpStr,_("Unable to fetch the remote file for %s"),filename); |
---|
571 | setMapInMaps(*main_conf,"lenv","message",tmpStr); |
---|
572 | free(tmpStr); |
---|
573 | return SERVICE_FAILED; |
---|
574 | } |
---|
575 | } |
---|
576 | }else{ |
---|
577 | fprintf(stderr,"%s %d\n",__FILE__,__LINE__); |
---|
578 | fflush(stderr); |
---|
579 | map* generatedFile=getMap(input->content,"generated_file"); |
---|
580 | if(generatedFile!=NULL){ |
---|
581 | char* filename=strrchr(generatedFile->value,'/'); |
---|
582 | char* targetPath=(char*)malloc((strlen(tmpPath->value)+strlen(filename)+2)*sizeof(char)); |
---|
583 | sprintf(targetPath,"%s/%s",tmpPath->value,filename); |
---|
584 | test=ssh_connect(*main_conf); |
---|
585 | if(ssh_fetch(*main_conf,targetPath,generatedFile->value,ssh_get_cnt(m))==0){ |
---|
586 | maps* tmp=getMaps(*real_outputs,input->name); |
---|
587 | freeMap(&tmp->content); |
---|
588 | free(tmp->content); |
---|
589 | tmp->content=NULL; |
---|
590 | maps* output=getMaps(*real_outputs,input->name); |
---|
591 | setMapInMaps(output->child,"download_link","generated_file",targetPath); |
---|
592 | setMapInMaps(output->child,"download_link","useMapserver","false"); |
---|
593 | setMapInMaps(output->child,"WMS_LINK","generated_file",targetPath); |
---|
594 | setMapInMaps(output->child,"WMS_LINK","useMapserver","true"); |
---|
595 | setMapInMaps(output->child,"WCS_LINK","generated_file",targetPath); |
---|
596 | setMapInMaps(output->child,"WCS_LINK","useMapserver","true"); |
---|
597 | } |
---|
598 | } |
---|
599 | } |
---|
600 | input=input->next; |
---|
601 | } |
---|
602 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
603 | fflush(stderr); |
---|
604 | invokeCallback(m,NULL,outputs,5,1); |
---|
605 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
606 | fflush(stderr); |
---|
607 | } |
---|
608 | //free(buf); |
---|
609 | } |
---|
610 | if(hasPassed<0){ |
---|
611 | perror("Failed to read"); |
---|
612 | setMapInMaps(*main_conf,"lenv","message",_("Unable to parse the value returned by remote execution")); |
---|
613 | sleep(120); |
---|
614 | return SERVICE_FAILED; |
---|
615 | } |
---|
616 | } |
---|
617 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
618 | fflush(stderr); |
---|
619 | ssh_close(*main_conf); |
---|
620 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
621 | fflush(stderr); |
---|
622 | return res; |
---|
623 | } |
---|
624 | |
---|
625 | |
---|