[822] | 1 | /* |
---|
| 2 | * Author : Gérald FENOY |
---|
| 3 | * |
---|
[839] | 4 | * Copyright (c) 2017 GeoLabs SARL |
---|
[822] | 5 | * |
---|
[839] | 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 | * |
---|
[822] | 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" |
---|
[839] | 33 | #include "service_callback.h" |
---|
| 34 | #include "mimetypes.h" |
---|
[822] | 35 | #include <sys/un.h> |
---|
| 36 | |
---|
[839] | 37 | typedef struct { |
---|
| 38 | maps* conf; |
---|
| 39 | char* local_file; |
---|
| 40 | char* target_file; |
---|
| 41 | } local_params; |
---|
| 42 | |
---|
[846] | 43 | /** |
---|
| 44 | * Add nested outputs to every outputs that is geographic format |
---|
| 45 | * @see isGeographic |
---|
| 46 | * @param s the service current definition |
---|
| 47 | */ |
---|
[839] | 48 | void addNestedOutputs(service** s){ |
---|
| 49 | if((*s)==NULL){ |
---|
| 50 | return; |
---|
| 51 | } |
---|
| 52 | if(*s==NULL || (*s)->outputs==NULL){ |
---|
| 53 | return; |
---|
[822] | 54 | } |
---|
[839] | 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"); |
---|
[862] | 62 | map* useMS=getMap(cur->defaults->content,"useMapserver"); |
---|
| 63 | if(mimeType!=NULL && useMS!=NULL && strncasecmp(useMS->value,"true",4)==0){ |
---|
[839] | 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); |
---|
[850] | 83 | if(tmp[i]->format!=NULL) |
---|
| 84 | free(tmp[i]->format); |
---|
[839] | 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); |
---|
[854] | 147 | free(cur->defaults); |
---|
[839] | 148 | cur->defaults=NULL; |
---|
| 149 | } |
---|
| 150 | if(cur->supported!=NULL){ |
---|
| 151 | freeIOType(&cur->supported); |
---|
[854] | 152 | free(cur->supported); |
---|
[839] | 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"); |
---|
[862] | 162 | } |
---|
[839] | 163 | } |
---|
| 164 | cur=cur->next; |
---|
| 165 | } |
---|
[850] | 166 | //dumpElements((*s)->outputs); |
---|
[822] | 167 | } |
---|
| 168 | |
---|
| 169 | /** |
---|
[854] | 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 | */ |
---|
| 174 | int 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 | */ |
---|
| 214 | int 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 | */ |
---|
| 253 | char* 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 | /** |
---|
[822] | 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 |
---|
[860] | 288 | * @return SERVICE_SUCCEEDED in case of success, -1 or SERVICE_FAILED when failing. |
---|
[822] | 289 | */ |
---|
| 290 | int 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; |
---|
[854] | 298 | // Get the configuration id depending on service type and defined thresholds |
---|
| 299 | // then, set the configId key in the lenv section |
---|
[839] | 300 | char *serviceType; |
---|
| 301 | map* mServiceType=getMap(s->content,"serviceType"); |
---|
| 302 | if(mServiceType!=NULL) |
---|
| 303 | serviceType=mServiceType->value; |
---|
| 304 | else |
---|
| 305 | serviceType="HPC"; |
---|
[822] | 306 | map* tmpPath=getMapFromMaps(*main_conf,"main","tmpPath"); |
---|
| 307 | map* uuid=getMapFromMaps(*main_conf,"lenv","usid"); |
---|
[854] | 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 | |
---|
[839] | 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 | } |
---|
[822] | 330 | |
---|
| 331 | maps* input=*real_inputs; |
---|
| 332 | char **parameters=NULL; |
---|
| 333 | int parameters_cnt=0; |
---|
[839] | 334 | while(input!=NULL && input->content!=NULL){ |
---|
[862] | 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) ) ){ |
---|
[839] | 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"); |
---|
[862] | 352 | //addToMap(input->content,"useMapserver","true"); |
---|
[839] | 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); |
---|
[862] | 369 | map* tmp1=getMapArray(input->content,"value",i); |
---|
| 370 | if(tmp1!=NULL){ |
---|
| 371 | free(tmp1->value); |
---|
| 372 | tmp1->value=strdup("empty"); |
---|
| 373 | } |
---|
[839] | 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{ |
---|
[862] | 378 | fprintf(stderr,"%s %d\n",__FILE__,__LINE__); |
---|
| 379 | fflush(stderr); |
---|
[839] | 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); |
---|
[862] | 384 | fprintf(stderr,"%s %d\n",__FILE__,__LINE__); |
---|
| 385 | fflush(stderr); |
---|
[839] | 386 | } |
---|
| 387 | free(targetPath); |
---|
| 388 | } |
---|
[862] | 389 | addToUploadQueue(main_conf,input); |
---|
[839] | 390 | }else{ |
---|
| 391 | // ??? |
---|
| 392 | fprintf(stderr,"%s %d\n",__FILE__,__LINE__); |
---|
| 393 | fflush(stderr); |
---|
| 394 | } |
---|
[822] | 395 | }else{ |
---|
[839] | 396 | // LitteralData and BboxData |
---|
| 397 | if(getMap(input->content,"dataType")!=NULL){ |
---|
| 398 | // For LitteralData, simply pass the value |
---|
| 399 | map* val=getMap(input->content,"value"); |
---|
| 400 | parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(val->value)+3)*sizeof(char)); |
---|
| 401 | sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,val->value); |
---|
| 402 | } |
---|
[822] | 403 | } |
---|
| 404 | } |
---|
| 405 | input=input->next; |
---|
| 406 | } |
---|
| 407 | |
---|
[860] | 408 | #ifdef HPC_DEBUG |
---|
[839] | 409 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
[860] | 410 | #endif |
---|
| 411 | invokeCallback(m,inputs,NULL,1,1); |
---|
[862] | 412 | invokeCallback(m,inputs,NULL,2,0); |
---|
[854] | 413 | if(getMapFromMaps(m,"lenv","mapError")!=NULL){ |
---|
[862] | 414 | invokeCallback(*main_conf,inputs,NULL,7,0); |
---|
[854] | 415 | return -1; |
---|
| 416 | } |
---|
[860] | 417 | #ifdef HPC_DEBUG |
---|
| 418 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 419 | dumpMaps(inputs); |
---|
| 420 | #endif |
---|
[839] | 421 | |
---|
| 422 | // Upload data on HPC |
---|
[854] | 423 | if(runUpload(main_conf)==false){ |
---|
[862] | 424 | errorException (*main_conf, _("Unable to lock the file for upload!"), |
---|
[854] | 425 | "InternalError", NULL); |
---|
[860] | 426 | #ifdef HPC_DEBUG |
---|
[854] | 427 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
[860] | 428 | #endif |
---|
[862] | 429 | invokeCallback(*main_conf,inputs,NULL,7,0); |
---|
[860] | 430 | #ifdef HPC_DEBUG |
---|
[854] | 431 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
[860] | 432 | #endif |
---|
[854] | 433 | return -1; |
---|
| 434 | } |
---|
[860] | 435 | #ifdef HPC_DEBUG |
---|
[839] | 436 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
[860] | 437 | #endif |
---|
[839] | 438 | invokeCallback(m,inputs,NULL,2,1); |
---|
[860] | 439 | #ifdef HPC_DEBUG |
---|
[839] | 440 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
[860] | 441 | #endif |
---|
[839] | 442 | |
---|
[854] | 443 | // Add the filename to generate for every output to parameters |
---|
[822] | 444 | input=*real_outputs; |
---|
[860] | 445 | #ifdef HPC_DEBUG |
---|
[854] | 446 | dumpMaps(input); |
---|
[860] | 447 | #endif |
---|
[822] | 448 | while(input!=NULL){ |
---|
[862] | 449 | // Parse all outputs including inner outputs if required. |
---|
[822] | 450 | if(input->child==NULL){ |
---|
| 451 | // Name every files that should be produced by the service execution |
---|
[839] | 452 | map* mime=getMap(input->content,"mimeType"); |
---|
| 453 | char* targetName; |
---|
| 454 | if(mime!=NULL){ |
---|
| 455 | bool hasExt=false; |
---|
| 456 | map* fileExt=getFileExtensionMap(mime->value,&hasExt); |
---|
| 457 | targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+strlen(fileExt->value)+11)*sizeof(char)); |
---|
| 458 | sprintf(targetName,"output_%s_%s_%s.%s",s->name,input->name,uuid->value,fileExt->value); |
---|
| 459 | freeMap(&fileExt); |
---|
| 460 | free(fileExt); |
---|
| 461 | }else{ |
---|
| 462 | targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+14)*sizeof(char)); |
---|
| 463 | sprintf(targetName,"output_%s_%s_%s.tif",s->name,input->name,uuid->value); |
---|
| 464 | } |
---|
[822] | 465 | char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char)); |
---|
| 466 | sprintf(targetPath,"%s/%s",targetPathMap->value,targetName); |
---|
[862] | 467 | map *tmpUrl=getMapFromMaps(*main_conf,"main","tmpUrl"); |
---|
| 468 | char *targetUrl=(char*)malloc((strlen(tmpUrl->value)+strlen(targetName)+2)*sizeof(char)); |
---|
| 469 | sprintf(targetUrl,"%s/%s",tmpUrl->value,targetName); |
---|
[851] | 470 | free(targetName); |
---|
[822] | 471 | setMapInMaps(*real_outputs,input->name,"generated_file",targetPath); |
---|
[862] | 472 | addToMap(input->content,"generated_url",targetUrl); |
---|
| 473 | free(targetUrl); |
---|
| 474 | { |
---|
[854] | 475 | parameters_cnt+=1; |
---|
| 476 | if(parameters_cnt==1) |
---|
| 477 | parameters=(char**)malloc(parameters_cnt*sizeof(char*)); |
---|
| 478 | else |
---|
| 479 | parameters=(char**)realloc(parameters,parameters_cnt*sizeof(char*)); |
---|
| 480 | // We should verify if any optional tag for output is required |
---|
| 481 | // (i.e. -out output.tiff *int8*), meaning that we should search |
---|
| 482 | // for a corresponding inputs name. |
---|
| 483 | map* inValue=getMapFromMaps(*real_inputs,input->name,"value"); |
---|
| 484 | if(inValue!=NULL){ |
---|
| 485 | parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+strlen(inValue->value)+4)*sizeof(char)); |
---|
| 486 | sprintf(parameters[parameters_cnt-1],"-%s %s %s",input->name,targetPath,inValue->value); |
---|
| 487 | }else{ |
---|
| 488 | parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+3)*sizeof(char)); |
---|
| 489 | sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,targetPath); |
---|
| 490 | } |
---|
[839] | 491 | } |
---|
| 492 | free(targetPath); |
---|
| 493 | }// In other case it means we need to return the cache_file as generated_file |
---|
| 494 | else{ |
---|
| 495 | // Name every files that should be produced by the service execution |
---|
| 496 | map* mime=getMap(input->child->content,"mimeType"); |
---|
| 497 | char* targetName; |
---|
| 498 | if(mime!=NULL){ |
---|
| 499 | bool hasExt=false; |
---|
| 500 | map* fileExt=getFileExtensionMap(mime->value,&hasExt); |
---|
| 501 | targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+strlen(fileExt->value)+11)*sizeof(char)); |
---|
| 502 | sprintf(targetName,"output_%s_%s_%s.%s",s->name,input->name,uuid->value,fileExt->value); |
---|
| 503 | freeMap(&fileExt); |
---|
| 504 | free(fileExt); |
---|
| 505 | }else{ |
---|
| 506 | targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+14)*sizeof(char)); |
---|
| 507 | sprintf(targetName,"output_%s_%s_%s.tif",s->name,input->name,uuid->value); |
---|
| 508 | } |
---|
| 509 | char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char)); |
---|
| 510 | sprintf(targetPath,"%s/%s",targetPathMap->value,targetName); |
---|
[862] | 511 | map *tmpUrl=getMapFromMaps(*main_conf,"main","tmpUrl"); |
---|
| 512 | char *targetUrl=(char*)malloc((strlen(tmpUrl->value)+strlen(targetName)+2)*sizeof(char)); |
---|
| 513 | sprintf(targetUrl,"%s/%s",tmpUrl->value,targetName); |
---|
[851] | 514 | free(targetName); |
---|
[839] | 515 | addToMap(input->content,"generated_file",targetPath); |
---|
[854] | 516 | addToMap(input->content,"storage",targetPath); |
---|
[862] | 517 | addToMap(input->content,"generated_url",targetUrl); |
---|
| 518 | free(targetUrl); |
---|
[854] | 519 | if(strcasecmp(input->name,"wms_link")!=0&& |
---|
| 520 | strcasecmp(input->name,"wcs_link")!=0 && |
---|
| 521 | strcasecmp(input->name,"wfs_link")!=0){ |
---|
| 522 | parameters_cnt+=1; |
---|
| 523 | if(parameters_cnt==1) |
---|
| 524 | parameters=(char**)malloc(parameters_cnt*sizeof(char*)); |
---|
| 525 | else |
---|
| 526 | parameters=(char**)realloc(parameters,parameters_cnt*sizeof(char*)); |
---|
| 527 | // We should verify if any optional tag for output is required |
---|
| 528 | // (i.e. -out output.tiff *int8*), meaning that we should search |
---|
| 529 | // for a corresponding inputs name. |
---|
| 530 | map* inValue=getMapFromMaps(*real_inputs,input->name,"value"); |
---|
| 531 | if(inValue!=NULL){ |
---|
| 532 | parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+strlen(inValue->value)+4)*sizeof(char)); |
---|
| 533 | sprintf(parameters[parameters_cnt-1],"-%s %s %s",input->name,targetPath,inValue->value); |
---|
| 534 | }else{ |
---|
| 535 | parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+3)*sizeof(char)); |
---|
| 536 | sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,targetPath); |
---|
| 537 | } |
---|
[839] | 538 | } |
---|
| 539 | free(targetPath); |
---|
[822] | 540 | } |
---|
| 541 | input=input->next; |
---|
| 542 | } |
---|
| 543 | // Produce the SBATCH File locally |
---|
[839] | 544 | char *scriptPath=(char*)malloc((strlen(s->name)+strlen(tmpPath->value)+strlen(uuid->value)+10)*sizeof(char)); |
---|
[822] | 545 | sprintf(scriptPath,"%s/zoo_%s_%s.sh",tmpPath->value,s->name,uuid->value); |
---|
[839] | 546 | setMapInMaps(*main_conf,"lenv","local_script",scriptPath); |
---|
[860] | 547 | #ifdef HPC_DEBUG |
---|
[839] | 548 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 549 | fflush(stderr); |
---|
[860] | 550 | #endif |
---|
[839] | 551 | invokeCallback(m,inputs,NULL,3,0); |
---|
[860] | 552 | #ifdef HPC_DEBUG |
---|
[839] | 553 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 554 | fflush(stderr); |
---|
[860] | 555 | #endif |
---|
[822] | 556 | FILE* scriptFile=fopen(scriptPath,"w+"); |
---|
[854] | 557 | map* headerMap=getMapFromMaps(*main_conf,configurationId,"jobscript_header"); |
---|
[822] | 558 | if(headerMap!=NULL){ |
---|
| 559 | // Use the header file if defined in the HPC section of the main.cfg file |
---|
| 560 | struct stat f_status; |
---|
| 561 | int s=stat(headerMap->value, &f_status); |
---|
| 562 | if(s==0){ |
---|
| 563 | char* fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1)); |
---|
| 564 | FILE* f=fopen(headerMap->value,"rb"); |
---|
| 565 | fread(fcontent,f_status.st_size,1,f); |
---|
| 566 | int fsize=f_status.st_size; |
---|
| 567 | fcontent[fsize]=0; |
---|
| 568 | fclose(f); |
---|
[839] | 569 | fprintf(scriptFile,"%s\n### --- ZOO-Service HEADER end --- ###\n\n",fcontent); |
---|
[822] | 570 | free(fcontent); |
---|
| 571 | }else |
---|
| 572 | fprintf(scriptFile,"#!/bin/bash\n\n### *** Default ZOO-Service HEADER (no header found) *** ###\n\n"); |
---|
| 573 | }else |
---|
| 574 | fprintf(scriptFile,"#!/bin/bash\n\n### *** Default ZOO-Service HEADER *** ###\n\n"); |
---|
[854] | 575 | maps* hpc_opts=getMaps(*main_conf,configurationId); |
---|
[839] | 576 | if(hpc_opts!=NULL){ |
---|
| 577 | map* hpc_opts_content=hpc_opts->content; |
---|
| 578 | while(hpc_opts_content!=NULL){ |
---|
[854] | 579 | if(strncasecmp(hpc_opts_content->name,"sbatch_options_",15)==0) |
---|
| 580 | fprintf(scriptFile,"#SBATCH --%s=%s\n",strstr(hpc_opts_content->name,"sbatch_options_")+15,hpc_opts_content->value); |
---|
[839] | 581 | hpc_opts_content=hpc_opts_content->next; |
---|
| 582 | } |
---|
[822] | 583 | } |
---|
| 584 | fprintf(scriptFile,"#SBATCH --job-name=ZOO-Project_%s_%s\n\n",uuid->value,s->name); |
---|
| 585 | map* mods=getMap(s->content,"hpcModules"); |
---|
| 586 | if(mods!=NULL) |
---|
| 587 | fprintf(scriptFile,"#SBATCH --export=MODULES=%s\n",mods->value); |
---|
[839] | 588 | |
---|
[854] | 589 | map* bodyMap=getMapFromMaps(*main_conf,configurationId,"jobscript_body"); |
---|
[839] | 590 | if(bodyMap!=NULL){ |
---|
| 591 | // Use the header file if defined in the HPC section of the main.cfg file |
---|
| 592 | struct stat f_status; |
---|
| 593 | int s=stat(bodyMap->value, &f_status); |
---|
| 594 | if(s==0){ |
---|
| 595 | char* fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1)); |
---|
| 596 | FILE* f=fopen(bodyMap->value,"rb"); |
---|
| 597 | fread(fcontent,f_status.st_size,1,f); |
---|
| 598 | int fsize=f_status.st_size; |
---|
| 599 | fcontent[fsize]=0; |
---|
| 600 | fclose(f); |
---|
| 601 | fprintf(scriptFile,"%s\n### --- ZOO-Service BODY end --- ###\n\n",fcontent); |
---|
| 602 | free(fcontent); |
---|
| 603 | }else |
---|
[860] | 604 | fprintf(scriptFile,"\n### *** Default ZOO-Service BODY (no body found) *** ###\n\n"); |
---|
[839] | 605 | }else |
---|
[860] | 606 | fprintf(scriptFile,"\n### *** Default ZOO-Service BODY *** ###\n\n"); |
---|
[839] | 607 | |
---|
[822] | 608 | map* sp=getMap(s->content,"serviceProvider"); |
---|
[839] | 609 | |
---|
[822] | 610 | // Require to produce the command line to be executed |
---|
| 611 | fprintf(scriptFile,"\n\necho \"Job started at: $(date)\"\n"); |
---|
| 612 | fprintf(scriptFile,"echo \"Running service: [%s]\"\n",sp->value); |
---|
| 613 | fprintf(scriptFile,"%s ",sp->value); |
---|
| 614 | for(int i=0;i<parameters_cnt;i++){ |
---|
| 615 | fprintf(scriptFile," %s",parameters[i]); |
---|
| 616 | } |
---|
[839] | 617 | for(int i=parameters_cnt-1;i>=0;i--){ |
---|
| 618 | free(parameters[i]); |
---|
| 619 | } |
---|
[822] | 620 | free(parameters); |
---|
| 621 | fprintf(scriptFile,"\n"); |
---|
| 622 | fprintf(scriptFile,"echo \"Job finished at: $(date)\"\n"); |
---|
| 623 | fflush(scriptFile); |
---|
| 624 | fclose(scriptFile); |
---|
[860] | 625 | #ifdef HPC_DEBUG |
---|
[839] | 626 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
[860] | 627 | #endif |
---|
[839] | 628 | invokeCallback(m,inputs,NULL,3,1); |
---|
[860] | 629 | #ifdef HPC_DEBUG |
---|
[839] | 630 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
[860] | 631 | #endif |
---|
[822] | 632 | |
---|
| 633 | // Upload the SBATCH File to the remote host |
---|
[860] | 634 | #ifdef HPC_DEBUG |
---|
[839] | 635 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
[860] | 636 | #endif |
---|
[839] | 637 | invokeCallback(m,inputs,NULL,4,0); |
---|
[860] | 638 | #ifdef HPC_DEBUG |
---|
[839] | 639 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
[860] | 640 | #endif |
---|
[854] | 641 | targetPathMap=getMapFromMaps(*main_conf,configurationId,"remote_work_path"); |
---|
[822] | 642 | if(targetPathMap==NULL){ |
---|
[854] | 643 | setMapInMaps(*main_conf,"lenv","message",_("There is no remote_work_path defined in your section!")); |
---|
| 644 | setMapInMaps(*main_conf,"lenv","status","failed"); |
---|
[862] | 645 | errorException (*main_conf, _("There is no remote_work_path defined in your section!"), |
---|
[854] | 646 | "InternalError", NULL); |
---|
[860] | 647 | #ifdef HPC_DEBUG |
---|
[854] | 648 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 649 | fflush(stderr); |
---|
[860] | 650 | #endif |
---|
[862] | 651 | invokeCallback(*main_conf,NULL,NULL,7,0); |
---|
[860] | 652 | #ifdef HPC_DEBUG |
---|
[854] | 653 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 654 | fflush(stderr); |
---|
[860] | 655 | #endif |
---|
[822] | 656 | return SERVICE_FAILED; |
---|
| 657 | } |
---|
| 658 | char* targetName=strrchr(scriptPath,'/'); |
---|
| 659 | char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char)); |
---|
| 660 | sprintf(targetPath,"%s/%s",targetPathMap->value,targetName); |
---|
[839] | 661 | setMapInMaps(*main_conf,"lenv","remote_script",targetPath); |
---|
[822] | 662 | SSHCON *test=ssh_connect(*main_conf); |
---|
[860] | 663 | int copy0=ssh_copy(*main_conf,scriptPath,targetPath,ssh_get_cnt(*main_conf)); |
---|
[854] | 664 | unlink(scriptPath); |
---|
| 665 | free(scriptPath); |
---|
[860] | 666 | if(copy0!=true){ |
---|
[862] | 667 | setMapInMaps(*main_conf,"lenv","message",_("Unable to upload the script")); |
---|
| 668 | invokeCallback(*main_conf,NULL,NULL,7,0); |
---|
| 669 | errorException(*main_conf,_("Unable to upload the script"),"NoApplicableCode",NULL); |
---|
[860] | 670 | return -1; |
---|
| 671 | } |
---|
[822] | 672 | // Execute the SBATCH script remotely |
---|
[854] | 673 | addReadLocks(main_conf); |
---|
| 674 | map* subStr=getMapFromMaps(*main_conf,configurationId,"sbatch_substr"); |
---|
[822] | 675 | char *command=(char*)malloc((strlen(targetPath)+strlen(targetPathMap->value)+strlen(subStr->value)+strlen(uuid->value)+137)*sizeof(char)); |
---|
| 676 | sprintf(command,"sbatch %s 2> %s/error_%s.log | sed \"s:%s::g\"",targetPath,targetPathMap->value,uuid->value,subStr->value); |
---|
[860] | 677 | if(ssh_exec(*main_conf,command,ssh_get_cnt(m))<=0){ |
---|
[822] | 678 | // The sbatch command has failed! |
---|
| 679 | // Download the error log file from the HPC server |
---|
| 680 | char tmpS[1024]; |
---|
| 681 | free(command); |
---|
[839] | 682 | command=(char*)malloc((strlen(targetPathMap->value)+strlen(uuid->value)+11)*sizeof(char)); |
---|
[822] | 683 | sprintf(command,"%s/error_%s.log",targetPathMap->value,uuid->value); |
---|
| 684 | targetName=strrchr(command,'/'); |
---|
| 685 | free(targetPath); |
---|
| 686 | targetPath=(char*)malloc((strlen(tmpPath->value)+strlen(targetName)+2)*sizeof(char)); |
---|
| 687 | sprintf(targetPath,"%s/%s",tmpPath->value,targetName); |
---|
[839] | 688 | if(ssh_fetch(*main_conf,targetPath,command,ssh_get_cnt(m))==0){ |
---|
[822] | 689 | struct stat f_status; |
---|
| 690 | int ts=stat(targetPath, &f_status); |
---|
| 691 | if(ts==0) { |
---|
| 692 | char* fcontent = NULL; |
---|
| 693 | fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1)); |
---|
| 694 | FILE* f=fopen(targetPath,"rb"); |
---|
| 695 | fread(fcontent,f_status.st_size,1,f); |
---|
| 696 | int fsize=f_status.st_size; |
---|
| 697 | fcontent[fsize]=0; |
---|
| 698 | fclose(f); |
---|
| 699 | setMapInMaps(*main_conf,"lenv","message",fcontent); |
---|
| 700 | free(fcontent); |
---|
| 701 | }else |
---|
| 702 | setMapInMaps(*main_conf,"lenv","message",_("No message provided")); |
---|
| 703 | }else |
---|
| 704 | setMapInMaps(*main_conf,"lenv","message",_("Unable to fetch the remote error log file")); |
---|
[860] | 705 | tmpPath=getMapFromMaps(m,"lenv","message"); |
---|
| 706 | #ifdef HPC_DEBUG |
---|
[850] | 707 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 708 | fflush(stderr); |
---|
[860] | 709 | #endif |
---|
[862] | 710 | invokeCallback(*main_conf,NULL,NULL,7,0); |
---|
[860] | 711 | #ifdef HPC_DEBUG |
---|
[850] | 712 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 713 | fflush(stderr); |
---|
[860] | 714 | #endif |
---|
| 715 | sprintf(tmpS, "Cannot execute the HPC ZOO-Service %s using %s: %s", s->name, configurationId, tmpPath->value); |
---|
[862] | 716 | errorException(*main_conf,tmpS,"NoApplicableCode",NULL); |
---|
[822] | 717 | free(command); |
---|
| 718 | free(targetPath); |
---|
| 719 | ssh_close(*main_conf); |
---|
[860] | 720 | removeReadLocks(main_conf); |
---|
[822] | 721 | return -1; |
---|
| 722 | } |
---|
[851] | 723 | free(targetPath); |
---|
[860] | 724 | #ifdef HPC_DEBUG |
---|
[839] | 725 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 726 | fflush(stderr); |
---|
[860] | 727 | #endif |
---|
[839] | 728 | invokeCallback(m,NULL,NULL,4,1); |
---|
[860] | 729 | #ifdef HPC_DEBUG |
---|
[839] | 730 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 731 | fflush(stderr); |
---|
[860] | 732 | #endif |
---|
[839] | 733 | free(command); |
---|
[860] | 734 | #ifdef HPC_DEBUG |
---|
[854] | 735 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 736 | fflush(stderr); |
---|
[860] | 737 | #endif |
---|
[822] | 738 | |
---|
| 739 | struct sockaddr_un addr; |
---|
| 740 | memset(&addr, 0, sizeof(addr)); |
---|
| 741 | addr.sun_family = AF_UNIX; |
---|
| 742 | int rc, cl, fd = socket(AF_UNIX, SOCK_STREAM, 0); |
---|
[839] | 743 | char *sname=(char*)malloc((strlen(tmpPath->value)+strlen(uuid->value)+20)); |
---|
[822] | 744 | sprintf(sname,"%s/.wait_socket_%s.sock",tmpPath->value,uuid->value); |
---|
| 745 | strncpy(addr.sun_path, sname, sizeof(addr.sun_path)-1); |
---|
[839] | 746 | |
---|
[822] | 747 | if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) { |
---|
| 748 | perror("bind error"); |
---|
[862] | 749 | setMapInMaps(*main_conf,"lenv","message",_("Unable to bind socket!")); |
---|
| 750 | errorException (*main_conf, _("Unable to bind socket!"), |
---|
[854] | 751 | "InternalError", NULL); |
---|
[860] | 752 | #ifdef HPC_DEBUG |
---|
[854] | 753 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 754 | fflush(stderr); |
---|
[860] | 755 | #endif |
---|
[862] | 756 | invokeCallback(*main_conf,NULL,NULL,7,0); |
---|
[860] | 757 | removeReadLocks(main_conf); |
---|
| 758 | #ifdef HPC_DEBUG |
---|
[854] | 759 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 760 | fflush(stderr); |
---|
[860] | 761 | #endif |
---|
[822] | 762 | return -1; |
---|
| 763 | } |
---|
[860] | 764 | #ifdef HPC_DEBUG |
---|
[854] | 765 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 766 | fflush(stderr); |
---|
[860] | 767 | #endif |
---|
[822] | 768 | if (listen(fd, 5) == -1) { |
---|
[839] | 769 | setMapInMaps(*main_conf,"lenv","message",_("Listen error")); |
---|
[862] | 770 | errorException (*main_conf, _("Listen error"), |
---|
[854] | 771 | "InternalError", NULL); |
---|
[860] | 772 | #ifdef HPC_DEBUG |
---|
[854] | 773 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 774 | fflush(stderr); |
---|
[860] | 775 | #endif |
---|
[862] | 776 | invokeCallback(*main_conf,NULL,NULL,7,0); |
---|
[860] | 777 | removeReadLocks(main_conf); |
---|
| 778 | #ifdef HPC_DEBUG |
---|
[854] | 779 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 780 | fflush(stderr); |
---|
[860] | 781 | #endif |
---|
[822] | 782 | return -1; |
---|
| 783 | } |
---|
[839] | 784 | if ( (cl = accept(fd, NULL, NULL)) == -1) { |
---|
| 785 | setMapInMaps(*main_conf,"lenv","message",_("Accept error")); |
---|
[862] | 786 | errorException (*main_conf, _("Accept error"), |
---|
[854] | 787 | "InternalError", NULL); |
---|
[860] | 788 | #ifdef HPC_DEBUG |
---|
[854] | 789 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 790 | fflush(stderr); |
---|
[860] | 791 | #endif |
---|
[862] | 792 | invokeCallback(*main_conf,NULL,NULL,7,0); |
---|
[860] | 793 | removeReadLocks(main_conf); |
---|
| 794 | #ifdef HPC_DEBUG |
---|
[854] | 795 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 796 | fflush(stderr); |
---|
[860] | 797 | #endif |
---|
[822] | 798 | return -1; |
---|
[839] | 799 | }else{ |
---|
[860] | 800 | #ifdef HPC_DEBUG |
---|
[854] | 801 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 802 | fflush(stderr); |
---|
[860] | 803 | #endif |
---|
[839] | 804 | int hasPassed=-1; |
---|
| 805 | char buf[11]; |
---|
| 806 | memset(&buf,0,11); |
---|
| 807 | while ( (rc=read(cl,buf,10)) ) { |
---|
| 808 | if(rc==0){ |
---|
| 809 | setMapInMaps(*main_conf,"lenv","message",_("Read closed")); |
---|
[862] | 810 | errorException (*main_conf, _("Read closed"), |
---|
[854] | 811 | "InternalError", NULL); |
---|
[860] | 812 | #ifdef HPC_DEBUG |
---|
[854] | 813 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 814 | fflush(stderr); |
---|
[860] | 815 | #endif |
---|
[862] | 816 | invokeCallback(*main_conf,NULL,NULL,7,0); |
---|
[860] | 817 | removeReadLocks(main_conf); |
---|
| 818 | #ifdef HPC_DEBUG |
---|
[854] | 819 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 820 | fflush(stderr); |
---|
[860] | 821 | #endif |
---|
[822] | 822 | return -1; |
---|
[839] | 823 | }else{ |
---|
| 824 | if(rc<0){ |
---|
| 825 | setMapInMaps(*main_conf,"lenv","message",_("Read error")); |
---|
[862] | 826 | errorException (*main_conf, _("Read error"), |
---|
[854] | 827 | "InternalError", NULL); |
---|
[860] | 828 | #ifdef HPC_DEBUG |
---|
[854] | 829 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 830 | fflush(stderr); |
---|
[860] | 831 | #endif |
---|
[862] | 832 | invokeCallback(*main_conf,NULL,NULL,7,0); |
---|
[860] | 833 | removeReadLocks(main_conf); |
---|
| 834 | #ifdef HPC_DEBUG |
---|
[854] | 835 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 836 | fflush(stderr); |
---|
[860] | 837 | #endif |
---|
[822] | 838 | return -1; |
---|
| 839 | } |
---|
[839] | 840 | } |
---|
| 841 | hasPassed=1; |
---|
| 842 | res=atoi(buf); |
---|
| 843 | unlink(sname); |
---|
[854] | 844 | free(sname); |
---|
| 845 | removeReadLocks(main_conf); |
---|
| 846 | |
---|
[839] | 847 | if(res==3){ |
---|
[860] | 848 | #ifdef HPC_DEBUG |
---|
[839] | 849 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 850 | fflush(stderr); |
---|
[860] | 851 | #endif |
---|
[839] | 852 | invokeCallback(m,NULL,outputs,5,0); |
---|
[860] | 853 | #ifdef HPC_DEBUG |
---|
[839] | 854 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 855 | fflush(stderr); |
---|
[860] | 856 | #endif |
---|
| 857 | |
---|
| 858 | // Read informations provided by FinalizeHPC as a configuration file |
---|
| 859 | // then, remove the file. |
---|
| 860 | map* jobid=getMapFromMaps(*main_conf,"lenv","usid"); |
---|
| 861 | map* tmpPath=getMapFromMaps(*main_conf,"main","tmpPath"); |
---|
| 862 | char *filePath=(char*)malloc((strlen(tmpPath->value)+strlen(jobid->value)+15)*sizeof(char)); |
---|
| 863 | sprintf(filePath,"%s/exec_status_%s",tmpPath->value,jobid->value); |
---|
| 864 | maps* m = (maps *) malloc (MAPS_SIZE); |
---|
| 865 | m->child=NULL; |
---|
| 866 | m->next=NULL; |
---|
| 867 | int saved_stdout = dup (fileno (stdout)); |
---|
| 868 | dup2 (fileno (stderr), fileno (stdout)); |
---|
| 869 | conf_read(filePath,m); |
---|
| 870 | fflush(stdout); |
---|
| 871 | dup2 (saved_stdout, fileno (stdout)); |
---|
| 872 | close(saved_stdout); |
---|
| 873 | unlink(filePath); |
---|
| 874 | free(filePath); |
---|
| 875 | addMapsToMaps(main_conf,m); |
---|
| 876 | freeMaps(&m); |
---|
| 877 | free(m); |
---|
| 878 | |
---|
[839] | 879 | input=*real_outputs; |
---|
| 880 | while(input!=NULL){ |
---|
| 881 | if(input->child==NULL){ |
---|
| 882 | map* generatedFile=getMap(input->content,"generated_file"); |
---|
| 883 | if(generatedFile!=NULL){ |
---|
| 884 | char* filename=strrchr(generatedFile->value,'/'); |
---|
| 885 | char* targetPath=(char*)malloc((strlen(tmpPath->value)+strlen(filename)+2)*sizeof(char)); |
---|
| 886 | sprintf(targetPath,"%s/%s",tmpPath->value,filename); |
---|
| 887 | test=ssh_connect(*main_conf); |
---|
| 888 | if(ssh_fetch(*main_conf,targetPath,generatedFile->value,ssh_get_cnt(m))==0){ |
---|
| 889 | setMapInMaps(*real_outputs,input->name,"generated_file",targetPath); |
---|
| 890 | free(targetPath); |
---|
| 891 | }else{ |
---|
[862] | 892 | map* hpcStdErr=getMapFromMaps(*main_conf,"henv","StdErr"); |
---|
| 893 | if(hpcStdErr!=NULL && ssh_fetch(*main_conf,targetPath,hpcStdErr->value,ssh_get_cnt(m))==0){ |
---|
| 894 | struct stat f_status; |
---|
| 895 | int ts=stat(targetPath, &f_status); |
---|
| 896 | if(ts==0) { |
---|
| 897 | char* fcontent = NULL; |
---|
| 898 | fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1)); |
---|
| 899 | FILE* f=fopen(targetPath,"rb"); |
---|
| 900 | fread(fcontent,f_status.st_size,1,f); |
---|
| 901 | int fsize=f_status.st_size; |
---|
| 902 | fcontent[fsize]=0; |
---|
| 903 | fclose(f); |
---|
| 904 | setMapInMaps(*main_conf,"lenv","message",fcontent); |
---|
| 905 | free(fcontent); |
---|
| 906 | }else{ |
---|
| 907 | char *tmpStr=(char*)malloc((strlen(filename)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char)); |
---|
| 908 | sprintf(tmpStr,_("Unable to fetch the remote file for %s"),filename); |
---|
| 909 | setMapInMaps(*main_conf,"lenv","message",tmpStr); |
---|
| 910 | free(tmpStr); |
---|
| 911 | } |
---|
| 912 | }else{ |
---|
| 913 | char *tmpStr=(char*)malloc((strlen(filename)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char)); |
---|
| 914 | sprintf(tmpStr,_("Unable to fetch the remote file for %s"),filename); |
---|
| 915 | setMapInMaps(*main_conf,"lenv","message",tmpStr); |
---|
| 916 | free(tmpStr); |
---|
| 917 | } |
---|
| 918 | invokeCallback(*main_conf,NULL,NULL,7,0); |
---|
[839] | 919 | return SERVICE_FAILED; |
---|
[822] | 920 | } |
---|
[839] | 921 | } |
---|
| 922 | }else{ |
---|
| 923 | map* generatedFile=getMap(input->content,"generated_file"); |
---|
[862] | 924 | map* generatedUrl=getMap(input->content,"generated_url"); |
---|
[839] | 925 | if(generatedFile!=NULL){ |
---|
| 926 | char* filename=strrchr(generatedFile->value,'/'); |
---|
| 927 | char* targetPath=(char*)malloc((strlen(tmpPath->value)+strlen(filename)+2)*sizeof(char)); |
---|
| 928 | sprintf(targetPath,"%s/%s",tmpPath->value,filename); |
---|
| 929 | test=ssh_connect(*main_conf); |
---|
| 930 | if(ssh_fetch(*main_conf,targetPath,generatedFile->value,ssh_get_cnt(m))==0){ |
---|
| 931 | maps* tmp=getMaps(*real_outputs,input->name); |
---|
[854] | 932 | char serviceName[9]; |
---|
[839] | 933 | freeMap(&tmp->content); |
---|
| 934 | free(tmp->content); |
---|
| 935 | tmp->content=NULL; |
---|
| 936 | maps* output=getMaps(*real_outputs,input->name); |
---|
| 937 | setMapInMaps(output->child,"download_link","generated_file",targetPath); |
---|
[862] | 938 | setMapInMaps(output->child,"download_link","generated_url",generatedUrl->value); |
---|
[854] | 939 | setMapInMaps(output->child,"download_link","storage",targetPath); |
---|
[839] | 940 | setMapInMaps(output->child,"download_link","useMapserver","false"); |
---|
[854] | 941 | setMapInMaps(output->child,"download_link","replicateStorageNext","true"); |
---|
| 942 | setMapInMaps(output->child,"download_link","asReference","true"); |
---|
| 943 | setMapInMaps(output->child,"download_link","inRequest","true"); |
---|
[851] | 944 | setMapInMaps(output->child,"wms_link","generated_file",targetPath); |
---|
[854] | 945 | setMapInMaps(output->child,"wms_link","storage",targetPath); |
---|
[851] | 946 | setMapInMaps(output->child,"wms_link","useMapserver","true"); |
---|
| 947 | setMapInMaps(output->child,"wms_link","msOgc","WMS"); |
---|
| 948 | setMapInMaps(output->child,"wms_link","requestedMimeType","image/png"); |
---|
[854] | 949 | setMapInMaps(output->child,"wms_link","asReference","true"); |
---|
| 950 | if(getMaps(output->child,"wcs_link")!=NULL){ |
---|
| 951 | sprintf(serviceName,"wcs_link"); |
---|
[862] | 952 | setMapInMaps(output->child,"wcs_link","msOgc","WCS"); |
---|
| 953 | }else{ |
---|
[854] | 954 | sprintf(serviceName,"wfs_link"); |
---|
[862] | 955 | setMapInMaps(output->child,"wfs_link","msOgc","WFS"); |
---|
| 956 | } |
---|
[854] | 957 | setMapInMaps(output->child,serviceName,"storage",targetPath); |
---|
| 958 | setMapInMaps(output->child,serviceName,"generated_file",targetPath); |
---|
| 959 | setMapInMaps(output->child,serviceName,"useMapserver","true"); |
---|
| 960 | setMapInMaps(output->child,serviceName,"asReference","true"); |
---|
| 961 | }else{ |
---|
[860] | 962 | map* hpcStdErr=getMapFromMaps(*main_conf,"henv","StdErr"); |
---|
| 963 | if(hpcStdErr!=NULL && ssh_fetch(*main_conf,targetPath,hpcStdErr->value,ssh_get_cnt(m))==0){ |
---|
| 964 | struct stat f_status; |
---|
| 965 | int ts=stat(targetPath, &f_status); |
---|
| 966 | if(ts==0) { |
---|
| 967 | char* fcontent = NULL; |
---|
| 968 | fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1)); |
---|
| 969 | FILE* f=fopen(targetPath,"rb"); |
---|
| 970 | fread(fcontent,f_status.st_size,1,f); |
---|
| 971 | int fsize=f_status.st_size; |
---|
| 972 | fcontent[fsize]=0; |
---|
| 973 | fclose(f); |
---|
| 974 | setMapInMaps(*main_conf,"lenv","message",fcontent); |
---|
| 975 | free(fcontent); |
---|
| 976 | }else{ |
---|
| 977 | char *tmpStr=(char*)malloc((strlen(filename)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char)); |
---|
| 978 | sprintf(tmpStr,_("Unable to fetch the remote file for %s"),filename); |
---|
| 979 | setMapInMaps(*main_conf,"lenv","message",tmpStr); |
---|
| 980 | free(tmpStr); |
---|
| 981 | } |
---|
| 982 | }else{ |
---|
| 983 | char *tmpStr=(char*)malloc((strlen(filename)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char)); |
---|
| 984 | sprintf(tmpStr,_("Unable to fetch the remote file for %s"),filename); |
---|
| 985 | setMapInMaps(*main_conf,"lenv","message",tmpStr); |
---|
| 986 | free(tmpStr); |
---|
| 987 | } |
---|
| 988 | invokeCallback(*main_conf,NULL,NULL,7,0); |
---|
[854] | 989 | return SERVICE_FAILED; |
---|
[839] | 990 | } |
---|
[851] | 991 | free(targetPath); |
---|
[839] | 992 | } |
---|
[822] | 993 | } |
---|
[839] | 994 | input=input->next; |
---|
[822] | 995 | } |
---|
[854] | 996 | |
---|
| 997 | }else{ |
---|
| 998 | // Try to access remotely to the log file and return a more relevant error message |
---|
[862] | 999 | setMapInMaps(*main_conf,"lenv","message",_("HPC Execution failed!")); |
---|
| 1000 | errorException (*main_conf, _("HPC Execution failed!"), |
---|
[854] | 1001 | "InternalError", NULL); |
---|
[860] | 1002 | #ifdef HPC_DEBUG |
---|
[854] | 1003 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 1004 | fflush(stderr); |
---|
[860] | 1005 | #endif |
---|
[862] | 1006 | invokeCallback(*main_conf,NULL,NULL,7,0); |
---|
[860] | 1007 | #ifdef HPC_DEBUG |
---|
[854] | 1008 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 1009 | fflush(stderr); |
---|
[860] | 1010 | #endif |
---|
[822] | 1011 | } |
---|
| 1012 | } |
---|
[839] | 1013 | if(hasPassed<0){ |
---|
| 1014 | perror("Failed to read"); |
---|
| 1015 | setMapInMaps(*main_conf,"lenv","message",_("Unable to parse the value returned by remote execution")); |
---|
[862] | 1016 | errorException (*main_conf, _("Unable to parse the value returned by remote execution"), |
---|
[854] | 1017 | "InternalError", NULL); |
---|
[860] | 1018 | #ifdef HPC_DEBUG |
---|
[854] | 1019 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 1020 | fflush(stderr); |
---|
[860] | 1021 | #endif |
---|
[862] | 1022 | invokeCallback(*main_conf,NULL,NULL,7,0); |
---|
[860] | 1023 | #ifdef HPC_DEBUG |
---|
[854] | 1024 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 1025 | fflush(stderr); |
---|
[860] | 1026 | #endif |
---|
[839] | 1027 | return SERVICE_FAILED; |
---|
| 1028 | } |
---|
| 1029 | } |
---|
[860] | 1030 | #ifdef HPC_DEBUG |
---|
[850] | 1031 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 1032 | fflush(stderr); |
---|
[860] | 1033 | #endif |
---|
[822] | 1034 | ssh_close(*main_conf); |
---|
[860] | 1035 | #ifdef HPC_DEBUG |
---|
[850] | 1036 | fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__); |
---|
| 1037 | fflush(stderr); |
---|
[860] | 1038 | #endif |
---|
[822] | 1039 | return res; |
---|
| 1040 | } |
---|