node-api/docker/docker.go

223 lines
5.3 KiB
Go

package docker
import (
"context"
"errors"
"log"
"strconv"
"strings"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
dockerClient "github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
)
// Docker timeout
const dockerTimeout = 10
// DOCKER_SOCK tells where to connect to docker, it will be always local sock
const dockerSock = "unix:///var/run/docker.sock"
// DOCKER_API_VERSION set API version of Docker, 1.40 belongs to Docker 19.03.11
const dockerAPIVersion = "1.40"
// Driver keeps everything for connection to Docker
type Driver struct{}
func (d *Driver) getClient() (*dockerClient.Client, error) {
cli, err := dockerClient.NewClient(dockerSock, dockerAPIVersion, nil, nil)
return cli, err
}
// ConnectionStatus checks connection to the Docker daemon
func (d *Driver) ConnectionStatus() (bool, error) {
cli, err := d.getClient()
if err != nil {
return false, err
}
_, err = cli.ServerVersion(context.TODO())
if err != nil {
return false, err
}
return true, nil
}
func (d *Driver) nameToID(name string) (string, error) {
containerIDs, err := d.IsExist(name)
if err != nil {
return "", err
}
if len(containerIDs) == 0 {
return "", errors.New("no container found")
}
if len(containerIDs) > 1 {
return "", errors.New("multiple containers with the same name")
}
return containerIDs[0], nil
}
// Remove removes container represented by containerID
func (d *Driver) Remove(name string) error {
log.Println("Removing container " + name)
cli, err := d.getClient()
if err != nil {
return err
}
containerID, err := d.nameToID(name)
if err != nil {
return err
}
timeout := time.Duration(dockerTimeout * time.Second)
err = cli.ContainerStop(context.TODO(), containerID, &timeout)
if err != nil {
return err
}
err = cli.ContainerRemove(context.TODO(), containerID, types.ContainerRemoveOptions{})
return err
}
// Start starts container represented by containerID
func (d *Driver) Start(name string) error {
log.Println("Starting container " + name)
cli, err := d.getClient()
if err != nil {
return err
}
containerID, err := d.nameToID(name)
if err != nil {
return err
}
err = cli.ContainerStart(context.TODO(), containerID, types.ContainerStartOptions{})
return err
}
// Stop stops container represented by containerID
func (d *Driver) Stop(name string) error {
log.Println("Stopping container " + name)
cli, err := d.getClient()
if err != nil {
return err
}
containerID, err := d.nameToID(name)
if err != nil {
return err
}
timeout := time.Duration(dockerTimeout * time.Second)
err = cli.ContainerStop(context.TODO(), containerID, &timeout)
return err
}
// IsExist checks existence of the container based on container name
// Returns container IDs in case of existence. Otherwise
// empty slice.
func (d *Driver) IsExist(name string) ([]string, error) {
var containerIDs = make([]string, 0)
cli, err := d.getClient()
if err != nil {
return containerIDs, err
}
containers, err := cli.ContainerList(context.TODO(), types.ContainerListOptions{})
if err != nil {
return containerIDs, err
}
// We go through the containers and pick the ones which match the task name
for _, containerObject := range containers {
for _, name := range containerObject.Names {
name = strings.Trim(name, "/")
if strings.Split(name, ".")[0] == name {
containerIDs = append(containerIDs, containerObject.ID)
}
}
}
return containerIDs, nil
}
// Create creates the container
// image - docker image
// cmd - string slice of command and its arguments
// volumePath - host's directory to mount into the container
// returns container ID
func (d *Driver) Create(name string, image string, volumePath string, HTTPPort int, SSHPort int, CPU int, memory int, cmd []string) (string, error) {
log.Println("Creating container " + name)
cli, err := d.getClient()
if err != nil {
return "", err
}
portmaps := make(nat.PortMap, 1)
portbindingsHTTP := make([]nat.PortBinding, 1)
portbindingsHTTP[0] = nat.PortBinding{
HostPort: strconv.Itoa(HTTPPort) + "/tcp",
}
portmaps["8000/tcp"] = portbindingsHTTP
if SSHPort != 0 {
portbindingsSSH := make([]nat.PortBinding, 1)
portbindingsSSH[0] = nat.PortBinding{
HostPort: strconv.Itoa(SSHPort) + "/tcp",
}
portmaps["22/tcp"] = portbindingsSSH
}
createdContainer, err := cli.ContainerCreate(
context.TODO(),
&container.Config{
Hostname: name,
Env: []string{},
Image: image,
Cmd: cmd,
},
&container.HostConfig{
Resources: container.Resources{
CPUPeriod: 100,
CPUQuota: int64(CPU),
Memory: int64(memory*110/100)*1024 ^ 2, // Allow 10 % more memory so we have space for MemoryReservation
MemoryReservation: int64(memory)*1024 ^ 2, // This should provide softer way how to limit the memory of our containers
},
PortBindings: portmaps,
AutoRemove: false,
RestartPolicy: container.RestartPolicy{
Name: "unless-stopped",
MaximumRetryCount: 3,
},
Binds: []string{
volumePath + ":/srv",
},
},
&network.NetworkingConfig{},
name,
)
if err != nil {
return "", err
}
containerID := createdContainer.ID
// I dunno if we want this
// err = cli.ContainerStart(context.TODO(), createdContainer.ID, types.ContainerStartOptions{})
return containerID, nil
}