github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/drivers/docker/driver_pre09.go (about)

     1  package docker
     2  
     3  import (
     4  	"fmt"
     5  
     6  	docker "github.com/fsouza/go-dockerclient"
     7  	"github.com/hashicorp/nomad/client/state"
     8  	"github.com/hashicorp/nomad/drivers/docker/docklog"
     9  	"github.com/hashicorp/nomad/drivers/shared/executor"
    10  	"github.com/hashicorp/nomad/plugins/drivers"
    11  	pstructs "github.com/hashicorp/nomad/plugins/shared/structs"
    12  )
    13  
    14  func (d *Driver) recoverPre09Task(h *drivers.TaskHandle) error {
    15  	handle, err := state.UnmarshalPre09HandleID(h.DriverState)
    16  	if err != nil {
    17  		return fmt.Errorf("failed to decode pre09 driver handle: %v", err)
    18  	}
    19  
    20  	reattach, err := pstructs.ReattachConfigToGoPlugin(handle.ReattachConfig())
    21  	if err != nil {
    22  		return fmt.Errorf("failed to decode reattach config from pre09 handle: %v", err)
    23  	}
    24  
    25  	exec, pluginClient, err := executor.ReattachToPre09Executor(reattach,
    26  		d.logger.With("task_name", h.Config.Name, "alloc_id", h.Config.AllocID))
    27  	if err != nil {
    28  		d.logger.Error("failed to reattach to executor", "error", err, "task_name", h.Config.Name)
    29  		return fmt.Errorf("failed to reattach to executor: %v", err)
    30  	}
    31  
    32  	client, _, err := d.dockerClients()
    33  	if err != nil {
    34  		return fmt.Errorf("failed to get docker client: %v", err)
    35  	}
    36  
    37  	container, err := client.InspectContainerWithOptions(docker.InspectContainerOptions{
    38  		ID: handle.ContainerID,
    39  	})
    40  	if err != nil {
    41  		return fmt.Errorf("failed to inspect container for id %q: %v", handle.ContainerID, err)
    42  	}
    43  
    44  	th := &taskHandle{
    45  		client:                client,
    46  		waitClient:            waitClient,
    47  		dlogger:               &executorDockerLoggerShim{exec: exec},
    48  		dloggerPluginClient:   pluginClient,
    49  		logger:                d.logger.With("container_id", container.ID),
    50  		task:                  h.Config,
    51  		containerID:           container.ID,
    52  		containerImage:        container.Image,
    53  		doneCh:                make(chan bool),
    54  		waitCh:                make(chan struct{}),
    55  		removeContainerOnExit: d.config.GC.Container,
    56  	}
    57  
    58  	d.tasks.Set(h.Config.ID, th)
    59  	go th.run()
    60  	return nil
    61  }
    62  
    63  // executorDockerLoggerShim is used by upgraded tasks as the docker logger. When
    64  // the task exits, the Stop() func of the docker logger is called, this shim
    65  // will proxy that call to the executor Shutdown() func which will stop the
    66  // syslog server started by the pre09 executor
    67  type executorDockerLoggerShim struct {
    68  	exec executor.Executor
    69  }
    70  
    71  func (e *executorDockerLoggerShim) Start(*docklog.StartOpts) error { return nil }
    72  func (e *executorDockerLoggerShim) Stop() error {
    73  	if err := e.exec.Shutdown("docker", 0); err != nil {
    74  		return err
    75  	}
    76  
    77  	return nil
    78  }