2020-08-11 04:38:42 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-03-08 01:31:45 +00:00
|
|
|
"io"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"emperror.dev/errors"
|
2020-08-11 04:38:42 +00:00
|
|
|
"github.com/apex/log"
|
2022-01-23 20:17:40 +00:00
|
|
|
"github.com/buger/jsonparser"
|
2020-08-11 04:38:42 +00:00
|
|
|
"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"
|
2022-01-31 00:51:23 +00:00
|
|
|
"github.com/docker/docker/daemon/logger/local"
|
2021-08-02 21:07:00 +00:00
|
|
|
|
2020-08-11 04:38:42 +00:00
|
|
|
"github.com/pterodactyl/wings/config"
|
|
|
|
"github.com/pterodactyl/wings/environment"
|
2020-12-26 01:07:57 +00:00
|
|
|
"github.com/pterodactyl/wings/system"
|
2020-08-11 04:38:42 +00:00
|
|
|
)
|
|
|
|
|
2021-03-08 01:31:45 +00:00
|
|
|
var ErrNotAttached = errors.Sentinel("not attached to instance")
|
2020-09-13 04:37:48 +00:00
|
|
|
|
2021-01-09 05:21:09 +00:00
|
|
|
// A custom console writer that allows us to keep a function blocked until the
|
|
|
|
// given stream is properly closed. This does nothing special, only exists to
|
|
|
|
// make a noop io.Writer.
|
|
|
|
type noopWriter struct{}
|
|
|
|
|
|
|
|
var _ io.Writer = noopWriter{}
|
|
|
|
|
|
|
|
// Implement the required Write function to satisfy the io.Writer interface.
|
|
|
|
func (nw noopWriter) Write(b []byte) (int, error) {
|
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
|
2021-03-08 01:31:45 +00:00
|
|
|
// Attach attaches to the docker container itself and ensures that we can pipe
|
2022-01-30 17:56:25 +00:00
|
|
|
// data in and out of the process stream. This should always be called before
|
|
|
|
// you have started the container, but after you've ensured it exists.
|
2021-01-07 04:36:29 +00:00
|
|
|
//
|
2021-03-08 01:31:45 +00:00
|
|
|
// Calling this function will poll resources for the container in the background
|
2022-01-30 17:56:25 +00:00
|
|
|
// until the container is stopped. The context provided to this function is used
|
|
|
|
// for the purposes of attaching to the container, a seecond context is created
|
|
|
|
// within the function for managing polling.
|
2021-09-11 21:13:19 +00:00
|
|
|
func (e *Environment) Attach(ctx context.Context) error {
|
2020-08-11 04:38:42 +00:00
|
|
|
if e.IsAttached() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := e.followOutput(); err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return err
|
2020-08-11 04:38:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
opts := types.ContainerAttachOptions{
|
|
|
|
Stdin: true,
|
|
|
|
Stdout: true,
|
|
|
|
Stderr: true,
|
|
|
|
Stream: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the stream again with the container.
|
2021-09-11 21:13:19 +00:00
|
|
|
if st, err := e.client.ContainerAttach(ctx, e.Id, opts); err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return err
|
2020-08-11 04:38:42 +00:00
|
|
|
} else {
|
|
|
|
e.SetStream(&st)
|
|
|
|
}
|
|
|
|
|
2021-01-07 04:36:29 +00:00
|
|
|
go func() {
|
2021-09-11 21:13:19 +00:00
|
|
|
// Don't use the context provided to the function, that'll cause the polling to
|
|
|
|
// exit unexpectedly. We want a custom context for this, the one passed to the
|
|
|
|
// function is to avoid a hang situation when trying to attach to a container.
|
|
|
|
pollCtx, cancel := context.WithCancel(context.Background())
|
2020-08-19 04:38:42 +00:00
|
|
|
defer cancel()
|
2021-01-09 05:21:09 +00:00
|
|
|
defer e.stream.Close()
|
2020-08-11 04:38:42 +00:00
|
|
|
defer func() {
|
2020-11-07 05:53:00 +00:00
|
|
|
e.SetState(environment.ProcessOfflineState)
|
2020-08-11 04:38:42 +00:00
|
|
|
e.SetStream(nil)
|
|
|
|
}()
|
|
|
|
|
2021-01-07 04:36:29 +00:00
|
|
|
go func() {
|
2021-09-11 21:13:19 +00:00
|
|
|
if err := e.pollResources(pollCtx); err != nil {
|
2020-11-11 04:36:40 +00:00
|
|
|
if !errors.Is(err, context.Canceled) {
|
2021-01-07 04:36:29 +00:00
|
|
|
e.log().WithField("error", err).Error("error during environment resource polling")
|
2020-11-11 04:36:40 +00:00
|
|
|
} else {
|
2021-01-07 04:36:29 +00:00
|
|
|
e.log().Warn("stopping server resource polling: context canceled")
|
2020-11-11 04:36:40 +00:00
|
|
|
}
|
2020-09-04 04:19:06 +00:00
|
|
|
}
|
2021-01-07 04:36:29 +00:00
|
|
|
}()
|
2020-08-19 04:38:42 +00:00
|
|
|
|
2021-01-07 04:47:44 +00:00
|
|
|
// Block the completion of this routine until the container is no longer running. This allows
|
|
|
|
// the pollResources function to run until it needs to be stopped. Because the container
|
2021-01-08 02:32:15 +00:00
|
|
|
// can be polled for resource usage, even when stopped, we need to have this logic present
|
2021-01-07 04:47:44 +00:00
|
|
|
// in order to cancel the context and therefore stop the routine that is spawned.
|
2021-01-09 05:21:09 +00:00
|
|
|
//
|
|
|
|
// For now, DO NOT use client#ContainerWait from the Docker package. There is a nasty
|
|
|
|
// bug causing containers to hang on deletion and cause servers to lock up on the system.
|
|
|
|
//
|
|
|
|
// This weird code isn't intuitive, but it keeps the function from ending until the container
|
|
|
|
// is stopped and therefore the stream reader ends up closed.
|
|
|
|
// @see https://github.com/moby/moby/issues/41827
|
|
|
|
c := new(noopWriter)
|
|
|
|
if _, err := io.Copy(c, e.stream.Reader); err != nil {
|
|
|
|
e.log().WithField("error", err).Error("could not copy from environment stream to noop writer")
|
2020-09-04 04:21:42 +00:00
|
|
|
}
|
2021-01-07 04:36:29 +00:00
|
|
|
}()
|
2020-08-11 04:38:42 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-08 01:31:45 +00:00
|
|
|
// InSituUpdate performs an in-place update of the Docker container's resource
|
|
|
|
// limits without actually making any changes to the operational state of the
|
|
|
|
// container. This allows memory, cpu, and IO limitations to be adjusted on the
|
|
|
|
// fly for individual instances.
|
2020-08-11 04:38:42 +00:00
|
|
|
func (e *Environment) InSituUpdate() error {
|
2021-03-08 01:31:45 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
|
|
|
defer cancel()
|
|
|
|
|
2022-01-23 22:13:49 +00:00
|
|
|
if _, err := e.ContainerInspect(ctx); err != nil {
|
2020-08-11 04:38:42 +00:00
|
|
|
// If the container doesn't exist for some reason there really isn't anything
|
|
|
|
// we can do to fix that in this process (it doesn't make sense at least). In those
|
|
|
|
// cases just return without doing anything since we still want to save the configuration
|
|
|
|
// to the disk.
|
|
|
|
//
|
|
|
|
// We'll let a boot process make modifications to the container if needed at this point.
|
|
|
|
if client.IsErrNotFound(err) {
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-08 01:31:45 +00:00
|
|
|
return errors.Wrap(err, "environment/docker: could not inspect container")
|
2020-08-11 04:38:42 +00:00
|
|
|
}
|
|
|
|
|
2021-03-08 01:31:45 +00:00
|
|
|
// CPU pinning cannot be removed once it is applied to a container. The same is true
|
|
|
|
// for removing memory limits, a container must be re-created.
|
|
|
|
//
|
|
|
|
// @see https://github.com/moby/moby/issues/41946
|
|
|
|
if _, err := e.client.ContainerUpdate(ctx, e.Id, container.UpdateConfig{
|
2021-06-21 00:21:51 +00:00
|
|
|
Resources: e.Configuration.Limits().AsContainerResources(),
|
2021-03-08 01:31:45 +00:00
|
|
|
}); err != nil {
|
|
|
|
return errors.Wrap(err, "environment/docker: could not update container")
|
2020-08-11 04:38:42 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-02 21:07:00 +00:00
|
|
|
// Create creates a new container for the server using all the data that is
|
2021-03-08 01:31:45 +00:00
|
|
|
// currently available for it. If the container already exists it will be
|
|
|
|
// returned.
|
2020-08-11 04:38:42 +00:00
|
|
|
func (e *Environment) Create() error {
|
|
|
|
// If the container already exists don't hit the user with an error, just return
|
|
|
|
// the current information about it which is what we would do when creating the
|
|
|
|
// container anyways.
|
2022-01-23 22:13:49 +00:00
|
|
|
if _, err := e.ContainerInspect(context.Background()); err == nil {
|
2020-08-11 04:38:42 +00:00
|
|
|
return nil
|
|
|
|
} else if !client.IsErrNotFound(err) {
|
2021-04-03 19:52:32 +00:00
|
|
|
return errors.Wrap(err, "environment/docker: failed to inspect container")
|
2020-08-11 04:38:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try to pull the requested image before creating the container.
|
|
|
|
if err := e.ensureImageExists(e.meta.Image); err != nil {
|
2021-04-03 19:52:32 +00:00
|
|
|
return errors.WithStackIf(err)
|
2020-08-11 04:38:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
a := e.Configuration.Allocations()
|
|
|
|
|
2020-09-07 22:37:35 +00:00
|
|
|
evs := e.Configuration.EnvironmentVariables()
|
|
|
|
for i, v := range evs {
|
|
|
|
// Convert 127.0.0.1 to the pterodactyl0 network interface if the environment is Docker
|
|
|
|
// so that the server operates as expected.
|
|
|
|
if v == "SERVER_IP=127.0.0.1" {
|
2020-09-13 04:37:48 +00:00
|
|
|
evs[i] = "SERVER_IP=" + config.Get().Docker.Network.Interface
|
2020-09-07 22:37:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 04:38:42 +00:00
|
|
|
conf := &container.Config{
|
|
|
|
Hostname: e.Id,
|
|
|
|
Domainname: config.Get().Docker.Domainname,
|
2022-01-20 01:05:53 +00:00
|
|
|
User: strconv.Itoa(config.Get().System.User.Uid) + ":" + strconv.Itoa(config.Get().System.User.Gid),
|
2020-08-11 04:38:42 +00:00
|
|
|
AttachStdin: true,
|
|
|
|
AttachStdout: true,
|
|
|
|
AttachStderr: true,
|
|
|
|
OpenStdin: true,
|
|
|
|
Tty: true,
|
|
|
|
ExposedPorts: a.Exposed(),
|
2021-05-02 18:00:10 +00:00
|
|
|
Image: strings.TrimPrefix(e.meta.Image, "~"),
|
2020-08-28 03:28:29 +00:00
|
|
|
Env: e.Configuration.EnvironmentVariables(),
|
2020-08-11 04:38:42 +00:00
|
|
|
Labels: map[string]string{
|
|
|
|
"Service": "Pterodactyl",
|
|
|
|
"ContainerType": "server_process",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-08-13 03:38:02 +00:00
|
|
|
tmpfsSize := strconv.Itoa(int(config.Get().Docker.TmpfsSize))
|
|
|
|
|
2020-08-11 04:38:42 +00:00
|
|
|
hostConf := &container.HostConfig{
|
2020-09-07 22:33:47 +00:00
|
|
|
PortBindings: a.DockerBindings(),
|
2020-08-11 04:38:42 +00:00
|
|
|
|
|
|
|
// Configure the mounts for this container. First mount the server data directory
|
2021-08-02 21:07:00 +00:00
|
|
|
// into the container as an r/w bind.
|
2020-08-11 04:38:42 +00:00
|
|
|
Mounts: e.convertMounts(),
|
|
|
|
|
|
|
|
// Configure the /tmp folder mapping in containers. This is necessary for some
|
|
|
|
// games that need to make use of it for downloads and other installation processes.
|
|
|
|
Tmpfs: map[string]string{
|
2020-09-05 19:08:40 +00:00
|
|
|
"/tmp": "rw,exec,nosuid,size=" + tmpfsSize + "M",
|
2020-08-11 04:38:42 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// Define resource limits for the container based on the data passed through
|
|
|
|
// from the Panel.
|
2021-06-21 00:21:51 +00:00
|
|
|
Resources: e.Configuration.Limits().AsContainerResources(),
|
2020-08-11 04:38:42 +00:00
|
|
|
|
|
|
|
DNS: config.Get().Docker.Network.Dns,
|
|
|
|
|
|
|
|
// Configure logging for the container to make it easier on the Daemon to grab
|
|
|
|
// the server output. Ensure that we don't use too much space on the host machine
|
|
|
|
// since we only need it for the last few hundred lines of output and don't care
|
|
|
|
// about anything else in it.
|
|
|
|
LogConfig: container.LogConfig{
|
2022-01-31 00:51:23 +00:00
|
|
|
Type: local.Name,
|
2020-08-11 04:38:42 +00:00
|
|
|
Config: map[string]string{
|
|
|
|
"max-size": "5m",
|
|
|
|
"max-file": "1",
|
2020-12-11 23:51:11 +00:00
|
|
|
"compress": "false",
|
2022-01-31 00:51:23 +00:00
|
|
|
"mode": "non-blocking",
|
2020-08-11 04:38:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
SecurityOpt: []string{"no-new-privileges"},
|
|
|
|
ReadonlyRootfs: true,
|
|
|
|
CapDrop: []string{
|
|
|
|
"setpcap", "mknod", "audit_write", "net_raw", "dac_override",
|
|
|
|
"fowner", "fsetid", "net_bind_service", "sys_chroot", "setfcap",
|
|
|
|
},
|
|
|
|
NetworkMode: container.NetworkMode(config.Get().Docker.Network.Mode),
|
|
|
|
}
|
|
|
|
|
2020-12-27 18:49:08 +00:00
|
|
|
if _, err := e.client.ContainerCreate(context.Background(), conf, hostConf, nil, nil, e.Id); err != nil {
|
2021-04-03 19:52:32 +00:00
|
|
|
return errors.Wrap(err, "environment/docker: failed to create container")
|
2020-08-11 04:38:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-08 01:31:45 +00:00
|
|
|
// Destroy will remove the Docker container from the server. If the container
|
|
|
|
// is currently running it will be forcibly stopped by Docker.
|
2020-08-11 04:38:42 +00:00
|
|
|
func (e *Environment) Destroy() error {
|
2020-09-05 19:08:40 +00:00
|
|
|
// We set it to stopping than offline to prevent crash detection from being triggered.
|
2020-11-07 05:53:00 +00:00
|
|
|
e.SetState(environment.ProcessStoppingState)
|
2020-08-11 04:38:42 +00:00
|
|
|
|
|
|
|
err := e.client.ContainerRemove(context.Background(), e.Id, types.ContainerRemoveOptions{
|
|
|
|
RemoveVolumes: true,
|
|
|
|
RemoveLinks: false,
|
|
|
|
Force: true,
|
|
|
|
})
|
|
|
|
|
2021-01-08 02:32:15 +00:00
|
|
|
e.SetState(environment.ProcessOfflineState)
|
|
|
|
|
2020-08-11 04:38:42 +00:00
|
|
|
// Don't trigger a destroy failure if we try to delete a container that does not
|
|
|
|
// exist on the system. We're just a step ahead of ourselves in that case.
|
|
|
|
//
|
|
|
|
// @see https://github.com/pterodactyl/panel/issues/2001
|
|
|
|
if err != nil && client.IsErrNotFound(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-08 01:31:45 +00:00
|
|
|
// SendCommand sends the specified command to the stdin of the running container
|
|
|
|
// instance. There is no confirmation that this data is sent successfully, only
|
|
|
|
// that it gets pushed into the stdin.
|
|
|
|
func (e *Environment) SendCommand(c string) error {
|
|
|
|
if !e.IsAttached() {
|
|
|
|
return errors.Wrap(ErrNotAttached, "environment/docker: cannot send command to container")
|
|
|
|
}
|
|
|
|
|
|
|
|
e.mu.RLock()
|
|
|
|
defer e.mu.RUnlock()
|
|
|
|
|
|
|
|
// If the command being processed is the same as the process stop command then we
|
|
|
|
// want to mark the server as entering the stopping state otherwise the process will
|
|
|
|
// stop and Wings will think it has crashed and attempt to restart it.
|
|
|
|
if e.meta.Stop.Type == "command" && c == e.meta.Stop.Value {
|
|
|
|
e.SetState(environment.ProcessStoppingState)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := e.stream.Conn.Write([]byte(c + "\n"))
|
|
|
|
|
|
|
|
return errors.Wrap(err, "environment/docker: could not write to container stream")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Readlog reads the log file for the server. This does not care if the server
|
|
|
|
// is running or not, it will simply try to read the last X bytes of the file
|
|
|
|
// and return them.
|
|
|
|
func (e *Environment) Readlog(lines int) ([]string, error) {
|
|
|
|
r, err := e.client.ContainerLogs(context.Background(), e.Id, types.ContainerLogsOptions{
|
|
|
|
ShowStdout: true,
|
|
|
|
ShowStderr: true,
|
|
|
|
Tail: strconv.Itoa(lines),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
defer r.Close()
|
|
|
|
|
|
|
|
var out []string
|
|
|
|
scanner := bufio.NewScanner(r)
|
|
|
|
for scanner.Scan() {
|
|
|
|
out = append(out, scanner.Text())
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attaches to the log for the container. This avoids us missing crucial output
|
|
|
|
// that happens in the split seconds before the code moves from 'Starting' to
|
|
|
|
// 'Attaching' on the process.
|
2020-08-11 04:38:42 +00:00
|
|
|
func (e *Environment) followOutput() error {
|
|
|
|
if exists, err := e.Exists(); !exists {
|
|
|
|
if err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return err
|
2020-08-11 04:38:42 +00:00
|
|
|
}
|
|
|
|
return errors.New(fmt.Sprintf("no such container: %s", e.Id))
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := types.ContainerLogsOptions{
|
|
|
|
ShowStderr: true,
|
|
|
|
ShowStdout: true,
|
|
|
|
Follow: true,
|
|
|
|
Since: time.Now().Format(time.RFC3339),
|
|
|
|
}
|
|
|
|
|
|
|
|
reader, err := e.client.ContainerLogs(context.Background(), e.Id, opts)
|
2020-12-26 01:09:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-08 15:15:19 +00:00
|
|
|
|
2021-01-08 15:19:33 +00:00
|
|
|
go e.scanOutput(reader)
|
2021-01-08 15:15:19 +00:00
|
|
|
|
2020-12-26 01:09:35 +00:00
|
|
|
return nil
|
2020-08-11 04:38:42 +00:00
|
|
|
}
|
|
|
|
|
2021-01-08 15:19:33 +00:00
|
|
|
func (e *Environment) scanOutput(reader io.ReadCloser) {
|
|
|
|
defer reader.Close()
|
2021-01-08 15:15:19 +00:00
|
|
|
|
2022-01-18 03:23:29 +00:00
|
|
|
if err := system.ScanReader(reader, func(v []byte) {
|
|
|
|
e.logCallbackMx.Lock()
|
|
|
|
defer e.logCallbackMx.Unlock()
|
|
|
|
e.logCallback(v)
|
2021-08-02 21:07:00 +00:00
|
|
|
}); err != nil && err != io.EOF {
|
2021-01-08 15:19:33 +00:00
|
|
|
log.WithField("error", err).WithField("container_id", e.Id).Warn("error processing scanner line in console output")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return here if the server is offline or currently stopping.
|
|
|
|
if e.State() == environment.ProcessStoppingState || e.State() == environment.ProcessOfflineState {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-02 21:07:00 +00:00
|
|
|
// Close the current reader before starting a new one, the defer will still run,
|
2021-01-08 15:19:33 +00:00
|
|
|
// but it will do nothing if we already closed the stream.
|
|
|
|
_ = reader.Close()
|
|
|
|
|
|
|
|
// Start following the output of the server again.
|
|
|
|
go e.followOutput()
|
2020-08-11 04:38:42 +00:00
|
|
|
}
|
|
|
|
|
2021-03-08 01:31:45 +00:00
|
|
|
// Pulls the image from Docker. If there is an error while pulling the image
|
|
|
|
// from the source but the image already exists locally, we will report that
|
|
|
|
// error to the logger but continue with the process.
|
2020-08-11 04:38:42 +00:00
|
|
|
//
|
2021-03-08 01:31:45 +00:00
|
|
|
// The reasoning behind this is that Quay has had some serious outages as of
|
2021-08-02 21:07:00 +00:00
|
|
|
// late, and we don't need to block all the servers from booting just because
|
2021-03-08 01:31:45 +00:00
|
|
|
// of that. I'd imagine in a lot of cases an outage shouldn't affect users too
|
|
|
|
// badly. It'll at least keep existing servers working correctly if anything.
|
2020-08-11 04:38:42 +00:00
|
|
|
func (e *Environment) ensureImageExists(image string) error {
|
2020-09-13 04:37:48 +00:00
|
|
|
e.Events().Publish(environment.DockerImagePullStarted, "")
|
|
|
|
defer e.Events().Publish(environment.DockerImagePullCompleted, "")
|
|
|
|
|
2020-10-19 23:18:33 +00:00
|
|
|
// Images prefixed with a ~ are local images that we do not need to try and pull.
|
|
|
|
if strings.HasPrefix(image, "~") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-11 04:38:42 +00:00
|
|
|
// Give it up to 15 minutes to pull the image. I think this should cover 99.8% of cases where an
|
|
|
|
// image pull might fail. I can't imagine it will ever take more than 15 minutes to fully pull
|
|
|
|
// an image. Let me know when I am inevitably wrong here...
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*15)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Get a registry auth configuration from the config.
|
|
|
|
var registryAuth *config.RegistryConfiguration
|
|
|
|
for registry, c := range config.Get().Docker.Registries {
|
|
|
|
if !strings.HasPrefix(image, registry) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithField("registry", registry).Debug("using authentication for registry")
|
|
|
|
registryAuth = &c
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the ImagePullOptions.
|
|
|
|
imagePullOptions := types.ImagePullOptions{All: false}
|
|
|
|
if registryAuth != nil {
|
|
|
|
b64, err := registryAuth.Base64()
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("failed to get registry auth credentials")
|
|
|
|
}
|
|
|
|
|
|
|
|
// b64 is a string so if there is an error it will just be empty, not nil.
|
|
|
|
imagePullOptions.RegistryAuth = b64
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := e.client.ImagePull(ctx, image, imagePullOptions)
|
|
|
|
if err != nil {
|
|
|
|
images, ierr := e.client.ImageList(ctx, types.ImageListOptions{})
|
|
|
|
if ierr != nil {
|
|
|
|
// Well damn, something has gone really wrong here, just go ahead and abort there
|
|
|
|
// isn't much anything we can do to try and self-recover from this.
|
2021-04-03 19:52:32 +00:00
|
|
|
return errors.Wrap(ierr, "environment/docker: failed to list images")
|
2020-08-11 04:38:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, img := range images {
|
|
|
|
for _, t := range img.RepoTags {
|
|
|
|
if t != image {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"image": image,
|
|
|
|
"container_id": e.Id,
|
2020-09-20 20:20:42 +00:00
|
|
|
"err": err.Error(),
|
2020-08-11 04:38:42 +00:00
|
|
|
}).Warn("unable to pull requested image from remote source, however the image exists locally")
|
|
|
|
|
|
|
|
// Okay, we found a matching container image, in that case just go ahead and return
|
|
|
|
// from this function, since there is nothing else we need to do here.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-03 19:52:32 +00:00
|
|
|
return errors.Wrapf(err, "environment/docker: failed to pull \"%s\" image for server", image)
|
2020-08-11 04:38:42 +00:00
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
|
|
|
|
log.WithField("image", image).Debug("pulling docker image... this could take a bit of time")
|
|
|
|
|
|
|
|
// I'm not sure what the best approach here is, but this will block execution until the image
|
2020-09-05 19:08:40 +00:00
|
|
|
// is done being pulled, which is what we need.
|
2020-08-11 04:38:42 +00:00
|
|
|
scanner := bufio.NewScanner(out)
|
2021-01-08 15:15:19 +00:00
|
|
|
|
2020-08-11 04:38:42 +00:00
|
|
|
for scanner.Scan() {
|
2022-01-23 20:17:40 +00:00
|
|
|
b := scanner.Bytes()
|
|
|
|
status, _ := jsonparser.GetString(b, "status")
|
|
|
|
progress, _ := jsonparser.GetString(b, "progress")
|
2021-01-08 15:15:19 +00:00
|
|
|
|
2022-01-23 20:17:40 +00:00
|
|
|
e.Events().Publish(environment.DockerImagePullStatus, status+" "+progress)
|
2020-08-11 04:38:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-13 04:37:48 +00:00
|
|
|
log.WithField("image", image).Debug("completed docker image pull")
|
|
|
|
|
2020-08-11 04:38:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-03-08 01:31:45 +00:00
|
|
|
|
|
|
|
func (e *Environment) convertMounts() []mount.Mount {
|
|
|
|
var out []mount.Mount
|
|
|
|
|
|
|
|
for _, m := range e.Configuration.Mounts() {
|
|
|
|
out = append(out, mount.Mount{
|
|
|
|
Type: mount.TypeBind,
|
|
|
|
Source: m.Source,
|
|
|
|
Target: m.Target,
|
|
|
|
ReadOnly: m.ReadOnly,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|