1 | /** |
---|
2 | * Author : Gérald FENOY |
---|
3 | * |
---|
4 | * Copyright 2017 GeoLabs SARL. All rights reserved. |
---|
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.h" |
---|
31 | #include "service_internal.h" |
---|
32 | #include "sshapi.h" |
---|
33 | #include "server_internal.h" |
---|
34 | |
---|
35 | #include <sys/socket.h> |
---|
36 | #include <sys/un.h> |
---|
37 | |
---|
38 | #include <libxml/tree.h> |
---|
39 | #include <libxml/parser.h> |
---|
40 | #include <libxml/xpath.h> |
---|
41 | #include <libxml/xpathInternals.h> |
---|
42 | |
---|
43 | #include <libxslt/xslt.h> |
---|
44 | #include <libxslt/xsltInternals.h> |
---|
45 | #include <libxslt/transform.h> |
---|
46 | #include <libxslt/xsltutils.h> |
---|
47 | |
---|
48 | #include <dirent.h> |
---|
49 | extern "C" { |
---|
50 | |
---|
51 | /** |
---|
52 | * FinalizeHPC ZOO Service : |
---|
53 | * This service is used to inform a ZOO-Kernel waiting for the end of the |
---|
54 | * execution of a HPC service |
---|
55 | */ |
---|
56 | ZOO_DLL_EXPORT int FinalizeHPC(maps*& conf,maps*& inputs,maps*& outputs){ |
---|
57 | // Retrieve the jobid corresponding to the identifier generated by SLURM |
---|
58 | // by reading the file generated when running the SBATCH file |
---|
59 | map* jobid=getMapFromMaps(inputs,"jobid","value"); |
---|
60 | struct sockaddr_un addr; |
---|
61 | char buf[100]="3"; |
---|
62 | int fd,rc=NULL; |
---|
63 | int i=0; |
---|
64 | map* usid=getMapFromMaps(conf,"lenv","usid"); |
---|
65 | map* tmpPath=getMapFromMaps(conf,"main","tmpPath"); |
---|
66 | |
---|
67 | char *flenv = |
---|
68 | (char *) malloc ((strlen (tmpPath->value) + |
---|
69 | strlen (jobid->value) + 12) * sizeof (char)); |
---|
70 | sprintf (flenv, "%s/%s_lenv.cfg", tmpPath->value, jobid->value); |
---|
71 | maps* m = (maps *) malloc (MAPS_SIZE); |
---|
72 | m->child=NULL; |
---|
73 | m->next=NULL; |
---|
74 | map* configId=NULL; |
---|
75 | |
---|
76 | |
---|
77 | if(conf_read(flenv, m) != 2){ |
---|
78 | configId=getMapFromMaps(m,"lenv","configId"); |
---|
79 | setMapInMaps(conf,"lenv","configId",configId->value); |
---|
80 | }else{ |
---|
81 | setMapInMaps(conf,"lenv","message",_("Unable to read the lenv section file of the requested jobid")); |
---|
82 | return SERVICE_FAILED; |
---|
83 | } |
---|
84 | unlink(flenv); |
---|
85 | free(flenv); |
---|
86 | |
---|
87 | SSHCON *test=ssh_connect(conf); |
---|
88 | if(test==NULL){ |
---|
89 | setMapInMaps(conf,"lenv","message",_("Unable to connect using through ssh.")); |
---|
90 | return SERVICE_FAILED; |
---|
91 | } |
---|
92 | |
---|
93 | char *logPath=(char*)malloc((strlen(tmpPath->value)+strlen(jobid->value)+12)*sizeof(char)); |
---|
94 | sprintf(logPath,"%s/exec_out_%s",tmpPath->value,jobid->value); |
---|
95 | struct stat f_status; |
---|
96 | int ts=stat(logPath, &f_status); |
---|
97 | char* fcontent = NULL; |
---|
98 | if(ts==0) { |
---|
99 | fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1)); |
---|
100 | FILE* f=fopen(logPath,"rb"); |
---|
101 | fread(fcontent,f_status.st_size,1,f); |
---|
102 | int fsize=f_status.st_size; |
---|
103 | fcontent[fsize]=0; |
---|
104 | fclose(f); |
---|
105 | }else{ |
---|
106 | setMapInMaps(conf,"lenv","message",_("No service with this jobid can be found")); |
---|
107 | return SERVICE_FAILED; |
---|
108 | } |
---|
109 | free(logPath); |
---|
110 | // Run scontrol to check if the service execution ended. |
---|
111 | // Store all the informations returned by scontrol command as a cfg file to |
---|
112 | // be parsed back by the ZOO-Kernel waiting for the execution of the remote |
---|
113 | // service |
---|
114 | maps* tmpMaps=createMaps("henv"); |
---|
115 | char* command=(char*)malloc((126)*sizeof(char)); |
---|
116 | sprintf(command,"scontrol show jobid | grep -A24 JobId=%s",fcontent); |
---|
117 | if(ssh_exec(conf,command,ssh_get_cnt(conf))==0){ |
---|
118 | free(command); |
---|
119 | setMapInMaps(conf,"lenv","message",_("Failed to run scontrol remotely")); |
---|
120 | // TODO: check status in db and if available continue in other case return SERVICE_FAILED |
---|
121 | return SERVICE_FAILED; |
---|
122 | }else{ |
---|
123 | free(command); |
---|
124 | logPath=(char*)malloc((strlen(tmpPath->value)+strlen(usid->value)+11)*sizeof(char)); |
---|
125 | sprintf(logPath,"%s/exec_out_%s",tmpPath->value,usid->value); |
---|
126 | int ts=stat(logPath, &f_status); |
---|
127 | if(ts==0) { |
---|
128 | fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1)); |
---|
129 | FILE* f=fopen(logPath,"rb"); |
---|
130 | fread(fcontent,f_status.st_size,1,f); |
---|
131 | int fsize=f_status.st_size; |
---|
132 | fcontent[fsize]=0; |
---|
133 | fclose(f); |
---|
134 | free(logPath); |
---|
135 | char *token, *saveptr; |
---|
136 | token = strtok_r (fcontent, " ", &saveptr); |
---|
137 | while (token != NULL) |
---|
138 | { |
---|
139 | char *token1, *saveptr1; |
---|
140 | char *tmpToken=strdup(token); |
---|
141 | token1 = strtok_r (tmpToken, "=", &saveptr1); |
---|
142 | int isNext=-1; |
---|
143 | int hasTwoElements=0; |
---|
144 | char *name=NULL; |
---|
145 | while (token1 != NULL) |
---|
146 | { |
---|
147 | if(hasTwoElements==0) |
---|
148 | name=strdup(token1); |
---|
149 | if(hasTwoElements<1) |
---|
150 | hasTwoElements+=1; |
---|
151 | else{ |
---|
152 | char *value=strdup(token1); |
---|
153 | if(value[strlen(value)-1]=='\n') |
---|
154 | value[strlen(value)-1]=0; |
---|
155 | if(strlen(name)>0 && strlen(value)>0){ |
---|
156 | if(tmpMaps->content==NULL) |
---|
157 | tmpMaps->content=createMap(name,value); |
---|
158 | else |
---|
159 | addToMap(tmpMaps->content,name,value); |
---|
160 | free(value); |
---|
161 | } |
---|
162 | free(name); |
---|
163 | hasTwoElements=0; |
---|
164 | } |
---|
165 | token1 = strtok_r (NULL, "=", &saveptr1); |
---|
166 | } |
---|
167 | free(tmpToken); |
---|
168 | token = strtok_r (NULL, " ", &saveptr); |
---|
169 | } |
---|
170 | }else{ |
---|
171 | setMapInMaps(conf,"lenv","message",_("Unable to access the downloaded execution log file")); |
---|
172 | return SERVICE_FAILED; |
---|
173 | } |
---|
174 | } |
---|
175 | logPath=(char*)malloc((strlen(tmpPath->value)+strlen(jobid->value)+15)*sizeof(char)); |
---|
176 | sprintf(logPath,"%s/exec_status_%s",tmpPath->value,jobid->value); |
---|
177 | dumpMapsToFile(tmpMaps,logPath,0); |
---|
178 | char *sname=(char*)malloc((strlen(tmpPath->value)+strlen(jobid->value)+21)); |
---|
179 | sprintf(sname,"%s/.wait_socket_%s.sock",tmpPath->value,jobid->value); |
---|
180 | if ( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { |
---|
181 | perror("socket error"); |
---|
182 | setMapInMaps(conf,"lenv","message",_("Socket error")); |
---|
183 | return SERVICE_FAILED; |
---|
184 | } |
---|
185 | memset(&addr, 0, sizeof(addr)); |
---|
186 | addr.sun_family = AF_UNIX; |
---|
187 | strncpy(addr.sun_path, sname, sizeof(addr.sun_path)-1); |
---|
188 | if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) { |
---|
189 | perror("connect error"); |
---|
190 | setMapInMaps(conf,"lenv","message",_("Unable to connect")); |
---|
191 | return SERVICE_FAILED; |
---|
192 | } |
---|
193 | if (write(fd, "3", 1) != rc) { |
---|
194 | if (rc < 0) { |
---|
195 | perror("write error"); |
---|
196 | setMapInMaps(conf,"lenv","message",_("Unable to announce the successful execution of the HPC service")); |
---|
197 | close(fd); |
---|
198 | return SERVICE_FAILED; |
---|
199 | } |
---|
200 | } |
---|
201 | close(fd); |
---|
202 | setOutputValue(outputs,"Result",(char*)"\"FinalizeHPC run successfully\"",32); |
---|
203 | |
---|
204 | return SERVICE_SUCCEEDED; |
---|
205 | } |
---|
206 | |
---|
207 | } |
---|