source: branches/prototype-v0/zoo-project/zoo-kernel/service_internal_hpc.c @ 886

Last change on this file since 886 was 886, checked in by djay, 6 years ago

Check for md5sum of any file in the cache to avoid sending the same file on HPC server twice. Add the multiple LiteralData? inputs support for HPC services. Remove condition for defining SCALE for every band in the outputed MapServer? mapfile.

  • Property svn:keywords set to Id
File size: 41.2 KB
Line 
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
37typedef 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 */ 
48void addNestedOutputs(service** s){
49  if((*s)==NULL){
50    return;
51  }   
52  if(*s==NULL || (*s)->outputs==NULL || (*s)->content==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    map* useMS=getMap(cur->defaults->content,"useMapserver");
63    if(mimeType!=NULL && useMS!=NULL && strncasecmp(useMS->value,"true",4)==0){
64      int geo=isGeographic(mimeType->value);
65      if(geo>0){
66        elements *tmp[3]={
67          dupElements(cur),
68          dupElements(cur),
69          dupElements(cur)
70        };
71        char *geoLink="wcs_link";
72        if(geo==2){
73          geoLink="wfs_link";
74        }
75        int i=0;
76        for(;i<3;i++){
77          if(tmp[i]->next!=NULL){
78            freeElements(&tmp[i]->next);
79            free(tmp[i]->next);
80            tmp[i]->next=NULL;
81          }
82          free(tmp[i]->name);
83          if(tmp[i]->format!=NULL)
84            free(tmp[i]->format);
85          tmp[i]->format=zStrdup("ComplexData");
86          freeMap(&tmp[i]->content);
87          free(tmp[i]->content);
88          tmp[i]->content=NULL;
89          switch(i){
90          case 0:
91            tmp[i]->name=zStrdup("download_link");
92            tmp[i]->content=createMap("Title",_("Download link"));
93            addToMap(tmp[i]->content,"Abstract",_("The download link"));
94            addToMap(tmp[i]->defaults->content,"useMapserver","false");
95            if(tmp[i]->supported!=NULL){
96              freeIOType(&tmp[i]->supported);
97              free(tmp[i]->supported);
98              tmp[i]->supported=NULL;
99            }
100            break;
101          case 1:
102            tmp[i]->name=zStrdup("wms_link");
103            tmp[i]->content=createMap("Title",_("WMS link"));
104            addToMap(tmp[i]->content,"Abstract",_("The WMS link"));
105            if(tmp[i]->supported!=NULL && tmp[i]->supported->next!=NULL){
106              freeIOType(&tmp[i]->supported->next);
107              free(tmp[i]->supported->next);
108              tmp[i]->supported->next=NULL;
109            }else{
110              if(tmp[i]->supported!=NULL)
111                addToMap(tmp[i]->supported->content,"useMapserver","true");
112              addToMap(tmp[i]->defaults->content,"useMapserver","true");
113            }
114            break;
115          case 2:
116            if(geo==2){
117              tmp[i]->name=zStrdup("wfs_link");
118              tmp[i]->content=createMap("Title",_("WFS link"));
119              addToMap(tmp[i]->content,"Abstract",_("The WFS link"));
120            }else{
121              tmp[i]->name=zStrdup("wcs_link");
122              tmp[i]->content=createMap("Title",_("WCS link"));
123              addToMap(tmp[i]->content,"Abstract",_("The WCS link"));
124            }
125            if(tmp[i]->supported!=NULL && tmp[i]->supported->next!=NULL &&
126               tmp[i]->supported->next->content!=NULL){
127              freeIOType(&tmp[i]->supported);
128              free(tmp[i]->supported);
129              tmp[i]->supported=NULL;
130              tmp[i]->supported=createIoType();
131              iotype* cnext=cur->supported->next;
132              tmp[i]->supported->content=createMap(cnext->content->name,cnext->content->value);
133              addMapToMap(&tmp[i]->supported->content,cnext->content->next);
134              addToMap(tmp[i]->supported->content,"useMapserver","true");
135            }else
136              addToMap(tmp[i]->defaults->content,"useMapserver","true");
137            break;
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          free(cur->defaults);
148          cur->defaults=NULL;
149        }
150        if(cur->supported!=NULL){
151          freeIOType(&cur->supported);
152          free(cur->supported);
153          cur->supported=NULL;
154        }
155        freeElements(&tmp[2]);
156        free(tmp[2]);
157        freeElements(&tmp[1]);
158        free(tmp[1]);
159        freeElements(&tmp[0]);
160        free(tmp[0]);
161        //addToMap(cur->content,"internal","true");
162      }     
163    }
164    cur=cur->next;
165  }
166  //dumpElements((*s)->outputs);
167}
168
169/**
170 * Acquire a read lock on every files used as input for executing a service.
171 * @param conf the main configuration file map
172 * @return 0 if every file can be locked, -1 if one lock has failed.
173 */
174int addReadLocks(maps** conf){
175  map* queueLengthMap=getMapFromMaps(*conf,"uploadQueue","length");
176  maps* queueMaps=getMaps(*conf,"uploadQueue");
177  if(queueLengthMap!=NULL){
178    int cnt=atoi(queueLengthMap->value);
179    int i=0;
180    for(i=0;i<cnt;i++){
181      map* argv[2]={
182        getMapArray(queueMaps->content,"input",i),
183        getMapArray(queueMaps->content,"localPath",i)
184      };
185      zooLock* lck;
186      if((lck=lockFile(*conf,argv[1]->value,'r'))==NULL){
187        char* templateStr=_("Unable to lock the file for %s in read mode.");
188        char *tmpMessage=(char*)malloc((strlen(templateStr)+strlen(argv[0]->value)+1)*sizeof(char));
189        sprintf(tmpMessage,templateStr,argv[0]->value);
190        setMapInMaps(*conf,"lenv","message",tmpMessage);
191        free(tmpMessage);
192        return -1;
193      }else{
194        if(zoo_file_locks_cnt==0){
195          zoo_file_locks=(zooLock**)malloc(sizeof(zooLock*));
196        }
197        else{
198          zoo_file_locks=(zooLock**)realloc(zoo_file_locks,(zoo_file_locks_cnt+1)*sizeof(zooLock*));
199        }
200        zoo_file_locks[zoo_file_locks_cnt]=lck;
201        zoo_file_locks_cnt++;
202      }
203    }
204  }
205  return 0;
206}
207
208/**
209 * Remove all read locks set for files used as input for executing the service.
210 * @param conf the main configuration maps pointer
211 * @return 0 in case of success, -1 if any error occured. In case of error, one
212 * can refer to the message map array from the lenv section.
213 */
214int removeReadLocks(maps** conf){
215  int res=0;
216  int nberr=0;
217  map* queueLengthMap=getMapFromMaps(*conf,"uploadQueue","length");
218  maps* queueMaps=getMaps(*conf,"uploadQueue");
219  if(queueLengthMap!=NULL){
220    int cnt=atoi(queueLengthMap->value);
221    int i=0;
222    for(i=0;i<cnt;i++){
223      if(unlockFile(*conf,zoo_file_locks[i])<1){
224        map* argv=getMapArray(queueMaps->content,"input",i);
225        char* templateStr=_("Unable to unlock the file for %s after execution.");
226        char *tmpMessage=(char*)malloc((strlen(templateStr)+strlen(argv->value)+1)*sizeof(char));
227        sprintf(tmpMessage,templateStr,argv->value);
228        maps* lenv=getMaps(*conf,"lenv");
229        setMapArray(lenv->content,"message",nberr,tmpMessage);
230        free(tmpMessage);
231        res=-1;
232        nberr++;
233      }
234    }
235  }
236  free(zoo_file_locks);
237  return res;
238}
239
240/**
241 * Get the section name depending on number of features and/or pixels of each
242 * inputs and the threshold defined in a section.
243 * It supposes that your inputs has been published using MapServer support,
244 * implying that the number of features (nb_features), respectively pixels
245 * (nb_pixels), are defined. The section, identified by confId, should contain
246 * preview_max_features and preview_max_pixels defining the threshold values.
247 * @param conf the main configuration file maps pointer
248 * @param inputs the inputs maps pointer
249 * @param confId the section identifier
250 * @return "preview_conf" in case the numbers are lower than the threshold,
251 * "fullres_conf" in other cases.
252 */
253char* getConfiguration(maps** conf,maps** inputs,const char* confId){
254  maps* input=*inputs;
255  map* max_pixels=getMapFromMaps(*conf,confId,"preview_max_pixels");
256  map* max_features=getMapFromMaps(*conf,confId,"preview_max_features");
257  int i_max_pixels=atoi(max_pixels->value);
258  int i_max_features=atoi(max_features->value);
259  while(input!=NULL && input->content!=NULL){
260    map* tmpMap=getMap(input->content,"geodatatype");
261    if(tmpMap!=NULL){
262      map* currentNb;
263      if(strcasecmp(tmpMap->value,"raster")==0 ){
264        currentNb=getMap(input->content,"nb_pixels");
265        if(atoi(currentNb->value)>i_max_pixels)
266          return "fullres_conf";
267      }else{
268        if(strcasecmp(tmpMap->value,"vector")==0 ){
269          currentNb=getMap(input->content,"nb_features");
270          if(atoi(currentNb->value)>i_max_features)
271            return "fullres_conf";
272        }
273      }
274    }
275    input=input->next;
276  }
277  return "preview_conf";
278}
279
280/**
281 * Load and run a HPC Application corresponding to the service.
282 *
283 * @param main_conf the conf maps containing the main.cfg settings
284 * @param request the map containing the HTTP request
285 * @param s the service structure
286 * @param real_inputs the maps containing the inputs
287 * @param real_outputs the maps containing the outputs
288 * @return SERVICE_SUCCEEDED in case of success, -1 or SERVICE_FAILED when failing.
289 */
290int zoo_hpc_support(maps** main_conf,map* request,service* s,maps **real_inputs,maps **real_outputs){
291  maps* m=*main_conf;
292  maps* inputs=*real_inputs;
293  maps* outputs=*real_outputs;
294  map* tmp0=getMapFromMaps(*main_conf,"lenv","cwd");
295  char *ntmp=tmp0->value;
296  map* tmp=NULL;
297  int res=-1;
298  // Get the configuration id depending on service type and defined thresholds
299  // then, set the configId key in the lenv section
300  char *serviceType;
301  map* mServiceType=getMap(s->content,"confId");
302  if(mServiceType!=NULL)
303    serviceType=mServiceType->value;
304  else
305    serviceType="HPC";
306  map* tmpPath=getMapFromMaps(*main_conf,"main","tmpPath");
307  map* uuid=getMapFromMaps(*main_conf,"lenv","usid");
308  map* confMap=getMapFromMaps(*main_conf,serviceType,getConfiguration(main_conf,real_inputs,serviceType));
309  char * configurationId=confMap->value;
310  setMapInMaps(*main_conf,"lenv","configId",configurationId);
311  // Dump lenv maps again after having set the configId ...
312  char *flenv =
313    (char *)
314    malloc ((strlen (tmpPath->value) + 
315             strlen (uuid->value) + 12) * sizeof (char));
316  sprintf (flenv, "%s/%s_lenv.cfg", tmpPath->value, uuid->value);
317  maps* lenvMaps=getMaps(m,"lenv");
318  dumpMapsToFile(lenvMaps,flenv,0);
319  free(flenv);
320
321  map* targetPathMap=getMapFromMaps(*main_conf,configurationId,"remote_data_path");
322 
323  pthread_t threads_pool[50];
324  // Force the HPC services to be called asynchronously
325  map* isAsync=getMapFromMaps(*main_conf,"lenv","async");
326  if(isAsync==NULL){
327    errorException(*main_conf,_("The synchronous mode is not supported by this type of service"),"NoSuchMode",s->name);
328    return -1;
329  }
330
331  maps* input=*real_inputs;
332  char **parameters=NULL;
333  int parameters_cnt=0;
334  while(input!=NULL && input->content!=NULL){
335    map* isInRequest=getMap(input->content,"inRequest");
336    map* minNb=getMap(input->content,"minOccurs");
337    if(getMaps(*real_outputs,input->name)==NULL &&
338       ( (isInRequest!=NULL && strncasecmp(isInRequest->value,"true",4)==0)
339         || (minNb!=NULL && atoi(minNb->value)>0) ) ){
340      parameters_cnt+=1;
341      if(parameters_cnt==1)
342        parameters=(char**)malloc(parameters_cnt*sizeof(char*));
343      else
344        parameters=(char**)realloc(parameters,parameters_cnt*sizeof(char*));
345      if(getMap(input->content,"mimeType")!=NULL){
346        // Input is ComplexData
347        if(getMap(input->content,"cache_file")==NULL){
348          // Input data has been passed by value
349          // TODO: publish input through MapServer / use output publication
350          dumpMapsValuesToFiles(main_conf,&input);
351          addToMap(input->content,"toPublish","true");
352          //addToMap(input->content,"useMapserver","true");
353        }
354        if(getMap(input->content,"cache_file")!=NULL){
355          map* length=getMap(input->content,"length");
356          if(length==NULL){
357            addToMap(input->content,"length","1");
358            length=getMap(input->content,"length");
359          }
360          int len=atoi(length->value);
361          int i=0;
362          for(i=0;i<len;i++){
363            map* tmp=getMapArray(input->content,"cache_file",i);
364            char* targetName=strrchr(tmp->value,'/');
365            char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
366            sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
367            setMapArray(input->content,"targetPath",i,targetPath);
368            setMapArray(input->content,"localPath",i,tmp->value);
369            map* tmp1=getMapArray(input->content,"value",i);
370            if(tmp1!=NULL){
371              free(tmp1->value);
372              tmp1->value=strdup("empty");
373            }
374            if(i==0){
375              parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+3)*sizeof(char));
376              sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,targetPath);
377            }else{
378              fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
379              fflush(stderr);
380              char *tmpStr=zStrdup(parameters[parameters_cnt-1]);
381              parameters[parameters_cnt-1]=(char*)realloc(parameters[parameters_cnt-1],(strlen(tmpStr)+strlen(targetPath)+2)*sizeof(char));
382              sprintf(parameters[parameters_cnt-1],"%s %s",tmpStr,targetPath);
383              free(tmpStr);
384              fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
385              fflush(stderr);
386            }
387            free(targetPath);
388          }
389          addToUploadQueue(main_conf,input);
390        }else{
391          // ???
392          fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
393          fflush(stderr);
394        }
395      }else{
396        // LitteralData and BboxData
397        if(getMap(input->content,"dataType")!=NULL){
398          // For LitteralData, simply pass the value
399          map* length=getMap(input->content,"length");
400          if(length!=NULL){
401            char* value=NULL;
402            int len=atoi(length->value);
403            int i=0;
404            for(i=0;i<len;i++){
405              map* val=getMapArray(input->content,"value",i);
406              if(val!=NULL){
407                if(value==NULL){
408                  value=(char*)malloc((strlen(val->value)+3)*sizeof(char));
409                  sprintf(value,"\"%s\"",val->value);
410                }
411                else{
412                  value=(char*)realloc(value,(strlen(value)+strlen(val->value)+4)*sizeof(char));
413                  sprintf(value,"%s \"%s\"",value,val->value);
414                }
415              }
416            }
417            if(value!=NULL){
418              parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(value)+3)*sizeof(char));
419              sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,value);
420            }
421          }else{
422            map* val=getMap(input->content,"value");
423            parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(val->value)+5)*sizeof(char));
424            sprintf(parameters[parameters_cnt-1],"-%s \"%s\"",input->name,val->value);
425          }
426        }
427      }
428    }
429    input=input->next;
430  }
431
432#ifdef HPC_DEBUG
433  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
434#endif
435  invokeCallback(m,inputs,NULL,1,1);
436  invokeCallback(m,inputs,NULL,2,0);
437  if(getMapFromMaps(m,"lenv","mapError")!=NULL){
438    invokeCallback(*main_conf,inputs,NULL,7,0);
439    return -1;
440  }
441#ifdef HPC_DEBUG
442  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
443  dumpMaps(inputs);
444#endif
445
446  // Upload data on HPC
447  if(runUpload(main_conf)==false){
448    errorException (*main_conf, _("Unable to lock the file for upload!"),
449                    "InternalError", NULL);
450#ifdef HPC_DEBUG
451    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
452#endif
453    invokeCallback(*main_conf,inputs,NULL,7,0);
454#ifdef HPC_DEBUG
455    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
456#endif
457    return -1;
458  }
459#ifdef HPC_DEBUG
460  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
461#endif
462  invokeCallback(m,inputs,NULL,2,1);
463#ifdef HPC_DEBUG
464  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
465#endif
466
467  // Add the filename to generate for every output to parameters
468  input=*real_outputs;
469#ifdef HPC_DEBUG
470  dumpMaps(input);
471#endif
472  while(input!=NULL){
473    // Parse all outputs including inner outputs if required.
474    if(input->child==NULL){
475      // Name every files that should be produced by the service execution
476      map* mime=getMap(input->content,"mimeType");
477      char* targetName;
478      if(mime!=NULL){
479        bool hasExt=false;
480        map* fileExt=getFileExtensionMap(mime->value,&hasExt);
481        targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+strlen(fileExt->value)+11)*sizeof(char));
482        sprintf(targetName,"output_%s_%s_%s.%s",s->name,input->name,uuid->value,fileExt->value);
483        freeMap(&fileExt);
484        free(fileExt);
485      }else{
486        targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+14)*sizeof(char));
487        sprintf(targetName,"output_%s_%s_%s.tif",s->name,input->name,uuid->value);
488      }
489      char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
490      sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
491      map *tmpUrl=getMapFromMaps(*main_conf,"main","tmpUrl");
492      char *targetUrl=(char*)malloc((strlen(tmpUrl->value)+strlen(targetName)+2)*sizeof(char));
493      sprintf(targetUrl,"%s/%s",tmpUrl->value,targetName);
494      free(targetName);
495      setMapInMaps(*real_outputs,input->name,"generated_file",targetPath);
496      addToMap(input->content,"generated_url",targetUrl);
497      free(targetUrl);
498      {
499        parameters_cnt+=1;
500        if(parameters_cnt==1)
501          parameters=(char**)malloc(parameters_cnt*sizeof(char*));
502        else
503          parameters=(char**)realloc(parameters,parameters_cnt*sizeof(char*));
504        // We should verify if any optional tag for output is required
505        // (i.e. -out output.tiff *int8*), meaning that we should search
506        // for a corresponding inputs name.
507        map* inValue=getMapFromMaps(*real_inputs,input->name,"value");
508        if(inValue!=NULL){
509          parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+strlen(inValue->value)+4)*sizeof(char));
510          sprintf(parameters[parameters_cnt-1],"-%s %s %s",input->name,targetPath,inValue->value);
511        }else{
512          parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+3)*sizeof(char));
513          sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,targetPath);
514        }
515      }
516      free(targetPath);
517    }// In other case it means we need to return the cache_file as generated_file
518    else{
519      // Name every files that should be produced by the service execution
520      map* mime=getMap(input->child->content,"mimeType");
521      char* targetName;
522      if(mime!=NULL){
523        bool hasExt=false;
524        map* fileExt=getFileExtensionMap(mime->value,&hasExt);
525        targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+strlen(fileExt->value)+11)*sizeof(char));
526        sprintf(targetName,"output_%s_%s_%s.%s",s->name,input->name,uuid->value,fileExt->value);
527        freeMap(&fileExt);
528        free(fileExt);
529      }else{
530        targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+14)*sizeof(char));
531        sprintf(targetName,"output_%s_%s_%s.tif",s->name,input->name,uuid->value);
532      }
533      char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
534      sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
535      map *tmpUrl=getMapFromMaps(*main_conf,"main","tmpUrl");
536      char *targetUrl=(char*)malloc((strlen(tmpUrl->value)+strlen(targetName)+2)*sizeof(char));
537      sprintf(targetUrl,"%s/%s",tmpUrl->value,targetName);
538      free(targetName);
539      addToMap(input->content,"generated_file",targetPath);
540      addToMap(input->content,"storage",targetPath);
541      addToMap(input->content,"generated_url",targetUrl);
542      free(targetUrl);
543      if(strcasecmp(input->name,"wms_link")!=0&&
544         strcasecmp(input->name,"wcs_link")!=0 &&
545         strcasecmp(input->name,"wfs_link")!=0){
546        parameters_cnt+=1;
547        if(parameters_cnt==1)
548          parameters=(char**)malloc(parameters_cnt*sizeof(char*));
549        else
550          parameters=(char**)realloc(parameters,parameters_cnt*sizeof(char*));
551        // We should verify if any optional tag for output is required
552        // (i.e. -out output.tiff *int8*), meaning that we should search
553        // for a corresponding inputs name.
554        map* inValue=getMapFromMaps(*real_inputs,input->name,"value");
555        if(inValue!=NULL){
556          parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+strlen(inValue->value)+4)*sizeof(char));
557          sprintf(parameters[parameters_cnt-1],"-%s %s %s",input->name,targetPath,inValue->value);
558        }else{
559          parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+3)*sizeof(char));
560          sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,targetPath);
561        }
562      }
563      free(targetPath);
564    }
565    input=input->next;
566  }
567  // Produce the SBATCH File locally
568  char *scriptPath=(char*)malloc((strlen(s->name)+strlen(tmpPath->value)+strlen(uuid->value)+10)*sizeof(char));
569  sprintf(scriptPath,"%s/zoo_%s_%s.sh",tmpPath->value,s->name,uuid->value);
570  setMapInMaps(*main_conf,"lenv","local_script",scriptPath);
571#ifdef HPC_DEBUG
572  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
573  fflush(stderr);
574#endif
575  invokeCallback(m,inputs,NULL,3,0);
576#ifdef HPC_DEBUG
577  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
578  fflush(stderr);
579#endif
580  FILE* scriptFile=fopen(scriptPath,"w+");
581  map* headerMap=getMapFromMaps(*main_conf,configurationId,"jobscript_header");
582  if(headerMap!=NULL){
583    // Use the header file if defined in the HPC section of the main.cfg file
584    struct stat f_status;
585    int s=stat(headerMap->value, &f_status);
586    if(s==0){
587      char* fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
588      FILE* f=fopen(headerMap->value,"rb");
589      fread(fcontent,f_status.st_size,1,f);
590      int fsize=f_status.st_size;
591      fcontent[fsize]=0;
592      fclose(f);
593      fprintf(scriptFile,"%s\n### --- ZOO-Service HEADER end --- ###\n\n",fcontent);
594      free(fcontent);
595    }else
596      fprintf(scriptFile,"#!/bin/bash\n\n### *** Default ZOO-Service HEADER (no header found) *** ###\n\n");
597  }else
598    fprintf(scriptFile,"#!/bin/bash\n\n### *** Default ZOO-Service HEADER *** ###\n\n");
599  maps* hpc_opts=getMaps(*main_conf,configurationId);
600  if(hpc_opts!=NULL){
601    map* hpc_opts_content=hpc_opts->content;
602    while(hpc_opts_content!=NULL){
603      if(strncasecmp(hpc_opts_content->name,"sbatch_options_",15)==0)
604        fprintf(scriptFile,"#SBATCH --%s=%s\n",strstr(hpc_opts_content->name,"sbatch_options_")+15,hpc_opts_content->value);
605      hpc_opts_content=hpc_opts_content->next;
606    }
607  }
608  fprintf(scriptFile,"#SBATCH --job-name=ZOO-Project_%s_%s\n\n",uuid->value,s->name);
609  map* mods=getMap(s->content,"hpcModules");
610  if(mods!=NULL)
611    fprintf(scriptFile,"#SBATCH --export=MODULES=%s\n",mods->value);
612
613  map* bodyMap=getMapFromMaps(*main_conf,configurationId,"jobscript_body");
614  if(bodyMap!=NULL){
615    // Use the header file if defined in the HPC section of the main.cfg file
616    struct stat f_status;
617    int s=stat(bodyMap->value, &f_status);
618    if(s==0){
619      char* fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
620      FILE* f=fopen(bodyMap->value,"rb");
621      fread(fcontent,f_status.st_size,1,f);
622      int fsize=f_status.st_size;
623      fcontent[fsize]=0;
624      fclose(f);
625      fprintf(scriptFile,"%s\n### --- ZOO-Service BODY end --- ###\n\n",fcontent);
626      free(fcontent);
627    }else
628      fprintf(scriptFile,"\n### *** Default ZOO-Service BODY (no body found) *** ###\n\n");
629  }else
630    fprintf(scriptFile,"\n### *** Default ZOO-Service BODY *** ###\n\n");
631
632  map* sp=getMap(s->content,"serviceProvider");
633 
634  // Require to produce the command line to be executed
635  fprintf(scriptFile,"\n\necho \"Job started at: $(date)\"\n");
636  fprintf(scriptFile,"echo \"Running service: [%s]\"\n",sp->value);
637  fprintf(scriptFile,"%s ",sp->value);
638  for(int i=0;i<parameters_cnt;i++){
639    fprintf(scriptFile," %s",parameters[i]);
640  }
641  for(int i=parameters_cnt-1;i>=0;i--){
642    free(parameters[i]);
643  }
644  free(parameters);
645  fprintf(scriptFile,"\n");
646  fprintf(scriptFile,"echo \"Job finished at: $(date)\"\n");
647  map* footerMap=getMapFromMaps(*main_conf,configurationId,"jobscript_footer");
648  if(footerMap!=NULL){
649    // Use the footer file if defined in the HPC section of the main.cfg file
650    struct stat f_status;
651    int s=stat(footerMap->value, &f_status);
652    if(s==0){
653      char* fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
654      FILE* f=fopen(footerMap->value,"rb");
655      fread(fcontent,f_status.st_size,1,f);
656      int fsize=f_status.st_size;
657      fcontent[fsize]=0;
658      fclose(f);
659      char* ffcontent=(char*)malloc((strlen(fcontent)+(3*strlen(uuid->value))+1)*sizeof(char));
660      sprintf(ffcontent,fcontent,uuid->value,uuid->value,uuid->value);
661      fprintf(scriptFile,"%s\n### --- ZOO-Service FOOTER end --- ###\n\n",ffcontent);
662      free(fcontent);
663    }else
664      fprintf(scriptFile,"### *** Default ZOO-Service FOOTER (footer file failed to load) *** ###\n\n");
665  }else
666      fprintf(scriptFile,"### *** Default ZOO-Service FOOTER (no footer found) *** ###\n\n");
667  fflush(scriptFile);
668  fclose(scriptFile);
669#ifdef HPC_DEBUG
670  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
671#endif
672  invokeCallback(m,inputs,NULL,3,1);
673#ifdef HPC_DEBUG
674  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
675#endif
676
677  // Upload the SBATCH File to the remote host
678#ifdef HPC_DEBUG
679  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
680#endif
681  invokeCallback(m,inputs,NULL,4,0);
682#ifdef HPC_DEBUG
683  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
684#endif
685  targetPathMap=getMapFromMaps(*main_conf,configurationId,"remote_work_path");
686  if(targetPathMap==NULL){
687    setMapInMaps(*main_conf,"lenv","message",_("There is no remote_work_path defined in your section!"));
688    setMapInMaps(*main_conf,"lenv","status","failed");
689    errorException (*main_conf, _("There is no remote_work_path defined in your section!"),
690                    "InternalError", NULL);
691#ifdef HPC_DEBUG
692    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
693    fflush(stderr);
694#endif
695    invokeCallback(*main_conf,NULL,NULL,7,0);
696#ifdef HPC_DEBUG
697    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
698    fflush(stderr);
699#endif
700    return SERVICE_FAILED;
701  }
702  char* targetName=strrchr(scriptPath,'/');
703  char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
704  sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
705  setMapInMaps(*main_conf,"lenv","remote_script",targetPath);
706  SSHCON *test=ssh_connect(*main_conf);
707  int copy0=ssh_copy(*main_conf,scriptPath,targetPath,ssh_get_cnt(*main_conf));
708  unlink(scriptPath);
709  free(scriptPath);
710  if(copy0!=true){
711    setMapInMaps(*main_conf,"lenv","message",_("Unable to upload the script"));
712    invokeCallback(*main_conf,NULL,NULL,7,0);
713    errorException(*main_conf,_("Unable to upload the script"),"NoApplicableCode",NULL);
714    return -1;
715  }
716  // Execute the SBATCH script remotely
717  addReadLocks(main_conf);
718  map* subStr=getMapFromMaps(*main_conf,configurationId,"sbatch_substr");
719  char *command=(char*)malloc((strlen(targetPath)+strlen(targetPathMap->value)+strlen(subStr->value)+strlen(uuid->value)+137)*sizeof(char));
720  sprintf(command,"sbatch %s 2> %s/error_%s.log | sed \"s:%s::g\"",targetPath,targetPathMap->value,uuid->value,subStr->value);
721  if(ssh_exec(*main_conf,command,ssh_get_cnt(m))<=0){
722    // The sbatch command has failed!
723    // Download the error log file from the HPC server
724    char tmpS[1024];
725    free(command);
726    command=(char*)malloc((strlen(targetPathMap->value)+strlen(uuid->value)+11)*sizeof(char));
727    sprintf(command,"%s/error_%s.log",targetPathMap->value,uuid->value);
728    targetName=strrchr(command,'/');
729    free(targetPath);
730    targetPath=(char*)malloc((strlen(tmpPath->value)+strlen(targetName)+2)*sizeof(char));
731    sprintf(targetPath,"%s/%s",tmpPath->value,targetName);
732    if(ssh_fetch(*main_conf,targetPath,command,ssh_get_cnt(m))==0){
733      struct stat f_status;
734      int ts=stat(targetPath, &f_status);
735      if(ts==0) {
736        char* fcontent = NULL;
737        fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
738        FILE* f=fopen(targetPath,"rb");
739        fread(fcontent,f_status.st_size,1,f);
740        int fsize=f_status.st_size;
741        fcontent[fsize]=0;
742        fclose(f);
743        setMapInMaps(*main_conf,"lenv","message",fcontent);
744        free(fcontent);
745      }else
746        setMapInMaps(*main_conf,"lenv","message",_("No message provided"));
747    }else
748      setMapInMaps(*main_conf,"lenv","message",_("Unable to fetch the remote error log file"));
749    tmpPath=getMapFromMaps(m,"lenv","message");
750#ifdef HPC_DEBUG
751    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
752    fflush(stderr);
753#endif
754    invokeCallback(*main_conf,NULL,NULL,7,0);
755#ifdef HPC_DEBUG
756    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
757    fflush(stderr);
758#endif
759    sprintf(tmpS, "Cannot execute the HPC ZOO-Service %s using %s: %s", s->name, configurationId, tmpPath->value);
760    errorException(*main_conf,tmpS,"NoApplicableCode",NULL);
761    free(command);
762    free(targetPath);
763    ssh_close(*main_conf);
764    removeReadLocks(main_conf);
765    return -1;
766  }
767  free(targetPath);
768#ifdef HPC_DEBUG
769  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
770  fflush(stderr);
771#endif
772  invokeCallback(m,NULL,NULL,4,1);
773#ifdef HPC_DEBUG
774  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
775  fflush(stderr);
776#endif
777  free(command);
778#ifdef HPC_DEBUG
779  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
780  fflush(stderr);
781#endif
782
783  struct sockaddr_un addr;
784  memset(&addr, 0, sizeof(addr));
785  addr.sun_family = AF_UNIX;
786  int rc, cl, fd = socket(AF_UNIX, SOCK_STREAM, 0);
787  char *sname=(char*)malloc((strlen(tmpPath->value)+strlen(uuid->value)+20));
788  sprintf(sname,"%s/.wait_socket_%s.sock",tmpPath->value,uuid->value);
789  strncpy(addr.sun_path, sname, sizeof(addr.sun_path)-1);
790 
791  if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
792    perror("bind error");
793    setMapInMaps(*main_conf,"lenv","message",_("Unable to bind socket!"));
794    errorException (*main_conf, _("Unable to bind socket!"),
795                    "InternalError", NULL);
796#ifdef HPC_DEBUG
797    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
798    fflush(stderr);
799#endif
800    invokeCallback(*main_conf,NULL,NULL,7,0);
801    removeReadLocks(main_conf);
802#ifdef HPC_DEBUG
803    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
804    fflush(stderr);
805#endif
806    return -1;
807  }
808#ifdef HPC_DEBUG
809  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
810  fflush(stderr);
811#endif
812  if (listen(fd, 5) == -1) {
813    setMapInMaps(*main_conf,"lenv","message",_("Listen error"));
814    errorException (*main_conf, _("Listen error"),
815                    "InternalError", NULL);
816#ifdef HPC_DEBUG
817    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
818    fflush(stderr);
819#endif
820    invokeCallback(*main_conf,NULL,NULL,7,0);
821    removeReadLocks(main_conf);
822#ifdef HPC_DEBUG
823    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
824    fflush(stderr);
825#endif
826    return -1;
827  }
828  if ( (cl = accept(fd, NULL, NULL)) == -1) {
829    setMapInMaps(*main_conf,"lenv","message",_("Accept error"));
830    errorException (*main_conf, _("Accept error"),
831                    "InternalError", NULL);
832#ifdef HPC_DEBUG
833    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
834    fflush(stderr);
835#endif
836    invokeCallback(*main_conf,NULL,NULL,7,0);
837    removeReadLocks(main_conf);
838#ifdef HPC_DEBUG
839    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
840    fflush(stderr);
841#endif
842    return -1;
843  }else{
844#ifdef HPC_DEBUG
845    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
846    fflush(stderr);
847#endif
848    int hasPassed=-1;
849    char buf[11];
850    memset(&buf,0,11);
851    while ( (rc=read(cl,buf,10)) ) {     
852      if(rc==0){
853        setMapInMaps(*main_conf,"lenv","message",_("Read closed"));
854        errorException (*main_conf, _("Read closed"),
855                        "InternalError", NULL);
856#ifdef HPC_DEBUG
857        fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
858        fflush(stderr);
859#endif
860        invokeCallback(*main_conf,NULL,NULL,7,0);
861        removeReadLocks(main_conf);
862#ifdef HPC_DEBUG
863        fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
864        fflush(stderr);
865#endif
866        return -1;
867      }else{
868        if(rc<0){
869          setMapInMaps(*main_conf,"lenv","message",_("Read error"));
870          errorException (*main_conf, _("Read error"),
871                          "InternalError", NULL);
872#ifdef HPC_DEBUG
873          fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
874          fflush(stderr);
875#endif
876          invokeCallback(*main_conf,NULL,NULL,7,0);
877          removeReadLocks(main_conf);
878#ifdef HPC_DEBUG
879          fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
880          fflush(stderr);
881#endif
882          return -1;
883        }
884      }
885      hasPassed=1;
886      res=atoi(buf);
887      unlink(sname);
888      free(sname);
889      removeReadLocks(main_conf);
890 
891      if(res==3){
892#ifdef HPC_DEBUG
893        fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
894        fflush(stderr);
895#endif
896        invokeCallback(m,NULL,outputs,5,0);
897#ifdef HPC_DEBUG
898        fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
899        fflush(stderr);
900#endif
901
902        // Read informations provided by FinalizeHPC as a configuration file
903        // then, remove the file.
904        map* jobid=getMapFromMaps(*main_conf,"lenv","usid");
905        map* tmpPath=getMapFromMaps(*main_conf,"main","tmpPath");
906        char *filePath=(char*)malloc((strlen(tmpPath->value)+strlen(jobid->value)+15)*sizeof(char));
907        sprintf(filePath,"%s/exec_status_%s",tmpPath->value,jobid->value);
908        maps* m = (maps *) malloc (MAPS_SIZE);
909        m->child=NULL;
910        m->next=NULL;
911        int saved_stdout = dup (fileno (stdout));
912        dup2 (fileno (stderr), fileno (stdout));
913        conf_read(filePath,m);
914        //dumpMaps(m);
915        fflush(stdout);
916        dup2 (saved_stdout, fileno (stdout));
917        close(saved_stdout);
918        unlink(filePath);
919        free(filePath);
920        addMapsToMaps(main_conf,m);
921        freeMaps(&m);
922        free(m);
923
924        input=*real_outputs;
925        while(input!=NULL){
926          if(input->child==NULL){
927            map* generatedFile=getMap(input->content,"generated_file");
928            if(generatedFile!=NULL){
929              char* filename=strrchr(generatedFile->value,'/');
930              char* targetPath=(char*)malloc((strlen(tmpPath->value)+strlen(filename)+2)*sizeof(char));
931              sprintf(targetPath,"%s/%s",tmpPath->value,filename);
932              test=ssh_connect(*main_conf);
933              if(ssh_fetch(*main_conf,targetPath,generatedFile->value,ssh_get_cnt(m))==0){
934                setMapInMaps(*real_outputs,input->name,"generated_file",targetPath);
935                free(targetPath);
936              }else{
937                map* hpcStdErr=getMapFromMaps(*main_conf,"henv","StdErr");
938                // Added for using sacct in place of scontrol
939                char *sourcePath=NULL;
940                if(hpcStdErr!=NULL){
941                  sourcePath=(char*)malloc((strlen(targetPathMap->value)+strlen(hpcStdErr->value)+2)*sizeof(char));
942                  sprintf(sourcePath,"%s/%s",targetPathMap->value,hpcStdErr->value);
943                }
944                if(hpcStdErr!=NULL && sourcePath!=NULL && ssh_fetch(*main_conf,targetPath,sourcePath,ssh_get_cnt(m))==0){
945                  free(sourcePath);
946                  struct stat f_status;
947                  int ts=stat(targetPath, &f_status);
948                  if(ts==0) {
949                    char* fcontent = NULL;
950                    fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
951                    FILE* f=fopen(targetPath,"rb");
952                    fread(fcontent,f_status.st_size,1,f);
953                    int fsize=f_status.st_size;
954                    fcontent[fsize]=0;
955                    fclose(f);
956                    setMapInMaps(*main_conf,"lenv","message",fcontent);
957                    free(fcontent);
958                  }else{
959                    char *tmpStr=(char*)malloc((strlen(targetPath)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char));
960                    sprintf(tmpStr,_("Unable to fetch the remote file for %s"),targetPath);
961                    setMapInMaps(*main_conf,"lenv","message",tmpStr);
962                    free(tmpStr);
963                  }
964                }else{
965                  char *tmpStr=(char*)malloc((strlen(filename)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char));
966                  sprintf(tmpStr,_("Unable to fetch the remote file for %s"),filename);
967                  setMapInMaps(*main_conf,"lenv","message",tmpStr);
968                  free(tmpStr);
969                }
970                invokeCallback(*main_conf,NULL,NULL,7,0);
971                return SERVICE_FAILED;
972              }
973            }
974          }else{
975            map* generatedFile=getMap(input->content,"generated_file");
976            map* generatedUrl=getMap(input->content,"generated_url");
977            if(generatedFile!=NULL){
978              char* filename=strrchr(generatedFile->value,'/');
979              char* targetPath=(char*)malloc((strlen(tmpPath->value)+strlen(filename)+2)*sizeof(char));
980              sprintf(targetPath,"%s/%s",tmpPath->value,filename);
981              test=ssh_connect(*main_conf);
982              if(ssh_fetch(*main_conf,targetPath,generatedFile->value,ssh_get_cnt(m))==0){
983                maps* tmp=getMaps(*real_outputs,input->name);
984                char serviceName[9];
985                freeMap(&tmp->content);
986                free(tmp->content);
987                tmp->content=NULL;
988                maps* output=getMaps(*real_outputs,input->name);
989                setMapInMaps(output->child,"download_link","generated_file",targetPath);
990                setMapInMaps(output->child,"download_link","generated_url",generatedUrl->value);
991                setMapInMaps(output->child,"download_link","storage",targetPath);
992                setMapInMaps(output->child,"download_link","useMapserver","false");
993                setMapInMaps(output->child,"download_link","replicateStorageNext","true");
994                setMapInMaps(output->child,"download_link","asReference","true");
995                setMapInMaps(output->child,"download_link","inRequest","true");
996                setMapInMaps(output->child,"wms_link","generated_file",targetPath);
997                setMapInMaps(output->child,"wms_link","storage",targetPath);
998                setMapInMaps(output->child,"wms_link","useMapserver","true");
999                setMapInMaps(output->child,"wms_link","msOgc","WMS");
1000                setMapInMaps(output->child,"wms_link","requestedMimeType","image/png");
1001                setMapInMaps(output->child,"wms_link","asReference","true");
1002                if(getMaps(output->child,"wcs_link")!=NULL){
1003                  sprintf(serviceName,"wcs_link");
1004                  setMapInMaps(output->child,"wcs_link","msOgc","WCS");
1005                }else{
1006                  sprintf(serviceName,"wfs_link");
1007                  setMapInMaps(output->child,"wfs_link","msOgc","WFS");
1008                }
1009                setMapInMaps(output->child,serviceName,"storage",targetPath);
1010                setMapInMaps(output->child,serviceName,"generated_file",targetPath);
1011                setMapInMaps(output->child,serviceName,"useMapserver","true");
1012                setMapInMaps(output->child,serviceName,"asReference","true");
1013              }else{
1014                map* hpcStdErr=getMapFromMaps(*main_conf,"henv","StdErr");
1015                char *sourcePath=NULL;
1016                if(hpcStdErr!=NULL){
1017                  dumpMap(hpcStdErr);
1018                  sourcePath=(char*)malloc((strlen(targetPathMap->value)+strlen(hpcStdErr->value)+2)*sizeof(char));
1019                  sprintf(sourcePath,"%s/%s",targetPathMap->value,hpcStdErr->value);
1020                }
1021                if(hpcStdErr!=NULL && sourcePath!=NULL && ssh_fetch(*main_conf,targetPath,sourcePath,ssh_get_cnt(m))==0){
1022                  free(sourcePath);
1023                  struct stat f_status;
1024                  int ts=stat(targetPath, &f_status);
1025                  if(ts==0) {
1026                    char* fcontent = NULL;
1027                    fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
1028                    FILE* f=fopen(targetPath,"rb");
1029                    fread(fcontent,f_status.st_size,1,f);
1030                    int fsize=f_status.st_size;
1031                    fcontent[fsize]=0;
1032                    fclose(f);
1033                    setMapInMaps(*main_conf,"lenv","message",fcontent);
1034                    free(fcontent);
1035                  }else{
1036                    char *tmpStr=(char*)malloc((strlen(targetPath)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char));
1037                    sprintf(tmpStr,_("Unable to fetch the remote file for %s"),targetPath);
1038                    setMapInMaps(*main_conf,"lenv","message",tmpStr);
1039                    free(tmpStr);
1040                  }
1041                }else{
1042                  char *tmpStr=(char*)malloc((strlen(sourcePath)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char));
1043                  sprintf(tmpStr,_("Unable to fetch the remote file for %s"),sourcePath);
1044                  setMapInMaps(*main_conf,"lenv","message",tmpStr);
1045                  free(tmpStr);
1046                }
1047                invokeCallback(*main_conf,NULL,NULL,7,0);
1048                return SERVICE_FAILED;
1049              }
1050              free(targetPath);
1051            }
1052          }
1053          input=input->next;
1054        }
1055
1056      }else{
1057        // Try to access remotely to the log file and return a more relevant error message
1058        setMapInMaps(*main_conf,"lenv","message",_("HPC Execution failed!"));
1059        errorException (*main_conf, _("HPC Execution failed!"),
1060                        "InternalError", NULL);
1061#ifdef HPC_DEBUG
1062        fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
1063        fflush(stderr);
1064#endif
1065        invokeCallback(*main_conf,NULL,NULL,7,0);
1066#ifdef HPC_DEBUG
1067        fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
1068        fflush(stderr);
1069#endif
1070      }
1071    }
1072    if(hasPassed<0){
1073      perror("Failed to read");
1074      setMapInMaps(*main_conf,"lenv","message",_("Unable to parse the value returned by remote execution"));
1075      errorException (*main_conf, _("Unable to parse the value returned by remote execution"),
1076                      "InternalError", NULL);
1077#ifdef HPC_DEBUG
1078      fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
1079      fflush(stderr);
1080#endif
1081      invokeCallback(*main_conf,NULL,NULL,7,0);
1082#ifdef HPC_DEBUG
1083      fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
1084      fflush(stderr);
1085#endif
1086      return SERVICE_FAILED;
1087    }
1088  }
1089#ifdef HPC_DEBUG
1090  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
1091  fflush(stderr);
1092#endif
1093  ssh_close(*main_conf);
1094#ifdef HPC_DEBUG
1095  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
1096  fflush(stderr);
1097#endif
1098  return res;
1099}
Note: See TracBrowser for help on using the repository browser.

Search

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png