forked from interTwin-eu/interLink
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package slurm | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"io" | ||
"net/http" | ||
"strings" | ||
"time" | ||
|
||
OSexec "os/exec" | ||
|
||
"github.com/containerd/containerd/log" | ||
commonIL "github.com/intertwin-eu/interlink/pkg/common" | ||
) | ||
|
||
func GetLogsHandler(w http.ResponseWriter, r *http.Request) { | ||
log.G(Ctx).Info("Docker Sidecar: received GetLogs call") | ||
var req commonIL.LogStruct | ||
statusCode := http.StatusOK | ||
currentTime := time.Now() | ||
|
||
bodyBytes, err := io.ReadAll(r.Body) | ||
if err != nil { | ||
statusCode = http.StatusInternalServerError | ||
w.WriteHeader(statusCode) | ||
w.Write([]byte("Some errors occurred while checking container status. Check Docker Sidecar's logs")) | ||
log.G(Ctx).Error(err) | ||
return | ||
} | ||
|
||
err = json.Unmarshal(bodyBytes, &req) | ||
if err != nil { | ||
statusCode = http.StatusInternalServerError | ||
w.WriteHeader(statusCode) | ||
w.Write([]byte("Some errors occurred while checking container status. Check Docker Sidecar's logs")) | ||
log.G(Ctx).Error(err) | ||
return | ||
} | ||
|
||
var cmd *OSexec.Cmd | ||
if req.Opts.Timestamps { | ||
log.G(Ctx).Error(errors.New("Not Implemented")) | ||
statusCode = http.StatusInternalServerError | ||
w.WriteHeader(statusCode) | ||
return | ||
} else { | ||
JIDs = append(JIDs, JidStruct{PodUID: "58b735d3-3973-4474-bdda-949978cd13a3", JID: "123456"}) | ||
for _, jid := range JIDs { | ||
if jid.PodUID == req.PodUID { | ||
cmd = OSexec.Command("cat", "slurm-"+jid.JID+".out") | ||
} | ||
} | ||
} | ||
|
||
output, err := cmd.CombinedOutput() | ||
|
||
if err != nil { | ||
log.G(Ctx).Error(err) | ||
statusCode = http.StatusInternalServerError | ||
w.WriteHeader(statusCode) | ||
return | ||
} | ||
|
||
var returnedLogs string | ||
|
||
if req.Opts.Tail != 0 { | ||
var lastLines []string | ||
|
||
splittedLines := strings.Split(string(output), "\n") | ||
|
||
if req.Opts.Tail > len(splittedLines) { | ||
lastLines = splittedLines | ||
} else { | ||
lastLines = splittedLines[len(splittedLines)-req.Opts.Tail-1:] | ||
} | ||
|
||
for _, line := range lastLines { | ||
returnedLogs += line + "\n" | ||
} | ||
} else { | ||
var lastBytes []byte | ||
if req.Opts.LimitBytes > len(output) { | ||
lastBytes = output | ||
} else { | ||
lastBytes = output[len(output)-req.Opts.LimitBytes-1:] | ||
} | ||
|
||
returnedLogs = string(lastBytes) | ||
} | ||
|
||
if req.Opts.Timestamps && (req.Opts.SinceSeconds != 0 || !req.Opts.SinceTime.IsZero()) { | ||
temp := returnedLogs | ||
returnedLogs = "" | ||
splittedLogs := strings.Split(temp, "\n") | ||
timestampFormat := "2006-01-02T15:04:05.999999999Z" | ||
|
||
for _, Log := range splittedLogs { | ||
part := strings.SplitN(Log, " ", 2) | ||
timestampString := part[0] | ||
timestamp, err := time.Parse(timestampFormat, timestampString) | ||
if err != nil { | ||
continue | ||
} | ||
if req.Opts.SinceSeconds != 0 { | ||
if currentTime.Sub(timestamp).Seconds() > float64(req.Opts.SinceSeconds) { | ||
returnedLogs += Log + "\n" | ||
} | ||
} else { | ||
if timestamp.Sub(req.Opts.SinceTime).Seconds() >= 0 { | ||
returnedLogs += Log + "\n" | ||
} | ||
} | ||
} | ||
} | ||
|
||
if statusCode != http.StatusOK { | ||
w.Write([]byte("Some errors occurred while checking container status. Check Docker Sidecar's logs")) | ||
} else { | ||
w.WriteHeader(statusCode) | ||
w.Write([]byte(returnedLogs)) | ||
} | ||
} |