2018-12-07 01:24:43 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2019-01-14 17:25:59 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-01-23 19:44:42 +00:00
|
|
|
"strings"
|
2019-01-14 17:25:59 +00:00
|
|
|
|
2018-12-07 01:24:43 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/taskrunner/state"
|
2019-01-30 19:22:29 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2018-12-07 01:24:43 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
2019-01-15 18:54:37 +00:00
|
|
|
pstructs "github.com/hashicorp/nomad/plugins/shared/structs"
|
2018-12-07 01:24:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// allocRunnerMutableState08 is state that had to be written on each save as it
|
|
|
|
// changed over the life-cycle of the alloc_runner in Nomad 0.8.
|
|
|
|
//
|
|
|
|
// https://github.com/hashicorp/nomad/blob/v0.8.6/client/alloc_runner.go#L146-L153
|
|
|
|
//
|
|
|
|
type allocRunnerMutableState08 struct {
|
|
|
|
// AllocClientStatus does not need to be upgraded as it is computed
|
|
|
|
// from task states.
|
|
|
|
AllocClientStatus string
|
|
|
|
|
|
|
|
// AllocClientDescription does not need to be upgraded as it is computed
|
|
|
|
// from task states.
|
|
|
|
AllocClientDescription string
|
|
|
|
|
|
|
|
TaskStates map[string]*structs.TaskState
|
|
|
|
DeploymentStatus *structs.AllocDeploymentStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
// taskRunnerState08 was used to snapshot the state of the task runner in Nomad
|
|
|
|
// 0.8.
|
|
|
|
//
|
|
|
|
// https://github.com/hashicorp/nomad/blob/v0.8.6/client/task_runner.go#L188-L197
|
2019-01-18 23:09:38 +00:00
|
|
|
// COMPAT(0.10): Allows upgrading from 0.8.X to 0.9.0.
|
2018-12-07 01:24:43 +00:00
|
|
|
type taskRunnerState08 struct {
|
|
|
|
Version string
|
|
|
|
HandleID string
|
|
|
|
ArtifactDownloaded bool
|
|
|
|
TaskDirBuilt bool
|
|
|
|
PayloadRendered bool
|
2019-01-04 23:01:35 +00:00
|
|
|
DriverNetwork *drivers.DriverNetwork
|
2018-12-07 01:24:43 +00:00
|
|
|
// Created Resources are no longer used.
|
|
|
|
//CreatedResources *driver.CreatedResources
|
|
|
|
}
|
|
|
|
|
2019-01-23 19:44:42 +00:00
|
|
|
type TaskRunnerHandle08 struct {
|
|
|
|
// Docker specific handle info
|
|
|
|
ContainerID string `json:"ContainerID"`
|
|
|
|
Image string `json:"Image"`
|
|
|
|
|
|
|
|
// LXC specific handle info
|
|
|
|
ContainerName string `json:"ContainerName"`
|
|
|
|
LxcPath string `json:"LxcPath"`
|
|
|
|
|
|
|
|
// Executor reattach config
|
2019-01-14 17:25:59 +00:00
|
|
|
PluginConfig struct {
|
|
|
|
Pid int `json:"Pid"`
|
|
|
|
AddrNet string `json:"AddrNet"`
|
|
|
|
AddrName string `json:"AddrName"`
|
|
|
|
} `json:"PluginConfig"`
|
|
|
|
}
|
|
|
|
|
2019-01-24 02:01:24 +00:00
|
|
|
func (t *TaskRunnerHandle08) ReattachConfig() *pstructs.ReattachConfig {
|
2019-01-15 18:54:37 +00:00
|
|
|
return &pstructs.ReattachConfig{
|
2019-01-14 17:25:59 +00:00
|
|
|
Network: t.PluginConfig.AddrNet,
|
|
|
|
Addr: t.PluginConfig.AddrName,
|
|
|
|
Pid: t.PluginConfig.Pid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-17 02:52:31 +00:00
|
|
|
func (t *taskRunnerState08) Upgrade(allocID, taskName string) (*state.LocalState, error) {
|
2018-12-07 01:24:43 +00:00
|
|
|
ls := state.NewLocalState()
|
|
|
|
|
|
|
|
// Reuse DriverNetwork
|
|
|
|
ls.DriverNetwork = t.DriverNetwork
|
|
|
|
|
|
|
|
// Upgrade artifact state
|
|
|
|
ls.Hooks["artifacts"] = &state.HookState{
|
|
|
|
PrestartDone: t.ArtifactDownloaded,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upgrade task dir state
|
|
|
|
ls.Hooks["task_dir"] = &state.HookState{
|
2019-01-29 21:17:10 +00:00
|
|
|
Data: map[string]string{
|
|
|
|
// "is_done" is equivalent to task_dir_hook.TaskDirHookIsDoneKey
|
|
|
|
// Does not import to avoid import cycle
|
|
|
|
"is_done": fmt.Sprintf("%v", t.TaskDirBuilt),
|
|
|
|
},
|
2018-12-07 01:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Upgrade dispatch payload state
|
|
|
|
ls.Hooks["dispatch_payload"] = &state.HookState{
|
|
|
|
PrestartDone: t.PayloadRendered,
|
|
|
|
}
|
|
|
|
|
2019-01-17 02:52:31 +00:00
|
|
|
// Add necessary fields to TaskConfig
|
|
|
|
ls.TaskHandle = drivers.NewTaskHandle(drivers.Pre09TaskHandleVersion)
|
2019-01-14 17:25:59 +00:00
|
|
|
ls.TaskHandle.Config = &drivers.TaskConfig{
|
2019-01-30 19:22:29 +00:00
|
|
|
ID: fmt.Sprintf("pre09-%s", uuid.Generate()),
|
2019-01-14 17:25:59 +00:00
|
|
|
Name: taskName,
|
|
|
|
AllocID: allocID,
|
|
|
|
}
|
2018-12-07 01:24:43 +00:00
|
|
|
|
|
|
|
ls.TaskHandle.State = drivers.TaskStateUnknown
|
|
|
|
|
2019-01-23 19:44:42 +00:00
|
|
|
// The docker driver prefixed the handle with 'DOCKER:'
|
|
|
|
// Strip so that it can be unmarshalled
|
|
|
|
data := t.HandleID
|
|
|
|
if strings.HasPrefix(data, "DOCKER:") {
|
|
|
|
data = data[7:]
|
2019-01-14 17:25:59 +00:00
|
|
|
}
|
2019-01-23 19:44:42 +00:00
|
|
|
|
|
|
|
// The pre09 driver handle ID is given to the driver. It is unmarshalled
|
|
|
|
// here to check for errors
|
|
|
|
if _, err := UnmarshalPre09HandleID([]byte(data)); err != nil {
|
|
|
|
return nil, err
|
2019-01-14 17:25:59 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 19:44:42 +00:00
|
|
|
ls.TaskHandle.DriverState = []byte(data)
|
2018-12-07 01:24:43 +00:00
|
|
|
|
2019-01-17 02:52:31 +00:00
|
|
|
return ls, nil
|
2018-12-07 01:24:43 +00:00
|
|
|
}
|
2019-01-23 19:44:42 +00:00
|
|
|
|
|
|
|
// UnmarshalPre09HandleID decodes the pre09 json encoded handle ID
|
|
|
|
func UnmarshalPre09HandleID(raw []byte) (*TaskRunnerHandle08, error) {
|
|
|
|
var handle TaskRunnerHandle08
|
|
|
|
if err := json.Unmarshal(raw, &handle); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to decode 0.8 driver state: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &handle, nil
|
|
|
|
}
|