2020-11-30 09:37:17 +00:00
|
|
|
package metabase
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-01-08 13:32:29 +00:00
|
|
|
"runtime"
|
2020-11-30 09:37:17 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/api/types/mount"
|
|
|
|
"github.com/docker/docker/client"
|
|
|
|
"github.com/docker/go-connections/nat"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Container struct {
|
2021-02-10 08:23:33 +00:00
|
|
|
ListenAddr string
|
|
|
|
ListenPort string
|
|
|
|
SharedFolder string
|
|
|
|
Image string
|
|
|
|
Name string
|
|
|
|
ID string
|
|
|
|
CLI *client.Client
|
|
|
|
MBDBUri string
|
|
|
|
DockerGroupID string
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 16:15:22 +00:00
|
|
|
func NewContainer(listenAddr string, listenPort string, sharedFolder string, containerName string, image string, mbDBURI string, dockerGroupID string) (*Container, error) {
|
2020-11-30 09:37:17 +00:00
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create docker client : %s", err)
|
|
|
|
}
|
|
|
|
return &Container{
|
2021-02-10 08:23:33 +00:00
|
|
|
ListenAddr: listenAddr,
|
|
|
|
ListenPort: listenPort,
|
|
|
|
SharedFolder: sharedFolder,
|
|
|
|
Image: image,
|
2021-07-01 16:15:22 +00:00
|
|
|
Name: containerName,
|
2021-02-10 08:23:33 +00:00
|
|
|
CLI: cli,
|
|
|
|
MBDBUri: mbDBURI,
|
|
|
|
DockerGroupID: dockerGroupID,
|
2020-11-30 09:37:17 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) Create() error {
|
|
|
|
ctx := context.Background()
|
|
|
|
log.Printf("Pulling docker image %s", c.Image)
|
|
|
|
reader, err := c.CLI.ImagePull(ctx, c.Image, types.ImagePullOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to pull docker image : %s", err)
|
|
|
|
}
|
|
|
|
defer reader.Close()
|
|
|
|
scanner := bufio.NewScanner(reader)
|
|
|
|
for scanner.Scan() {
|
|
|
|
fmt.Print(".")
|
|
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return fmt.Errorf("failed to read imagepull reader: %s", err)
|
|
|
|
}
|
|
|
|
fmt.Print("\n")
|
|
|
|
|
|
|
|
hostConfig := &container.HostConfig{
|
|
|
|
PortBindings: nat.PortMap{
|
|
|
|
"3000/tcp": []nat.PortBinding{
|
|
|
|
{
|
|
|
|
HostIP: c.ListenAddr,
|
|
|
|
HostPort: c.ListenPort,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Mounts: []mount.Mount{
|
|
|
|
{
|
|
|
|
Type: mount.TypeBind,
|
|
|
|
Source: c.SharedFolder,
|
|
|
|
Target: containerSharedFolder,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
env := []string{
|
|
|
|
fmt.Sprintf("MB_DB_FILE=%s/metabase.db", containerSharedFolder),
|
|
|
|
}
|
|
|
|
if c.MBDBUri != "" {
|
|
|
|
env = append(env, c.MBDBUri)
|
|
|
|
}
|
|
|
|
|
2021-02-10 08:23:33 +00:00
|
|
|
env = append(env, fmt.Sprintf("MGID=%s", c.DockerGroupID))
|
2020-11-30 09:37:17 +00:00
|
|
|
dockerConfig := &container.Config{
|
|
|
|
Image: c.Image,
|
|
|
|
Tty: true,
|
|
|
|
Env: env,
|
|
|
|
}
|
2021-01-08 13:32:29 +00:00
|
|
|
os := runtime.GOOS
|
|
|
|
switch os {
|
|
|
|
case "linux":
|
|
|
|
case "windows", "darwin":
|
|
|
|
return fmt.Errorf("Mac and Windows are not supported yet")
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("OS '%s' is not supported", os)
|
|
|
|
}
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
log.Infof("creating container '%s'", c.Name)
|
2021-01-18 14:25:07 +00:00
|
|
|
resp, err := c.CLI.ContainerCreate(ctx, dockerConfig, hostConfig, nil, nil, c.Name)
|
2020-11-30 09:37:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create container : %s", err)
|
|
|
|
}
|
|
|
|
c.ID = resp.ID
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) Start() error {
|
|
|
|
ctx := context.Background()
|
|
|
|
if err := c.CLI.ContainerStart(ctx, c.Name, types.ContainerStartOptions{}); err != nil {
|
|
|
|
return fmt.Errorf("failed while starting %s : %s", c.ID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func StartContainer(name string) error {
|
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create docker client : %s", err)
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
if err := cli.ContainerStart(ctx, name, types.ContainerStartOptions{}); err != nil {
|
|
|
|
return fmt.Errorf("failed while starting %s : %s", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func StopContainer(name string) error {
|
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create docker client : %s", err)
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
2023-03-09 10:56:02 +00:00
|
|
|
to := 20 * time.Second
|
2020-11-30 09:37:17 +00:00
|
|
|
if err := cli.ContainerStop(ctx, name, &to); err != nil {
|
|
|
|
return fmt.Errorf("failed while stopping %s : %s", name, err)
|
|
|
|
}
|
|
|
|
log.Printf("container stopped successfully")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func RemoveContainer(name string) error {
|
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create docker client : %s", err)
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
log.Printf("Removing docker metabase %s", name)
|
|
|
|
if err := cli.ContainerRemove(ctx, name, types.ContainerRemoveOptions{}); err != nil {
|
2022-11-29 08:16:07 +00:00
|
|
|
return fmt.Errorf("failed to remove container %s : %s", name, err)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-10 08:23:33 +00:00
|
|
|
func RemoveImageContainer() error {
|
2020-11-30 09:37:17 +00:00
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create docker client : %s", err)
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
2021-02-10 08:23:33 +00:00
|
|
|
log.Printf("Removing docker image '%s'", metabaseImage)
|
|
|
|
if _, err := cli.ImageRemove(ctx, metabaseImage, types.ImageRemoveOptions{}); err != nil {
|
2022-11-29 08:16:07 +00:00
|
|
|
return fmt.Errorf("failed to remove image container %s : %s", metabaseImage, err)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsContainerExist(name string) bool {
|
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to create docker client : %s", err)
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
if _, err := cli.ContainerInspect(ctx, name); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2021-01-08 13:32:29 +00:00
|
|
|
}
|