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