github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/exec/artifact_input_step.go (about)

     1  package exec
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"code.cloudfoundry.org/lager"
     8  	"code.cloudfoundry.org/lager/lagerctx"
     9  	"github.com/pf-qiu/concourse/v6/atc"
    10  	"github.com/pf-qiu/concourse/v6/atc/db"
    11  	"github.com/pf-qiu/concourse/v6/atc/exec/build"
    12  	"github.com/pf-qiu/concourse/v6/atc/runtime"
    13  	"github.com/pf-qiu/concourse/v6/atc/worker"
    14  )
    15  
    16  type ArtifactVolumeNotFoundError struct {
    17  	ArtifactName string
    18  }
    19  
    20  func (e ArtifactVolumeNotFoundError) Error() string {
    21  	return fmt.Sprintf("volume for worker artifact '%s' not found", e.ArtifactName)
    22  }
    23  
    24  type ArtifactInputStep struct {
    25  	plan         atc.Plan
    26  	build        db.Build
    27  	workerClient worker.Client
    28  }
    29  
    30  func NewArtifactInputStep(plan atc.Plan, build db.Build, workerClient worker.Client) Step {
    31  	return &ArtifactInputStep{
    32  		plan:         plan,
    33  		build:        build,
    34  		workerClient: workerClient,
    35  	}
    36  }
    37  
    38  func (step *ArtifactInputStep) Run(ctx context.Context, state RunState) (bool, error) {
    39  	logger := lagerctx.FromContext(ctx).WithData(lager.Data{
    40  		"plan-id": step.plan.ID,
    41  	})
    42  
    43  	buildArtifact, err := step.build.Artifact(step.plan.ArtifactInput.ArtifactID)
    44  	if err != nil {
    45  		return false, err
    46  	}
    47  
    48  	// TODO (runtime/#3607): artifact_input_step shouldn't know about db Volumem
    49  	//		has a runState with artifact repo. We could use that.
    50  	createdVolume, found, err := buildArtifact.Volume(step.build.TeamID())
    51  	if err != nil {
    52  		return false, err
    53  	}
    54  
    55  	if !found {
    56  		return false, ArtifactVolumeNotFoundError{buildArtifact.Name()}
    57  	}
    58  
    59  	// TODO (runtime/#3607): artifact_input_step shouldn't be looking up the volume on the worker
    60  	_, found, err = step.workerClient.FindVolume(logger, createdVolume.TeamID(), createdVolume.Handle())
    61  	if err != nil {
    62  		return false, err
    63  	}
    64  
    65  	if !found {
    66  		return false, ArtifactVolumeNotFoundError{buildArtifact.Name()}
    67  	}
    68  
    69  	art := runtime.TaskArtifact{
    70  		VolumeHandle: createdVolume.Handle(),
    71  	}
    72  
    73  	logger.Info("register-artifact-source", lager.Data{
    74  		"artifact_id": buildArtifact.ID(),
    75  		"handle":      art.ID(),
    76  	})
    77  
    78  	state.ArtifactRepository().RegisterArtifact(build.ArtifactName(step.plan.ArtifactInput.Name), &art)
    79  
    80  	return true, nil
    81  }