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 {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
//XXX add ctx to GetArtifact to allow cancelling long downloads
|
2018-10-10 19:31:59 +00:00
|
|
|
if err := getter.GetArtifact(req.TaskEnv, artifact, req.TaskDir.Dir); err != nil {
|
2018-07-17 00:19:56 +00:00
|
|
|
wrapped := fmt.Errorf("failed to download artifact %q: %v", artifact.GetterSource, err)
|
|
|
|
h.logger.Debug(wrapped.Error())
|
2018-07-17 20:48:53 +00:00
|
|
|
h.eventEmitter.EmitEvent(structs.NewTaskEvent(structs.TaskArtifactDownloadFailed).SetDownloadError(wrapped))
|
2018-07-17 00:19:56 +00:00
|
|
|
return wrapped
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Done = true
|
|
|
|
return nil
|
|
|
|
}
|