github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/hack/integration-cli-on-swarm/agent/worker/worker.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"log"
     7  	"time"
     8  
     9  	"github.com/bfirsh/funker-go"
    10  	"github.com/docker/distribution/reference"
    11  	"github.com/docker/docker/hack/integration-cli-on-swarm/agent/types"
    12  )
    13  
    14  func main() {
    15  	if err := xmain(); err != nil {
    16  		log.Fatalf("fatal error: %v", err)
    17  	}
    18  }
    19  
    20  func validImageDigest(s string) bool {
    21  	return reference.DigestRegexp.FindString(s) != ""
    22  }
    23  
    24  func xmain() error {
    25  	workerImageDigest := flag.String("worker-image-digest", "", "Needs to be the digest of this worker image itself")
    26  	dryRun := flag.Bool("dry-run", false, "Dry run")
    27  	keepExecutor := flag.Bool("keep-executor", false, "Do not auto-remove executor containers, which is used for running privileged programs on Swarm")
    28  	flag.Parse()
    29  	if !validImageDigest(*workerImageDigest) {
    30  		// Because of issue #29582.
    31  		// `docker service create localregistry.example.com/blahblah:latest` pulls the image data to local, but not a tag.
    32  		// So, `docker run localregistry.example.com/blahblah:latest` fails: `Unable to find image 'localregistry.example.com/blahblah:latest' locally`
    33  		return fmt.Errorf("worker-image-digest must be a digest, got %q", *workerImageDigest)
    34  	}
    35  	executor := privilegedTestChunkExecutor(!*keepExecutor)
    36  	if *dryRun {
    37  		executor = dryTestChunkExecutor()
    38  	}
    39  	return handle(*workerImageDigest, executor)
    40  }
    41  
    42  func handle(workerImageDigest string, executor testChunkExecutor) error {
    43  	log.Printf("Waiting for a funker request")
    44  	return funker.Handle(func(args *types.Args) types.Result {
    45  		log.Printf("Executing chunk %d, contains %d test filters",
    46  			args.ChunkID, len(args.Tests))
    47  		begin := time.Now()
    48  		code, rawLog, err := executor(workerImageDigest, args.Tests)
    49  		if err != nil {
    50  			log.Printf("Error while executing chunk %d: %v", args.ChunkID, err)
    51  			if code == 0 {
    52  				// Make sure this is a failure
    53  				code = 1
    54  			}
    55  			return types.Result{
    56  				ChunkID: args.ChunkID,
    57  				Code:    int(code),
    58  				RawLog:  rawLog,
    59  			}
    60  		}
    61  		elapsed := time.Since(begin)
    62  		log.Printf("Finished chunk %d, code=%d, elapsed=%v", args.ChunkID, code, elapsed)
    63  		return types.Result{
    64  			ChunkID: args.ChunkID,
    65  			Code:    int(code),
    66  			RawLog:  rawLog,
    67  		}
    68  	})
    69  }