|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: BUSL-1.1 |
| 3 | + |
| 4 | +package hostvolumemanager |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "context" |
| 9 | + "encoding/json" |
| 10 | + "fmt" |
| 11 | + "io" |
| 12 | + "os" |
| 13 | + "os/exec" |
| 14 | + "path/filepath" |
| 15 | + |
| 16 | + "github.com/hashicorp/go-hclog" |
| 17 | + "github.com/hashicorp/go-multierror" |
| 18 | + cstructs "github.com/hashicorp/nomad/client/structs" |
| 19 | + "github.com/hashicorp/nomad/helper" |
| 20 | +) |
| 21 | + |
| 22 | +type HostVolumePlugin interface { |
| 23 | + Version(ctx context.Context) (string, error) |
| 24 | + Create(ctx context.Context, req *cstructs.ClientHostVolumeCreateRequest) (*HostVolumePluginCreateResponse, error) |
| 25 | + Delete(ctx context.Context, req *cstructs.ClientHostVolumeDeleteRequest) error |
| 26 | + // db TODO(1.10.0): update? resize? ?? |
| 27 | +} |
| 28 | + |
| 29 | +type HostVolumePluginCreateResponse struct { |
| 30 | + Path string `json:"path"` |
| 31 | + SizeBytes int64 `json:"bytes"` |
| 32 | + Context map[string]string `json:"context"` // metadata |
| 33 | +} |
| 34 | + |
| 35 | +var _ HostVolumePlugin = &HostVolumePluginMkdir{} |
| 36 | + |
| 37 | +type HostVolumePluginMkdir struct { |
| 38 | + ID string |
| 39 | + TargetPath string |
| 40 | + |
| 41 | + log hclog.Logger |
| 42 | +} |
| 43 | + |
| 44 | +func (p *HostVolumePluginMkdir) Version(_ context.Context) (string, error) { |
| 45 | + return "0.0.1", nil |
| 46 | +} |
| 47 | + |
| 48 | +func (p *HostVolumePluginMkdir) Create(_ context.Context, |
| 49 | + req *cstructs.ClientHostVolumeCreateRequest) (*HostVolumePluginCreateResponse, error) { |
| 50 | + |
| 51 | + path := filepath.Join(p.TargetPath, req.ID) |
| 52 | + log := p.log.With( |
| 53 | + "operation", "create", |
| 54 | + "volume_id", req.ID, |
| 55 | + "path", path) |
| 56 | + log.Debug("running plugin") |
| 57 | + |
| 58 | + err := os.Mkdir(path, 0o700) |
| 59 | + if err != nil { |
| 60 | + log.Debug("error with plugin", "error", err) |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + |
| 64 | + log.Debug("plugin ran successfully") |
| 65 | + return &HostVolumePluginCreateResponse{ |
| 66 | + Path: path, |
| 67 | + SizeBytes: 0, |
| 68 | + Context: map[string]string{}, |
| 69 | + }, nil |
| 70 | +} |
| 71 | + |
| 72 | +func (p *HostVolumePluginMkdir) Delete(_ context.Context, req *cstructs.ClientHostVolumeDeleteRequest) error { |
| 73 | + path := filepath.Join(p.TargetPath, req.ID) |
| 74 | + log := p.log.With( |
| 75 | + "operation", "delete", |
| 76 | + "volume_id", req.ID, |
| 77 | + "path", path) |
| 78 | + log.Debug("running plugin") |
| 79 | + |
| 80 | + err := os.RemoveAll(path) |
| 81 | + if err != nil { |
| 82 | + log.Debug("error with plugin", "error", err) |
| 83 | + return err |
| 84 | + } |
| 85 | + |
| 86 | + log.Debug("plugin ran successfully") |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +var _ HostVolumePlugin = &HostVolumePluginExternal{} |
| 91 | + |
| 92 | +type HostVolumePluginExternal struct { |
| 93 | + ID string |
| 94 | + Executable string |
| 95 | + TargetPath string |
| 96 | + |
| 97 | + log hclog.Logger |
| 98 | +} |
| 99 | + |
| 100 | +func (p *HostVolumePluginExternal) Version(_ context.Context) (string, error) { |
| 101 | + return "0.0.1", nil // db TODO(1.10.0): call the plugin, use in fingerprint |
| 102 | +} |
| 103 | + |
| 104 | +func (p *HostVolumePluginExternal) Create(ctx context.Context, |
| 105 | + req *cstructs.ClientHostVolumeCreateRequest) (*HostVolumePluginCreateResponse, error) { |
| 106 | + |
| 107 | + params, err := json.Marshal(req.Parameters) // db TODO(1.10.0): if this is nil, then PARAMETERS env will be "null" |
| 108 | + if err != nil { |
| 109 | + return nil, fmt.Errorf("error marshaling volume pramaters: %w", err) |
| 110 | + } |
| 111 | + envVars := []string{ |
| 112 | + "NODE_ID=" + req.NodeID, |
| 113 | + "VOLUME_NAME=" + req.Name, |
| 114 | + fmt.Sprintf("CAPACITY_MIN_BYTES=%d", req.RequestedCapacityMinBytes), |
| 115 | + fmt.Sprintf("CAPACITY_MAX_BYTES=%d", req.RequestedCapacityMaxBytes), |
| 116 | + "PARAMETERS=" + string(params), |
| 117 | + } |
| 118 | + |
| 119 | + stdout, _, err := p.runPlugin(ctx, "create", req.ID, envVars) |
| 120 | + if err != nil { |
| 121 | + return nil, fmt.Errorf("error creating volume %q with plugin %q: %w", req.ID, req.PluginID, err) |
| 122 | + } |
| 123 | + |
| 124 | + var pluginResp HostVolumePluginCreateResponse |
| 125 | + err = json.Unmarshal(stdout, &pluginResp) |
| 126 | + if err != nil { |
| 127 | + return nil, err |
| 128 | + } |
| 129 | + return &pluginResp, nil |
| 130 | +} |
| 131 | + |
| 132 | +func (p *HostVolumePluginExternal) Delete(ctx context.Context, |
| 133 | + req *cstructs.ClientHostVolumeDeleteRequest) error { |
| 134 | + |
| 135 | + params, err := json.Marshal(req.Parameters) |
| 136 | + if err != nil { |
| 137 | + return fmt.Errorf("error marshaling volume pramaters: %w", err) |
| 138 | + } |
| 139 | + envVars := []string{ |
| 140 | + "NODE_ID=" + req.NodeID, |
| 141 | + "PARAMETERS=" + string(params), |
| 142 | + } |
| 143 | + |
| 144 | + _, _, err = p.runPlugin(ctx, "delete", req.ID, envVars) |
| 145 | + if err != nil { |
| 146 | + return fmt.Errorf("error deleting volume %q with plugin %q: %w", req.ID, req.PluginID, err) |
| 147 | + } |
| 148 | + return nil |
| 149 | +} |
| 150 | + |
| 151 | +func (p *HostVolumePluginExternal) runPlugin(ctx context.Context, |
| 152 | + op, volID string, env []string) (stdout, stderr []byte, err error) { |
| 153 | + |
| 154 | + path := filepath.Join(p.TargetPath, volID) |
| 155 | + log := p.log.With( |
| 156 | + "operation", op, |
| 157 | + "volume_id", volID, |
| 158 | + "path", path) |
| 159 | + log.Debug("running plugin") |
| 160 | + |
| 161 | + // set up plugin execution |
| 162 | + cmd := exec.CommandContext(ctx, p.Executable, op, path) |
| 163 | + |
| 164 | + cmd.Env = append([]string{ |
| 165 | + "OPERATION=" + op, |
| 166 | + "HOST_PATH=" + path, |
| 167 | + }, env...) |
| 168 | + |
| 169 | + var errBuf bytes.Buffer |
| 170 | + cmd.Stderr = io.Writer(&errBuf) |
| 171 | + |
| 172 | + // run the command and capture output |
| 173 | + mErr := &multierror.Error{} |
| 174 | + stdout, err = cmd.Output() |
| 175 | + if err != nil { |
| 176 | + mErr = multierror.Append(mErr, err) |
| 177 | + } |
| 178 | + stderr, err = io.ReadAll(&errBuf) |
| 179 | + if err != nil { |
| 180 | + mErr = multierror.Append(mErr, err) |
| 181 | + } |
| 182 | + |
| 183 | + log = log.With( |
| 184 | + "stdout", string(stdout), |
| 185 | + "stderr", string(stderr), |
| 186 | + ) |
| 187 | + if mErr.ErrorOrNil() != nil { |
| 188 | + err = helper.FlattenMultierror(mErr) |
| 189 | + log.Debug("error with plugin", "error", err) |
| 190 | + return stdout, stderr, err |
| 191 | + } |
| 192 | + log.Debug("plugin ran successfully") |
| 193 | + return stdout, stderr, nil |
| 194 | +} |
0 commit comments