Fix global --offline usage

When --offline is passed, any command executed should not reach over the
network. This introduces a tricky scenario when it is passed to a build
command with --native=false (default), because the command in the
container will be run with --offline, but the native mixer binary will
reach over the network to pull/update docker containers before executing
in them. This patch also makes that process offline only, attempting to
use a cached docker image so that it can run offline fully, but exits if
none exist.

Signed-off-by: Tudor Marcu <tudor.marcu@intel.com>
This commit is contained in:
Tudor Marcu
2018-08-31 14:35:37 -07:00
committed by tmarcu
parent d29cc44340
commit e61ff4e4bc
2 changed files with 54 additions and 12 deletions
+46 -12
View File
@@ -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()
+8
View File
@@ -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 {