diff --git a/builder/docker.go b/builder/docker.go index 2e4208c..8a58298 100644 --- a/builder/docker.go +++ b/builder/docker.go @@ -160,26 +160,60 @@ func (b *Builder) getDockerMounts() ([]string, error) { return reduceDockerMounts(mounts), nil } +func (b *Builder) prepareContainer() (string, error) { + format, err := b.getUpstreamFormat(b.UpstreamVer) + if err != nil { + return "", err + } + imageName, err := b.getDockerImageName(format) + if err != nil { + return "", errors.Wrap(err, "Unable to get docker image name for format "+format) + } + fmt.Println("Updating docker image") + if err = helpers.RunCommand("docker", "pull", imageName); err != nil { + fmt.Printf("WARNING: Unable to pull docker image for format %s. Trying with cached image\n", format) + } + + return imageName, nil + +} + +func (b *Builder) prepareContainerOffline() (string, error) { + imageName, err := b.getDockerImageName(b.State.Mix.Format) + if err != nil { + var hostFormat []byte + if hostFormat, err = ioutil.ReadFile("/usr/share/defaults/swupd/format"); err != nil { + return "", err + } + imageName, err = b.getDockerImageName(string(hostFormat)) + if err != nil { + return "", errors.Wrapf(err, "Unable to get docker image name for format %s", string(hostFormat)) + } + } + + // We know the right image name and format now so only need to run this once + if err := helpers.RunCommandSilent("docker", "image", "inspect", imageName); err != nil { + return "", errors.Errorf("Failed to find usable docker image, cannot run offline: %s", err) + } + + return imageName, nil +} + // RunCommandInContainer will pull the content necessary to build a docker // image capable of running the desired command, build that image, and then // run the command in that image. func (b *Builder) RunCommandInContainer(cmd []string) error { - format, err := b.getUpstreamFormat(b.UpstreamVer) + var imageName string + var err error + if Offline { + imageName, err = b.prepareContainerOffline() + } else { + imageName, err = b.prepareContainer() + } if err != nil { return err } - imageName, err := b.getDockerImageName(format) - if err != nil { - return errors.Wrap(err, "Unable to get docker image name for format "+format) - } - - fmt.Println("Updating docker image") - - if err = helpers.RunCommand("docker", "pull", imageName); err != nil { - fmt.Printf("WARNING: Unable to pull docker image for format %s. Trying with cached image\n", format) - } - fmt.Printf("Running command in container: %q\n", strings.Join(cmd, " ")) wd, _ := os.Getwd() diff --git a/mixer/cmd/root.go b/mixer/cmd/root.go index f83d831..ce4e2fb 100644 --- a/mixer/cmd/root.go +++ b/mixer/cmd/root.go @@ -110,6 +110,14 @@ var RootCmd = &cobra.Command{ // If so: inform, stage, and exit. // If not: run command in container and cancel pre-run if !cmdContains(cmd, "format-bump") && !cmdContains(cmd, "upstream-format") && cmdContains(cmd, "build") { + // --offline=true AND --native=false, try to see if container exists + if builder.Offline && !builder.Native { + fmt.Println("Warning: Unable to determine upstream format in --offline mode, build may fail if building across format boundaries.") + if err := b.RunCommandInContainer(reconstructCommand(cmd, args)); err != nil { + fail(err) + } + return nil + } if bumpNeeded, err := b.CheckBumpNeeded(false); err != nil { return err } else if bumpNeeded {