Split run and logs

This commit is contained in:
nemunaire 2021-05-02 16:26:23 +02:00
parent cce72978bf
commit 539cbd68a9
4 changed files with 56 additions and 13 deletions

10
app.go
View File

@ -41,7 +41,15 @@ func NewApp() App {
}) })
router.GET("/api/run", func(c *gin.Context) { router.GET("/api/run", func(c *gin.Context) {
stream, err := docker.RunHello() ctrid, err := docker.Run("hello-world")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err})
}
c.JSON(http.StatusOK, gin.H{"jobid": ctrid})
})
router.GET("/api/jobs/:jobid/logs", func(c *gin.Context) {
stream, err := docker.Logs(c.Param("jobid"))
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err}) c.JSON(http.StatusInternalServerError, gin.H{"error": err})
} }

View File

@ -11,8 +11,12 @@ import (
oci "github.com/opencontainers/image-spec/specs-go/v1" oci "github.com/opencontainers/image-spec/specs-go/v1"
) )
func newCli() (*client.Client, error) {
return client.NewClientWithOpts(client.FromEnv)
}
func Ps() ([]types.Container, error) { func Ps() ([]types.Container, error) {
cli, err := client.NewClientWithOpts(client.FromEnv) cli, err := newCli()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -25,10 +29,10 @@ func Ps() ([]types.Container, error) {
return containers, nil return containers, nil
} }
func RunHello() (io.ReadCloser, error) { func Run(image string) (string, error) {
cli, err := client.NewClientWithOpts(client.FromEnv) cli, err := newCli()
if err != nil { if err != nil {
return nil, err return "", err
} }
ctnr, err := cli.ContainerCreate( ctnr, err := cli.ContainerCreate(
@ -36,7 +40,7 @@ func RunHello() (io.ReadCloser, error) {
&container.Config{ &container.Config{
AttachStdout: true, AttachStdout: true,
AttachStderr: true, AttachStderr: true,
Image: "hello-world", Image: image,
}, },
&container.HostConfig{}, &container.HostConfig{},
&network.NetworkingConfig{}, &network.NetworkingConfig{},
@ -47,15 +51,24 @@ func RunHello() (io.ReadCloser, error) {
"", "",
) )
if err != nil { if err != nil {
return nil, err return "", err
} }
err = cli.ContainerStart(context.Background(), ctnr.ID, types.ContainerStartOptions{}) err = cli.ContainerStart(context.Background(), ctnr.ID, types.ContainerStartOptions{})
if err != nil {
return "", err
}
return ctnr.ID, nil
}
func Logs(id string) (io.ReadCloser, error) {
cli, err := newCli()
if err != nil { if err != nil {
return nil, err return nil, err
} }
return cli.ContainerLogs(context.Background(), ctnr.ID, types.ContainerLogsOptions{ return cli.ContainerLogs(context.Background(), id, types.ContainerLogsOptions{
ShowStdout: true, ShowStdout: true,
ShowStderr: true, ShowStderr: true,
Follow: true, Follow: true,

View File

@ -18,14 +18,16 @@
</form> </form>
</div> </div>
<div id="started-alert" class="alert alert-info" style="display: none;">
<strong>Job running...</strong>
it's ID is <code id="jobid"></code>
</div>
<div id="logs-card" class="card" style="display: none;"> <div id="logs-card" class="card" style="display: none;">
<h5 class="card-header"> <h5 class="card-header">
Here is your container logs output: Here is your container logs output:
</h5> </h5>
<pre id="logs" class="card-body bg-dark text-light"> <pre id="logs" class="card-body bg-dark text-light"></pre>
test
test
</pre>
</div> </div>
<script src="/js/minifass.js"></script> <script src="/js/minifass.js"></script>

View File

@ -8,19 +8,39 @@ function getVersion() {
}); });
} }
let current_job = null;
function runctnr() { function runctnr() {
document.getElementById("btnlaunch").disabled = true; document.getElementById("btnlaunch").disabled = true;
document.getElementById("btnlaunchspinner").style.display = "inline-block"; document.getElementById("btnlaunchspinner").style.display = "inline-block";
fetch('/api/run') fetch('/api/run')
.then(function(response) {
return response.json();
})
.then(function(data) {
current_job = data.jobid;
document.getElementById("jobid").textContent = current_job;
document.getElementById("started-alert").style.display = "block";
fetchLogs();
})
return false;
}
function fetchLogs() {
fetch('/api/jobs/' + current_job + '/logs')
.then(function(response) { .then(function(response) {
document.getElementById("btnlaunch").disabled = false; document.getElementById("btnlaunch").disabled = false;
document.getElementById("btnlaunchspinner").style.display = "none"; document.getElementById("btnlaunchspinner").style.display = "none";
document.getElementById("started-alert").style.display = "none";
return response.text(); return response.text();
}) })
.then(function(logs) { .then(function(logs) {
document.getElementById("logs").textContent = logs; document.getElementById("logs").textContent = logs;
document.getElementById("logs-card").style.display = "block"; document.getElementById("logs-card").style.display = "block";
current_job = null;
}) })
return false;
} }