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

Last change on this file since 888 was 888, checked in by djay, 5 years ago

Add support for output style definition depending on geometry type (msStylePoly, msStylePoint, msStrylLine). Upload shared data in remote_persitent_data_path rather than remote_data_path (used for not shared data). In case output data should be published and the data is empty (nb_pixels or nb_features is set to 0) then, the ZOO-Kernel returns an ExceptionReport? with an Exception for every output.

  • Property svn:keywords set to Id
File size: 41.7 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  map* targetPersistentPathMap=getMapFromMaps(*main_conf,configurationId,"remote_persistent_data_path");
323 
324  pthread_t threads_pool[50];
325  // Force the HPC services to be called asynchronously
326  map* isAsync=getMapFromMaps(*main_conf,"lenv","async");
327  if(isAsync==NULL){
328    errorException(*main_conf,_("The synchronous mode is not supported by this type of service"),"NoSuchMode",s->name);
329    return -1;
330  }
331
332  maps* input=*real_inputs;
333  char **parameters=NULL;
334  int parameters_cnt=0;
335  while(input!=NULL && input->content!=NULL){
336    map* isInRequest=getMap(input->content,"inRequest");
337    map* minNb=getMap(input->content,"minOccurs");
338    if(getMaps(*real_outputs,input->name)==NULL &&
339       ( (isInRequest!=NULL && strncasecmp(isInRequest->value,"true",4)==0)
340         || (minNb!=NULL && atoi(minNb->value)>0) ) ){
341      parameters_cnt+=1;
342      if(parameters_cnt==1)
343        parameters=(char**)malloc(parameters_cnt*sizeof(char*));
344      else
345        parameters=(char**)realloc(parameters,parameters_cnt*sizeof(char*));
346      if(getMap(input->content,"mimeType")!=NULL){
347        // Input is ComplexData
348        if(getMap(input->content,"cache_file")==NULL){
349          // Input data has been passed by value
350          // TODO: publish input through MapServer / use output publication
351          dumpMapsValuesToFiles(main_conf,&input);
352          addToMap(input->content,"toPublish","true");
353          //addToMap(input->content,"useMapserver","true");
354        }
355        if(getMap(input->content,"cache_file")!=NULL){
356          map* length=getMap(input->content,"length");
357          if(length==NULL){
358            addToMap(input->content,"length","1");
359            length=getMap(input->content,"length");
360          }
361          int len=atoi(length->value);
362          int i=0;
363          for(i=0;i<len;i++){
364            map* tmp=getMapArray(input->content,"cache_file",i);
365            map* origin=getMapArray(input->content,"origin",i);
366            char* targetName=strrchr(tmp->value,'/');
367            char *targetPath;
368            if(origin!=NULL && strcasecmp(origin->value,"SHARED")==0 && targetPersistentPathMap!=NULL){
369              targetPath=(char*)malloc((strlen(targetPersistentPathMap->value)+strlen(targetName)+2)*sizeof(char));
370              sprintf(targetPath,"%s/%s",targetPersistentPathMap->value,targetName);
371            }
372            else{
373              targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
374              sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
375            }
376            setMapArray(input->content,"targetPath",i,targetPath);
377            setMapArray(input->content,"localPath",i,tmp->value);
378            map* tmp1=getMapArray(input->content,"value",i);
379            if(tmp1!=NULL){
380              free(tmp1->value);
381              tmp1->value=strdup("empty");
382            }
383            if(i==0){
384              parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+3)*sizeof(char));
385              sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,targetPath);
386            }else{
387              fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
388              fflush(stderr);
389              char *tmpStr=zStrdup(parameters[parameters_cnt-1]);
390              parameters[parameters_cnt-1]=(char*)realloc(parameters[parameters_cnt-1],(strlen(tmpStr)+strlen(targetPath)+2)*sizeof(char));
391              sprintf(parameters[parameters_cnt-1],"%s %s",tmpStr,targetPath);
392              free(tmpStr);
393              fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
394              fflush(stderr);
395            }
396            free(targetPath);
397          }
398          addToUploadQueue(main_conf,input);
399        }else{
400          // ???
401          fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
402          fflush(stderr);
403        }
404      }else{
405        // LitteralData and BboxData
406        if(getMap(input->content,"dataType")!=NULL){
407          // For LitteralData, simply pass the value
408          map* length=getMap(input->content,"length");
409          if(length!=NULL){
410            char* value=NULL;
411            int len=atoi(length->value);
412            int i=0;
413            for(i=0;i<len;i++){
414              map* val=getMapArray(input->content,"value",i);
415              if(val!=NULL){
416                if(value==NULL){
417                  value=(char*)malloc((strlen(val->value)+3)*sizeof(char));
418                  sprintf(value,"\"%s\"",val->value);
419                }
420                else{
421                  value=(char*)realloc(value,(strlen(value)+strlen(val->value)+4)*sizeof(char));
422                  sprintf(value,"%s \"%s\"",value,val->value);
423                }
424              }
425            }
426            if(value!=NULL){
427              parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(value)+3)*sizeof(char));
428              sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,value);
429            }
430          }else{
431            map* val=getMap(input->content,"value");
432            parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(val->value)+5)*sizeof(char));
433            sprintf(parameters[parameters_cnt-1],"-%s \"%s\"",input->name,val->value);
434          }
435        }
436      }
437    }
438    input=input->next;
439  }
440
441#ifdef HPC_DEBUG
442  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
443#endif
444  invokeCallback(m,inputs,NULL,1,1);
445  invokeCallback(m,inputs,NULL,2,0);
446  if(getMapFromMaps(m,"lenv","mapError")!=NULL){
447    invokeCallback(*main_conf,inputs,NULL,7,0);
448    return -1;
449  }
450#ifdef HPC_DEBUG
451  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
452  dumpMaps(inputs);
453#endif
454
455  // Upload data on HPC
456  if(runUpload(main_conf)==false){
457    errorException (*main_conf, _("Unable to lock the file for upload!"),
458                    "InternalError", NULL);
459#ifdef HPC_DEBUG
460    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
461#endif
462    invokeCallback(*main_conf,inputs,NULL,7,0);
463#ifdef HPC_DEBUG
464    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
465#endif
466    return -1;
467  }
468#ifdef HPC_DEBUG
469  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
470#endif
471  invokeCallback(m,inputs,NULL,2,1);
472#ifdef HPC_DEBUG
473  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
474#endif
475
476  // Add the filename to generate for every output to parameters
477  input=*real_outputs;
478#ifdef HPC_DEBUG
479  dumpMaps(input);
480#endif
481  while(input!=NULL){
482    // Parse all outputs including inner outputs if required.
483    if(input->child==NULL){
484      // Name every files that should be produced by the service execution
485      map* mime=getMap(input->content,"mimeType");
486      char* targetName;
487      if(mime!=NULL){
488        bool hasExt=false;
489        map* fileExt=getFileExtensionMap(mime->value,&hasExt);
490        targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+strlen(fileExt->value)+11)*sizeof(char));
491        sprintf(targetName,"output_%s_%s_%s.%s",s->name,input->name,uuid->value,fileExt->value);
492        freeMap(&fileExt);
493        free(fileExt);
494      }else{
495        targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+14)*sizeof(char));
496        sprintf(targetName,"output_%s_%s_%s.tif",s->name,input->name,uuid->value);
497      }
498      char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
499      sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
500      map *tmpUrl=getMapFromMaps(*main_conf,"main","tmpUrl");
501      char *targetUrl=(char*)malloc((strlen(tmpUrl->value)+strlen(targetName)+2)*sizeof(char));
502      sprintf(targetUrl,"%s/%s",tmpUrl->value,targetName);
503      free(targetName);
504      setMapInMaps(*real_outputs,input->name,"generated_file",targetPath);
505      addToMap(input->content,"generated_url",targetUrl);
506      free(targetUrl);
507      {
508        parameters_cnt+=1;
509        if(parameters_cnt==1)
510          parameters=(char**)malloc(parameters_cnt*sizeof(char*));
511        else
512          parameters=(char**)realloc(parameters,parameters_cnt*sizeof(char*));
513        // We should verify if any optional tag for output is required
514        // (i.e. -out output.tiff *int8*), meaning that we should search
515        // for a corresponding inputs name.
516        map* inValue=getMapFromMaps(*real_inputs,input->name,"value");
517        if(inValue!=NULL){
518          parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+strlen(inValue->value)+4)*sizeof(char));
519          sprintf(parameters[parameters_cnt-1],"-%s %s %s",input->name,targetPath,inValue->value);
520        }else{
521          parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+3)*sizeof(char));
522          sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,targetPath);
523        }
524      }
525      free(targetPath);
526    }// In other case it means we need to return the cache_file as generated_file
527    else{
528      // Name every files that should be produced by the service execution
529      map* mime=getMap(input->child->content,"mimeType");
530      char* targetName;
531      if(mime!=NULL){
532        bool hasExt=false;
533        map* fileExt=getFileExtensionMap(mime->value,&hasExt);
534        targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+strlen(fileExt->value)+11)*sizeof(char));
535        sprintf(targetName,"output_%s_%s_%s.%s",s->name,input->name,uuid->value,fileExt->value);
536        freeMap(&fileExt);
537        free(fileExt);
538      }else{
539        targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+14)*sizeof(char));
540        sprintf(targetName,"output_%s_%s_%s.tif",s->name,input->name,uuid->value);
541      }
542      char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
543      sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
544      map *tmpUrl=getMapFromMaps(*main_conf,"main","tmpUrl");
545      char *targetUrl=(char*)malloc((strlen(tmpUrl->value)+strlen(targetName)+2)*sizeof(char));
546      sprintf(targetUrl,"%s/%s",tmpUrl->value,targetName);
547      free(targetName);
548      addToMap(input->content,"generated_file",targetPath);
549      addToMap(input->content,"storage",targetPath);
550      addToMap(input->content,"generated_url",targetUrl);
551      free(targetUrl);
552      if(strcasecmp(input->name,"wms_link")!=0&&
553         strcasecmp(input->name,"wcs_link")!=0 &&
554         strcasecmp(input->name,"wfs_link")!=0){
555        parameters_cnt+=1;
556        if(parameters_cnt==1)
557          parameters=(char**)malloc(parameters_cnt*sizeof(char*));
558        else
559          parameters=(char**)realloc(parameters,parameters_cnt*sizeof(char*));
560        // We should verify if any optional tag for output is required
561        // (i.e. -out output.tiff *int8*), meaning that we should search
562        // for a corresponding inputs name.
563        map* inValue=getMapFromMaps(*real_inputs,input->name,"value");
564        if(inValue!=NULL){
565          parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+strlen(inValue->value)+4)*sizeof(char));
566          sprintf(parameters[parameters_cnt-1],"-%s %s %s",input->name,targetPath,inValue->value);
567        }else{
568          parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+3)*sizeof(char));
569          sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,targetPath);
570        }
571      }
572      free(targetPath);
573    }
574    input=input->next;
575  }
576  // Produce the SBATCH File locally
577  char *scriptPath=(char*)malloc((strlen(s->name)+strlen(tmpPath->value)+strlen(uuid->value)+10)*sizeof(char));
578  sprintf(scriptPath,"%s/zoo_%s_%s.sh",tmpPath->value,s->name,uuid->value);
579  setMapInMaps(*main_conf,"lenv","local_script",scriptPath);
580#ifdef HPC_DEBUG
581  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
582  fflush(stderr);
583#endif
584  invokeCallback(m,inputs,NULL,3,0);
585#ifdef HPC_DEBUG
586  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
587  fflush(stderr);
588#endif
589  FILE* scriptFile=fopen(scriptPath,"w+");
590  map* headerMap=getMapFromMaps(*main_conf,configurationId,"jobscript_header");
591  if(headerMap!=NULL){
592    // Use the header file if defined in the HPC section of the main.cfg file
593    struct stat f_status;
594    int s=stat(headerMap->value, &f_status);
595    if(s==0){
596      char* fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
597      FILE* f=fopen(headerMap->value,"rb");
598      fread(fcontent,f_status.st_size,1,f);
599      int fsize=f_status.st_size;
600      fcontent[fsize]=0;
601      fclose(f);
602      fprintf(scriptFile,"%s\n### --- ZOO-Service HEADER end --- ###\n\n",fcontent);
603      free(fcontent);
604    }else
605      fprintf(scriptFile,"#!/bin/bash\n\n### *** Default ZOO-Service HEADER (no header found) *** ###\n\n");
606  }else
607    fprintf(scriptFile,"#!/bin/bash\n\n### *** Default ZOO-Service HEADER *** ###\n\n");
608  maps* hpc_opts=getMaps(*main_conf,configurationId);
609  if(hpc_opts!=NULL){
610    map* hpc_opts_content=hpc_opts->content;
611    while(hpc_opts_content!=NULL){
612      if(strncasecmp(hpc_opts_content->name,"sbatch_options_",15)==0)
613        fprintf(scriptFile,"#SBATCH --%s=%s\n",strstr(hpc_opts_content->name,"sbatch_options_")+15,hpc_opts_content->value);
614      hpc_opts_content=hpc_opts_content->next;
615    }
616  }
617  fprintf(scriptFile,"#SBATCH --job-name=ZOO-Project_%s_%s\n\n",uuid->value,s->name);
618  map* mods=getMap(s->content,"hpcModules");
619  if(mods!=NULL)
620    fprintf(scriptFile,"#SBATCH --export=MODULES=%s\n",mods->value);
621
622  map* bodyMap=getMapFromMaps(*main_conf,configurationId,"jobscript_body");
623  if(bodyMap!=NULL){
624    // Use the header file if defined in the HPC section of the main.cfg file
625    struct stat f_status;
626    int s=stat(bodyMap->value, &f_status);
627    if(s==0){
628      char* fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
629      FILE* f=fopen(bodyMap->value,"rb");
630      fread(fcontent,f_status.st_size,1,f);
631      int fsize=f_status.st_size;
632      fcontent[fsize]=0;
633      fclose(f);
634      fprintf(scriptFile,"%s\n### --- ZOO-Service BODY end --- ###\n\n",fcontent);
635      free(fcontent);
636    }else
637      fprintf(scriptFile,"\n### *** Default ZOO-Service BODY (no body found) *** ###\n\n");
638  }else
639    fprintf(scriptFile,"\n### *** Default ZOO-Service BODY *** ###\n\n");
640
641  map* sp=getMap(s->content,"serviceProvider");
642 
643  // Require to produce the command line to be executed
644  fprintf(scriptFile,"\n\necho \"Job started at: $(date)\"\n");
645  fprintf(scriptFile,"echo \"Running service: [%s]\"\n",sp->value);
646  fprintf(scriptFile,"%s ",sp->value);
647  for(int i=0;i<parameters_cnt;i++){
648    fprintf(scriptFile," %s",parameters[i]);
649  }
650  for(int i=parameters_cnt-1;i>=0;i--){
651    free(parameters[i]);
652  }
653  free(parameters);
654  fprintf(scriptFile,"\n");
655  fprintf(scriptFile,"echo \"Job finished at: $(date)\"\n");
656  map* footerMap=getMapFromMaps(*main_conf,configurationId,"jobscript_footer");
657  if(footerMap!=NULL){
658    // Use the footer file if defined in the HPC section of the main.cfg file
659    struct stat f_status;
660    int s=stat(footerMap->value, &f_status);
661    if(s==0){
662      char* fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
663      FILE* f=fopen(footerMap->value,"rb");
664      fread(fcontent,f_status.st_size,1,f);
665      int fsize=f_status.st_size;
666      fcontent[fsize]=0;
667      fclose(f);
668      char* ffcontent=(char*)malloc((strlen(fcontent)+(3*strlen(uuid->value))+1)*sizeof(char));
669      sprintf(ffcontent,fcontent,uuid->value,uuid->value,uuid->value);
670      fprintf(scriptFile,"%s\n### --- ZOO-Service FOOTER end --- ###\n\n",ffcontent);
671      free(fcontent);
672    }else
673      fprintf(scriptFile,"### *** Default ZOO-Service FOOTER (footer file failed to load) *** ###\n\n");
674  }else
675      fprintf(scriptFile,"### *** Default ZOO-Service FOOTER (no footer found) *** ###\n\n");
676  fflush(scriptFile);
677  fclose(scriptFile);
678#ifdef HPC_DEBUG
679  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
680#endif
681  invokeCallback(m,inputs,NULL,3,1);
682#ifdef HPC_DEBUG
683  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
684#endif
685
686  // Upload the SBATCH File to the remote host
687#ifdef HPC_DEBUG
688  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
689#endif
690  invokeCallback(m,inputs,NULL,4,0);
691#ifdef HPC_DEBUG
692  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
693#endif
694  targetPathMap=getMapFromMaps(*main_conf,configurationId,"remote_work_path");
695  if(targetPathMap==NULL){
696    setMapInMaps(*main_conf,"lenv","message",_("There is no remote_work_path defined in your section!"));
697    setMapInMaps(*main_conf,"lenv","status","failed");
698    errorException (*main_conf, _("There is no remote_work_path defined in your section!"),
699                    "InternalError", NULL);
700#ifdef HPC_DEBUG
701    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
702    fflush(stderr);
703#endif
704    invokeCallback(*main_conf,NULL,NULL,7,0);
705#ifdef HPC_DEBUG
706    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
707    fflush(stderr);
708#endif
709    return SERVICE_FAILED;
710  }
711  char* targetName=strrchr(scriptPath,'/');
712  char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
713  sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
714  setMapInMaps(*main_conf,"lenv","remote_script",targetPath);
715  SSHCON *test=ssh_connect(*main_conf);
716  int copy0=ssh_copy(*main_conf,scriptPath,targetPath,ssh_get_cnt(*main_conf));
717  unlink(scriptPath);
718  free(scriptPath);
719  if(copy0!=true){
720    setMapInMaps(*main_conf,"lenv","message",_("Unable to upload the script"));
721    invokeCallback(*main_conf,NULL,NULL,7,0);
722    errorException(*main_conf,_("Unable to upload the script"),"NoApplicableCode",NULL);
723    return -1;
724  }
725  // Execute the SBATCH script remotely
726  addReadLocks(main_conf);
727  map* subStr=getMapFromMaps(*main_conf,configurationId,"sbatch_substr");
728  char *command=(char*)malloc((strlen(targetPath)+strlen(targetPathMap->value)+strlen(subStr->value)+strlen(uuid->value)+137)*sizeof(char));
729  sprintf(command,"sbatch %s 2> %s/error_%s.log | sed \"s:%s::g\"",targetPath,targetPathMap->value,uuid->value,subStr->value);
730  if(ssh_exec(*main_conf,command,ssh_get_cnt(m))<=0){
731    // The sbatch command has failed!
732    // Download the error log file from the HPC server
733    char tmpS[1024];
734    free(command);
735    command=(char*)malloc((strlen(targetPathMap->value)+strlen(uuid->value)+11)*sizeof(char));
736    sprintf(command,"%s/error_%s.log",targetPathMap->value,uuid->value);
737    targetName=strrchr(command,'/');
738    free(targetPath);
739    targetPath=(char*)malloc((strlen(tmpPath->value)+strlen(targetName)+2)*sizeof(char));
740    sprintf(targetPath,"%s/%s",tmpPath->value,targetName);
741    if(ssh_fetch(*main_conf,targetPath,command,ssh_get_cnt(m))==0){
742      struct stat f_status;
743      int ts=stat(targetPath, &f_status);
744      if(ts==0) {
745        char* fcontent = NULL;
746        fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
747        FILE* f=fopen(targetPath,"rb");
748        fread(fcontent,f_status.st_size,1,f);
749        int fsize=f_status.st_size;
750        fcontent[fsize]=0;
751        fclose(f);
752        setMapInMaps(*main_conf,"lenv","message",fcontent);
753        free(fcontent);
754      }else
755        setMapInMaps(*main_conf,"lenv","message",_("No message provided"));
756    }else
757      setMapInMaps(*main_conf,"lenv","message",_("Unable to fetch the remote error log file"));
758    tmpPath=getMapFromMaps(m,"lenv","message");
759#ifdef HPC_DEBUG
760    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
761    fflush(stderr);
762#endif
763    invokeCallback(*main_conf,NULL,NULL,7,0);
764#ifdef HPC_DEBUG
765    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
766    fflush(stderr);
767#endif
768    sprintf(tmpS, "Cannot execute the HPC ZOO-Service %s using %s: %s", s->name, configurationId, tmpPath->value);
769    errorException(*main_conf,tmpS,"NoApplicableCode",NULL);
770    free(command);
771    free(targetPath);
772    ssh_close(*main_conf);
773    removeReadLocks(main_conf);
774    return -1;
775  }
776  free(targetPath);
777#ifdef HPC_DEBUG
778  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
779  fflush(stderr);
780#endif
781  invokeCallback(m,NULL,NULL,4,1);
782#ifdef HPC_DEBUG
783  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
784  fflush(stderr);
785#endif
786  free(command);
787#ifdef HPC_DEBUG
788  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
789  fflush(stderr);
790#endif
791
792  struct sockaddr_un addr;
793  memset(&addr, 0, sizeof(addr));
794  addr.sun_family = AF_UNIX;
795  int rc, cl, fd = socket(AF_UNIX, SOCK_STREAM, 0);
796  char *sname=(char*)malloc((strlen(tmpPath->value)+strlen(uuid->value)+20));
797  sprintf(sname,"%s/.wait_socket_%s.sock",tmpPath->value,uuid->value);
798  strncpy(addr.sun_path, sname, sizeof(addr.sun_path)-1);
799 
800  if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
801    perror("bind error");
802    setMapInMaps(*main_conf,"lenv","message",_("Unable to bind socket!"));
803    errorException (*main_conf, _("Unable to bind socket!"),
804                    "InternalError", NULL);
805#ifdef HPC_DEBUG
806    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
807    fflush(stderr);
808#endif
809    invokeCallback(*main_conf,NULL,NULL,7,0);
810    removeReadLocks(main_conf);
811#ifdef HPC_DEBUG
812    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
813    fflush(stderr);
814#endif
815    return -1;
816  }
817#ifdef HPC_DEBUG
818  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
819  fflush(stderr);
820#endif
821  if (listen(fd, 5) == -1) {
822    setMapInMaps(*main_conf,"lenv","message",_("Listen error"));
823    errorException (*main_conf, _("Listen error"),
824                    "InternalError", NULL);
825#ifdef HPC_DEBUG
826    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
827    fflush(stderr);
828#endif
829    invokeCallback(*main_conf,NULL,NULL,7,0);
830    removeReadLocks(main_conf);
831#ifdef HPC_DEBUG
832    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
833    fflush(stderr);
834#endif
835    return -1;
836  }
837  if ( (cl = accept(fd, NULL, NULL)) == -1) {
838    setMapInMaps(*main_conf,"lenv","message",_("Accept error"));
839    errorException (*main_conf, _("Accept error"),
840                    "InternalError", NULL);
841#ifdef HPC_DEBUG
842    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
843    fflush(stderr);
844#endif
845    invokeCallback(*main_conf,NULL,NULL,7,0);
846    removeReadLocks(main_conf);
847#ifdef HPC_DEBUG
848    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
849    fflush(stderr);
850#endif
851    return -1;
852  }else{
853#ifdef HPC_DEBUG
854    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
855    fflush(stderr);
856#endif
857    int hasPassed=-1;
858    char buf[11];
859    memset(&buf,0,11);
860    while ( (rc=read(cl,buf,10)) ) {     
861      if(rc==0){
862        setMapInMaps(*main_conf,"lenv","message",_("Read closed"));
863        errorException (*main_conf, _("Read closed"),
864                        "InternalError", NULL);
865#ifdef HPC_DEBUG
866        fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
867        fflush(stderr);
868#endif
869        invokeCallback(*main_conf,NULL,NULL,7,0);
870        removeReadLocks(main_conf);
871#ifdef HPC_DEBUG
872        fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
873        fflush(stderr);
874#endif
875        return -1;
876      }else{
877        if(rc<0){
878          setMapInMaps(*main_conf,"lenv","message",_("Read error"));
879          errorException (*main_conf, _("Read error"),
880                          "InternalError", NULL);
881#ifdef HPC_DEBUG
882          fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
883          fflush(stderr);
884#endif
885          invokeCallback(*main_conf,NULL,NULL,7,0);
886          removeReadLocks(main_conf);
887#ifdef HPC_DEBUG
888          fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
889          fflush(stderr);
890#endif
891          return -1;
892        }
893      }
894      hasPassed=1;
895      res=atoi(buf);
896      unlink(sname);
897      free(sname);
898      removeReadLocks(main_conf);
899 
900      if(res==3){
901#ifdef HPC_DEBUG
902        fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
903        fflush(stderr);
904#endif
905        invokeCallback(m,NULL,outputs,5,0);
906#ifdef HPC_DEBUG
907        fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
908        fflush(stderr);
909#endif
910
911        // Read informations provided by FinalizeHPC as a configuration file
912        // then, remove the file.
913        map* jobid=getMapFromMaps(*main_conf,"lenv","usid");
914        map* tmpPath=getMapFromMaps(*main_conf,"main","tmpPath");
915        char *filePath=(char*)malloc((strlen(tmpPath->value)+strlen(jobid->value)+15)*sizeof(char));
916        sprintf(filePath,"%s/exec_status_%s",tmpPath->value,jobid->value);
917        maps* lm = (maps *) malloc (MAPS_SIZE);
918        lm->child=NULL;
919        lm->next=NULL;
920        int saved_stdout = dup (fileno (stdout));
921        dup2 (fileno (stderr), fileno (stdout));
922        conf_read(filePath,lm);
923        //dumpMaps(m);
924        fflush(stdout);
925        dup2 (saved_stdout, fileno (stdout));
926        close(saved_stdout);
927        unlink(filePath);
928        free(filePath);
929        addMapsToMaps(main_conf,lm);
930        freeMaps(&lm);
931        free(lm);
932
933        input=*real_outputs;
934        while(input!=NULL){
935          if(input->child==NULL){
936            map* generatedFile=getMap(input->content,"generated_file");
937            if(generatedFile!=NULL){
938              char* filename=strrchr(generatedFile->value,'/');
939              char* targetPath=(char*)malloc((strlen(tmpPath->value)+strlen(filename)+2)*sizeof(char));
940              sprintf(targetPath,"%s/%s",tmpPath->value,filename);
941              test=ssh_connect(*main_conf);
942              if(ssh_fetch(*main_conf,targetPath,generatedFile->value,ssh_get_cnt(m))==0){
943                setMapInMaps(*real_outputs,input->name,"generated_file",targetPath);
944                free(targetPath);
945              }else{
946                map* hpcStdErr=getMapFromMaps(*main_conf,"henv","StdErr");
947                // Added for using sacct in place of scontrol
948                char *sourcePath=NULL;
949                if(hpcStdErr!=NULL){
950                  sourcePath=(char*)malloc((strlen(targetPathMap->value)+strlen(hpcStdErr->value)+2)*sizeof(char));
951                  sprintf(sourcePath,"%s/%s",targetPathMap->value,hpcStdErr->value);
952                }
953                if(hpcStdErr!=NULL && sourcePath!=NULL && ssh_fetch(*main_conf,targetPath,sourcePath,ssh_get_cnt(m))==0){
954                  free(sourcePath);
955                  struct stat f_status;
956                  int ts=stat(targetPath, &f_status);
957                  if(ts==0) {
958                    char* fcontent = NULL;
959                    fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
960                    FILE* f=fopen(targetPath,"rb");
961                    fread(fcontent,f_status.st_size,1,f);
962                    int fsize=f_status.st_size;
963                    fcontent[fsize]=0;
964                    fclose(f);
965                    setMapInMaps(*main_conf,"lenv","message",fcontent);
966                    free(fcontent);
967                  }else{
968                    char *tmpStr=(char*)malloc((strlen(targetPath)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char));
969                    sprintf(tmpStr,_("Unable to fetch the remote file for %s"),targetPath);
970                    setMapInMaps(*main_conf,"lenv","message",tmpStr);
971                    free(tmpStr);
972                  }
973                }else{
974                  char *tmpStr=(char*)malloc((strlen(filename)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char));
975                  sprintf(tmpStr,_("Unable to fetch the remote file for %s"),filename);
976                  setMapInMaps(*main_conf,"lenv","message",tmpStr);
977                  free(tmpStr);
978                }
979                invokeCallback(*main_conf,NULL,NULL,7,0);
980                return SERVICE_FAILED;
981              }
982            }
983          }else{
984            map* generatedFile=getMap(input->content,"generated_file");
985            map* generatedUrl=getMap(input->content,"generated_url");
986            if(generatedFile!=NULL){
987              char* filename=strrchr(generatedFile->value,'/');
988              char* targetPath=(char*)malloc((strlen(tmpPath->value)+strlen(filename)+2)*sizeof(char));
989              sprintf(targetPath,"%s/%s",tmpPath->value,filename);
990              test=ssh_connect(*main_conf);
991              if(ssh_fetch(*main_conf,targetPath,generatedFile->value,ssh_get_cnt(m))==0){
992                maps* tmp=getMaps(*real_outputs,input->name);
993                char serviceName[9];
994                freeMap(&tmp->content);
995                free(tmp->content);
996                tmp->content=NULL;
997                maps* output=getMaps(*real_outputs,input->name);
998                setMapInMaps(output->child,"download_link","generated_file",targetPath);
999                setMapInMaps(output->child,"download_link","generated_url",generatedUrl->value);
1000                setMapInMaps(output->child,"download_link","storage",targetPath);
1001                setMapInMaps(output->child,"download_link","useMapserver","false");
1002                setMapInMaps(output->child,"download_link","replicateStorageNext","true");
1003                setMapInMaps(output->child,"download_link","asReference","true");
1004                setMapInMaps(output->child,"download_link","inRequest","true");
1005                setMapInMaps(output->child,"wms_link","generated_file",targetPath);
1006                setMapInMaps(output->child,"wms_link","storage",targetPath);
1007                setMapInMaps(output->child,"wms_link","useMapserver","true");
1008                setMapInMaps(output->child,"wms_link","msOgc","WMS");
1009                setMapInMaps(output->child,"wms_link","requestedMimeType","image/png");
1010                setMapInMaps(output->child,"wms_link","asReference","true");
1011                if(getMaps(output->child,"wcs_link")!=NULL){
1012                  sprintf(serviceName,"wcs_link");
1013                  setMapInMaps(output->child,"wcs_link","msOgc","WCS");
1014                }else{
1015                  sprintf(serviceName,"wfs_link");
1016                  setMapInMaps(output->child,"wfs_link","msOgc","WFS");
1017                }
1018                setMapInMaps(output->child,serviceName,"storage",targetPath);
1019                setMapInMaps(output->child,serviceName,"generated_file",targetPath);
1020                setMapInMaps(output->child,serviceName,"useMapserver","true");
1021                setMapInMaps(output->child,serviceName,"asReference","true");
1022              }else{
1023                map* hpcStdErr=getMapFromMaps(*main_conf,"henv","StdErr");
1024                char *sourcePath=NULL;
1025                if(hpcStdErr!=NULL){
1026                  dumpMap(hpcStdErr);
1027                  sourcePath=(char*)malloc((strlen(targetPathMap->value)+strlen(hpcStdErr->value)+2)*sizeof(char));
1028                  sprintf(sourcePath,"%s/%s",targetPathMap->value,hpcStdErr->value);
1029                }
1030                if(hpcStdErr!=NULL && sourcePath!=NULL && ssh_fetch(*main_conf,targetPath,sourcePath,ssh_get_cnt(m))==0){
1031                  free(sourcePath);
1032                  struct stat f_status;
1033                  int ts=stat(targetPath, &f_status);
1034                  if(ts==0) {
1035                    char* fcontent = NULL;
1036                    fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
1037                    FILE* f=fopen(targetPath,"rb");
1038                    fread(fcontent,f_status.st_size,1,f);
1039                    int fsize=f_status.st_size;
1040                    fcontent[fsize]=0;
1041                    fclose(f);
1042                    setMapInMaps(*main_conf,"lenv","message",fcontent);
1043                    free(fcontent);
1044                  }else{
1045                    char *tmpStr=(char*)malloc((strlen(targetPath)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char));
1046                    sprintf(tmpStr,_("Unable to fetch the remote file for %s"),targetPath);
1047                    setMapInMaps(*main_conf,"lenv","message",tmpStr);
1048                    free(tmpStr);
1049                  }
1050                }else{
1051                  char *tmpStr=(char*)malloc((strlen(sourcePath)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char));
1052                  sprintf(tmpStr,_("Unable to fetch the remote file for %s"),sourcePath);
1053                  setMapInMaps(*main_conf,"lenv","message",tmpStr);
1054                  free(tmpStr);
1055                }
1056                invokeCallback(*main_conf,NULL,NULL,7,0);
1057                return SERVICE_FAILED;
1058              }
1059              free(targetPath);
1060            }
1061          }
1062          input=input->next;
1063        }
1064
1065      }else{
1066        // Try to access remotely to the log file and return a more relevant error message
1067        setMapInMaps(*main_conf,"lenv","message",_("HPC Execution failed!"));
1068        errorException (*main_conf, _("HPC Execution failed!"),
1069                        "InternalError", NULL);
1070#ifdef HPC_DEBUG
1071        fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
1072        fflush(stderr);
1073#endif
1074        invokeCallback(*main_conf,NULL,NULL,7,0);
1075#ifdef HPC_DEBUG
1076        fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
1077        fflush(stderr);
1078#endif
1079      }
1080    }
1081    if(hasPassed<0){
1082      perror("Failed to read");
1083      setMapInMaps(*main_conf,"lenv","message",_("Unable to parse the value returned by remote execution"));
1084      errorException (*main_conf, _("Unable to parse the value returned by remote execution"),
1085                      "InternalError", NULL);
1086#ifdef HPC_DEBUG
1087      fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
1088      fflush(stderr);
1089#endif
1090      invokeCallback(*main_conf,NULL,NULL,7,0);
1091#ifdef HPC_DEBUG
1092      fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
1093      fflush(stderr);
1094#endif
1095      return SERVICE_FAILED;
1096    }
1097  }
1098#ifdef HPC_DEBUG
1099  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
1100  fflush(stderr);
1101#endif
1102  ssh_close(*main_conf);
1103#ifdef HPC_DEBUG
1104  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
1105  fflush(stderr);
1106#endif
1107  return res;
1108}
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