This repository has been archived on 2024-03-03. You can view files and clone it, but cannot push or open issues or pull requests.
minifaas/engine/docker/docker.go

128 lines
2.7 KiB
Go
Raw Normal View History

2021-05-02 11:47:08 +00:00
package docker
import (
"context"
"io"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
2021-05-02 11:47:08 +00:00
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
oci "github.com/opencontainers/image-spec/specs-go/v1"
)
2021-05-02 14:26:23 +00:00
func newCli() (*client.Client, error) {
return client.NewClientWithOpts(client.FromEnv)
}
2021-07-31 15:16:38 +00:00
func Ps(opts types.ContainerListOptions) ([]types.Container, error) {
2021-05-08 22:55:45 +00:00
cli, err := newCli()
if err != nil {
return nil, err
}
2021-07-31 15:16:38 +00:00
return cli.ContainerList(context.Background(), opts)
2021-05-08 22:55:45 +00:00
}
func PsName() (ret map[string]string, err error) {
2021-07-31 15:16:38 +00:00
containers, err := Ps(types.ContainerListOptions{})
2021-05-08 22:55:45 +00:00
if err != nil {
return nil, err
}
ret = map[string]string{}
for _, container := range containers {
for _, name := range container.Names {
ret[name] = container.ID
}
}
return ret, err
}
2021-07-31 15:16:38 +00:00
func GetContainer(containerID string) (ctr types.ContainerJSON, err error) {
cli, err := newCli()
if err != nil {
return types.ContainerJSON{}, err
}
return cli.ContainerInspect(context.Background(), containerID)
}
func HasStarted(containerID string) bool {
ctr, err := GetContainer(containerID)
if err != nil {
return false
}
return ctr.State.Status == "running" || ctr.State.Status == "paused" || ctr.State.Status == "restarting" || ctr.State.Status == "removing" || ctr.State.Status == "exited" || ctr.State.Status == "dead"
}
func Create(jobtype string, image string, cmd []string, mounts []mount.Mount) (string, error) {
2021-05-02 14:26:23 +00:00
cli, err := newCli()
2021-05-02 11:47:08 +00:00
if err != nil {
2021-05-02 14:26:23 +00:00
return "", err
2021-05-02 11:47:08 +00:00
}
ctnr, err := cli.ContainerCreate(
context.Background(),
&container.Config{
AttachStdout: true,
AttachStderr: true,
2021-05-02 14:26:23 +00:00
Image: image,
2021-05-02 15:59:55 +00:00
Cmd: cmd,
},
&container.HostConfig{
Mounts: mounts,
2021-05-02 11:47:08 +00:00
},
&network.NetworkingConfig{},
&oci.Platform{
Architecture: "amd64",
OS: "linux",
},
2021-05-09 16:02:23 +00:00
genContainerName(jobtype),
2021-05-02 11:47:08 +00:00
)
if err != nil {
2021-05-02 14:26:23 +00:00
return "", err
2021-05-02 11:47:08 +00:00
}
return ctnr.ID, nil
}
func Start(id string) error {
cli, err := newCli()
2021-05-02 14:26:23 +00:00
if err != nil {
return err
2021-05-02 14:26:23 +00:00
}
err = cli.ContainerStart(context.Background(), id, types.ContainerStartOptions{})
if err != nil {
return err
}
return nil
}
func Run(jobtype string, image string, cmd []string, mounts []mount.Mount) (string, error) {
ctrid, err := Create(jobtype, image, cmd, mounts)
if err != nil {
return ctrid, err
}
return ctrid, Start(ctrid)
2021-05-02 14:26:23 +00:00
}
func Logs(id string) (io.ReadCloser, error) {
cli, err := newCli()
2021-05-02 11:47:08 +00:00
if err != nil {
return nil, err
}
2021-05-02 14:26:23 +00:00
return cli.ContainerLogs(context.Background(), id, types.ContainerLogsOptions{
2021-05-02 11:47:08 +00:00
ShowStdout: true,
ShowStderr: true,
Follow: true,
})
}