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

     1  package worker
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  
     7  	"code.cloudfoundry.org/garden"
     8  	"code.cloudfoundry.org/lager"
     9  	"github.com/pf-qiu/concourse/v6/atc/db"
    10  	"github.com/pf-qiu/concourse/v6/atc/worker/gclient"
    11  )
    12  
    13  type workerHelper struct {
    14  	gardenClient  gclient.Client
    15  	volumeClient  VolumeClient
    16  	volumeRepo    db.VolumeRepository
    17  	dbTeamFactory db.TeamFactory
    18  	dbWorker      db.Worker
    19  }
    20  
    21  func (w workerHelper) createGardenContainer(
    22  	containerSpec ContainerSpec,
    23  	fetchedImage FetchedImage,
    24  	handleToCreate string,
    25  	bindMounts []garden.BindMount,
    26  ) (gclient.Container, error) {
    27  
    28  	gardenProperties := garden.Properties{}
    29  
    30  	if containerSpec.User != "" {
    31  		gardenProperties[userPropertyName] = containerSpec.User
    32  	} else {
    33  		gardenProperties[userPropertyName] = fetchedImage.Metadata.User
    34  	}
    35  
    36  	env := append(fetchedImage.Metadata.Env, containerSpec.Env...)
    37  
    38  	if w.dbWorker.HTTPProxyURL() != "" {
    39  		env = append(env, fmt.Sprintf("http_proxy=%s", w.dbWorker.HTTPProxyURL()))
    40  	}
    41  
    42  	if w.dbWorker.HTTPSProxyURL() != "" {
    43  		env = append(env, fmt.Sprintf("https_proxy=%s", w.dbWorker.HTTPSProxyURL()))
    44  	}
    45  
    46  	if w.dbWorker.NoProxy() != "" {
    47  		env = append(env, fmt.Sprintf("no_proxy=%s", w.dbWorker.NoProxy()))
    48  	}
    49  
    50  	return w.gardenClient.Create(
    51  		garden.ContainerSpec{
    52  			Handle:     handleToCreate,
    53  			RootFSPath: fetchedImage.URL,
    54  			Privileged: fetchedImage.Privileged,
    55  			BindMounts: bindMounts,
    56  			Limits:     containerSpec.Limits.ToGardenLimits(),
    57  			Env:        env,
    58  			Properties: gardenProperties,
    59  		})
    60  }
    61  
    62  func (w workerHelper) constructGardenWorkerContainer(
    63  	logger lager.Logger,
    64  	createdContainer db.CreatedContainer,
    65  	gardenContainer gclient.Container,
    66  ) (Container, error) {
    67  	createdVolumes, err := w.volumeRepo.FindVolumesForContainer(createdContainer)
    68  	if err != nil {
    69  		logger.Error("failed-to-find-container-volumes", err)
    70  		return nil, err
    71  	}
    72  	return newGardenWorkerContainer(
    73  		logger,
    74  		gardenContainer,
    75  		createdContainer,
    76  		createdVolumes,
    77  		w.gardenClient,
    78  		w.volumeClient,
    79  		w.dbWorker.Name(),
    80  	)
    81  }
    82  
    83  func anyMountTo(path string, destinationPaths []string) bool {
    84  	for _, destinationPath := range destinationPaths {
    85  		if filepath.Clean(destinationPath) == filepath.Clean(path) {
    86  			return true
    87  		}
    88  	}
    89  
    90  	return false
    91  }
    92  
    93  func getDestinationPathsFromInputs(inputs []InputSource) []string {
    94  	destinationPaths := make([]string, len(inputs))
    95  
    96  	for idx, input := range inputs {
    97  		destinationPaths[idx] = input.DestinationPath()
    98  	}
    99  
   100  	return destinationPaths
   101  }
   102  
   103  func getDestinationPathsFromOutputs(outputs OutputPaths) []string {
   104  	idx := 0
   105  	destinationPaths := make([]string, len(outputs))
   106  
   107  	for _, destinationPath := range outputs {
   108  		destinationPaths[idx] = destinationPath
   109  		idx++
   110  	}
   111  
   112  	return destinationPaths
   113  }