aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/api/docker.go
blob: 248880eb60a5bdf849ce4a89ddbeae885414142d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package api

import (
	"context"
	"fmt"
	"io"
	"log/slog"
	"time"

	"github.com/docker/docker/api/types/container"
	"github.com/docker/docker/client"
)

const vttContainerId string = "foundry-foundry-1"

func stopVtt(ctx context.Context) error {
	apiClient, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
	if err != nil {
		Logger.ErrorContext(ctx, "Failed to get docker api client", slog.Any("err", err))
		return err
	}

	Logger.InfoContext(ctx, "Stopping foundry container")
	err = apiClient.ContainerStop(ctx, vttContainerId, container.StopOptions{})
	if err != nil {
		Logger.ErrorContext(ctx, "Error occured while stopping foundry container", slog.Any("err", err))
		return err
	}

	return nil
}

// Get readcloser for logs
// If lines is zero, returns all logs.
func vttLogs(ctx context.Context, lines uint) (io.ReadCloser, error) {
	apiClient, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
	if err != nil {
		Logger.ErrorContext(ctx, "Failed to get docker api client", slog.Any("err", err))
		return nil, err
	}

	opts := container.LogsOptions{
		ShowStdout: true,
		ShowStderr: true,
		Timestamps: true,
	}
	if lines != 0 {
		opts.Tail = fmt.Sprint(lines)
	}

	Logger.DebugContext(ctx, "Getting container logs")
	r, err := apiClient.ContainerLogs(ctx, vttContainerId, opts)
	if err != nil {
		Logger.ErrorContext(ctx, "Failed to get foundry container's logs", slog.Any("err", err))
		return nil, err
	}

	return r, nil
}

func vttStatus(ctx context.Context) ServerStatus {
	status := ServerStatus{}
	status.Site = VTT_URL
	status.Timestamp = time.Now()

	apiClient, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
	if err != nil {
		Logger.ErrorContext(ctx, "Failed to get docker api client", slog.Any("err", err))
		return status
	}

	json, err := apiClient.ContainerInspect(ctx, vttContainerId)
	if err != nil {
		Logger.ErrorContext(ctx, "Error occured while geting foundry container stats", slog.Any("err", err))
		return status
	}

	status.Online = json.State.Running

	return status
}