Exemple #1
0
func pullCommand(c *cli.Context) {
	args := c.Args()
	if len(args) < 1 {
		log.Fatal("rocker pull <image>")
	}

	dockerClient, err := dockerclient.NewFromCli(c)
	if err != nil {
		log.Fatal(err)
	}

	cacheDir, err := util.MakeAbsolute(c.String("cache-dir"))
	if err != nil {
		log.Fatal(err)
	}

	options := build.DockerClientOptions{
		Client:                   dockerClient,
		Auth:                     initAuth(c),
		Log:                      log.StandardLogger(),
		S3storage:                s3.New(dockerClient, cacheDir),
		StdoutContainerFormatter: log.StandardLogger().Formatter,
		StderrContainerFormatter: log.StandardLogger().Formatter,
	}
	client := build.NewDockerClient(options)

	if err := client.PullImage(args[0]); err != nil {
		log.Fatal(err)
	}
}
Exemple #2
0
func buildCommand(c *cli.Context) {

	var (
		rockerfile *build.Rockerfile
		err        error
	)

	// We don't want info level for 'print' mode
	// So log only errors unless 'debug' is on
	if c.Bool("print") && log.StandardLogger().Level != log.DebugLevel {
		log.StandardLogger().Level = log.ErrorLevel
	}

	vars, err := template.VarsFromFileMulti(c.StringSlice("vars"))
	if err != nil {
		log.Fatal(err)
		os.Exit(1)
	}

	cliVars, err := template.VarsFromStrings(c.StringSlice("var"))
	if err != nil {
		log.Fatal(err)
	}

	vars = vars.Merge(cliVars)

	if c.Bool("demand-artifacts") {
		vars["DemandArtifacts"] = true
	}

	wd, err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}

	configFilename := c.String("file")
	contextDir := wd

	if configFilename == "-" {

		rockerfile, err = build.NewRockerfile(filepath.Base(wd), os.Stdin, vars, template.Funs{})
		if err != nil {
			log.Fatal(err)
		}

	} else {

		if !filepath.IsAbs(configFilename) {
			configFilename = filepath.Join(wd, configFilename)
		}

		rockerfile, err = build.NewRockerfileFromFile(configFilename, vars, template.Funs{})
		if err != nil {
			log.Fatal(err)
		}

		// Initialize context dir
		contextDir = filepath.Dir(configFilename)
	}

	args := c.Args()
	if len(args) > 0 {
		contextDir = args[0]
		if !filepath.IsAbs(contextDir) {
			contextDir = filepath.Join(wd, args[0])
		}
	} else if contextDir != wd {
		log.Warningf("Implicit context directory used: %s. You can override context directory using the last argument.", contextDir)
	}

	dir, err := os.Stat(contextDir)
	if err != nil {
		log.Errorf("Problem with opening directory %s, error: %s", contextDir, err)
		os.Exit(2)
	}
	if !dir.IsDir() {
		log.Errorf("Context directory %s is not a directory.", contextDir)
		os.Exit(2)

	}
	log.Debugf("Context directory: %s", contextDir)

	if c.Bool("print") {
		fmt.Print(rockerfile.Content)
		os.Exit(0)
	}

	dockerignore := []string{}

	dockerignoreFilename := filepath.Join(contextDir, ".dockerignore")
	if _, err := os.Stat(dockerignoreFilename); err == nil {
		if dockerignore, err = build.ReadDockerignoreFile(dockerignoreFilename); err != nil {
			log.Fatal(err)
		}
	}

	var config *dockerclient.Config
	config = dockerclient.NewConfigFromCli(c)

	dockerClient, err := dockerclient.NewFromConfig(config)
	if err != nil {
		log.Fatal(err)
	}

	cacheDir, err := util.MakeAbsolute(c.String("cache-dir"))
	if err != nil {
		log.Fatal(err)
	}

	var cache build.Cache
	if !c.Bool("no-cache") {
		cache = build.NewCacheFS(cacheDir)
	}

	var (
		stdoutContainerFormatter log.Formatter = &log.JSONFormatter{}
		stderrContainerFormatter log.Formatter = &log.JSONFormatter{}
	)
	if !c.GlobalBool("json") {
		stdoutContainerFormatter = build.NewMonochromeContainerFormatter()
		stderrContainerFormatter = build.NewColoredContainerFormatter()
	}

	options := build.DockerClientOptions{
		Client:                   dockerClient,
		Auth:                     initAuth(c),
		Log:                      log.StandardLogger(),
		S3storage:                s3.New(dockerClient, cacheDir),
		StdoutContainerFormatter: stdoutContainerFormatter,
		StderrContainerFormatter: stderrContainerFormatter,
		PushRetryCount:           c.Int("push-retry"),
		Host:                     config.Host,
		LogExactSizes:            c.GlobalBool("json"),
	}
	client := build.NewDockerClient(options)

	builder := build.New(client, rockerfile, cache, build.Config{
		InStream:      os.Stdin,
		OutStream:     os.Stdout,
		ContextDir:    contextDir,
		Dockerignore:  dockerignore,
		ArtifactsPath: c.String("artifacts-path"),
		Pull:          c.Bool("pull"),
		NoGarbage:     c.Bool("no-garbage"),
		Attach:        c.Bool("attach"),
		Verbose:       c.GlobalBool("verbose"),
		ID:            c.String("id"),
		NoCache:       c.Bool("no-cache"),
		ReloadCache:   c.Bool("reload-cache"),
		Push:          c.Bool("push"),
		CacheDir:      cacheDir,
		LogJSON:       c.GlobalBool("json"),
		BuildArgs:     runconfigopts.ConvertKVStringsToMap(c.StringSlice("build-arg")),
	})

	plan, err := build.NewPlan(rockerfile.Commands(), true)
	if err != nil {
		log.Fatal(err)
	}

	// Check the docker connection before we actually run
	if err := dockerclient.Ping(dockerClient, 5000); err != nil {
		log.Fatal(err)
	}

	if err := builder.Run(plan); err != nil {
		log.Fatal(err)
	}

	fields := log.Fields{}
	if c.GlobalBool("json") {
		fields["size"] = builder.VirtualSize
		fields["delta"] = builder.ProducedSize
	}

	size := fmt.Sprintf("final size %s (+%s from the base image)",
		units.HumanSize(float64(builder.VirtualSize)),
		units.HumanSize(float64(builder.ProducedSize)),
	)

	log.WithFields(fields).Infof("Successfully built %.12s | %s", builder.GetImageID(), size)
}