github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/client/state/08types.go (about) 1 package state 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "strings" 7 8 "github.com/hashicorp/nomad/client/allocrunner/taskrunner/state" 9 "github.com/hashicorp/nomad/helper/uuid" 10 "github.com/hashicorp/nomad/nomad/structs" 11 "github.com/hashicorp/nomad/plugins/drivers" 12 pstructs "github.com/hashicorp/nomad/plugins/shared/structs" 13 ) 14 15 // allocRunnerMutableState08 is state that had to be written on each save as it 16 // changed over the life-cycle of the alloc_runner in Nomad 0.8. 17 // 18 // https://github.com/hashicorp/nomad/blob/v0.8.6/client/alloc_runner.go#L146-L153 19 type allocRunnerMutableState08 struct { 20 // AllocClientStatus does not need to be upgraded as it is computed 21 // from task states. 22 AllocClientStatus string 23 24 // AllocClientDescription does not need to be upgraded as it is computed 25 // from task states. 26 AllocClientDescription string 27 28 TaskStates map[string]*structs.TaskState 29 DeploymentStatus *structs.AllocDeploymentStatus 30 } 31 32 // taskRunnerState08 was used to snapshot the state of the task runner in Nomad 33 // 0.8. 34 // 35 // https://github.com/hashicorp/nomad/blob/v0.8.6/client/task_runner.go#L188-L197 36 // COMPAT(0.10): Allows upgrading from 0.8.X to 0.9.0. 37 type taskRunnerState08 struct { 38 Version string 39 HandleID string 40 ArtifactDownloaded bool 41 TaskDirBuilt bool 42 PayloadRendered bool 43 DriverNetwork *drivers.DriverNetwork 44 // Created Resources are no longer used. 45 //CreatedResources *driver.CreatedResources 46 } 47 48 type TaskRunnerHandle08 struct { 49 // Docker specific handle info 50 ContainerID string `json:"ContainerID"` 51 Image string `json:"Image"` 52 53 // LXC specific handle info 54 ContainerName string `json:"ContainerName"` 55 LxcPath string `json:"LxcPath"` 56 57 // Executor reattach config 58 PluginConfig struct { 59 Pid int `json:"Pid"` 60 AddrNet string `json:"AddrNet"` 61 AddrName string `json:"AddrName"` 62 } `json:"PluginConfig"` 63 } 64 65 func (t *TaskRunnerHandle08) ReattachConfig() *pstructs.ReattachConfig { 66 return &pstructs.ReattachConfig{ 67 Network: t.PluginConfig.AddrNet, 68 Addr: t.PluginConfig.AddrName, 69 Pid: t.PluginConfig.Pid, 70 } 71 } 72 73 func (t *taskRunnerState08) Upgrade(allocID, taskName string) (*state.LocalState, error) { 74 ls := state.NewLocalState() 75 76 // Reuse DriverNetwork 77 ls.DriverNetwork = t.DriverNetwork 78 79 // Upgrade artifact state 80 ls.Hooks["artifacts"] = &state.HookState{ 81 PrestartDone: t.ArtifactDownloaded, 82 } 83 84 // Upgrade task dir state 85 ls.Hooks["task_dir"] = &state.HookState{ 86 Data: map[string]string{ 87 // "is_done" is equivalent to task_dir_hook.TaskDirHookIsDoneKey 88 // Does not import to avoid import cycle 89 "is_done": fmt.Sprintf("%v", t.TaskDirBuilt), 90 }, 91 } 92 93 // Upgrade dispatch payload state 94 ls.Hooks["dispatch_payload"] = &state.HookState{ 95 PrestartDone: t.PayloadRendered, 96 } 97 98 // Add necessary fields to TaskConfig 99 ls.TaskHandle = drivers.NewTaskHandle(drivers.Pre09TaskHandleVersion) 100 ls.TaskHandle.Config = &drivers.TaskConfig{ 101 ID: fmt.Sprintf("pre09-%s", uuid.Generate()), 102 Name: taskName, 103 AllocID: allocID, 104 } 105 106 ls.TaskHandle.State = drivers.TaskStateUnknown 107 108 // The docker driver prefixed the handle with 'DOCKER:' 109 // Strip so that it can be unmarshalled 110 data := strings.TrimPrefix(t.HandleID, "DOCKER:") 111 112 // The pre09 driver handle ID is given to the driver. It is unmarshalled 113 // here to check for errors 114 if _, err := UnmarshalPre09HandleID([]byte(data)); err != nil { 115 return nil, err 116 } 117 118 ls.TaskHandle.DriverState = []byte(data) 119 120 return ls, nil 121 } 122 123 // UnmarshalPre09HandleID decodes the pre09 json encoded handle ID 124 func UnmarshalPre09HandleID(raw []byte) (*TaskRunnerHandle08, error) { 125 var handle TaskRunnerHandle08 126 if err := json.Unmarshal(raw, &handle); err != nil { 127 return nil, fmt.Errorf("failed to decode 0.8 driver state: %v", err) 128 } 129 130 return &handle, nil 131 }