github.com/ilhicas/nomad@v0.11.8/drivers/docker/driver_pre09.go (about)

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