2018-07-17 00:19:56 +00:00
|
|
|
package taskrunner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
log "github.com/hashicorp/go-hclog"
|
2018-10-04 23:22:01 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
|
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/taskrunner/getter"
|
|
|
|
ti "github.com/hashicorp/nomad/client/allocrunner/taskrunner/interfaces"
|
2018-07-17 00:19:56 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// artifactHook downloads artifacts for a task.
|
|
|
|
type artifactHook struct {
|
|
|
|
eventEmitter ti.EventEmitter
|
|
|
|
logger log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func newArtifactHook(e ti.EventEmitter, logger log.Logger) *artifactHook {
|
|
|
|
h := &artifactHook{
|
|
|
|
eventEmitter: e,
|
|
|
|
}
|
|
|
|
h.logger = logger.Named(h.Name())
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*artifactHook) Name() string {
|
2018-12-07 01:24:43 +00:00
|
|
|
// Copied in client/state when upgrading from <0.9 schemas, so if you
|
|
|
|
// change it here you also must change it there.
|
2018-07-17 00:19:56 +00:00
|
|
|
return "artifacts"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *artifactHook) Prestart(ctx context.Context, req *interfaces.TaskPrestartRequest, resp *interfaces.TaskPrestartResponse) error {
|
2018-08-08 00:38:13 +00:00
|
|
|
if len(req.Task.Artifacts) == 0 {
|
|
|
|
resp.Done = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-21 23:37:22 +00:00
|
|
|
// Initialize hook state to store download progress
|
|
|
|
resp.State = make(map[string]string, len(req.Task.Artifacts))
|
2019-02-20 16:41:51 +00:00
|
|
|
|
2018-07-17 20:48:53 +00:00
|
|
|
h.eventEmitter.EmitEvent(structs.NewTaskEvent(structs.TaskDownloadingArtifacts))
|
2018-07-17 00:19:56 +00:00
|
|
|
|
|
|
|
for _, artifact := range req.Task.Artifacts {
|
2019-02-20 16:41:51 +00:00
|
|
|
aid := artifact.Hash()
|
2019-02-21 23:37:22 +00:00
|
|
|
if req.PreviousState[aid] != "" {
|
2019-02-20 16:41:51 +00:00
|
|
|
h.logger.Trace("skipping already downloaded artifact", "artifact", artifact.GetterSource)
|
2019-02-21 23:37:22 +00:00
|
|
|
resp.State[aid] = req.PreviousState[aid]
|
2019-02-20 16:41:51 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
h.logger.Debug("downloading artifact", "artifact", artifact.GetterSource)
|
2018-07-17 00:19:56 +00:00
|
|
|
//XXX add ctx to GetArtifact to allow cancelling long downloads
|
2020-12-14 17:56:34 +00:00
|
|
|
if err := getter.GetArtifact(req.TaskEnv, artifact); err != nil {
|
|
|
|
|
2019-02-13 16:26:23 +00:00
|
|
|
wrapped := structs.NewRecoverableError(
|
|
|
|
fmt.Errorf("failed to download artifact %q: %v", artifact.GetterSource, err),
|
|
|
|
true,
|
|
|
|
)
|
2018-12-13 17:21:32 +00:00
|
|
|
herr := NewHookError(wrapped, structs.NewTaskEvent(structs.TaskArtifactDownloadFailed).SetDownloadError(wrapped))
|
|
|
|
|
|
|
|
return herr
|
2018-07-17 00:19:56 +00:00
|
|
|
}
|
2019-02-20 16:41:51 +00:00
|
|
|
|
|
|
|
// Mark artifact as downloaded to avoid re-downloading due to
|
|
|
|
// retries caused by subsequent artifacts failing. Any
|
|
|
|
// non-empty value works.
|
2019-02-21 23:37:22 +00:00
|
|
|
resp.State[aid] = "1"
|
2018-07-17 00:19:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp.Done = true
|
|
|
|
return nil
|
|
|
|
}
|