1 | /** |
---|
2 | * Author : Gérald FENOY |
---|
3 | * |
---|
4 | * Copyright 2008-2009 GeoLabs SARL. All rights reserved. |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
7 | * of this software and associated documentation files (the "Software"), to deal |
---|
8 | * in the Software without restriction, including without limitation the rights |
---|
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
10 | * copies of the Software, and to permit persons to whom the Software is |
---|
11 | * furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
22 | * THE SOFTWARE. |
---|
23 | */ |
---|
24 | |
---|
25 | |
---|
26 | #include "service.h" |
---|
27 | #include "service_internal.h" |
---|
28 | #include "sshapi.h" |
---|
29 | |
---|
30 | #include <sys/socket.h> |
---|
31 | #include <sys/un.h> |
---|
32 | |
---|
33 | #include <libxml/tree.h> |
---|
34 | #include <libxml/parser.h> |
---|
35 | #include <libxml/xpath.h> |
---|
36 | #include <libxml/xpathInternals.h> |
---|
37 | |
---|
38 | #include <libxslt/xslt.h> |
---|
39 | #include <libxslt/xsltInternals.h> |
---|
40 | #include <libxslt/transform.h> |
---|
41 | #include <libxslt/xsltutils.h> |
---|
42 | |
---|
43 | #include <dirent.h> |
---|
44 | extern "C" { |
---|
45 | |
---|
46 | /** |
---|
47 | * FinalizeHPC ZOO Service : |
---|
48 | * This service is used to inform a ZOO-Kernel waiting for the end of the |
---|
49 | * execution of a HPC service |
---|
50 | */ |
---|
51 | ZOO_DLL_EXPORT int FinalizeHPC(maps*& conf,maps*& inputs,maps*& outputs){ |
---|
52 | // Retrieve the jobid corresponding to the identifier generated by SLURM |
---|
53 | // by reading the file generated when running the SBATCH file |
---|
54 | map* jobid=getMapFromMaps(inputs,"jobid","value"); |
---|
55 | struct sockaddr_un addr; |
---|
56 | char buf[100]="3"; |
---|
57 | int fd,rc=NULL; |
---|
58 | int i=0; |
---|
59 | SSHCON *test=ssh_connect(conf); |
---|
60 | if(test==NULL){ |
---|
61 | return SERVICE_FAILED; |
---|
62 | } |
---|
63 | map* usid=getMapFromMaps(conf,"lenv","usid"); |
---|
64 | map* tmpPath=getMapFromMaps(conf,"main","tmpPath"); |
---|
65 | char *logPath=(char*)malloc((strlen(tmpPath->value)+strlen(jobid->value)+12)*sizeof(char)); |
---|
66 | sprintf(logPath,"%s/exec_out_%s",tmpPath->value,jobid->value); |
---|
67 | struct stat f_status; |
---|
68 | int ts=stat(logPath, &f_status); |
---|
69 | char* fcontent = NULL; |
---|
70 | if(ts==0) { |
---|
71 | fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1)); |
---|
72 | FILE* f=fopen(logPath,"rb"); |
---|
73 | fread(fcontent,f_status.st_size,1,f); |
---|
74 | int fsize=f_status.st_size; |
---|
75 | fcontent[fsize]=0; |
---|
76 | fclose(f); |
---|
77 | }else{ |
---|
78 | setMapInMaps(conf,"lenv","message",_("No service with this jobid can be found")); |
---|
79 | return SERVICE_FAILED; |
---|
80 | } |
---|
81 | free(logPath); |
---|
82 | // Run scontrol to see if the service execution ends |
---|
83 | // Store all the informations returned by scontrol command as a cfg file to |
---|
84 | // be parsed back by the ZOO-Kernel waiting for the execution of the remote |
---|
85 | // service |
---|
86 | maps* tmpMaps=createMaps("henv"); |
---|
87 | char* command=(char*)malloc((126)*sizeof(char)); |
---|
88 | //memset(&command,0,34); |
---|
89 | sprintf(command,"scontrol show jobid | grep -A24 %s",fcontent); |
---|
90 | if(ssh_exec(conf,command,ssh_get_cnt(conf))==0){ |
---|
91 | free(command); |
---|
92 | setMapInMaps(conf,"lenv","message",_("Failed to run scontrol remotely")); |
---|
93 | return SERVICE_FAILED; |
---|
94 | }else{ |
---|
95 | free(command); |
---|
96 | logPath=(char*)malloc((strlen(tmpPath->value)+strlen(usid->value)+11)*sizeof(char)); |
---|
97 | sprintf(logPath,"%s/exec_out_%s",tmpPath->value,usid->value); |
---|
98 | int ts=stat(logPath, &f_status); |
---|
99 | if(ts==0) { |
---|
100 | fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1)); |
---|
101 | FILE* f=fopen(logPath,"rb"); |
---|
102 | fread(fcontent,f_status.st_size,1,f); |
---|
103 | int fsize=f_status.st_size; |
---|
104 | fcontent[fsize]=0; |
---|
105 | fclose(f); |
---|
106 | free(logPath); |
---|
107 | fprintf(stderr,"%s \n",fcontent); |
---|
108 | char *token, *saveptr; |
---|
109 | token = strtok_r (fcontent, " ", &saveptr); |
---|
110 | while (token != NULL) |
---|
111 | { |
---|
112 | //fprintf(stderr,"%s %d\n",token,__LINE__); |
---|
113 | char *token1, *saveptr1; |
---|
114 | char *tmpToken=strdup(token); |
---|
115 | token1 = strtok_r (tmpToken, "=", &saveptr1); |
---|
116 | int isNext=-1; |
---|
117 | int hasTwoElements=0; |
---|
118 | char *name=NULL; |
---|
119 | while (token1 != NULL) |
---|
120 | { |
---|
121 | if(hasTwoElements==0) |
---|
122 | name=strdup(token1); |
---|
123 | if(hasTwoElements<1) |
---|
124 | hasTwoElements+=1; |
---|
125 | else{ |
---|
126 | char *value=strdup(token1); |
---|
127 | if(value[strlen(value)-1]=='\n') |
---|
128 | value[strlen(value)-1]=0; |
---|
129 | if(strlen(name)>0 && strlen(value)>0){ |
---|
130 | if(tmpMaps->content==NULL) |
---|
131 | tmpMaps->content=createMap(name,value); |
---|
132 | else |
---|
133 | addToMap(tmpMaps->content,name,value); |
---|
134 | free(value); |
---|
135 | } |
---|
136 | free(name); |
---|
137 | hasTwoElements=0; |
---|
138 | } |
---|
139 | token1 = strtok_r (NULL, "=", &saveptr1); |
---|
140 | } |
---|
141 | free(tmpToken); |
---|
142 | token = strtok_r (NULL, " ", &saveptr); |
---|
143 | } |
---|
144 | }else{ |
---|
145 | setMapInMaps(conf,"lenv","message",_("Unable to access the downloaded execution log file")); |
---|
146 | return SERVICE_FAILED; |
---|
147 | } |
---|
148 | } |
---|
149 | logPath=(char*)malloc((strlen(tmpPath->value)+strlen(usid->value)+15)*sizeof(char)); |
---|
150 | sprintf(logPath,"%s/exec_status_%s",tmpPath->value,usid->value); |
---|
151 | dumpMapsToFile(tmpMaps,logPath,0); |
---|
152 | |
---|
153 | char *sname=(char*)malloc((strlen(tmpPath->value)+strlen(jobid->value)+21)); |
---|
154 | sprintf(sname,"%s/.wait_socket_%s.sock",tmpPath->value,jobid->value); |
---|
155 | if ( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { |
---|
156 | perror("socket error"); |
---|
157 | setMapInMaps(conf,"lenv","message",_("Socket error")); |
---|
158 | return SERVICE_FAILED; |
---|
159 | } |
---|
160 | } |
---|
161 | logPath=(char*)malloc((strlen(tmpPath->value)+strlen(usid->value)+15)*sizeof(char)); |
---|
162 | sprintf(logPath,"%s/exec_status_%s",tmpPath->value,usid->value); |
---|
163 | dumpMapsToFile(tmpMaps,logPath,0); |
---|
164 | char *sname=(char*)malloc((strlen(tmpPath->value)+strlen(jobid->value)+21)); |
---|
165 | sprintf(sname,"%s/.wait_socket_%s.sock",tmpPath->value,jobid->value); |
---|
166 | if ( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { |
---|
167 | perror("socket error"); |
---|
168 | setMapInMaps(conf,"lenv","message",_("Socket error")); |
---|
169 | return SERVICE_FAILED; |
---|
170 | } |
---|
171 | memset(&addr, 0, sizeof(addr)); |
---|
172 | addr.sun_family = AF_UNIX; |
---|
173 | strncpy(addr.sun_path, sname, sizeof(addr.sun_path)-1); |
---|
174 | if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) { |
---|
175 | perror("connect error"); |
---|
176 | setMapInMaps(conf,"lenv","message",_("Unable to connect")); |
---|
177 | return SERVICE_FAILED; |
---|
178 | } |
---|
179 | if (write(fd, "3", 1) != rc) { |
---|
180 | if (rc < 0) { |
---|
181 | perror("write error"); |
---|
182 | setMapInMaps(conf,"lenv","message",_("Unable to announce the successful execution of the HPC service")); |
---|
183 | close(fd); |
---|
184 | return SERVICE_FAILED; |
---|
185 | } |
---|
186 | } |
---|
187 | close(fd); |
---|
188 | setOutputValue(outputs,"Result",(char*)"\"FinalizeHPC run successfully\"",32); |
---|
189 | |
---|
190 | return SERVICE_SUCCEEDED; |
---|
191 | } |
---|
192 | |
---|
193 | } |
---|