Use websocket to transmit logs
This commit is contained in:
parent
539cbd68a9
commit
d2a49e5740
35
app.go
35
app.go
@ -9,6 +9,7 @@ import (
|
||||
|
||||
"github.com/docker/docker/pkg/stdcopy"
|
||||
"github.com/gin-gonic/gin"
|
||||
"nhooyr.io/websocket"
|
||||
|
||||
"github.com/nemunaire/minifaas/engine/docker"
|
||||
"github.com/nemunaire/minifaas/ui"
|
||||
@ -41,7 +42,7 @@ func NewApp() App {
|
||||
})
|
||||
|
||||
router.GET("/api/run", func(c *gin.Context) {
|
||||
ctrid, err := docker.Run("hello-world")
|
||||
ctrid, err := docker.Run("alpine", []string{"sh", "-c", "for i in `seq 20`; do echo $i; sleep 0.5; done"})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err})
|
||||
}
|
||||
@ -49,15 +50,39 @@ func NewApp() App {
|
||||
})
|
||||
|
||||
router.GET("/api/jobs/:jobid/logs", func(c *gin.Context) {
|
||||
ws, err := websocket.Accept(c.Writer, c.Request, nil)
|
||||
if err != nil {
|
||||
log.Fatal("error get connection", err)
|
||||
}
|
||||
defer ws.Close(websocket.StatusInternalError, "the sky is falling")
|
||||
|
||||
stream, err := docker.Logs(c.Param("jobid"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err})
|
||||
log.Fatal("Unable to get log stream:", err)
|
||||
}
|
||||
|
||||
_, err = stdcopy.StdCopy(c.Writer, c.Writer, stream)
|
||||
if err != nil && err != io.EOF {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err})
|
||||
r, w := io.Pipe()
|
||||
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), time.Second*10)
|
||||
defer cancel()
|
||||
|
||||
go func(reader io.Reader) {
|
||||
buf := make([]byte, 128)
|
||||
for {
|
||||
n, err := reader.Read(buf)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
ws.Write(ctx, websocket.MessageText, buf[:n])
|
||||
}
|
||||
}(r)
|
||||
|
||||
_, err = stdcopy.StdCopy(w, w, stream)
|
||||
if err != nil && err != io.EOF {
|
||||
log.Fatal("Something goes wrong during stream:", err)
|
||||
}
|
||||
|
||||
ws.Close(websocket.StatusNormalClosure, "")
|
||||
})
|
||||
|
||||
app := App{
|
||||
|
@ -29,7 +29,7 @@ func Ps() ([]types.Container, error) {
|
||||
return containers, nil
|
||||
}
|
||||
|
||||
func Run(image string) (string, error) {
|
||||
func Run(image string, cmd []string) (string, error) {
|
||||
cli, err := newCli()
|
||||
if err != nil {
|
||||
return "", err
|
||||
@ -41,6 +41,7 @@ func Run(image string) (string, error) {
|
||||
AttachStdout: true,
|
||||
AttachStderr: true,
|
||||
Image: image,
|
||||
Cmd: cmd,
|
||||
},
|
||||
&container.HostConfig{},
|
||||
&network.NetworkingConfig{},
|
||||
|
1
go.mod
1
go.mod
@ -15,4 +15,5 @@ require (
|
||||
github.com/opencontainers/image-spec v1.0.1
|
||||
github.com/sirupsen/logrus v1.8.1 // indirect
|
||||
google.golang.org/grpc v1.37.0 // indirect
|
||||
nhooyr.io/websocket v1.8.7
|
||||
)
|
||||
|
12
go.sum
12
go.sum
@ -23,6 +23,7 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.m
|
||||
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
|
||||
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
|
||||
github.com/gin-gonic/gin v1.7.1 h1:qC89GU3p8TvKWMAVhEpmpB2CIb1hnqt2UdKZaP93mS8=
|
||||
github.com/gin-gonic/gin v1.7.1/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY=
|
||||
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
@ -30,8 +31,12 @@ github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8c
|
||||
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
|
||||
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
|
||||
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
|
||||
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
|
||||
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
|
||||
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
|
||||
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=
|
||||
github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
|
||||
github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
|
||||
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
|
||||
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||
@ -39,6 +44,7 @@ github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfb
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
|
||||
github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk=
|
||||
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
|
||||
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
|
||||
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
|
||||
@ -54,10 +60,13 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
|
||||
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
|
||||
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
|
||||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||
github.com/klauspost/compress v1.10.3 h1:OP96hzwJVBIHYU52pVTI6CczrxPvrGfgqF9N5eTO0Q8=
|
||||
github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
|
||||
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
|
||||
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
|
||||
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
|
||||
@ -124,6 +133,7 @@ golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7w
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
||||
@ -164,3 +174,5 @@ gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
||||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g=
|
||||
nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=
|
||||
|
@ -23,11 +23,11 @@
|
||||
it's ID is <code id="jobid"></code>
|
||||
</div>
|
||||
|
||||
<div id="logs-card" class="card" style="display: none;">
|
||||
<div id="logs-card" class="mb-3 card" style="display: none;">
|
||||
<h5 class="card-header">
|
||||
Here is your container logs output:
|
||||
</h5>
|
||||
<pre id="logs" class="card-body bg-dark text-light"></pre>
|
||||
<pre id="logs" class="mb-0 card-body bg-dark text-light"></pre>
|
||||
</div>
|
||||
|
||||
<script src="/js/minifass.js"></script>
|
||||
|
@ -30,17 +30,26 @@ function runctnr() {
|
||||
}
|
||||
|
||||
function fetchLogs() {
|
||||
fetch('/api/jobs/' + current_job + '/logs')
|
||||
.then(function(response) {
|
||||
var ws = new WebSocket('ws://' + window.location.host + '/api/jobs/' + current_job + '/logs');
|
||||
|
||||
ws.onopen = function (evt) {
|
||||
console.log("Connection open ...");
|
||||
document.getElementById("logs-card").style.display = "block";
|
||||
document.getElementById("logs").innerText = "";
|
||||
};
|
||||
|
||||
ws.onmessage = function (evt) {
|
||||
console.log("Received Message: " + evt.data);
|
||||
document.getElementById("logs").innerText += evt.data;
|
||||
};
|
||||
|
||||
ws.onclose = function (evt) {
|
||||
console.log("Connection closed.");
|
||||
|
||||
current_job = null;
|
||||
|
||||
document.getElementById("btnlaunch").disabled = false;
|
||||
document.getElementById("btnlaunchspinner").style.display = "none";
|
||||
document.getElementById("started-alert").style.display = "none";
|
||||
|
||||
return response.text();
|
||||
})
|
||||
.then(function(logs) {
|
||||
document.getElementById("logs").textContent = logs;
|
||||
document.getElementById("logs-card").style.display = "block";
|
||||
current_job = null;
|
||||
})
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user